CaaS Kit Lite是华为依托畅连业务通信能力,面向应用开发者和硬件开发者提供的开放接口,可以帮助开发者快速实现应用内视频通话服务。开发者通过华为设备虚拟化服务,可以使用任意视频数据作为视频通话的视频源。
CaaS Kit Lite使华为智能手机、海量应用、合作伙伴的智能设备实现系统级音视频通话能力,构建庞大实时通信网络,致力为消费者打造最佳的通信体验。
在这个Codelab中,你将创建Demo Project,并将CaaS Kit Lite的SDK集成到Demo Project中,以及完成CaaS Kit Lite服务的整体流程搭建。
集成CaaS Kit Lite能力,需要完成以下准备工作:
将下载好的sdk包放在app的libs目录下,在app的build.gradle文件中做如下配置:
另外在把app的build.gradle文件中的minSdkVersion版本改为26,如下图所示:
点击同步工程按钮,进行工程同步,如下图所示:
同步成功后,如下图所示:
在自身应用AndroidManifest.xml中添加开发者联盟创建应用时生成的appid信息,name为"com.huawei.hms.client.appid",value为"appid=*",*用自己注册的appid。权限申请,请依照《CaaS Kit Lite接入准备》中第三步"权限申请"的详细说明来完成。
广播可以动态注册也可以静态注册,本例中以动态注册为例:
CaasKitHelper.java
public class CaasKitHelper {
private static CaasKitHelper mCaasKitHelper;
private DeviceDiscoverReceiver mDiscoverReceiver;
private HwCaasServiceManager mHwCaasServiceManager;
private HwCaasHandler mHwCaasHandler;
private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() {
@Override
public void initSuccess(HwCaasHandler handler) {
...
}
@Override
public void initFail(int retCode) {
...
}
@Override
public void releaseSuccess() {
...
}
};
private void registerDiscoverReceiver() {
Log.d(TAG, "registerDiscoverReceiver.");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DMSDP_STARTDISCOVERY);
if (mDiscoverReceiver == null) {
Log.d(TAG, "mDiscoverReceiver.");
mDiscoverReceiver = new DeviceDiscoverReceiver();
mContext.registerReceiver(mDiscoverReceiver, intentFilter);
}
}
public void caasKitInit() {
Log.d(TAG, "caasKitInit." + mIsCaasKitInit);
if (!mIsCaasKitInit) {
registerDiscoverReceiver();
/** Initialize mHwCaasServiceManager instance. */
mHwCaasServiceManager = HwCaasServiceManager.init();
/** Initialize HwCaasHandler instance through handlerType. */
mHwCaasServiceManager.initHandler(mContext, HwCaasUtils.VIRTUAL_CAMERA_TYPE, mCallBack);
mIsCaasKitInit = true;
}
}
...
}
广播如下
DeviceDiscoverReceiver.java
public class DeviceDiscoverReceiver extends BroadcastReceiver {
private static final String TAG = "DmsdpStartDiscoverReceiver";
private Context mApplicationContext;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "DmsdpStartDiscoverReceiver.");
mApplicationContext = CaasKitApplication.getContext();
HwDmsdpService.init(mApplicationContext, new VirtualCameraListener());
}
}
在合适的位置调用CaasKitHelper.getInstance().caasKitInit()进行广播的注册,和CaaS服务的初始化
CaasKitDemoActivity.java
public class CaasKitDemoActivity extends AppCompatActivity {
...
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume.");
if (mVideoView != null) {
mVideoView.onResume();
}
CaasKitHelper.getInstance().caasKitInit();
}
...
}
VirtualCameraListener.java
public class VirtualCameraListener extends CameraListener {
private static final String TAG = "VirtualCameraListener";
private static final int VIDEO_BUFFER_MODE_YUV = 1;
private static final int DEVICE_CAMERA = 5;
private static final String VIRCAMERA_NAME = "CaasKitCamera";
/**
* This ID is customized by the developer
*/
private static final String VIRCAMERA_ID = "virtualcamera0";
/**
* This ID is recommended to be generated by
* java.util.UUID.randomUUID().toString()
* please do not directly use the ID
* public static final String DEVICE_ID = "ff66bf58-6413-48e7-8dcd-c084254b199e";
* provided in the demo
*/
private String mDeviceID;
public VirtualCameraListener() {
mDeviceID = UUID.randomUUID().toString();
Log.d(TAG, "deviceID: " + mDeviceID);
}
@Override
public List<DeviceInfo> getDeviceInfo() {
Log.d(TAG, "getDeviceInfo.");
List<DeviceInfo> listDeviceInfo = new ArrayList<>();
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setDeviceID(mDeviceID);
deviceInfo.setDeviceName(VIRCAMERA_NAME);
deviceInfo.setDeviceType(DEVICE_CAMERA);
deviceInfo.properties.put(CommonUtils.DEVICE_SUPPORTCAMERA_BOOLEAN, "true");
listDeviceInfo.add(deviceInfo);
return listDeviceInfo;
}
@Override
public HashMap<String, CameraInfo> getCameraInfo(String deviceId) {
Log.d(TAG, "getCameraInfo." + deviceId);
HashMap<String, CameraInfo> mapCameraInfo = new HashMap<>();
CameraInfo cameraInfo = new CameraInfo();
cameraInfo.setCameraId(VIRCAMERA_ID);
cameraInfo.setVideoType(VIDEO_BUFFER_MODE_YUV);
cameraInfo.setSupportedFpsRange("25000,25000");
cameraInfo.setSupportedResolutionRange("1080,1920");
mapCameraInfo.put(VIRCAMERA_ID, cameraInfo);
return mapCameraInfo;
}
@Override
public CameraParameters getCameraParameters(String cameraId) {
Log.d(TAG, "getCameraParameters." + cameraId);
CameraParameters cameraParams = new CameraParameters();
cameraParams.cameraId = VIRCAMERA_ID;
cameraParams.properties.put(CommonUtils.CURRENT_RESOLUTION, "1080,1920");
cameraParams.properties.put(CommonUtils.CURRENT_FRAMERATES, "25000,30000");
cameraParams.properties.put(CommonUtils.CURRENT_IMAGEFORMAT, CommonUtils.IMAGE_FORMAT_NV21);
cameraParams.properties.put(CommonUtils.CURRENT_DECODEFORMAT, CommonUtils.DECODE_FORMAT_RGBA);
return cameraParams;
}
@Override
public int startCaptureVideo(String cameraId, Surface surface) {
Log.d(TAG, "startCaptureVideo." + cameraId);
if (ExtSurfaceRender.getInstance().isEGLContextReady()) {
Log.d(TAG, "startCaptureVideo.");
ExtSurfaceRender.getInstance().startRendering(surface);
}
return 0;
}
@Override
public int stopCaptureVideo(String cameraId) {
Log.d(TAG, "stopCaptureVideo.");
ExtSurfaceRender.getInstance().stopRendering();
return 0;
}
@Override
public int notifyUnbindMSDPService() {
Log.d(TAG, "notifyUnbindMSDPService.");
HwDmsdpService.release();
return 0;
}
@Override
public void checkPermissionOnCallback(int result) {
Log.d(TAG, "checkPermissionOnCallback.result: " + result);
}
}
CaasKitHelper.java
public class CaasKitHelper {
...
private static final String BACKGROUND_COLOR = "#FF4081";
private static final int VIEWHEIGHT = 248;
private static final int VIEWWIDTH = 256;
private static final int LOCATION_X = 102;
private static final int LOCATION_Y = 40;
private static final int LOCATION_STARTY = 24;
private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() {
@Override
public void initSuccess(HwCaasHandler handler) {
Log.i(TAG, ":HwCaasServiceCallBack:initSuccess");
/** Callback after successful initialization of HwCaasHandler. */
mHwCaasHandler = handler;
if (mHwCaasHandler != null) {
mHwCaasHandler.setContactViewSize(VIEWWIDTH, VIEWHEIGHT);
mHwCaasHandler.setAppMode(HwCaasUtils.LANDSCAPE);
mHwCaasHandler.setStartViewBackgroundColor(BACKGROUND_COLOR);
boolean isSetSuccess = false;
isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.STARTVIEW,
HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_STARTY);
Log.i(TAG, "viewType: " + HwCaasUtils.STARTVIEW + " isSetSuccess: " + isSetSuccess);
isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CONTACTVIEW, HwCaasUtils.POINT_RIGHTANDUP, LOCATION_X, LOCATION_Y);
Log.i(TAG, "viewType: " + HwCaasUtils.CONTACTVIEW + " isSetSuccess: " + isSetSuccess);
isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CALLVIEW, HwCaasUtils.POINT_RIGHTANDUP,
LOCATION_X, LOCATION_Y);
Log.i(TAG, "viewType: " + HwCaasUtils.CALLVIEW + " isSetSuccess: " + isSetSuccess);
isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.VIDEOVIEW, HwCaasUtils.POINT_RIGHTANDUP,
LOCATION_X, LOCATION_Y);
Log.i(TAG, "viewType: " + HwCaasUtils.VIDEOVIEW + " isSetSuccess: " + isSetSuccess);
if (mIsSendShowFail) {
sendShow();
mIsSendShowFail = false;
}
}
}
@Override
public void initFail(int retCode) {
/** Callback if init Handler fail. */
Log.i(TAG, "retCode: " + retCode);
}
@Override
public void releaseSuccess() {
Log.i(TAG, ":HwCaasServiceCallBack:releaseSuccess");
/** Callback after successful release of mHwCaasServiceManager. */
mHwCaasHandler = null;
}
};
...
}
CaasKitHelper.java
public class CaasKitHelper {
...
public boolean sendShow() {
Log.d(TAG, "sendShow.");
if (mHwCaasHandler != null) {
/** Send SHOW event to show suspension ball. */
boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SHOW);
Log.d(TAG, "isSendoK: " + isSendoK);
return isSendoK;
}
mIsSendShowFail = true;
Log.e(TAG, "sendShow fail.");
return false;
}
public boolean sendHide() {
Log.d(TAG, "sendHide.");
if (mHwCaasHandler != null) {
/** Send HIDE event to hide suspension ball. */
boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE);
Log.d(TAG, "isSendoK: " + isSendoK);
return isSendoK;
}
Log.e(TAG, "sendHide fail.");
return false;
}
...
}
CaasKitHelper.java
public class CaasKitHelper {
...
public void caasKitRelease() {
Log.d(TAG, "caasKitRelease." + mIsCaasKitInit);
if (mIsCaasKitInit) {
if (mHwCaasServiceManager != null) {
/** Source release */
mHwCaasServiceManager.release();
mHwCaasServiceManager = null;
}
unregisterDiscoverReceiver();
mIsCaasKitInit = false;
}
}
...
}
干得好,你已经成功完成了Codelab并学到了:
您可以阅读下面链接,了解更多相关的信息。
相关文档