瀏覽代碼

sdp: refactor jingle2media

using more specific selectors
tags/v0.0.2
Philipp Hancke 5 年之前
父節點
當前提交
72c00b6a4d
共有 2 個文件被更改,包括 45 次插入46 次删除
  1. 41
    42
      modules/xmpp/SDP.js
  2. 4
    4
      modules/xmpp/SDP.spec.js

+ 41
- 42
modules/xmpp/SDP.js 查看文件

578
 
578
 
579
 // translate a jingle content element into an an SDP media part
579
 // translate a jingle content element into an an SDP media part
580
 SDP.prototype.jingle2media = function(content) {
580
 SDP.prototype.jingle2media = function(content) {
581
-    const desc = content.find('description');
581
+    const desc = content.find('>description');
582
+    const transport = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
582
     let media = '';
583
     let media = '';
583
-    const sctp = content.find(
584
-        '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
584
+    const sctp = transport.find(
585
+        '>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
585
 
586
 
586
     let tmp = { media: desc.attr('media') };
587
     let tmp = { media: desc.attr('media') };
587
 
588
 
590
         // estos hack to reject an m-line.
591
         // estos hack to reject an m-line.
591
         tmp.port = '0';
592
         tmp.port = '0';
592
     }
593
     }
593
-    if (content.find('>transport>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
594
+    if (transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
594
         tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
595
         tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
595
     } else {
596
     } else {
596
         tmp.proto = 'RTP/AVPF';
597
         tmp.proto = 'RTP/AVPF';
610
     } else {
611
     } else {
611
         tmp.fmt
612
         tmp.fmt
612
             = desc
613
             = desc
613
-                .find('payload-type')
614
+                .find('>payload-type')
614
                 .map((_, payloadType) => payloadType.getAttribute('id'))
615
                 .map((_, payloadType) => payloadType.getAttribute('id'))
615
                 .get();
616
                 .get();
616
         media += `${SDPUtil.buildMLine(tmp)}\r\n`;
617
         media += `${SDPUtil.buildMLine(tmp)}\r\n`;
620
     if (!sctp.length) {
621
     if (!sctp.length) {
621
         media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
622
         media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
622
     }
623
     }
623
-    tmp
624
-        = content.find(
625
-            '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
626
-    if (tmp.length) {
627
-        if (tmp.attr('ufrag')) {
628
-            media += `${SDPUtil.buildICEUfrag(tmp.attr('ufrag'))}\r\n`;
624
+
625
+    // XEP-0176 ICE parameters
626
+    if (transport.length) {
627
+        if (transport.attr('ufrag')) {
628
+            media += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
629
         }
629
         }
630
-        if (tmp.attr('pwd')) {
631
-            media += `${SDPUtil.buildICEPwd(tmp.attr('pwd'))}\r\n`;
630
+        if (transport.attr('pwd')) {
631
+            media += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
632
         }
632
         }
633
-        tmp.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
633
+        transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
634
             media += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
634
             media += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
635
             media += ` ${$(fingerprint).text()}`;
635
             media += ` ${$(fingerprint).text()}`;
636
             media += '\r\n';
636
             media += '\r\n';
639
             }
639
             }
640
         });
640
         });
641
     }
641
     }
642
+
643
+    // XEP-0176 ICE candidates
644
+    transport.find('>candidate')
645
+        .each((_, candidate) => {
646
+            let protocol = candidate.getAttribute('protocol');
647
+
648
+            protocol
649
+                = typeof protocol === 'string' ? protocol.toLowerCase() : '';
650
+
651
+            if ((this.removeTcpCandidates
652
+                    && (protocol === 'tcp' || protocol === 'ssltcp'))
653
+                || (this.removeUdpCandidates && protocol === 'udp')) {
654
+                return;
655
+            } else if (this.failICE) {
656
+                candidate.setAttribute('ip', '1.1.1.1');
657
+            }
658
+
659
+            media += SDPUtil.candidateFromJingle(candidate);
660
+        });
661
+
642
     switch (content.attr('senders')) {
662
     switch (content.attr('senders')) {
643
     case 'initiator':
663
     case 'initiator':
644
         media += 'a=sendonly\r\n';
664
         media += 'a=sendonly\r\n';
659
     // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
679
     // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
660
     // though
680
     // though
661
     // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
681
     // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
662
-    if (desc.find('rtcp-mux').length) {
682
+    if (desc.find('>rtcp-mux').length) {
663
         media += 'a=rtcp-mux\r\n';
683
         media += 'a=rtcp-mux\r\n';
664
     }
684
     }
665
 
685
 
666
-    desc.find('payload-type').each((_, payloadType) => {
686
+    desc.find('>payload-type').each((_, payloadType) => {
667
         media += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
687
         media += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
668
         if ($(payloadType).find('>parameter').length) {
688
         if ($(payloadType).find('>parameter').length) {
669
             media += `a=fmtp:${payloadType.getAttribute('id')} `;
689
             media += `a=fmtp:${payloadType.getAttribute('id')} `;
670
             media
690
             media
671
                 += $(payloadType)
691
                 += $(payloadType)
672
-                    .find('parameter')
692
+                    .find('>parameter')
673
                     .map((__, parameter) => {
693
                     .map((__, parameter) => {
674
                         const name = parameter.getAttribute('name');
694
                         const name = parameter.getAttribute('name');
675
 
695
 
699
                 hdrExt.getAttribute('uri')}\r\n`;
719
                 hdrExt.getAttribute('uri')}\r\n`;
700
     });
720
     });
701
 
721
 
702
-    content
703
-        .find(
704
-            '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]'
705
-                + '>candidate')
706
-        .each((_, transport) => {
707
-            let protocol = transport.getAttribute('protocol');
708
-
709
-            protocol
710
-                = typeof protocol === 'string' ? protocol.toLowerCase() : '';
711
-
712
-            if ((this.removeTcpCandidates
713
-                    && (protocol === 'tcp' || protocol === 'ssltcp'))
714
-                || (this.removeUdpCandidates && protocol === 'udp')) {
715
-                return;
716
-            } else if (this.failICE) {
717
-                transport.setAttribute('ip', '1.1.1.1');
718
-            }
719
-
720
-            media += SDPUtil.candidateFromJingle(transport);
721
-        });
722
-
723
     // XEP-0339 handle ssrc-group attributes
722
     // XEP-0339 handle ssrc-group attributes
724
-    content
725
-        .find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
723
+    desc
724
+        .find('>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
726
         .each((_, ssrcGroup) => {
725
         .each((_, ssrcGroup) => {
727
             const semantics = ssrcGroup.getAttribute('semantics');
726
             const semantics = ssrcGroup.getAttribute('semantics');
728
             const ssrcs
727
             const ssrcs
737
         });
736
         });
738
 
737
 
739
     tmp
738
     tmp
740
-        = content.find(
741
-            'description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
739
+        = desc.find(
740
+            '>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
742
     tmp.each((_, source) => {
741
     tmp.each((_, source) => {
743
         const ssrc = source.getAttribute('ssrc');
742
         const ssrc = source.getAttribute('ssrc');
744
 
743
 

+ 4
- 4
modules/xmpp/SDP.spec.js 查看文件

183
 a=ice-pwd:somepwd
183
 a=ice-pwd:somepwd
184
 a=fingerprint:sha-256 09:B1:51:0F:85:4C:80:19:A1:AF:81:73:47:EE:ED:3D:00:3A:84:C7:76:C1:4E:34:BE:56:F6:42:AD:15:D5:D7
184
 a=fingerprint:sha-256 09:B1:51:0F:85:4C:80:19:A1:AF:81:73:47:EE:ED:3D:00:3A:84:C7:76:C1:4E:34:BE:56:F6:42:AD:15:D5:D7
185
 a=setup:actpass
185
 a=setup:actpass
186
+a=candidate:1 1 udp 2130706431 10.0.0.1 10000 typ host generation 0
187
+a=candidate:2 1 udp 1694498815 10.0.0.2 10000 typ srflx raddr 10.0.0.1 rport 10000 generation 0
186
 a=sendrecv
188
 a=sendrecv
187
 a=mid:audio
189
 a=mid:audio
188
 a=rtcp-mux
190
 a=rtcp-mux
195
 a=fmtp:126 0-15
197
 a=fmtp:126 0-15
196
 a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
198
 a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
197
 a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
199
 a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
198
-a=candidate:1 1 udp 2130706431 10.0.0.1 10000 typ host generation 0
199
-a=candidate:2 1 udp 1694498815 10.0.0.2 10000 typ srflx raddr 10.0.0.1 rport 10000 generation 0
200
 a=ssrc:4039389863 cname:mixed
200
 a=ssrc:4039389863 cname:mixed
201
 a=ssrc:4039389863 label:mixedlabelaudio0
201
 a=ssrc:4039389863 label:mixedlabelaudio0
202
 a=ssrc:4039389863 msid:mixedmslabel mixedlabelaudio0
202
 a=ssrc:4039389863 msid:mixedmslabel mixedlabelaudio0
208
 a=ice-pwd:somepwd
208
 a=ice-pwd:somepwd
209
 a=fingerprint:sha-256 09:B1:51:0F:85:4C:80:19:A1:AF:81:73:47:EE:ED:3D:00:3A:84:C7:76:C1:4E:34:BE:56:F6:42:AD:15:D5:D7
209
 a=fingerprint:sha-256 09:B1:51:0F:85:4C:80:19:A1:AF:81:73:47:EE:ED:3D:00:3A:84:C7:76:C1:4E:34:BE:56:F6:42:AD:15:D5:D7
210
 a=setup:actpass
210
 a=setup:actpass
211
+a=candidate:1 1 udp 2130706431 10.0.0.1 10000 typ host generation 0
212
+a=candidate:2 1 udp 1694498815 10.0.0.2 10000 typ srflx raddr 10.0.0.1 rport 10000 generation 0
211
 a=sendrecv
213
 a=sendrecv
212
 a=mid:video
214
 a=mid:video
213
 a=rtcp-mux
215
 a=rtcp-mux
225
 a=rtcp-fb:96 nack pli
227
 a=rtcp-fb:96 nack pli
226
 a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
228
 a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
227
 a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
229
 a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
228
-a=candidate:1 1 udp 2130706431 10.0.0.1 10000 typ host generation 0
229
-a=candidate:2 1 udp 1694498815 10.0.0.2 10000 typ srflx raddr 10.0.0.1 rport 10000 generation 0
230
 a=ssrc:3758540092 cname:mixed
230
 a=ssrc:3758540092 cname:mixed
231
 a=ssrc:3758540092 label:mixedlabelvideo0
231
 a=ssrc:3758540092 label:mixedlabelvideo0
232
 a=ssrc:3758540092 msid:mixedmslabel mixedlabelvideo0
232
 a=ssrc:3758540092 msid:mixedmslabel mixedlabelvideo0

Loading…
取消
儲存