OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 12 * and/or other materials provided with the distribution. |
13 * 3. The name of the author may not be used to endorse or promote products | 13 * 3. The name of the author may not be used to endorse or promote products |
14 * derived from this software without specific prior written permission. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #import "ARDSDPUtils.h" | 28 #import "ARDSDPUtils.h" |
29 | 29 |
| 30 #import "ARDLogging.h" |
30 #import "RTCSessionDescription.h" | 31 #import "RTCSessionDescription.h" |
31 | 32 |
32 @implementation ARDSDPUtils | 33 @implementation ARDSDPUtils |
33 | 34 |
34 + (RTCSessionDescription *) | 35 + (RTCSessionDescription *) |
35 descriptionForDescription:(RTCSessionDescription *)description | 36 descriptionForDescription:(RTCSessionDescription *)description |
36 preferredVideoCodec:(NSString *)codec { | 37 preferredVideoCodec:(NSString *)codec { |
37 NSString *sdpString = description.description; | 38 NSString *sdpString = description.description; |
38 NSString *lineSeparator = @"\n"; | 39 NSString *lineSeparator = @"\n"; |
39 NSString *mLineSeparator = @" "; | 40 NSString *mLineSeparator = @" "; |
40 // Copied from PeerConnectionClient.java. | 41 // Copied from PeerConnectionClient.java. |
41 // TODO(tkchin): Move this to a shared C++ file. | 42 // TODO(tkchin): Move this to a shared C++ file. |
42 NSMutableArray *lines = | 43 NSMutableArray *lines = |
43 [NSMutableArray arrayWithArray: | 44 [NSMutableArray arrayWithArray: |
44 [sdpString componentsSeparatedByString:lineSeparator]]; | 45 [sdpString componentsSeparatedByString:lineSeparator]]; |
45 int mLineIndex = -1; | 46 NSInteger mLineIndex = -1; |
46 NSString *codecRtpMap = nil; | 47 NSString *codecRtpMap = nil; |
47 // a=rtpmap:<payload type> <encoding name>/<clock rate> | 48 // a=rtpmap:<payload type> <encoding name>/<clock rate> |
48 // [/<encoding parameters>] | 49 // [/<encoding parameters>] |
49 NSString *pattern = | 50 NSString *pattern = |
50 [NSString stringWithFormat:@"^a=rtpmap:(\\d+) %@(/\\d+)+[\r]?$", codec]; | 51 [NSString stringWithFormat:@"^a=rtpmap:(\\d+) %@(/\\d+)+[\r]?$", codec]; |
51 NSRegularExpression *regex = | 52 NSRegularExpression *regex = |
52 [NSRegularExpression regularExpressionWithPattern:pattern | 53 [NSRegularExpression regularExpressionWithPattern:pattern |
53 options:0 | 54 options:0 |
54 error:nil]; | 55 error:nil]; |
55 for (NSInteger i = 0; (i < lines.count) && (mLineIndex == -1 || !codecRtpMap); | 56 for (NSInteger i = 0; (i < lines.count) && (mLineIndex == -1 || !codecRtpMap); |
56 ++i) { | 57 ++i) { |
57 NSString *line = lines[i]; | 58 NSString *line = lines[i]; |
58 if ([line hasPrefix:@"m=video"]) { | 59 if ([line hasPrefix:@"m=video"]) { |
59 mLineIndex = i; | 60 mLineIndex = i; |
60 continue; | 61 continue; |
61 } | 62 } |
62 NSTextCheckingResult *codecMatches = | 63 NSTextCheckingResult *codecMatches = |
63 [regex firstMatchInString:line | 64 [regex firstMatchInString:line |
64 options:0 | 65 options:0 |
65 range:NSMakeRange(0, line.length)]; | 66 range:NSMakeRange(0, line.length)]; |
66 if (codecMatches) { | 67 if (codecMatches) { |
67 codecRtpMap = | 68 codecRtpMap = |
68 [line substringWithRange:[codecMatches rangeAtIndex:1]]; | 69 [line substringWithRange:[codecMatches rangeAtIndex:1]]; |
69 continue; | 70 continue; |
70 } | 71 } |
71 } | 72 } |
72 if (mLineIndex == -1) { | 73 if (mLineIndex == -1) { |
73 NSLog(@"No m=video line, so can't prefer %@", codec); | 74 ARDLog(@"No m=video line, so can't prefer %@", codec); |
74 return description; | 75 return description; |
75 } | 76 } |
76 if (!codecRtpMap) { | 77 if (!codecRtpMap) { |
77 NSLog(@"No rtpmap for %@", codec); | 78 ARDLog(@"No rtpmap for %@", codec); |
78 return description; | 79 return description; |
79 } | 80 } |
80 NSArray *origMLineParts = | 81 NSArray *origMLineParts = |
81 [lines[mLineIndex] componentsSeparatedByString:mLineSeparator]; | 82 [lines[mLineIndex] componentsSeparatedByString:mLineSeparator]; |
82 if (origMLineParts.count > 3) { | 83 if (origMLineParts.count > 3) { |
83 NSMutableArray *newMLineParts = | 84 NSMutableArray *newMLineParts = |
84 [NSMutableArray arrayWithCapacity:origMLineParts.count]; | 85 [NSMutableArray arrayWithCapacity:origMLineParts.count]; |
85 NSInteger origPartIndex = 0; | 86 NSInteger origPartIndex = 0; |
86 // Format is: m=<media> <port> <proto> <fmt> ... | 87 // Format is: m=<media> <port> <proto> <fmt> ... |
87 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 88 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
88 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 89 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
89 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 90 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
90 [newMLineParts addObject:codecRtpMap]; | 91 [newMLineParts addObject:codecRtpMap]; |
91 for (; origPartIndex < origMLineParts.count; ++origPartIndex) { | 92 for (; origPartIndex < origMLineParts.count; ++origPartIndex) { |
92 if (![codecRtpMap isEqualToString:origMLineParts[origPartIndex]]) { | 93 if (![codecRtpMap isEqualToString:origMLineParts[origPartIndex]]) { |
93 [newMLineParts addObject:origMLineParts[origPartIndex]]; | 94 [newMLineParts addObject:origMLineParts[origPartIndex]]; |
94 } | 95 } |
95 } | 96 } |
96 NSString *newMLine = | 97 NSString *newMLine = |
97 [newMLineParts componentsJoinedByString:mLineSeparator]; | 98 [newMLineParts componentsJoinedByString:mLineSeparator]; |
98 [lines replaceObjectAtIndex:mLineIndex | 99 [lines replaceObjectAtIndex:mLineIndex |
99 withObject:newMLine]; | 100 withObject:newMLine]; |
100 } else { | 101 } else { |
101 NSLog(@"Wrong SDP media description format: %@", lines[mLineIndex]); | 102 ARDLog(@"Wrong SDP media description format: %@", lines[mLineIndex]); |
102 } | 103 } |
103 NSString *mangledSdpString = [lines componentsJoinedByString:lineSeparator]; | 104 NSString *mangledSdpString = [lines componentsJoinedByString:lineSeparator]; |
104 return [[RTCSessionDescription alloc] initWithType:description.type | 105 return [[RTCSessionDescription alloc] initWithType:description.type |
105 sdp:mangledSdpString]; | 106 sdp:mangledSdpString]; |
106 } | 107 } |
107 | 108 |
108 @end | 109 @end |
OLD | NEW |