@@ -26,8 +26,21 @@ @implementation ARDSDPUtils
26
26
NSMutableArray *lines =
27
27
[NSMutableArray arrayWithArray:
28
28
[sdpString componentsSeparatedByString: lineSeparator]];
29
+ // Find the line starting with "m=video".
29
30
NSInteger mLineIndex = -1 ;
30
- NSString *codecRtpMap = nil ;
31
+ for (NSInteger i = 0 ; i < lines.count ; ++i) {
32
+ if ([lines[i] hasPrefix: @" m=video" ]) {
33
+ mLineIndex = i;
34
+ break ;
35
+ }
36
+ }
37
+ if (mLineIndex == -1 ) {
38
+ RTCLog (@" No m=video line, so can't prefer %@ " , codec);
39
+ return description;
40
+ }
41
+ // An array with all payload types with name |codec|. The payload types are
42
+ // integers in the range 96-127, but they are stored as strings here.
43
+ NSMutableArray *codecPayloadTypes = [[NSMutableArray alloc ] init ];
31
44
// a=rtpmap:<payload type> <encoding name>/<clock rate>
32
45
// [/<encoding parameters>]
33
46
NSString *pattern =
@@ -36,54 +49,47 @@ @implementation ARDSDPUtils
36
49
[NSRegularExpression regularExpressionWithPattern: pattern
37
50
options: 0
38
51
error: nil ];
39
- for (NSInteger i = 0 ; (i < lines.count ) && (mLineIndex == -1 || !codecRtpMap);
40
- ++i) {
41
- NSString *line = lines[i];
42
- if ([line hasPrefix: @" m=video" ]) {
43
- mLineIndex = i;
44
- continue ;
45
- }
52
+ for (NSString *line in lines) {
46
53
NSTextCheckingResult *codecMatches =
47
54
[regex firstMatchInString: line
48
55
options: 0
49
56
range: NSMakeRange (0 , line.length)];
50
57
if (codecMatches) {
51
- codecRtpMap =
52
- [line substringWithRange: [codecMatches rangeAtIndex: 1 ]];
53
- continue ;
58
+ [codecPayloadTypes
59
+ addObject: [line substringWithRange: [codecMatches rangeAtIndex: 1 ]]];
54
60
}
55
61
}
56
- if (mLineIndex == -1 ) {
57
- RTCLog (@" No m=video line, so can't prefer %@ " , codec);
58
- return description;
59
- }
60
- if (!codecRtpMap) {
61
- RTCLog (@" No rtpmap for %@ " , codec);
62
+ if ([codecPayloadTypes count ] == 0 ) {
63
+ RTCLog (@" No payload types with name %@ " , codec);
62
64
return description;
63
65
}
64
66
NSArray *origMLineParts =
65
67
[lines[mLineIndex] componentsSeparatedByString: mLineSeparator];
66
- if (origMLineParts.count > 3 ) {
67
- NSMutableArray *newMLineParts =
68
- [NSMutableArray arrayWithCapacity: origMLineParts.count];
69
- NSInteger origPartIndex = 0 ;
70
- // Format is: m=<media> <port> <proto> <fmt> ...
71
- [newMLineParts addObject: origMLineParts[origPartIndex++]];
72
- [newMLineParts addObject: origMLineParts[origPartIndex++]];
73
- [newMLineParts addObject: origMLineParts[origPartIndex++]];
74
- [newMLineParts addObject: codecRtpMap];
75
- for (; origPartIndex < origMLineParts.count ; ++origPartIndex) {
76
- if (![codecRtpMap isEqualToString: origMLineParts[origPartIndex]]) {
77
- [newMLineParts addObject: origMLineParts[origPartIndex]];
78
- }
79
- }
80
- NSString *newMLine =
81
- [newMLineParts componentsJoinedByString: mLineSeparator];
82
- [lines replaceObjectAtIndex: mLineIndex
83
- withObject: newMLine];
84
- } else {
68
+ // The format of ML should be: m=<media> <port> <proto> <fmt> ...
69
+ const int kHeaderLength = 3 ;
70
+ if (origMLineParts.count <= kHeaderLength ) {
85
71
RTCLogWarning (@" Wrong SDP media description format: %@ " , lines[mLineIndex]);
72
+ return description;
86
73
}
74
+ // Split the line into header and payloadTypes.
75
+ NSRange headerRange = NSMakeRange (0 , kHeaderLength );
76
+ NSRange payloadRange =
77
+ NSMakeRange (kHeaderLength , origMLineParts.count - kHeaderLength );
78
+ NSArray *header = [origMLineParts subarrayWithRange: headerRange];
79
+ NSMutableArray *payloadTypes = [NSMutableArray
80
+ arrayWithArray: [origMLineParts subarrayWithRange: payloadRange]];
81
+ // Reconstruct the line with |codecPayloadTypes| moved to the beginning of the
82
+ // payload types.
83
+ NSMutableArray *newMLineParts = [NSMutableArray arrayWithCapacity: origMLineParts.count];
84
+ [newMLineParts addObjectsFromArray: header];
85
+ [newMLineParts addObjectsFromArray: codecPayloadTypes];
86
+ [payloadTypes removeObjectsInArray: codecPayloadTypes];
87
+ [newMLineParts addObjectsFromArray: payloadTypes];
88
+
89
+ NSString *newMLine = [newMLineParts componentsJoinedByString: mLineSeparator];
90
+ [lines replaceObjectAtIndex: mLineIndex
91
+ withObject: newMLine];
92
+
87
93
NSString *mangledSdpString = [lines componentsJoinedByString: lineSeparator];
88
94
return [[RTCSessionDescription alloc ] initWithType: description.type
89
95
sdp: mangledSdpString];
0 commit comments