Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #import "ARDSDPUtils.h" | 11 #import "ARDSDPUtils.h" |
| 12 | 12 |
| 13 #import "WebRTC/RTCLogging.h" | 13 #import "WebRTC/RTCLogging.h" |
| 14 #import "WebRTC/RTCSessionDescription.h" | 14 #import "WebRTC/RTCSessionDescription.h" |
| 15 | 15 |
| 16 @implementation ARDSDPUtils | 16 @implementation ARDSDPUtils |
| 17 | 17 |
| 18 + (RTCSessionDescription *) | 18 + (RTCSessionDescription *) |
| 19 descriptionForDescription:(RTCSessionDescription *)description | 19 descriptionForDescription:(RTCSessionDescription *)description |
| 20 preferredVideoCodec:(NSString *)codec { | 20 preferredVideoCodec:(NSString *)codec { |
| 21 NSString *sdpString = description.sdp; | 21 NSString *sdpString = description.sdp; |
| 22 NSString *lineSeparator = @"\n"; | 22 NSString *lineSeparator = @"\n"; |
| 23 NSString *mLineSeparator = @" "; | 23 NSString *mLineSeparator = @" "; |
| 24 // Copied from PeerConnectionClient.java. | 24 // Copied from PeerConnectionClient.java. |
| 25 // TODO(tkchin): Move this to a shared C++ file. | 25 // TODO(tkchin): Move this to a shared C++ file. |
| 26 NSMutableArray *lines = | 26 NSMutableArray *lines = |
| 27 [NSMutableArray arrayWithArray: | 27 [NSMutableArray arrayWithArray: |
| 28 [sdpString componentsSeparatedByString:lineSeparator]]; | 28 [sdpString componentsSeparatedByString:lineSeparator]]; |
| 29 NSInteger mLineIndex = -1; | 29 NSInteger mLineIndex = -1; |
| 30 NSString *codecRtpMap = nil; | 30 // An array with all payload types with name |codec|. The payload types are |
| 31 // integers in the range 96-127, but they are stored as strings here. | |
| 32 NSMutableArray *codecPayloadTypes = [[NSMutableArray alloc] init]; | |
| 31 // a=rtpmap:<payload type> <encoding name>/<clock rate> | 33 // a=rtpmap:<payload type> <encoding name>/<clock rate> |
| 32 // [/<encoding parameters>] | 34 // [/<encoding parameters>] |
| 33 NSString *pattern = | 35 NSString *pattern = |
| 34 [NSString stringWithFormat:@"^a=rtpmap:(\\d+) %@(/\\d+)+[\r]?$", codec]; | 36 [NSString stringWithFormat:@"^a=rtpmap:(\\d+) %@(/\\d+)+[\r]?$", codec]; |
| 35 NSRegularExpression *regex = | 37 NSRegularExpression *regex = |
|
daniela-webrtc
2016/11/21 16:23:00
I know this is not part of this CL, but perhaps in
magjed_webrtc
2016/11/22 14:40:24
Yeah maybe. I'm not familiar with NSScanner so I'm
| |
| 36 [NSRegularExpression regularExpressionWithPattern:pattern | 38 [NSRegularExpression regularExpressionWithPattern:pattern |
| 37 options:0 | 39 options:0 |
| 38 error:nil]; | 40 error:nil]; |
| 39 for (NSInteger i = 0; (i < lines.count) && (mLineIndex == -1 || !codecRtpMap); | 41 for (NSInteger i = 0; i < lines.count; ++i) { |
| 40 ++i) { | |
| 41 NSString *line = lines[i]; | 42 NSString *line = lines[i]; |
| 42 if ([line hasPrefix:@"m=video"]) { | 43 if ([line hasPrefix:@"m=video"]) { |
| 43 mLineIndex = i; | 44 mLineIndex = i; |
| 44 continue; | 45 continue; |
| 45 } | 46 } |
| 46 NSTextCheckingResult *codecMatches = | 47 NSTextCheckingResult *codecMatches = |
| 47 [regex firstMatchInString:line | 48 [regex firstMatchInString:line |
| 48 options:0 | 49 options:0 |
| 49 range:NSMakeRange(0, line.length)]; | 50 range:NSMakeRange(0, line.length)]; |
| 50 if (codecMatches) { | 51 if (codecMatches) { |
| 51 codecRtpMap = | 52 [codecPayloadTypes |
| 52 [line substringWithRange:[codecMatches rangeAtIndex:1]]; | 53 addObject:[line substringWithRange:[codecMatches rangeAtIndex:1]]]; |
| 53 continue; | |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 if (mLineIndex == -1) { | 56 if (mLineIndex == -1) { |
|
daniela-webrtc
2016/11/21 16:23:00
can we extract the check for the "m=video" even be
magjed_webrtc
2016/11/22 14:40:24
Done, I extracted finding the "m=video" line out f
| |
| 57 RTCLog(@"No m=video line, so can't prefer %@", codec); | 57 RTCLog(@"No m=video line, so can't prefer %@", codec); |
| 58 return description; | 58 return description; |
| 59 } | 59 } |
| 60 if (!codecRtpMap) { | 60 if ([codecPayloadTypes count] == 0) { |
| 61 RTCLog(@"No rtpmap for %@", codec); | 61 RTCLog(@"No payload types with name %@", codec); |
| 62 return description; | 62 return description; |
| 63 } | 63 } |
| 64 NSArray *origMLineParts = | 64 NSArray *origMLineParts = |
| 65 [lines[mLineIndex] componentsSeparatedByString:mLineSeparator]; | 65 [lines[mLineIndex] componentsSeparatedByString:mLineSeparator]; |
| 66 if (origMLineParts.count > 3) { | 66 if (origMLineParts.count > 3) { |
| 67 NSMutableArray *newMLineParts = | 67 NSMutableArray *newMLineParts = |
| 68 [NSMutableArray arrayWithCapacity:origMLineParts.count]; | 68 [NSMutableArray arrayWithCapacity:origMLineParts.count]; |
| 69 NSInteger origPartIndex = 0; | 69 NSInteger origPartIndex = 0; |
| 70 // Format is: m=<media> <port> <proto> <fmt> ... | 70 // Format is: m=<media> <port> <proto> <fmt> ... |
| 71 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 71 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
| 72 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 72 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
| 73 [newMLineParts addObject:origMLineParts[origPartIndex++]]; | 73 [newMLineParts addObject:origMLineParts[origPartIndex++]]; |
| 74 [newMLineParts addObject:codecRtpMap]; | 74 [newMLineParts addObjectsFromArray:codecPayloadTypes]; |
|
daniela-webrtc
2016/11/21 16:23:00
Perhaps we can extend the usage of methods of NSMu
magjed_webrtc
2016/11/22 14:40:24
Done, I tried my best to make it cleaner.
| |
| 75 for (; origPartIndex < origMLineParts.count; ++origPartIndex) { | 75 for (; origPartIndex < origMLineParts.count; ++origPartIndex) { |
| 76 if (![codecRtpMap isEqualToString:origMLineParts[origPartIndex]]) { | 76 if (![codecPayloadTypes containsObject:origMLineParts[origPartIndex]]) { |
| 77 [newMLineParts addObject:origMLineParts[origPartIndex]]; | 77 [newMLineParts addObject:origMLineParts[origPartIndex]]; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 NSString *newMLine = | 80 NSString *newMLine = |
| 81 [newMLineParts componentsJoinedByString:mLineSeparator]; | 81 [newMLineParts componentsJoinedByString:mLineSeparator]; |
| 82 [lines replaceObjectAtIndex:mLineIndex | 82 [lines replaceObjectAtIndex:mLineIndex |
| 83 withObject:newMLine]; | 83 withObject:newMLine]; |
| 84 } else { | 84 } else { |
| 85 RTCLogWarning(@"Wrong SDP media description format: %@", lines[mLineIndex]); | 85 RTCLogWarning(@"Wrong SDP media description format: %@", lines[mLineIndex]); |
| 86 } | 86 } |
| 87 NSString *mangledSdpString = [lines componentsJoinedByString:lineSeparator]; | 87 NSString *mangledSdpString = [lines componentsJoinedByString:lineSeparator]; |
| 88 return [[RTCSessionDescription alloc] initWithType:description.type | 88 return [[RTCSessionDescription alloc] initWithType:description.type |
| 89 sdp:mangledSdpString]; | 89 sdp:mangledSdpString]; |
| 90 } | 90 } |
| 91 | 91 |
| 92 @end | 92 @end |
| OLD | NEW |