Live Streaming

Last Updated on : 2024-06-17 08:06:43download

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.

The following figure shows the streaming process.

Live Streaming

Initialization

Create a P2P object

API description

Create an IThingSmartCameraP2P object to enable the preview feature for the target device.

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.

IThingSmartCameraP2P createCameraP2P(String devId);

Parameters

Parameter Description
devId The device ID.

Example

IThingIPCCore cameraInstance = ThingIPCSdk.getCameraInstance();
if (cameraInstance != null) {
    cameraInstance.createCameraP2P(devId));
}

Import a view rendering container

ThingCameraView is the view rendering container that the IPC SDK provides for the page layout file.

To use your own rendered view, implement IRegistorIOTCListener and bind the view with the object IThingSmartCameraP2P.

<com.thingclips.smart.camera.middleware.widget.ThingCameraView
  android:id="@+id/camera_video_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  />

Callback for rendering view completion and tap events

API description

public void setViewCallback(AbsVideoViewCallback callback);

Parameters

Parameter Description
callback The callback.

Example

ThingCameraView mVideoView = findViewById(R.id.camera_video_view);
mVideoView.setViewCallback(new AbsVideoViewCallback() {
    @Override
    public void onCreated(Object view) {
        super.onCreated(view);
        // The callback to invoke when view rendering is finished.
    }
});

AbsVideoViewCallback

AbsVideoViewCallback is the callback abstract class for rendered views. You can rewrite the callback. In most cases, the onCreated method is rewritten.

API description

The callback to be invoked when the view rendering container is built.

public void onCreated(Object view);

API description

The callback to be invoked when the view is tapped.

public void videoViewClick();

Callback for the swipe events

API description

Set the callback to be invoked when a swipe event occurs.

public void setOnRenderDirectionCallback(OnRenderDirectionCallback onRenderDirectionCallback);

Parameters

Parameter Description
onRenderDirectionCallback The callback to be invoked when a tap event occurs.

OnRenderDirectionCallback

OnRenderDirectionCallback is the callback abstract class for rendered view touching events. You can use this method to implement the pan-tilt-zoom (PTZ) feature.

public interface OnRenderDirectionCallback {
    // Swipe left
    void onLeft();
    // Swipe right
    void onRight();
    // Swipe up
    void onUp();
    // Swipe down
    void onDown();
    // Release the finger to cancel.
    void onCancel();
}

Build a rendered view

API description

public void createVideoView(String devId);

Parameters

Parameter Description
devId The device ID.

Example

ThingCameraView mVideoView = findViewById(R.id.camera_video_view);
mVideoView.createVideoView(devId);

Get a rendered view

If the rendered view is not built, null is returned.

API description

public Object createdView();

Example

ThingCameraView mVideoView = findViewById(R.id.camera_video_view);
mVideoView.createdView();

Bind a P2P object with a rendered view

Bind the IThingSmartCameraP2P object with a rendered view.

API description

void generateCameraView(T view);

Example

mCameraP2P.generateCameraView(mVideoView.createdView());

Register a P2P listener

Register a listener with IThingSmartCameraP2P. Otherwise, video images cannot be displayed as expected.

API description

void registerP2PCameraListener(AbsP2pCameraListener listener);

Complete example code

// 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);
        // 3. Bind the rendered view with `IThingSmartCameraP2P`.
        if (null != mCameraP2P){
            mCameraP2P.generateCameraView(view);
        }
    }
});
// 4. Build the rendered view.
mVideoView.createVideoView(devId);
// 5. Register a P2P listener.
AbsP2pCameraListener absP2pCameraListener = new AbsP2pCameraListener() {
    @Override
    public void onSessionStatusChanged(Object camera, int sessionId, int sessionStatus) {
            super.onSessionStatusChanged(camera, sessionId, sessionStatus);
            // If sessionStatus = -3 (timeout) or  -105 (failed authentication), we recommend that you initiate a reconnection. Make sure to avoid an infinite loop.
            }
};
if (null != mCameraP2P){
    mCameraP2P.registerP2PCameraListener(absP2pCameraListener);
}

Create a P2P connection

Before live streaming, a P2P connection must be created.

The IPC SDK only sends and receives IPC data point (DP) data. The P2P status is subject to your maintenance.

API description

Create a P2P connection.

void connect(String devId, OperationDelegateCallBack callBack);

API description

Create a P2P connection and prioritize the P2P connection modes.

void connect(String devId, int mode, OperationDelegateCallBack callBack);

Parameters

Parameter Description
mode The prioritized connection mode.
  • 0: Automatically selected
  • 1: Internet connections prioritized
  • 2: LAN connections prioritized

Close a P2P connection

API description

void disconnect(String devId, OperationDelegateCallBack callBack);

Parameters

Parameter Description
callBack The callback.

Example

mCameraP2P.connect(devId, new OperationDelegateCallBack() {
    @Override
    public void onSuccess(int sessionId, int requestId, String data) {
        // A P2P connection is created.
    }

    @Override
    public void onFailure(int sessionId, int requestId, int errCode) {
        // Failed to create a P2P connection.
    }
});

Play live video

After a P2P connection is created, live streaming can be started.

API description

Start live streaming.

void startPreview(int clarity, OperationDelegateCallBack callBack);

API description

Stop live streaming.

int stopPreview(OperationDelegateCallBack callBack);

Parameters

Parameter Description
clarity The video definition mode.
  • 2: Standard definition (SD)
  • 4: High definition (HD)
callBack The callback.

Example

mCameraP2P.startPreview(new OperationDelegateCallBack() {
    @Override
    public void onSuccess(int sessionId, int requestId, String data) {
        // Live streaming is started.
    }

    @Override
    public void onFailure(int sessionId, int requestId, int errCode) {
        // Failed to start live streaming.
    }
});

After startPreview returns the success callback, the callback onReceiveFrameYUVData starts receiving video data and transmitting it to the business layer.

Destroy a P2P object

If the IPC video features are not required, the P2P listener must be unregistered and the IThingSmartCameraP2P object must be destroyed.

API description

Unregister a P2P listener.

void removeOnP2PCameraListener(AbsP2pCameraListener listener);

API description

Delete a P2P object.

void destroyP2P();

Example

@Override
public void onDestroy() {
    if (null != mCameraP2P) {
        mCameraP2P.removeOnP2PCameraListener(absP2pCameraListener);
        mCameraP2P.destroyP2P();
    }
}