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