blob: 539c90b14c13ab2e10bc8f166434311ab52102cb [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"
Artem Titov63ee39d2022-05-13 14:46:42 +000014#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 {
Mirko Bonadeif48d3cf2021-07-31 09:53:25 +000024 std::string string = [[self class] stdStringForType:type];
Artem Titov63ee39d2022-05-13 14:46:42 +000025 return [NSString stringForStdString:string];
hjona2f77982016-03-04 07:09:09 -080026}
27
28+ (RTCSdpType)typeForString:(NSString *)string {
29 std::string typeString = string.stdString;
Mirko Bonadeif48d3cf2021-07-31 09:53:25 +000030 return [[self class] typeForStdString:typeString];
hjona2f77982016-03-04 07:09:09 -080031}
32
Mirko Bonadeif48d3cf2021-07-31 09:53:25 +000033- (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
Byoungchan Lee33728152021-08-04 20:54:45 +090049- (std::unique_ptr<webrtc::SessionDescriptionInterface>)nativeDescription {
Mirko Bonadeif48d3cf2021-07-31 09:53:25 +000050 webrtc::SdpParseError error;
51
Byoungchan Lee33728152021-08-04 20:54:45 +090052 std::unique_ptr<webrtc::SessionDescriptionInterface> description(webrtc::CreateSessionDescription(
53 [[self class] stdStringForType:_type], _sdp.stdString, &error));
Mirko Bonadeif48d3cf2021-07-31 09:53:25 +000054
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 Hjelle67e83d62016-01-06 12:05:22 -080064- (instancetype)initWithNativeDescription:
hjonf396f602016-02-11 16:19:06 -080065 (const webrtc::SessionDescriptionInterface *)nativeDescription {
Jon Hjelle67e83d62016-01-06 12:05:22 -080066 NSParameterAssert(nativeDescription);
67 std::string sdp;
68 nativeDescription->ToString(&sdp);
hjona2f77982016-03-04 07:09:09 -080069 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
Jon Hjelle67e83d62016-01-06 12:05:22 -080070
Artem Titov63ee39d2022-05-13 14:46:42 +000071 return [self initWithType:type
72 sdp:[NSString stringForStdString:sdp]];
Jon Hjelle67e83d62016-01-06 12:05:22 -080073}
74
hjona2f77982016-03-04 07:09:09 -080075+ (std::string)stdStringForType:(RTCSdpType)type {
Jon Hjelle67e83d62016-01-06 12:05:22 -080076 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 Hancke5152ea52020-09-09 14:58:32 +020083 case RTCSdpTypeRollback:
84 return webrtc::SessionDescriptionInterface::kRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -080085 }
86}
87
hjona2f77982016-03-04 07:09:09 -080088+ (RTCSdpType)typeForStdString:(const std::string &)string {
Jon Hjelle67e83d62016-01-06 12:05:22 -080089 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 Hancke5152ea52020-09-09 14:58:32 +020095 } else if (string == webrtc::SessionDescriptionInterface::kRollback) {
96 return RTCSdpTypeRollback;
Jon Hjelle67e83d62016-01-06 12:05:22 -080097 } else {
Artem Titovd3251962021-11-15 16:57:07 +010098 RTC_DCHECK_NOTREACHED();
tkchinab8f82f2016-01-27 17:50:11 -080099 return RTCSdpTypeOffer;
Jon Hjelle67e83d62016-01-06 12:05:22 -0800100 }
101}
102
103@end