Live Video Streaming

Last Updated on : 2023-05-22 06:38:31

Before live video streaming, an ITuyaSmartCameraP2P object must be created to build a 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 process of the P2P connection.

Live Video Streaming

Initialize an object

Create a P2P object

API description

Creates an ITuyaSmartCameraP2P object to enable the preview feature for the target device.

ITuyaSmartCameraP2P createCameraP2P(String devId);

Parameters

Parameter Description
devId The device ID.

Example

ITuyaIPCCore cameraInstance = TuyaIPCSdk.getCameraInstance();
if (cameraInstance != null) {
    cameraInstance.createCameraP2P(devId));
}

Import a view rendering container

TuyaCameraView 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 ITuyaSmartCameraP2P.

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

Set callback for view rendering container

API description

public void setViewCallback(AbsVideoViewCallback callback);

Parameters

Parameter Description
callback The callback.

Example

TuyaCameraView mVideoView = findViewById(R.id.camera_video_view);
mVideoView.setViewCallback(new AbsVideoViewCallback() {
	@Override
	public void onCreated(Object view) {
		super.onCreated(view);
        // The callback for the view rendering container.
	}
});

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();

API description

Executes the callback when view sliding is triggered.

This API method only supports P2P 1.0 devices. Use setOnRenderDirectionCallback for other types of devices.

public void startCameraMove(String cameraDirection);

Parameters

Parameter Description
cameraDirection The direction in which the view slides. Valid values:
  • 0: upward
  • 2: rightward
  • 4: downward
  • 6: leftward

API description

Executes the callback when a finger is lifted after tapping the view.

public void onActionUP();

Build a rendered view

API description

public void createVideoView(String devId);

Parameters

Parameter Description
devId The device ID.

Example

TuyaCameraView 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

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

Bind a P2P object with a rendered view

Binds the ITuyaSmartCameraP2P object with a rendered view.

API description

void generateCameraView(T view);

Example

mCameraP2P.generateCameraView(mVideoView.createdView());

Registers a P2P listener.

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

API description

void registerP2PCameraListener(AbsP2pCameraListener listener);

Example

// 1. Creates `ITuyaSmartCameraP2P`.
ITuyaSmartCameraP2P mCameraP2P = null;
ITuyaIPCCore cameraInstance = TuyaIPCSdk.getCameraInstance();
if (cameraInstance != null) {
    mCameraP2P = cameraInstance.createCameraP2P(devId));
}
TuyaCameraView 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 `ITuyaSmartCameraP2P`.
		if (null != mCameraP2P){
			mCameraP2P.generateCameraView(view);
		}
	}
});
// 4. Build the rendered view.
mVideoView.createVideoView(devId);
// 5. Register a P2P listener.
if (null != mCameraP2P){
    mCameraP2P.registerP2PCameraListener(new AbsP2pCameraListener() {
        @Override
                public void onSessionStatusChanged(Object camera, int sessionId, int sessionStatus) {
            super.onSessionStatusChanged(camera, sessionId, sessionStatus);

        }
    });
}

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 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. 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 ITuyaSmartCameraP2P object must be destroyed.

API description

Unregisters a P2P listener.

void removeOnP2PCameraListener();

API description

Deletes a P2P object.

void destroyP2P();

Example

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