PeerConnectionFactory/PeerConnection:整個WebRTC中最核心的類,有了這個類才能獲得音視訊相關的其他操作。
PeerConnectionFactory類中包含了各種音視訊資料的初始化。
PeerConnectionFactory.initializeAndroidGlobals 中初始化了是否初始化音視訊,是否硬體加速,是否支援硬體渲染等内容。
PeerConnectionFactory簡化的類圖如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCM581dvRWYoNHLwEzX5xCMx8FesU2cfdGLwATMfRHLGZkRGZkRfJ3bs92YskmNhVTYykVNQJVMRhXVEF1X0hXZ0xiNx8VZ6l2cssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLzgDO5gDO2gTOmhjY1U2NxYzX3AjMwcTMxMzLcBTMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL2M3Lc9CX6MHc0RHaiojIsJye.png)
擷取媒體流
第一步:擷取視訊源videoSource
String frontCameraName = VideoCapturerAndroid.getNameOfFrontFacingDevice();
VideoCapturer videoCapturer = VideoCapturerAndroid.create(frontCameraName);
VideoSource videoSource = factory.createVideoSource(videoCapturer,videoConstraints);
其中videoConstraints是對視訊流的一些限制,按如下方法建立。
MediaConstraints videoConstraints = new MediaConstraints();
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", Integer.toString(pcParams.videoHeight)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", Integer.toString(pcParams.videoWidth)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxFrameRate", Integer.toString(pcParams.videoFps)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minFrameRate", Integer.toString(pcParams.videoFps)));
第二步:擷取音頻源audioSource
音頻源的擷取簡單許多:
AudioSource audioSource = factory.createAudioSource(new MediaConstraints());