Live Video Streaming

Last Updated on : 2023-05-25 06:23:52

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 video streaming, video screenshots, video recording, and real-time talk with IPCs.

The following figure shows the streaming process.

Live Video Streaming

Initialize an object

Create a P2P object

API description

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

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"
  />

Set callback for finished view rendering creation and tapping event

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

Executes the callback when the view rendering container is built.

public void onCreated(Object view);

API description

Executes the callback when the view is tapped.

public void videoViewClick();

Set callback for sliding and touching of rendered view

API description

Sets the callback when view sliding is triggered.

public void setOnRenderDirectionCallback(OnRenderDirectionCallback onRenderDirectionCallback);

Parameters

Parameter Description
onRenderDirectionCallback The callback to invoke when a touching 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 the operation.
    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

Binds the IThingSmartCameraP2P object with a rendered view.

API description

void generateCameraView(T view);

Example

mCameraP2P.generateCameraView(mVideoView.createdView());

Registers a P2P listener.

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

API description

void registerP2PCameraListener(AbsP2pCameraListener listener);

Example

// 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 video streaming, a P2P connection must be created.

The IPC SDK is only responsible to send and receive IPC data point (DP) data. The P2P status is subject to your maintenance.

API description

Creates a P2P connection.

void connect(String devId, OperationDelegateCallBack callBack);

API description

Creates a P2P connection and prioritizes P2P connection modes.

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

Parameters

Parameter Description
mode The prioritized connection mode. Valid values:
  • 0: automatically selected
  • 1: internet connections prioritized
  • 3: local area network (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 video streaming can be started.

API description

Starts live video streaming.

void startPreview(int clarity, OperationDelegateCallBack callBack);

API description

Stops live video streaming.

int stopPreview(OperationDelegateCallBack callBack);

Parameters

Parameter Description
clarity The video definition mode. Valid values:
  • 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 video streaming is started.
    }

    @Override
    public void onFailure(int sessionId, int requestId, int errCode) {
        // Failed to start live video 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

Unregisters a P2P listener.

void removeOnP2PCameraListener(AbsP2pCameraListener listener);

API description

Deletes a P2P object.

void destroyP2P();

Example

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