Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCSessionDescription+Private.h" |
| 12 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 13 | #import "base/RTCLogging.h" |
| 14 | #import "helpers/NSString+StdString.h" |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 17 | |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 18 | @implementation RTCSessionDescription |
| 19 | |
| 20 | @synthesize type = _type; |
| 21 | @synthesize sdp = _sdp; |
| 22 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 23 | + (NSString *)stringForType:(RTCSdpType)type { |
| 24 | std::string string = [[self class] stdStringForType:type]; |
| 25 | return [NSString stringForStdString:string]; |
| 26 | } |
| 27 | |
| 28 | + (RTCSdpType)typeForString:(NSString *)string { |
| 29 | std::string typeString = string.stdString; |
| 30 | return [[self class] typeForStdString:typeString]; |
| 31 | } |
| 32 | |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 33 | - (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp { |
| 34 | NSParameterAssert(sdp.length); |
| 35 | if (self = [super init]) { |
| 36 | _type = type; |
| 37 | _sdp = [sdp copy]; |
| 38 | } |
| 39 | return self; |
| 40 | } |
| 41 | |
| 42 | - (NSString *)description { |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 43 | return [NSString stringWithFormat:@"RTCSessionDescription:\n%@\n%@", |
| 44 | [[self class] stringForType:_type], |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 45 | _sdp]; |
| 46 | } |
| 47 | |
| 48 | #pragma mark - Private |
| 49 | |
| 50 | - (webrtc::SessionDescriptionInterface *)nativeDescription { |
| 51 | webrtc::SdpParseError error; |
| 52 | |
| 53 | webrtc::SessionDescriptionInterface *description = |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 54 | webrtc::CreateSessionDescription([[self class] stdStringForType:_type], |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 55 | _sdp.stdString, |
| 56 | &error); |
| 57 | |
| 58 | if (!description) { |
| 59 | RTCLogError(@"Failed to create session description: %s\nline: %s", |
| 60 | error.description.c_str(), |
| 61 | error.line.c_str()); |
| 62 | } |
| 63 | |
| 64 | return description; |
| 65 | } |
| 66 | |
| 67 | - (instancetype)initWithNativeDescription: |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 68 | (const webrtc::SessionDescriptionInterface *)nativeDescription { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 69 | NSParameterAssert(nativeDescription); |
| 70 | std::string sdp; |
| 71 | nativeDescription->ToString(&sdp); |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 72 | RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()]; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 73 | |
| 74 | return [self initWithType:type |
| 75 | sdp:[NSString stringForStdString:sdp]]; |
| 76 | } |
| 77 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 78 | + (std::string)stdStringForType:(RTCSdpType)type { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 79 | switch (type) { |
| 80 | case RTCSdpTypeOffer: |
| 81 | return webrtc::SessionDescriptionInterface::kOffer; |
| 82 | case RTCSdpTypePrAnswer: |
| 83 | return webrtc::SessionDescriptionInterface::kPrAnswer; |
| 84 | case RTCSdpTypeAnswer: |
| 85 | return webrtc::SessionDescriptionInterface::kAnswer; |
| 86 | } |
| 87 | } |
| 88 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 89 | + (RTCSdpType)typeForStdString:(const std::string &)string { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 90 | if (string == webrtc::SessionDescriptionInterface::kOffer) { |
| 91 | return RTCSdpTypeOffer; |
| 92 | } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) { |
| 93 | return RTCSdpTypePrAnswer; |
| 94 | } else if (string == webrtc::SessionDescriptionInterface::kAnswer) { |
| 95 | return RTCSdpTypeAnswer; |
| 96 | } else { |
| 97 | RTC_NOTREACHED(); |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 98 | return RTCSdpTypeOffer; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | @end |