Description

Currently, MobilecamSDK can render audio and video data itself, which is also our recommended way.
However, we also support customers to obtain audio and video information themselves to facilitate some unique processing, such as some AI-based object recognition, etc.

Related interfaces and methods

  1. interface ICatchIPancamRender
    • ICatchIStreamProvider disableRender() throws IchInvalidSessionException, IchPermissionDeniedException; Disable SDK render.
  2. interface ICatchIPancamPreview this interface extends ICatchIPancamRender
    • boolean start(ICatchStreamParam streamParam) throws IchStreamNotSupportException, IchInvalidSessionException, IchInvalidArgumentException, IchTransportException, IchStreamAlreadyStartedException;
      Open the stream, the first parameter specifies the information of the video stream.
    • boolean start(ICatchStreamParam streamParam, boolean enableAudio) throws IchStreamNotSupportException, IchInvalidSessionException, IchInvalidArgumentException, IchTransportException, IchStreamAlreadyStartedException;
      Open the stream, the second paramter tells the SDK whether to enable audio stream or not.
    • boolean stop() throws IchStreamNotRunningException, IchInvalidSessionException, IchTransportException;
      Stop the stream.
  3. interface ICatchIStreamProvider
    • boolean containsVideoStream() throws IchTransportException, IchInvalidSessionException, IchStreamNotRunningException, IchDeprecatedException;
      Query whether the sdk’s contains video stream or not.
    • boolean containsAudioStream() throws IchTransportException, IchInvalidSessionException, IchStreamNotRunningException, IchDeprecatedException;
      Query whether the sdk’s contains audio stream or not.
    • ICatchVideoFormat getVideoFormat() throws IchInvalidSessionException, IchStreamNotRunningException, IchTransportException, IchDeprecatedException;
      Get icatch video format, such as video width, video height, csd-0, csd-1, codec name.
    • ICatchAudioFormat getAudioFormat() throws IchInvalidSessionException, IchStreamNotRunningException, IchTransportException, IchDeprecatedException;
      Get icatch audio format, such codec, channel, frequency etc.
    • boolean getNextVideoFrame(ICatchFrameBuffer frameBuffer) throws IchTransportException, IchVideoStreamClosedException, IchStreamNotRunningException, IchTryAgainException, IchInvalidSessionException, IchBufferTooSmallException, IchPbStreamPausedException, IchDeprecatedException;
      Get video frame from SDK. if no frames stored in SDK, this method will be blocked for a while to wait new frames.
      • If you receive IchTryAgainException when calling this API, it does not mean that the call failed, but it indicates that the call did not get a valid frame, you should call again to get again.
      • For the IOS SDK, the error name is ICH_TRY_AGAIN, you can find the definition of the macro in the file ICatchError.h
    • boolean getNextAudioFrame(ICatchFrameBuffer frameBuffer) throws IchTransportException, IchAudioStreamClosedException, IchStreamNotRunningException, IchTryAgainException, IchInvalidSessionException, IchBufferTooSmallException, IchPbStreamPausedException, IchDeprecatedException;
      Get audio frame from SDK. if no frames stored in SDK, this method will be blocked for a while to wait new frames. after that this API will throws try again exception.
      • If you receive IchTryAgainException when calling this API, it does not mean that the call failed, but it indicates that the call did not get a valid frame, you should call again to get again.
      • For the IOS SDK, the error name is ICH_TRY_AGAIN, you can find the definition of the macro in the file ICatchError.h

General steps

  1. Disable SDK rendering, this method will return an instance of ICatchStreamProvider, Please save this reference, which will be used to get audio and video stream information in subsequent operations.

    1. /* Disable inner render
    2. * This operations will gives you an interface to get frames, formats... */
    3. if (renderType == MainActivityAttributes.RENDER_TYPE_NO) {
    4. try {
    5. this.streamProvider = pancamSession.getPreview().disableRender();
    6. } catch (Exception e) {
    7. e.printStackTrace();
    8. return false;
    9. }
    10. }
  2. Open the stream.

  1. try {
  2. boolean retVal = pancamSession.getPreview().start(new ICatchJPEGStreamParam(720, 400));
  3. return retVal;
  4. }
  5. catch(Exception ex) {
  6. ex.printStackTrace();
  7. }
  1. Use the instance saved in the first step to get audio and video information and do further processing. Take the video rendering as an example. The general operation is as follows:

    • Get video information, this method will return an instance of ICatchVideoFormat , this object contains the video encoding format, width, height and other related information, if necessary, use this information to initialize the decoding component and rendering component. ```java /*—————————————————————————————————-
    • Render H.264 stream **/ if (this.streamProvider.containsVideoStream()) {

      ICatchVideoFormat videoFormat = this.streamProvider.getVideoFormat(); if (videoFormat != null) {

      1. if (videoFormat.getCodec() == ICatchCodec.ICH_CODEC_H264) {
      2. PancamAppRenderH264 renderH264 = new PancamAppRenderH264(surfaceView.getHolder());
      3. renderH264.setFormat(videoFormat);
      4. this.videoRender = renderH264;
      5. }
      6. // The render thread
      7. this.videoRun = true;
      8. this.videoThread = new videoRenderThread();
      9. this.videoThread.start();

      } } else { Log.i(“no_render_flow“, “No video stream found”); } } catch (Exception ex) { ex.printStackTrace(); } ```

    • Now you can take frames and send them to decoder or render. There is an important exception here.IchTryAgainException(or err code called ICH_TRY_AGAIN), It indicates that this call operation did not get valid frame data, but the stream did not meet an error, the caller should wait for a period of time, and then call the API again to get frame. ```java /*—————————————————————————————————-
    • The videoRenderThread‘ runnable methods */ ICatchFrameBuffer frameBuffer = new ICatchFrameBuffer(2880 1440 * 4); while (videoRun) { boolean retVal = false; try { retVal = streamProvider.getNextVideoFrame(frameBuffer); } catch (IchTryAgainException e) { e.printStackTrace(); continue; } catch (Exception e) { e.printStackTrace(); break; }

      if (!retVal || frameBuffer.getFrameSize() <= 0) { Log.i(“video_render“, “failed, frameSize: “ + frameBuffer.getFrameSize()); break; }

      Log.i(“video_render“, “try to render, frameSize: “ + frameBuffer.getFrameSize()); videoRender.renderFrame(frameBuffer); } Log.i(“video_render“, “quit render thread”); ```

    • Close the decoder or rendering component.
      1. try {
      2. boolean retVal = pancamSession.getPreview().stop();
      3. return retVal;
      4. } catch (Exception e) {
      5. e.printStackTrace();
      6. return false;
      7. }

Some tips

  1. If you decide to receive audio data and there is audio data from the camera, at this time the MobilecamSDK will have audio and video synchronization operations. The MobilecamSDK will refers the time stamp of the audio frame and the time taken by the App from the MobilecamSDK to determine when to send the corresponding video frame . The user needs to ensure to get the audio frame in time, and keep the appropriate delay time before the next audio frame is gotten. If the app does not fetch audio frames, you may always get the same error called ICH_TRY_AGAIN when fetching video frames.
  2. In order to ensure smooth playing of audio and video data, the App should get frames in time. This is because the server does not stop sending frames if the App does not pick up the frames in time.

Imagine a situation. For a video stream with the format of H.264 IPPP, if the user forgets to fetch a frame int a period of time, the internal buffer of the MobilecamSDK is full and an I frame will be dropped. then all 29 P have to be dropped (assuming GOP is 30), then there may be a pause of one second (for the case of 30 frames per second of the video stream)

  1. To get frames in time is especially important for real-time preview
  1. How to get and render audio stream using AudioTrack: As you can see in the example below, in order to play the audio data correctly, you need to specify the sample rate, bit width, and number of channels to initialize the sound playback object that matches the Audio stream, and these parameters can be obtained via the API of the ICatchStreamProvider interface calledICatchAudioFormat getAudioFormat ().

    The audio render component, File PancamAppRenderPCMA ```java package com.icatchtek.vr360.streaming.apprender.core;

import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack;

import com.icatchtek.reliant.customer.type.ICatchAudioFormat; import com.icatchtek.reliant.customer.type.ICatchCodec; import com.icatchtek.reliant.customer.type.ICatchFrameBuffer; import com.icatchtek.vr360.streaming.pancam.SphereLogger;

public class PancamAppRenderPCMA implements PancamAppRender { private AudioTrack track; private int codec;

  1. public PancamAppRenderPCMA() {
  2. }
  3. public void freeRender() {
  4. if (track != null) track.stop();
  5. }
  6. public void setFormat(ICatchAudioFormat audioFormat) {
  7. SphereLogger.logI("render_Audio", "frequency: " + audioFormat.getFrequency());
  8. SphereLogger.logI("render_Audio", "nchannels: " + audioFormat.getNChannels());
  9. SphereLogger.logI("render_Audio", "sampleBits: " + audioFormat.getSampleBits());
  10. int frequency = audioFormat.getFrequency();
  11. int nchannels = audioFormat.getNChannels() == 2 ? AudioFormat.CHANNEL_IN_STEREO : AudioFormat.CHANNEL_IN_LEFT;
  12. int sampleBits = audioFormat.getSampleBits() == 16 ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
  13. int bufferSize = AudioTrack.getMinBufferSize(frequency, nchannels, sampleBits);
  14. SphereLogger.logI("render_Audio", "frequency: " + frequency);
  15. SphereLogger.logI("render_Audio", "nchannels: " + nchannels);
  16. SphereLogger.logI("render_Audio", "sampleBits: " + sampleBits);
  17. this.codec = audioFormat.getCodec();
  18. this.track = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, nchannels, sampleBits, bufferSize, AudioTrack.MODE_STREAM);
  19. this.track.play();
  20. }
  21. public boolean renderFrame(ICatchFrameBuffer frame) {
  22. SphereLogger.logI("render_Audio", "codec: " + codec);
  23. if (codec != ICatchCodec.ICH_CODEC_PCM) {
  24. return false;
  25. }
  26. track.write(frame.getBuffer(), 0, frame.getFrameSize());
  27. SphereLogger.logI("render_Audio", "put audio frame: " + frame.getPresentationTime());
  28. return true;
  29. }

} ```