天天看點

WebRTC源碼級深度解析,進階大廠進階音視訊開發者

PeerConnectionFactory/PeerConnection:整個WebRTC中最核心的類,有了這個類才能獲得音視訊相關的其他操作。

PeerConnectionFactory類中包含了各種音視訊資料的初始化。

PeerConnectionFactory.initializeAndroidGlobals 中初始化了是否初始化音視訊,是否硬體加速,是否支援硬體渲染等内容。

PeerConnectionFactory簡化的類圖如下:

WebRTC源碼級深度解析,進階大廠進階音視訊開發者

擷取媒體流

第一步:擷取視訊源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());      

​​​​