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 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 18 | @implementation RTC_OBJC_TYPE (RTCSessionDescription) |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 19 | |
Jake Bromberg | 48cd9db | 2021-07-30 10:12:07 -0700 | [diff] [blame] | 20 | @synthesize nativeDescription = _nativeDescription; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 21 | @synthesize type = _type; |
| 22 | @synthesize sdp = _sdp; |
| 23 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 24 | + (NSString *)stringForType:(RTCSdpType)type { |
Jake Bromberg | 48cd9db | 2021-07-30 10:12:07 -0700 | [diff] [blame] | 25 | std::string string = [self stdStringForType:type]; |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 26 | return [NSString stringForStdString:string]; |
| 27 | } |
| 28 | |
| 29 | + (RTCSdpType)typeForString:(NSString *)string { |
| 30 | std::string typeString = string.stdString; |
Jake Bromberg | 48cd9db | 2021-07-30 10:12:07 -0700 | [diff] [blame] | 31 | return [self typeForStdString:typeString]; |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Jake Bromberg | 48cd9db | 2021-07-30 10:12:07 -0700 | [diff] [blame] | 34 | + (webrtc::SessionDescriptionInterface *)nativeDescriptionForString:(NSString *)sdp |
| 35 | type:(RTCSdpType)type { |
| 36 | webrtc::SdpParseError error; |
| 37 | |
| 38 | webrtc::SessionDescriptionInterface *description = |
| 39 | webrtc::CreateSessionDescription([self stdStringForType:type], sdp.stdString, &error); |
| 40 | |
| 41 | if (!description) { |
| 42 | RTCLogError(@"Failed to create session description: %s\nline: %s", |
| 43 | error.description.c_str(), |
| 44 | error.line.c_str()); |
| 45 | } |
| 46 | |
| 47 | return description; |
| 48 | } |
| 49 | |
| 50 | - (nullable instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 51 | if (self = [super init]) { |
| 52 | _type = type; |
| 53 | _sdp = [sdp copy]; |
Jake Bromberg | 48cd9db | 2021-07-30 10:12:07 -0700 | [diff] [blame] | 54 | _nativeDescription = [[self class] nativeDescriptionForString:_sdp type:_type]; |
| 55 | |
| 56 | if (_nativeDescription == nil) { |
| 57 | return nil; |
| 58 | } |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 59 | } |
| 60 | return self; |
| 61 | } |
| 62 | |
| 63 | - (NSString *)description { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 64 | return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCSessionDescription):\n%@\n%@", |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 65 | [[self class] stringForType:_type], |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 66 | _sdp]; |
| 67 | } |
| 68 | |
| 69 | #pragma mark - Private |
| 70 | |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 71 | - (instancetype)initWithNativeDescription: |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 72 | (const webrtc::SessionDescriptionInterface *)nativeDescription { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 73 | NSParameterAssert(nativeDescription); |
| 74 | std::string sdp; |
| 75 | nativeDescription->ToString(&sdp); |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 76 | RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()]; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 77 | |
| 78 | return [self initWithType:type |
| 79 | sdp:[NSString stringForStdString:sdp]]; |
| 80 | } |
| 81 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 82 | + (std::string)stdStringForType:(RTCSdpType)type { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 83 | switch (type) { |
| 84 | case RTCSdpTypeOffer: |
| 85 | return webrtc::SessionDescriptionInterface::kOffer; |
| 86 | case RTCSdpTypePrAnswer: |
| 87 | return webrtc::SessionDescriptionInterface::kPrAnswer; |
| 88 | case RTCSdpTypeAnswer: |
| 89 | return webrtc::SessionDescriptionInterface::kAnswer; |
Philipp Hancke | 5152ea5 | 2020-09-09 14:58:32 +0200 | [diff] [blame] | 90 | case RTCSdpTypeRollback: |
| 91 | return webrtc::SessionDescriptionInterface::kRollback; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 95 | + (RTCSdpType)typeForStdString:(const std::string &)string { |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 96 | if (string == webrtc::SessionDescriptionInterface::kOffer) { |
| 97 | return RTCSdpTypeOffer; |
| 98 | } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) { |
| 99 | return RTCSdpTypePrAnswer; |
| 100 | } else if (string == webrtc::SessionDescriptionInterface::kAnswer) { |
| 101 | return RTCSdpTypeAnswer; |
Philipp Hancke | 5152ea5 | 2020-09-09 14:58:32 +0200 | [diff] [blame] | 102 | } else if (string == webrtc::SessionDescriptionInterface::kRollback) { |
| 103 | return RTCSdpTypeRollback; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 104 | } else { |
| 105 | RTC_NOTREACHED(); |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 106 | return RTCSdpTypeOffer; |
Jon Hjelle | 67e83d6 | 2016-01-06 12:05:22 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @end |