blob: 9fd97fee237a4607f1d6f9e400f1c7314c3a5dda [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
20@synthesize type = _type;
21@synthesize sdp = _sdp;
22
hjona2f77982016-03-04 07:09:09 -080023+ (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 Hjelle67e83d62016-01-06 12:05:22 -080033- (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
Jon Hjelle67e83d62016-01-06 12:05:22 -080034 if (self = [super init]) {
35 _type = type;
36 _sdp = [sdp copy];
37 }
38 return self;
39}
40
41- (NSString *)description {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020042 return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCSessionDescription):\n%@\n%@",
hjona2f77982016-03-04 07:09:09 -080043 [[self class] stringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080044 _sdp];
45}
46
47#pragma mark - Private
48
49- (webrtc::SessionDescriptionInterface *)nativeDescription {
50 webrtc::SdpParseError error;
51
52 webrtc::SessionDescriptionInterface *description =
hjona2f77982016-03-04 07:09:09 -080053 webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080054 _sdp.stdString,
55 &error);
56
57 if (!description) {
58 RTCLogError(@"Failed to create session description: %s\nline: %s",
59 error.description.c_str(),
60 error.line.c_str());
61 }
62
63 return description;
64}
65
66- (instancetype)initWithNativeDescription:
hjonf396f602016-02-11 16:19:06 -080067 (const webrtc::SessionDescriptionInterface *)nativeDescription {
Jon Hjelle67e83d62016-01-06 12:05:22 -080068 NSParameterAssert(nativeDescription);
69 std::string sdp;
70 nativeDescription->ToString(&sdp);
hjona2f77982016-03-04 07:09:09 -080071 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
Jon Hjelle67e83d62016-01-06 12:05:22 -080072
73 return [self initWithType:type
74 sdp:[NSString stringForStdString:sdp]];
75}
76
hjona2f77982016-03-04 07:09:09 -080077+ (std::string)stdStringForType:(RTCSdpType)type {
Jon Hjelle67e83d62016-01-06 12:05:22 -080078 switch (type) {
79 case RTCSdpTypeOffer:
80 return webrtc::SessionDescriptionInterface::kOffer;
81 case RTCSdpTypePrAnswer:
82 return webrtc::SessionDescriptionInterface::kPrAnswer;
83 case RTCSdpTypeAnswer:
84 return webrtc::SessionDescriptionInterface::kAnswer;
Philipp Hancke5152ea52020-09-09 14:58:32 +020085 case RTCSdpTypeRollback:
86 return webrtc::SessionDescriptionInterface::kRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -080087 }
88}
89
hjona2f77982016-03-04 07:09:09 -080090+ (RTCSdpType)typeForStdString:(const std::string &)string {
Jon Hjelle67e83d62016-01-06 12:05:22 -080091 if (string == webrtc::SessionDescriptionInterface::kOffer) {
92 return RTCSdpTypeOffer;
93 } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) {
94 return RTCSdpTypePrAnswer;
95 } else if (string == webrtc::SessionDescriptionInterface::kAnswer) {
96 return RTCSdpTypeAnswer;
Philipp Hancke5152ea52020-09-09 14:58:32 +020097 } else if (string == webrtc::SessionDescriptionInterface::kRollback) {
98 return RTCSdpTypeRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -080099 } else {
100 RTC_NOTREACHED();
tkchinab8f82f2016-01-27 17:50:11 -0800101 return RTCSdpTypeOffer;
Jon Hjelle67e83d62016-01-06 12:05:22 -0800102 }
103}
104
105@end