private PlayerClient mPlayerClient; private ProjectionDevice mProjectionDevice; //Obtain the PlayerClient instance. mPlayerClient = PlayerClient.getInstance();
private IEventListener mCallback = new IEventListener.Stub() {
//Report the connection status.
public boolean onEvent(int eventId, String type) {
//Perform configuration and logic processing based on the connection status.
...
return true;
}
//Report the display related events.
public boolean onDisplayEvent(int eventId, DisplayInfo displayInfo) {
//Perform configuration and logic processing based on the connection status.
...
return true;
}
}; mPlayerClient.init(context);
mPlayerClient.registerCallback(mCallback);
AuthInfo authInfo = null;
if (needPassword) {
//Pssword mode, where the password contains six characters.
authInfo = new AuthInfo(AuthInfo.AUTH_MODE_PWD);
authInfo.setAuthCode(password);
} else {
//PIN mode.
authInfo = new AuthInfo(AuthInfo.AUTH_MODE_GENERIC);
}
boolean isAuthModeSuccessfullySet = mPlayerClient.setAuthMode(authInfo);
If (isAuthModeSuccessfullySet) {
//Update the local password or UI.
}
The authentication mode settings will affect the UI. Configure the following to be subject to the execution result of the setAuthMode method: updating the locally stored encryption mode and password, and updating the UI. Ensure that the new and old passwords are different.
HiSightCapability capability = new HiSightCapability(1920, 1080, 1920, 1080);
capability.setVideoFps(30);
capability.setMediaCodecConfigureFlag(2);
mPlayerClient.setCapability(capability);
Select a method provided by HiSightCapability based on the platform used to optimize the decoder settings.
HiSightCapability.MediaCodecConfigureFlag(int flag)
HiSightCapability.MediaFormatInteger(String name, int value)
HiSightCapability.MediaFormatFloat(String name, float value)
HiSightCapability.MediaFormatLong(String name, long value)
HiSightCapability.MediaFormatString(String name, String value)
Contact the chipset manufacturer to confirm the platform-specific decoding optimization policy and set the decoding optimization parameters by calling the corresponding APIs.
private DeviceInfo mDeviceInfo = new DeviceInfo(mTvDeviceName, DeviceInfo.TYPE_TV);
Ensure that the set device type is consistent with the actual device type. Otherwise, functions may not work correctly. For example, the device type should be TYPE_TV for a TV and TYPE_WHITEBOARD for an electronic whiteboard.
mPlayerClient.setDiscoverable(true, mDeviceInfo);
When the mobile device and the display device apps are connected to the same LAN, the mobile device user can swipe down from the status bar to open the notification panel, enable wireless projection to search for nearby projection devices, then touch an available Cast Engine capable display device to request a connection.
If it is the first time the display device receives a connection request from the mobile device, the screen will report the EVENT_ID_PIN_CODE_SHOW message and provide the device information for the mobile device. No PIN authentication is required for future connections between this mobile device and display device in PIN mode.
if (displayInfo != null) {
//PIN required to connect to the display device.
String pinCode = displayInfo.getPinCode();
//Display the PIN in the way specified in the UX specification.
showPinCode(pinCode, mProjectionDevice.getDeviceName());
//Enable the phone to directly connect to the display device without the user's confirmation.
mPlayerClient.setConnectRequestChooseResult(new ConnectRequestChoice(Constant.CONNECT_REQ_CHOICE_ALWAYS, mProjectionDevice));
}
The RequestChooseResult API must be called to set whether to allow the connection request from the mobile device. Generally, your app can display a dialog box to prompt the user to select whether to allow the connection. If this function is not implemented, this API can also be called to deliver the Constant.CONNECT_REQ_CHOICE_ALWAYS parameter to allow the connection without the user's confirmation.
After the mobile device enters the correct PIN for authentication, the display device app reports the EVENT_ID_CONNECT_REQ message that carries the information about the mobile device requesting the connection.
if (displayInfo != null) {
//Obtain the information about the requesting device.
mProjectionDevice = displayInfo.getProjectionDevice();
}
In password mode, the mobile device only needs to enter the preset password. In this case, the PIN is not required, and the EVENT_ID_PIN_CODE_SHOW message will not be reported.
After the projection service is registered, the display device app automatically reports the EVENT_ID_NETWORK_QUALITY message when the network quality changes. You can use displayInfo of the onDisplayEvent callback function in IEventListener to obtain the current network quality strength and notify the user of the current network status.
onDisplayEvent(int eventId, DisplayInfo displayInfo){
swicth(eventId){
case Constant.EVENT_ID_NETWORK_QUALITY:
msg.obj= displayInfo;
... .}
}
{... ...
DisplayInfo displayInfo = msg.obj;
// Obtain the network strength.
int networkQuality = displayInfo.getNetworkQuality();
switch (networkQuality) {
//The projection stops due to a network error.
case Constant.NETWORK_QUALITY_EXCEPTION:
//The current signal strength is 1 bar, indicating that the network quality is very poor.
case Constant.NETWORK_QUALITY_WORSE:
//The current signal strength is 2 bars, indicating that the network quality is poor.
case Constant.NETWORK_QUALITY_BAD:
//The current signal strength is 3 bars, indicating that the network quality is average.
case Constant.NETWORK_QUALITY_GENERAL:
//The current signal strength is 4 bars, indicating that the network quality is excellent.
case Constant.NETWORK_QUALITY_GOOD:
... ...
}
When the network quality deteriorates, frame freezing, crackling noise, or even disconnection may occur, detracting from the user experience. As the network signal strength changes, the signal icon changes accordingly. The text prompt needs to be displayed only for the first time.
If the network signal is excellent, the icon disappears. For example, when the network signal strength is 4 bars, the icon is hidden. When the network signal strength changes from 3 bars to 1 bar, the icon corresponding to the signal strength is displayed. When the projection is disconnected due to a network error, a toast message is displayed, indicating that the projection is disconnected. For details, please refer to the HUAWEI Cast Engine UX Design Specification.
After receiving the EVENT_ID_CONNECT_REQ message, Cast Engine reports the EVENT_ID_DEVICE_CONNECTED and EVENT_ID_PAUSED messages to the display device in sequence. After the EVENT_ID_PAUSED message is received, the display device will set the surface and play the video stream. To speed up playback startup, it can initiate the activity and surface creation in advance (by, for example, sending the EVENT_ID_CONNECT_REQ message).
mProjectionDevice carries the peer device information obtained from DisplayInfo in the EVENT_ID_CONNECT_REQ message. It can be used to obtain the device ID, which is then used to build the TrackControl object for screen projection. The display device will report the EVENT_ID_CASTING message once the projection starts.
<com.huawei.castpluskit.HiSightSurfaceView
android:id="@+id/HiSightSurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
The width and height of the electronic whiteboard must be set based on the actual situation. The following is an example of setting the width and height in the EVENT_ID_DEVICE_CONNECTED message:
int mHiviewHeight = mHiview.getHeight(); int mHiviewWidth = mHiview.getWidth(); mPlayerClient.setSinkPlayerParameters(mHiviewWidth, mHiviewHeight, 0, 0);
mHiView.setSecure(true);
After the projection activity initializes HiSightSurfaceView, set mHiView.setSecure(true). If this parameter is not set, the setSecure setting may not take effect because the time when the EVENT_ID_PAUSED message is reported is earlier than the surface creation completion time. As a result, the projection image can be captured on the display device in some cases.
SurfaceHolder surfaceHolder = mHiView.getHolder();
surfaceHolder.addCallback(mSurfaceHolderCallback);
private SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
isSurfaceReady = true;
sendBroadcast(SinkTesterService.BROADCAST_ACTION_PLAY);
}
}
Before the surface is created, which takes some time to complete, some display devices may report the "surface invalid" error when performing the setHiSightSurface operation, causing a blank screen. Therefore, the surface must be set after it is created and the EVENT_ID_PAUSED message is received.
mPlayerClient.play(new TrackControl(mProjectionDevice.getDeviceId()));
mPlayerClient.disconnectDevice(mProjectionDevice);