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
- interface
ICatchIPancamRenderICatchIStreamProvider disableRender() throws IchInvalidSessionException, IchPermissionDeniedException;Disable SDK render.
- interface
ICatchIPancamPreviewthis interface extendsICatchIPancamRenderboolean 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.
- interface
ICatchIStreamProviderboolean 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
IchTryAgainExceptionwhen 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
- If you receive
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
IchTryAgainExceptionwhen 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
- If you receive
General steps
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./* Disable inner render* This operations will gives you an interface to get frames, formats... */if (renderType == MainActivityAttributes.RENDER_TYPE_NO) {try {this.streamProvider = pancamSession.getPreview().disableRender();} catch (Exception e) {e.printStackTrace();return false;}}
Open the stream.
try {boolean retVal = pancamSession.getPreview().start(new ICatchJPEGStreamParam(720, 400));return retVal;}catch(Exception ex) {ex.printStackTrace();}
Use the instance saved in the first step to get audio and video information and do further processing. Take the
video renderingas 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) {
if (videoFormat.getCodec() == ICatchCodec.ICH_CODEC_H264) {PancamAppRenderH264 renderH264 = new PancamAppRenderH264(surfaceView.getHolder());renderH264.setFormat(videoFormat);this.videoRender = renderH264;}// The render threadthis.videoRun = true;this.videoThread = new videoRenderThread();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.
try {boolean retVal = pancamSession.getPreview().stop();return retVal;} catch (Exception e) {e.printStackTrace();return false;}
- Get video information, this method will return an instance of
Some tips
- If you decide to receive audio data and there is audio data from the camera, at this time the
MobilecamSDKwill have audio and video synchronization operations. TheMobilecamSDKwill refers the time stamp of the audio frame and the time taken by the App from theMobilecamSDKto 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 calledICH_TRY_AGAINwhen fetching video frames. - 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 theMobilecamSDKis 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)
- To get frames in time is especially important for real-time preview
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 called
ICatchAudioFormat 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;
public PancamAppRenderPCMA() {}public void freeRender() {if (track != null) track.stop();}public void setFormat(ICatchAudioFormat audioFormat) {SphereLogger.logI("render_Audio", "frequency: " + audioFormat.getFrequency());SphereLogger.logI("render_Audio", "nchannels: " + audioFormat.getNChannels());SphereLogger.logI("render_Audio", "sampleBits: " + audioFormat.getSampleBits());int frequency = audioFormat.getFrequency();int nchannels = audioFormat.getNChannels() == 2 ? AudioFormat.CHANNEL_IN_STEREO : AudioFormat.CHANNEL_IN_LEFT;int sampleBits = audioFormat.getSampleBits() == 16 ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;int bufferSize = AudioTrack.getMinBufferSize(frequency, nchannels, sampleBits);SphereLogger.logI("render_Audio", "frequency: " + frequency);SphereLogger.logI("render_Audio", "nchannels: " + nchannels);SphereLogger.logI("render_Audio", "sampleBits: " + sampleBits);this.codec = audioFormat.getCodec();this.track = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, nchannels, sampleBits, bufferSize, AudioTrack.MODE_STREAM);this.track.play();}public boolean renderFrame(ICatchFrameBuffer frame) {SphereLogger.logI("render_Audio", "codec: " + codec);if (codec != ICatchCodec.ICH_CODEC_PCM) {return false;}track.write(frame.getBuffer(), 0, frame.getFrameSize());SphereLogger.logI("render_Audio", "put audio frame: " + frame.getPresentationTime());return true;}
} ```
