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