对于初次接触 WebRTC 的小白来说,想要理解如何建立一个点对点连接还是有一定的难度的,虽然网上的点对点连接 demo 一大堆,但是从代码角度去理解还是有一定的难度,还好 不仅提供了源代码,而且还添加了详细的日志信息,因此打算从运行的日志角度,再结合网上比较好的文章来理解分析,希望这个思路对您也有帮助!
先拜读一下“” 的WebRTC 入门文章,我感觉是WebRTC 中文文章中比较 OK 的。《》《》《》《》一、获取点对点日志1、使用Google 浏览器访问:,提供的“Basic peer connection” demo2、鼠标右键,点击“审查元素”,打开 "Console" 控制台3、点击“Basic peer connection” demo 页面的“Start”、“Call”按钮,建立一个点对点连接(基于同一个Web 页面,模拟点对点连接)4、在Consle 控制台,鼠标右键,点击“Save as”保存运行日志 二、日志信息与分析:This appears to be Chrome12.722: Requesting local stream(请求本地音视频流,即调用本地的摄像头与麦克风)spec: {"audio":true,"video":true}chrome: {"audio":true,"video":true}chrome: {"audio":true,"video":true}14.022: Received local stream(调用本地的摄像头与麦克风成功,获得音视频流 stream 对象)15.321: Local video videoWidth: 640px, videoHeight: 480px98.136: Starting call(呼叫远程 peer)98.137: Using video device: Lenovo EasyCamera (174f:14ee)98.137: Using audio device: 麦克风 (Realtek High Definition Audio)98.137: Created local peer connection object pc1(创建本地的 PeerConnection 对象 pc1)98.141: Created remote peer connection object pc2(创建远程的 PeerConnection 对象 pc1)98.143: Added local stream to pc1(本地音视频流添加到PeerConnection 对象 pc1 对象)98.144: pc1 createOffer start(pc1 开始创建 Offer 信令成功)98.146: Offer from pc1 (pc1 创建 Offer 信令成功,如下所示)v=0o=- 1942065961337070347 2 IN IP4 127.0.0.1s=-t=0 0a=group:BUNDLE audio videoa=msid-semantic: WMS rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23pm=audio 9 RTP/SAVPF 111 103 104 9 0 8 106 105 13 126c=IN IP4 0.0.0.0a=rtcp:9 IN IP4 0.0.0.0a=ice-ufrag:33zghkUynpdQ7JDGa=ice-pwd:gA6+vMk0K/TpuJoHN7QNTWSwa=fingerprint:sha-256 65:84:AE:4D:4C:75:21:15B:3E:57:62:A4:17:EB:C8:22:E1:FA:5B:6A:38:FB:A6:9E:E01:EA:B2:E1:34:49
a=setup:actpassa=mid:audioa=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-levela=extmap:3 a=sendrecva=rtcp-muxa=rtpmap:111 opus/48000/2a=fmtp:111 minptime=10; useinbandfec=1a=rtpmap:103 ISAC/16000a=rtpmap:104 ISAC/32000a=rtpmap:9 G722/8000a=rtpmap:0 PCMU/8000a=rtpmap:8 PCMA/8000a=rtpmap:106 CN/32000a=rtpmap:105 CN/16000a=rtpmap:13 CN/8000a=rtpmap:126 telephone-event/8000a=maxptime:60a=ssrc:2847625965 cname:FPxGs1+4IS0LJpcRa=ssrc:2847625965 msid:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23p d21f0789-d0f7-4b04-b9ee-134b56bd77a3a=ssrc:2847625965 mslabel:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23pa=ssrc:2847625965 label:d21f0789-d0f7-4b04-b9ee-134b56bd77a3m=video 9 RTP/SAVPF 100 116 117 96c=IN IP4 0.0.0.0a=rtcp:9 IN IP4 0.0.0.0a=ice-ufrag:33zghkUynpdQ7JDGa=ice-pwd:gA6+vMk0K/TpuJoHN7QNTWSwa=fingerprint:sha-256 65:84:AE:4D:4C:75:21:15B:3E:57:62:A4:17:EB:C8:22:E1:FA:5B:6A:38:FB:A6:9E:E01:EA:B2:E1:34:49
a=setup:actpassa=mid:videoa=extmap:2 urn:ietf:params:rtp-hdrext:toffseta=extmap:3 a=extmap:4 urn:3gpp:video-orientationa=sendrecva=rtcp-muxa=rtpmap:100 VP8/90000a=rtcp-fb:100 ccm fira=rtcp-fb:100 nacka=rtcp-fb:100 nack plia=rtcp-fb:100 goog-remba=rtpmap:116 red/90000a=rtpmap:117 ulpfec/90000a=rtpmap:96 rtx/90000a=fmtp:96 apt=100a=ssrc-group:FID 2259890580 1253285440a=ssrc:2259890580 cname:FPxGs1+4IS0LJpcRa=ssrc:2259890580 msid:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23p c402a35d-4298-4a6b-ad8a-c4d6dbfde67fa=ssrc:2259890580 mslabel:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23pa=ssrc:2259890580 label:c402a35d-4298-4a6b-ad8a-c4d6dbfde67fa=ssrc:1253285440 cname:FPxGs1+4IS0LJpcRa=ssrc:1253285440 msid:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23p c402a35d-4298-4a6b-ad8a-c4d6dbfde67fa=ssrc:1253285440 mslabel:rICA7eip7IWV3m6Yp08Hz5cA1AvhLrfQT23pa=ssrc:1253285440 label:c402a35d-4298-4a6b-ad8a-c4d6dbfde67f98.147: pc1 setLocalDescription start(pc1 对象将 offer信令通过服务器发送给远程的 pc2 对象)98.148: pc2 setRemoteDescription start(pc2 接收到 pc1 的 Offer 信令并提取其中所包含的SDP 描述符)98.149: pc2 createAnswer start(pc2 创建一个包含远程 peer 的SDP 描述符的 Answer 信令)98.178: pc1 setLocalDescription complete(pc1 发送 Offer 信令完毕)98.178: pc2 setRemoteDescription complete(pc2 接收 Offer 信令完毕)98.179: Answer from pc2:(pc2 开始创建 Answer 信令成功,如下所示)v=0o=- 5093582210222064371 2 IN IP4 127.0.0.1s=-t=0 0a=group:BUNDLE audio videoa=msid-semantic: WMSm=audio 9 RTP/SAVPF 111 103 104 9 0 8 106 105 13 126c=IN IP4 0.0.0.0a=rtcp:9 IN IP4 0.0.0.0a=ice-ufrag:ubQcfltTHAop9S73a=ice-pwd:tM0zrbhW4uKe+h8Y9hVYkRQKa=fingerprint:sha-256 65:84:AE:4D:4C:75:21:15B:3E:57:62:A4:17:EB:C8:22:E1:FA:5B:6A:38:FB:A6:9E:E01:EA:B2:E1:34:49
a=setup:activea=mid:audioa=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-levela=extmap:3 a=recvonlya=rtcp-muxa=rtpmap:111 opus/48000/2a=fmtp:111 minptime=10; useinbandfec=1a=rtpmap:103 ISAC/16000a=rtpmap:104 ISAC/32000a=rtpmap:9 G722/8000a=rtpmap:0 PCMU/8000a=rtpmap:8 PCMA/8000a=rtpmap:106 CN/32000a=rtpmap:105 CN/16000a=rtpmap:13 CN/8000a=rtpmap:126 telephone-event/8000a=maxptime:60m=video 9 RTP/SAVPF 100 116 117 96c=IN IP4 0.0.0.0a=rtcp:9 IN IP4 0.0.0.0a=ice-ufrag:ubQcfltTHAop9S73a=ice-pwd:tM0zrbhW4uKe+h8Y9hVYkRQKa=fingerprint:sha-256 65:84:AE:4D:4C:75:21:15B:3E:57:62:A4:17:EB:C8:22:E1:FA:5B:6A:38:FB:A6:9E:E01:EA:B2:E1:34:49
a=setup:activea=mid:videoa=extmap:2 urn:ietf:params:rtp-hdrext:toffseta=extmap:3 a=extmap:4 urn:3gpp:video-orientationa=recvonlya=rtcp-muxa=rtpmap:100 VP8/90000a=rtcp-fb:100 ccm fira=rtcp-fb:100 nacka=rtcp-fb:100 nack plia=rtcp-fb:100 goog-remba=rtpmap:116 red/90000a=rtpmap:117 ulpfec/90000a=rtpmap:96 rtx/90000a=fmtp:96 apt=10098.180: pc2 setLocalDescription start(远程的pc2 对象将 Answer 信令通过服务器发送给本地的 pc1 对象)98.182: pc1 setRemoteDescription start(pc1 接收到 pc2 的 Answer 信令并提取其中所包含的SDP 描述符)98.203: pc1 ICE candidate: candidate:3031090232 1 udp 2122260223 192.168.1.20 52118 typ host generation 098.203: pc1 ICE candidate: candidate:3031090232 2 udp 2122260222 192.168.1.20 52119 typ host generation 098.204: pc2 received remote stream(pc2 或得到 pc1 音视频流)98.204: pc2 setLocalDescription complete(pc2 发送 Answer 信令完毕)98.205: pc1 setRemoteDescription complete(pc1 接收 Answer 信令完毕)98.205: pc1 addIceCandidate success (pc1 添加 IceCandidate 成功)98.206: pc2 ICE state: checkingICE state change event: Event {}98.239: pc2 ICE candidate: candidate:3031090232 1 udp 2122260223 192.168.1.20 52122 typ host generation 098.240: pc2 ICE candidate: candidate:3031090232 1 udp 2122260223 192.168.1.20 52122 typ host generation 098.242: pc1 ICE candidate: candidate:3031090232 1 udp 2122260223 192.168.1.20 52120 typ host generation 098.249: pc1 ICE candidate: candidate:3031090232 2 udp 2122260222 192.168.1.20 52121 typ host generation 098.249: pc1 ICE state: checkingICE state change event: Event {}98.250: pc2 addIceCandidate success(pc2 添加 IceCandidate 成功)98.250: pc1 addIceCandidate success98.272: pc1 ICE state: completedICE state change event: Event {}98.275: pc1 ICE state: completedICE state change event: Event {}98.279: pc2 ICE state: connectedICE state change event: Event {}98.349: Remote video size changed to 640x48098.349: Setup time: 212.435ms98.349: Remote video videoWidth: 640px, videoHeight: 480px98.350: Remote video size changed to 640x480135.775: Ending call