blob: 3e235b19855e2a88068bb5ddc2e5ed0a98144480 [file] [log] [blame]
Jon Hjelle67e83d62016-01-06 12:05:22 -08001/*
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
tkchin9eeb6242016-04-27 01:54:20 -070011#import "RTCSessionDescription+Private.h"
12
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#import "base/RTCLogging.h"
14#import "helpers/NSString+StdString.h"
Jon Hjelle67e83d62016-01-06 12:05:22 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Jon Hjelle67e83d62016-01-06 12:05:22 -080017
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020018@implementation RTC_OBJC_TYPE (RTCSessionDescription)
Jon Hjelle67e83d62016-01-06 12:05:22 -080019
Jake Bromberg48cd9db2021-07-30 10:12:07 -070020@synthesize nativeDescription = _nativeDescription;
Jon Hjelle67e83d62016-01-06 12:05:22 -080021@synthesize type = _type;
22@synthesize sdp = _sdp;
23
hjona2f77982016-03-04 07:09:09 -080024+ (NSString *)stringForType:(RTCSdpType)type {
Jake Bromberg48cd9db2021-07-30 10:12:07 -070025 std::string string = [self stdStringForType:type];
hjona2f77982016-03-04 07:09:09 -080026 return [NSString stringForStdString:string];
27}
28
29+ (RTCSdpType)typeForString:(NSString *)string {
30 std::string typeString = string.stdString;
Jake Bromberg48cd9db2021-07-30 10:12:07 -070031 return [self typeForStdString:typeString];
hjona2f77982016-03-04 07:09:09 -080032}
33
Jake Bromberg48cd9db2021-07-30 10:12:07 -070034+ (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 Hjelle67e83d62016-01-06 12:05:22 -080051 if (self = [super init]) {
52 _type = type;
53 _sdp = [sdp copy];
Jake Bromberg48cd9db2021-07-30 10:12:07 -070054 _nativeDescription = [[self class] nativeDescriptionForString:_sdp type:_type];
55
56 if (_nativeDescription == nil) {
57 return nil;
58 }
Jon Hjelle67e83d62016-01-06 12:05:22 -080059 }
60 return self;
61}
62
63- (NSString *)description {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020064 return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCSessionDescription):\n%@\n%@",
hjona2f77982016-03-04 07:09:09 -080065 [[self class] stringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080066 _sdp];
67}
68
69#pragma mark - Private
70
Jon Hjelle67e83d62016-01-06 12:05:22 -080071- (instancetype)initWithNativeDescription:
hjonf396f602016-02-11 16:19:06 -080072 (const webrtc::SessionDescriptionInterface *)nativeDescription {
Jon Hjelle67e83d62016-01-06 12:05:22 -080073 NSParameterAssert(nativeDescription);
74 std::string sdp;
75 nativeDescription->ToString(&sdp);
hjona2f77982016-03-04 07:09:09 -080076 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
Jon Hjelle67e83d62016-01-06 12:05:22 -080077
78 return [self initWithType:type
79 sdp:[NSString stringForStdString:sdp]];
80}
81
hjona2f77982016-03-04 07:09:09 -080082+ (std::string)stdStringForType:(RTCSdpType)type {
Jon Hjelle67e83d62016-01-06 12:05:22 -080083 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 Hancke5152ea52020-09-09 14:58:32 +020090 case RTCSdpTypeRollback:
91 return webrtc::SessionDescriptionInterface::kRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -080092 }
93}
94
hjona2f77982016-03-04 07:09:09 -080095+ (RTCSdpType)typeForStdString:(const std::string &)string {
Jon Hjelle67e83d62016-01-06 12:05:22 -080096 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 Hancke5152ea52020-09-09 14:58:32 +0200102 } else if (string == webrtc::SessionDescriptionInterface::kRollback) {
103 return RTCSdpTypeRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -0800104 } else {
105 RTC_NOTREACHED();
tkchinab8f82f2016-01-27 17:50:11 -0800106 return RTCSdpTypeOffer;
Jon Hjelle67e83d62016-01-06 12:05:22 -0800107 }
108}
109
110@end