Live Video

For live video, you need to create an ITuyaSmartCameraP2P object, and then make a P2P connection to play the live video, make screenshots, record videos, and initiate real-time intercom data transfers.

Initialization

Create ITuyaSmartCameraP2P object

Description

ITuyaSmartCameraP2P createCameraP2P(String devId);

Parameter

Parameter Description
devId device ID

Example

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

Use TuyaCameraView in XML layout

TuyaCameraView is a video rendering view provided by the IPC SDK. If you need to use your own video rendering view, you only need to implement the IRegistorIOTCListener interface, and then bind your own video rendering view to ITuyaSmartCameraP2P.

Example

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

Description

public void setViewCallback(AbsVideoViewCallback callback);

Parameter

Parameter Description
callback Callback interface

Example

TuyaCameraView mVideoView = findViewById(R.id.camera_video_view); mVideoView.setViewCallback(new AbsVideoViewCallback() { @Override public void onCreated(Object view) { super.onCreated(view); //Callback when rendering view construction is completed } });

AbsVideoViewCallback

In the callback of TuyaCameraView, developers only need to overwrite the callbacks they care about, and generally only need to overwrite the ʻonCreated` method.

Description

callback when TuyaCameraView is ready to render view

public void onCreated(Object view);

Description

callback when clicking on the view

public void videoViewClick();

Description

callback when scrolling on the view

public void startCameraMove(String cameraDirection)

Parameter

Parameter Description
cameraDirection direction. “0” means up,“2” means right,“4” means down,“6” means left

Parameter

callback when action up

public void onActionUP()

Create render view

Description

public void createVideoView(int p2pType);

Parameters

Parameter Description
p2pType device type. Obtained from DeviceBean, see P2p Type

Example

TuyaCameraView mVideoView = findViewById(R.id.camera_video_view);
mVideoView.createVideoView(p2pType);

Description

Get render view

if the render view is not constructed, will return null

public Object createdView();

Example

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

Bind the render view for ITuyaSmartCameraP2P

Description

void generateCameraView(T view);

Example

mCameraP2P.generateCameraView(mVideoView.createdView());

Register listener

register a listener with ITuyaSmartCameraP2P, otherwise, the render view will not work.

Description

void registerP2PCameraListener(AbsP2pCameraListener listener);

Link code

Example

// 1. create ITuyaSmartCameraP2P object ITuyaSmartCameraP2P mCameraP2P = null; ITuyaIPCCore cameraInstance = TuyaIPCSdk.getCameraInstance(); if (cameraInstance != null) { mCameraP2P = cameraInstance.createCameraP2P(devId)); } // 2. set callback for TuyaCameraView mVideoView.setViewCallback(new AbsVideoViewCallback() { @Override public void onCreated(Object view) { super.onCreated(view); //4. when render view construction is completed, bind render view for ITuyaSmartCameraP2P if (null != mCameraP2P){ mCameraP2P.generateCameraView(view); } } }); // 3. create render view mVideoView.createVideoView(p2pType); // 4. register P2P listener if (null != mCameraP2P){ mCameraP2P.registerP2PCameraListener(new AbsP2pCameraListener() { @Override public void onSessionStatusChanged(Object o, int i, int i1) { super.onSessionStatusChanged(o, i, i1); } }); }

P2P connection

Before starting video playback, you need to connect to the P2P channel. The P2P status needs to be maintained by the user. The SDK is only responsible for issuing instructions and receiving camera response results.

Description

connect P2P

void connect(String devId, OperationDelegateCallBack callBack);

Description

connect P2P (Specify preferred connection mode)

Mode Description
0 automatically choose
1 internet connection priority
2 LAN connection priority
void connect(String devId, int mode, OperationDelegateCallBack callBack);

Description

disconnect P2P

void disconnect(String devId, OperationDelegateCallBack callBack);

Parameter

Parameter Description
callBack result callback

Example

mCameraP2P.connect(devId, new OperationDelegateCallBack() { @Override public void onSuccess(int sessionId, int requestId, String data) { } @Override public void onFailure(int sessionId, int requestId, int errCode) { } });

Live video

After the p2p channel is successfully connected, you can start playing the live video.

Description

Start playing the live video.

void startPreview(int clarity, OperationDelegateCallBack callBack);

Stop playing the live video.

int stopPreview(OperationDelegateCallBack callBack);

Parameter

Parameter Description
clarity sharpness
callBack result callback

Clarity model

mode value
SD 2
HD 4

Example

mCameraP2P.startPreview(new OperationDelegateCallBack() { @Override public void onSuccess(int sessionId, int requestId, String data) { } @Override public void onFailure(int sessionId, int requestId, int errCode) { } });

Note: After the startPreview callback is successful, the onReceiveFrameYUVData callback will start receiving video data and throw it to the business layer.

Delete ITuyaSmartCameraP2P

Destroy the object, when you no longer use the camera function, you must remove the P2P listener and call destroy.

Description

remove P2P listener

void removeOnP2PCameraListener();

Description

destroy camera object

void destroyP2P();

Example

if (null != mCameraP2P) { mCameraP2P.removeOnP2PCameraListener(); mCameraP2P.destroyP2P(); }

Flow chart