Live Streaming

Last Updated on : 2024-06-19 04:23:32download

Create an IThingSmartCameraP2P object to build a peer-to-peer (P2P) connection. Then, IP cameras (IPCs) capabilities can be implemented through the P2P connection, for example, live streaming, video screenshots, video recording, and live talk.

How it works

Live Streaming

Core methods

Demo Class Method Description Notes
IThingIPCCore createCameraP2P Create a camera object. One device ID matches one camera object.
IThingSmartCameraP2P connect Connect. When error -3 or -105 occurs, try reconnection.
For more information, see Error Codes
IThingSmartCameraP2P startPreview Start streaming. On successful streaming callback, the VideoView starts playing.
See Error Codes.
IThingSmartCameraP2P stopPreview Stop streaming. -
IThingSmartCameraP2P disconnect Disconnect. When not in use, disconnect the P2P connection to free up resources.

Important notes

It is recommended not to call other methods of the IThingSmartCameraP2P object within this object’s callback, which may be asynchronously called. Otherwise, a deadlock might occur.

Core code

Connect

void connect(String devId, OperationDelegateCallBack callBack);

Start streaming

void startPreview(int clarity, OperationDelegateCallBack callBack);

Stop streaming

int stopPreview(OperationDelegateCallBack callBack);

Disconnect

void destroyP2P();

Java:

// 1. Create IThingSmartCameraP2P.
IThingSmartCameraP2P mCameraP2P = null;
IThingIPCCore cameraInstance = ThingIPCSdk.getCameraInstance();
if (cameraInstance != null) {
    mCameraP2P = cameraInstance.createCameraP2P(devId));
}
ThingCameraView mVideoView = findViewById(R.id.camera_video_view);
// 2. Set the callback for the view rendering container.
mVideoView.setViewCallback(new AbsVideoViewCallback() {
    @Override
    public void onCreated(Object view) {
        super.onCreated(view);
        // When the rendering view is constructed, bind it to IThingSmartCameraP2P.
        if (null != mCameraP2P){
            mCameraP2P.generateCameraView(view);
        }
    }
});
// 3. Construct the rendering view.
mVideoView.createVideoView(devId);
// 4. Register a listener for P2P.
AbsP2pCameraListener absP2pCameraListener = new AbsP2pCameraListener() {
    @Override
    public void onSessionStatusChanged(Object camera, int sessionId, int sessionStatus) {
            super.onSessionStatusChanged(camera, sessionId, sessionStatus);
            // When sessionStatus = -3 (timeout) or -105 (authentication failed), try reconnecting and avoid loop.
            }
};
if (null != mCameraP2P){
    mCameraP2P.registerP2PCameraListener(absP2pCameraListener);
}
// 5. Connect to P2P.
if (null != mCameraP2P){
mCameraP2P.connect(devId, new OperationDelegateCallBack() {
    @Override
    public void onSuccess(int sessionId, int requestId, String data) {
        // Connection successful. Send a message to the handler to return to the main thread and then initiate streaming.
    }

    @Override
    public void onFailure(int sessionId, int requestId, int errCode) {
        // Connection failed.
    }
});
}
// 6. Start streaming.
if (null != mCameraP2P){
mCameraP2P.startPreview(new OperationDelegateCallBack() {
    @Override
    public void onSuccess(int sessionId, int requestId, String data) {
        // Live streaming starts successfully.
    }

    @Override
    public void onFailure(int sessionId, int requestId, int errCode) {
        // Failed to start live streaming.
    }
});
}
// 7. Stop releasing P2P sessions when the page is closed or streaming is not used.
@Override
public void onDestroy() {
    if (null != mCameraP2P) {
        // Stop streaming when not needed.
        mCameraP2P.stopPreview(null);

        mCameraP2P.disconnect(devId,null);
        mCameraP2P.removeOnP2PCameraListener(absP2pCameraListener);
        mCameraP2P.destroyP2P();
    }
}