OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Copyright 2016 The Chromium Authors. All rights reserved. | 2 * Copyright 2016 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /** | 7 /** |
8 * Parses the SDP to find all streams, tracks and stream-track relationships. | |
9 * Returns "{ streamIds, trackIds, streamTrackMap }" where |streamIds| and | |
10 * |trackIds| are |Set| of IDs and |streamTrackMap| is a string-set |Map| from | |
11 * stream IDs to track IDs (which tracks are members of which streams). | |
12 * | |
13 * @param {string} sdp | |
14 */ | |
15 function getSdpStreamsAndTracks(sdp) { | |
Taylor_Brandstetter
2017/07/06 22:45:14
Relying on SDP parsing in this test is somewhat fr
hbos_chromium
2017/07/07 12:07:48
Good point. Done. Removed SDP usage from test, and
| |
16 let sdpLines = splitSdpLines(sdp); | |
17 let streamIds = new Set(); | |
18 let trackIds = new Set(); | |
19 // Find streams and tracks. | |
20 // a=ssrc:<ssrc> mslabel:<stream id> | |
21 // a=ssrc:<ssrc> label:<track id> | |
22 for (let lineNo = 0; lineNo = findLine(sdpLines, 'a=ssrc:', lineNo); | |
23 lineNo++) { | |
24 let line = sdpLines[lineNo]; | |
25 let tokens = line.split(' '); | |
26 if (tokens.length !== 2) | |
27 continue; | |
28 if (tokens[1].startsWith('mslabel:')) { | |
29 streamIds.add(tokens[1].substring(8)); | |
30 } else if (tokens[1].startsWith('label:')) { | |
31 trackIds.add(tokens[1].substring(6)); | |
32 } | |
33 } | |
34 // Find stream and track relationships. | |
35 // a=ssrc:<ssrc> msid:<stream id> <track id> | |
36 let streamTrackMap = new Map(); | |
37 for (let lineNo = 0; lineNo = findLine(sdpLines, 'a=ssrc:', lineNo); | |
38 lineNo++) { | |
39 let line = sdpLines[lineNo]; | |
40 let tokens = line.split(' '); | |
41 if (tokens.length !== 3) | |
42 continue; | |
43 let ssrc = parseInt(tokens[0].substring(7)); | |
44 if (!tokens[1].startsWith('msid:')) | |
45 failure('getSdpStreamsAndTracks', 'Expected \'msid:<stream id>\'.'); | |
46 let streamId = tokens[1].substring(5); | |
47 if (!streamIds.has(streamId)) { | |
48 failure( | |
49 'getSdpStreamsAndTracks', | |
50 'Unknown stream ID \'' + streamId + '\' (\'mslabel\' line missing).'); | |
51 } | |
52 let trackId = tokens[2]; | |
53 if (!trackIds.has(trackId)) { | |
54 failure('getSdpStreamsAndTracks', | |
55 'Unknown track ID \'' + trackId + '\' (\'label\' line missing).'); | |
56 } | |
57 if (!streamTrackMap.has(streamId)) | |
58 streamTrackMap.set(streamId, new Set([ trackId ])); | |
59 else | |
60 streamTrackMap.get(streamId).add(trackId); | |
61 } | |
62 return { streamIds:streamIds, | |
63 trackIds:trackIds, | |
64 streamTrackMap:streamTrackMap }; | |
65 } | |
66 | |
67 /** | |
8 * See |setSdpDefaultCodec|. | 68 * See |setSdpDefaultCodec|. |
9 */ | 69 */ |
10 function setSdpDefaultAudioCodec(sdp, codec) { | 70 function setSdpDefaultAudioCodec(sdp, codec) { |
11 return setSdpDefaultCodec(sdp, 'audio', codec); | 71 return setSdpDefaultCodec(sdp, 'audio', codec); |
12 } | 72 } |
13 | 73 |
14 /** | 74 /** |
15 * See |setSdpDefaultCodec|. | 75 * See |setSdpDefaultCodec|. |
16 */ | 76 */ |
17 function setSdpDefaultVideoCodec(sdp, codec) { | 77 function setSdpDefaultVideoCodec(sdp, codec) { |
18 return setSdpDefaultCodec(sdp, 'video', codec); | 78 return setSdpDefaultCodec(sdp, 'video', codec); |
19 } | 79 } |
20 | 80 |
21 /** | 81 /** |
22 * Returns a modified version of |sdp| where the opus DTX flag has been | 82 * Returns a modified version of |sdp| where the opus DTX flag has been |
23 * enabled. | 83 * enabled. |
24 */ | 84 */ |
25 function setOpusDtxEnabled(sdp) { | 85 function setOpusDtxEnabled(sdp) { |
26 var sdpLines = splitSdpLines(sdp); | 86 var sdpLines = splitSdpLines(sdp); |
27 | 87 |
28 // Get default audio codec | 88 // Get default audio codec |
29 var defaultCodec = getSdpDefaultAudioCodec(sdp); | 89 var defaultCodec = getSdpDefaultAudioCodec(sdp); |
30 if (defaultCodec !== 'opus') { | 90 if (defaultCodec !== 'opus') { |
31 failure('setOpusDtxEnabled', | 91 failure('setOpusDtxEnabled', |
32 'Default audio codec is not set to \'opus\'.'); | 92 'Default audio codec is not set to \'opus\'.'); |
33 } | 93 } |
34 | 94 |
35 // Find codec ID for Opus, e.g. 111 if 'a=rtpmap:111 opus/48000/2'. | 95 // Find codec ID for Opus, e.g. 111 if 'a=rtpmap:111 opus/48000/2'. |
36 var codecId = findRtpmapId(sdpLines, 'opus'); | 96 var codecId = findRtpmapId(sdpLines, 'opus'); |
37 if (codecId === null) { | 97 if (codecId === null) { |
38 failure('setOpusDtxEnabled', 'Unknown ID for |codec| = \'opus\'.'); | 98 failure('setOpusDtxEnabled', 'Unknown ID for |codec| = \'opus\'.'); |
39 } | 99 } |
40 | 100 |
41 // Find 'a=fmtp:111' line, where 111 is the codecId | 101 // Find 'a=fmtp:111' line, where 111 is the codecId |
42 var fmtLineNo = findFmtpLine(sdpLines, codecId); | 102 var fmtLineNo = findFmtpLine(sdpLines, codecId); |
(...skipping 14 matching lines...) Expand all Loading... | |
57 * the default codec, i.e. the codec whose ID is first in the list of codecs on | 117 * the default codec, i.e. the codec whose ID is first in the list of codecs on |
58 * the 'm=|type|' line, where |type| is 'audio' or 'video'. | 118 * the 'm=|type|' line, where |type| is 'audio' or 'video'. |
59 * @private | 119 * @private |
60 */ | 120 */ |
61 function setSdpDefaultCodec(sdp, type, codec) { | 121 function setSdpDefaultCodec(sdp, type, codec) { |
62 var sdpLines = splitSdpLines(sdp); | 122 var sdpLines = splitSdpLines(sdp); |
63 | 123 |
64 // Find codec ID, e.g. 100 for 'VP8' if 'a=rtpmap:100 VP8/9000'. | 124 // Find codec ID, e.g. 100 for 'VP8' if 'a=rtpmap:100 VP8/9000'. |
65 var codecId = findRtpmapId(sdpLines, codec); | 125 var codecId = findRtpmapId(sdpLines, codec); |
66 if (codecId === null) { | 126 if (codecId === null) { |
67 failure('sdpPreferCodec', 'Unknown ID for |codec| = \'' + codec + '\'.'); | 127 failure('setSdpDefaultCodec', |
128 'Unknown ID for |codec| = \'' + codec + '\'.'); | |
68 } | 129 } |
69 | 130 |
70 // Find 'm=|type|' line, e.g. 'm=video 9 UDP/TLS/RTP/SAVPF 100 101 107 116'. | 131 // Find 'm=|type|' line, e.g. 'm=video 9 UDP/TLS/RTP/SAVPF 100 101 107 116'. |
71 var mLineNo = findLine(sdpLines, 'm=' + type); | 132 var mLineNo = findLine(sdpLines, 'm=' + type); |
72 if (mLineNo === null) { | 133 if (mLineNo === null) { |
73 failure('setSdpDefaultCodec', | 134 failure('setSdpDefaultCodec', |
74 '\'m=' + type + '\' line missing from |sdp|.'); | 135 '\'m=' + type + '\' line missing from |sdp|.'); |
75 } | 136 } |
76 | 137 |
77 // Modify video line to use the desired codec as the default. | 138 // Modify video line to use the desired codec as the default. |
78 sdpLines[mLineNo] = setMLineDefaultCodec(sdpLines[mLineNo], codecId); | 139 sdpLines[mLineNo] = setMLineDefaultCodec(sdpLines[mLineNo], codecId); |
79 return mergeSdpLines(sdpLines); | 140 return mergeSdpLines(sdpLines); |
80 } | 141 } |
81 | 142 |
82 /** | 143 /** |
83 * See |getSdpDefaultCodec|. | 144 * See |getSdpDefaultCodec|. |
84 */ | 145 */ |
(...skipping 14 matching lines...) Expand all Loading... | |
99 * is 'audio' or 'video'. | 160 * is 'audio' or 'video'. |
100 * @private | 161 * @private |
101 */ | 162 */ |
102 function getSdpDefaultCodec(sdp, type) { | 163 function getSdpDefaultCodec(sdp, type) { |
103 var sdpLines = splitSdpLines(sdp); | 164 var sdpLines = splitSdpLines(sdp); |
104 | 165 |
105 // Find 'm=|type|' line, e.g. 'm=video 9 UDP/TLS/RTP/SAVPF 100 101 107 116'. | 166 // Find 'm=|type|' line, e.g. 'm=video 9 UDP/TLS/RTP/SAVPF 100 101 107 116'. |
106 var mLineNo = findLine(sdpLines, 'm=' + type); | 167 var mLineNo = findLine(sdpLines, 'm=' + type); |
107 if (mLineNo === null) { | 168 if (mLineNo === null) { |
108 failure('getSdpDefaultCodec', | 169 failure('getSdpDefaultCodec', |
109 '\'m=' + type + '\' line missing from |sdp|.'); | 170 '\'m=' + type + '\' line missing from |sdp|.'); |
110 } | 171 } |
111 | 172 |
112 // The default codec's ID. | 173 // The default codec's ID. |
113 var defaultCodecId = getMLineDefaultCodec(sdpLines[mLineNo]); | 174 var defaultCodecId = getMLineDefaultCodec(sdpLines[mLineNo]); |
114 if (defaultCodecId === null) { | 175 if (defaultCodecId === null) { |
115 failure('getSdpDefaultCodec', | 176 failure('getSdpDefaultCodec', |
116 '\'m=' + type + '\' line contains no codecs.'); | 177 '\'m=' + type + '\' line contains no codecs.'); |
117 } | 178 } |
118 | 179 |
119 // Find codec name, e.g. 'VP8' for 100 if 'a=rtpmap:100 VP8/9000'. | 180 // Find codec name, e.g. 'VP8' for 100 if 'a=rtpmap:100 VP8/9000'. |
120 var defaultCodec = findRtpmapCodec(sdpLines, defaultCodecId); | 181 var defaultCodec = findRtpmapCodec(sdpLines, defaultCodecId); |
121 if (defaultCodec === null) { | 182 if (defaultCodec === null) { |
122 failure('getSdpDefaultCodec', | 183 failure('getSdpDefaultCodec', |
123 'Unknown codec name for default codec ' + defaultCodecId + '.'); | 184 'Unknown codec name for default codec ' + defaultCodecId + '.'); |
124 } | 185 } |
125 return defaultCodec; | 186 return defaultCodec; |
126 } | 187 } |
127 | 188 |
128 /** | 189 /** |
129 * Searches through all |sdpLines| for the 'a=rtpmap:' line for the codec of | 190 * Searches through all |sdpLines| for the 'a=rtpmap:' line for the codec of |
130 * the specified name, returning its ID as an int if found, or null otherwise. | 191 * the specified name, returning its ID as an int if found, or null otherwise. |
131 * |codec| is the case-sensitive name of the codec. | 192 * |codec| is the case-sensitive name of the codec. |
132 * For example, if |sdpLines| contains 'a=rtpmap:100 VP8/9000' and |codec| is | 193 * For example, if |sdpLines| contains 'a=rtpmap:100 VP8/9000' and |codec| is |
133 * 'VP8', this function returns 100. | 194 * 'VP8', this function returns 100. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 function splitSdpLines(sdp) { | 298 function splitSdpLines(sdp) { |
238 return sdp.split('\r\n'); | 299 return sdp.split('\r\n'); |
239 } | 300 } |
240 | 301 |
241 /** @private */ | 302 /** @private */ |
242 function mergeSdpLines(sdpLines) { | 303 function mergeSdpLines(sdpLines) { |
243 return sdpLines.join('\r\n'); | 304 return sdpLines.join('\r\n'); |
244 } | 305 } |
245 | 306 |
246 /** @private */ | 307 /** @private */ |
247 function findLine(lines, startsWith) { | 308 function findLine(lines, lineStartsWith, startingLine = 0) { |
248 for (var i = 0; i < lines.length; i++) { | 309 for (var i = startingLine; i < lines.length; i++) { |
249 if (lines[i].startsWith(startsWith)) | 310 if (lines[i].startsWith(lineStartsWith)) |
250 return i; | 311 return i; |
251 } | 312 } |
252 return null; | 313 return null; |
253 } | 314 } |
OLD | NEW |