1
- var codecs = { } ;
2
-
3
- /* this function credit goes to Google Chrome WebRTC team! */
4
- codecs . opus = function ( sessionDescription ) {
5
-
6
- /* no opus? use other codec! */
7
- if ( ! isopus ) return sessionDescription ;
8
-
9
- var sdp = sessionDescription . sdp ;
10
-
11
- /* Opus? use it! */
12
- function preferOpus ( ) {
13
- var sdpLines = sdp . split ( '\r\n' ) ;
14
-
15
- // Search for m line.
16
- for ( var i = 0 ; i < sdpLines . length ; i ++ ) {
17
- if ( sdpLines [ i ] . search ( 'm=audio' ) !== - 1 ) {
18
- var mLineIndex = i ;
19
- break ;
20
- }
21
- }
22
- if ( mLineIndex === null )
23
- return sdp ;
24
-
25
- // If Opus is available, set it as the default in m line.
26
- for ( i = 0 ; i < sdpLines . length ; i ++ ) {
27
- if ( sdpLines [ i ] . search ( 'opus/48000' ) !== - 1 ) {
28
- var opusPayload = extractSdp ( sdpLines [ i ] , / : ( \d + ) o p u s \/ 4 8 0 0 0 / i) ;
29
- if ( opusPayload )
30
- sdpLines [ mLineIndex ] = setDefaultCodec ( sdpLines [ mLineIndex ] , opusPayload ) ;
31
- break ;
32
- }
33
- }
34
-
35
- // Remove CN in m line and sdp.
36
- sdpLines = removeCN ( sdpLines , mLineIndex ) ;
37
-
38
- sdp = sdpLines . join ( '\r\n' ) ;
39
- return sdp ;
40
- }
41
-
42
- function extractSdp ( sdpLine , pattern ) {
43
- var _result = sdpLine . match ( pattern ) ;
44
- return ( _result && _result . length == 2 ) ? _result [ 1 ] : null ;
45
- }
46
-
47
- // Set the selected codec to the first in m line.
48
- function setDefaultCodec ( mLine , payload ) {
49
- var elements = mLine . split ( ' ' ) ;
50
- var newLine = new Array ( ) ;
51
- var index = 0 ;
52
- for ( var i = 0 ; i < elements . length ; i ++ ) {
53
- if ( index === 3 ) // Format of media starts from the fourth.
54
- newLine [ index ++ ] = payload ; // Put target payload to the first.
55
- if ( elements [ i ] !== payload )
56
- newLine [ index ++ ] = elements [ i ] ;
57
- }
58
- return newLine . join ( ' ' ) ;
59
- }
60
-
61
- // Strip CN from sdp before CN constraints is ready.
62
- function removeCN ( sdpLines , mLineIndex ) {
63
- var mLineElements = sdpLines [ mLineIndex ] . split ( ' ' ) ;
64
- // Scan from end for the convenience of removing an item.
65
- for ( var i = sdpLines . length - 1 ; i >= 0 ; i -- ) {
66
- var payload = extractSdp ( sdpLines [ i ] , / a = r t p m a p : ( \d + ) C N \/ \d + / i) ;
67
- if ( payload ) {
68
- var cnPos = mLineElements . indexOf ( payload ) ;
69
- if ( cnPos !== - 1 ) {
70
- // Remove CN payload from m line.
71
- mLineElements . splice ( cnPos , 1 ) ;
72
- }
73
- // Remove CN line in sdp
74
- sdpLines . splice ( i , 1 ) ;
75
- }
76
- }
77
-
78
- sdpLines [ mLineIndex ] = mLineElements . join ( ' ' ) ;
79
- return sdpLines ;
80
- }
81
-
82
-
83
- var result ;
84
-
85
- /* in case of error; use default codec; otherwise use opus */
86
- try {
87
- result = preferOpus ( ) ;
88
- console . log ( 'using opus codec!' ) ;
89
- }
90
- catch ( e ) {
91
- console . error ( e ) ;
92
- result = sessionDescription . sdp ;
93
-
94
- window . messenger && window . messenger . deliver ( e . stack + '\n\n Location: ' + location . href + '\n UserAgen: ' + navigator . userAgent ) ;
95
- }
96
-
97
- return new window . SessionDescription ( {
98
- sdp : result ,
99
- type : sessionDescription . type
100
- } ) ;
101
- } ;
102
-
103
- /* check support of opus codec */
104
- codecs . isopus = function ( ) {
105
- var result = true ;
106
- new PeerConnection ( defaults . iceServers ) . createOffer ( function ( sessionDescription ) {
107
- result = sessionDescription . sdp . indexOf ( 'opus' ) !== - 1 ;
108
- } , null , defaults . constraints ) ;
109
- return result ;
110
- } ;
111
-
112
- /* used to know opus codec support */
1
+ /* MIT License: https://webrtc-experiment.appspot.com/licence/ */
2
+
3
+ var codecs = { } ;
4
+
5
+ /* this function credit goes to Google Chrome WebRTC team! */
6
+ codecs . opus = function ( sessionDescription ) {
7
+
8
+ /* no opus? use other codec! */
9
+ if ( ! isopus ) return sessionDescription ;
10
+
11
+ var sdp = sessionDescription . sdp ;
12
+
13
+ /* Opus? use it! */
14
+ function preferOpus ( ) {
15
+ var sdpLines = sdp . split ( '\r\n' ) ;
16
+
17
+ // Search for m line.
18
+ for ( var i = 0 ; i < sdpLines . length ; i ++ ) {
19
+ if ( sdpLines [ i ] . search ( 'm=audio' ) !== - 1 ) {
20
+ var mLineIndex = i ;
21
+ break ;
22
+ }
23
+ }
24
+ if ( mLineIndex === null )
25
+ return sdp ;
26
+
27
+ // If Opus is available, set it as the default in m line.
28
+ for ( i = 0 ; i < sdpLines . length ; i ++ ) {
29
+ if ( sdpLines [ i ] . search ( 'opus/48000' ) !== - 1 ) {
30
+ var opusPayload = extractSdp ( sdpLines [ i ] , / : ( \d + ) o p u s \/ 4 8 0 0 0 / i) ;
31
+ if ( opusPayload )
32
+ sdpLines [ mLineIndex ] = setDefaultCodec ( sdpLines [ mLineIndex ] , opusPayload ) ;
33
+ break ;
34
+ }
35
+ }
36
+
37
+ // Remove CN in m line and sdp.
38
+ sdpLines = removeCN ( sdpLines , mLineIndex ) ;
39
+
40
+ sdp = sdpLines . join ( '\r\n' ) ;
41
+ return sdp ;
42
+ }
43
+
44
+ function extractSdp ( sdpLine , pattern ) {
45
+ var _result = sdpLine . match ( pattern ) ;
46
+ return ( _result && _result . length == 2 ) ? _result [ 1 ] : null ;
47
+ }
48
+
49
+ // Set the selected codec to the first in m line.
50
+ function setDefaultCodec ( mLine , payload ) {
51
+ var elements = mLine . split ( ' ' ) ;
52
+ var newLine = new Array ( ) ;
53
+ var index = 0 ;
54
+ for ( var i = 0 ; i < elements . length ; i ++ ) {
55
+ if ( index === 3 ) // Format of media starts from the fourth.
56
+ newLine [ index ++ ] = payload ; // Put target payload to the first.
57
+ if ( elements [ i ] !== payload )
58
+ newLine [ index ++ ] = elements [ i ] ;
59
+ }
60
+ return newLine . join ( ' ' ) ;
61
+ }
62
+
63
+ // Strip CN from sdp before CN constraints is ready.
64
+ function removeCN ( sdpLines , mLineIndex ) {
65
+ var mLineElements = sdpLines [ mLineIndex ] . split ( ' ' ) ;
66
+ // Scan from end for the convenience of removing an item.
67
+ for ( var i = sdpLines . length - 1 ; i >= 0 ; i -- ) {
68
+ var payload = extractSdp ( sdpLines [ i ] , / a = r t p m a p : ( \d + ) C N \/ \d + / i) ;
69
+ if ( payload ) {
70
+ var cnPos = mLineElements . indexOf ( payload ) ;
71
+ if ( cnPos !== - 1 ) {
72
+ // Remove CN payload from m line.
73
+ mLineElements . splice ( cnPos , 1 ) ;
74
+ }
75
+ // Remove CN line in sdp
76
+ sdpLines . splice ( i , 1 ) ;
77
+ }
78
+ }
79
+
80
+ sdpLines [ mLineIndex ] = mLineElements . join ( ' ' ) ;
81
+ return sdpLines ;
82
+ }
83
+
84
+
85
+ var result ;
86
+
87
+ /* in case of error; use default codec; otherwise use opus */
88
+ try {
89
+ result = preferOpus ( ) ;
90
+ console . log ( 'using opus codec!' ) ;
91
+ }
92
+ catch ( e ) {
93
+ console . error ( e ) ;
94
+ result = sessionDescription . sdp ;
95
+
96
+ window . messenger && window . messenger . deliver ( e . stack + '\n\n Location: ' + location . href + '\n UserAgen: ' + navigator . userAgent ) ;
97
+ }
98
+
99
+ return new window . SessionDescription ( {
100
+ sdp : result ,
101
+ type : sessionDescription . type
102
+ } ) ;
103
+ } ;
104
+
105
+ /* check support of opus codec */
106
+ codecs . isopus = function ( ) {
107
+ var result = true ;
108
+ new PeerConnection ( defaults . iceServers ) . createOffer ( function ( sessionDescription ) {
109
+ result = sessionDescription . sdp . indexOf ( 'opus' ) !== - 1 ;
110
+ } , null , defaults . constraints ) ;
111
+ return result ;
112
+ } ;
113
+
114
+ /* used to know opus codec support */
113
115
var isopus = ! ! codecs . isopus ( ) ;
0 commit comments