blob: 417ff7dfca2ed4dab5984d4407598027d08fe848 [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
13#import "NSString+StdString.h"
14#import "WebRTC/RTCLogging.h"
Jon Hjelle67e83d62016-01-06 12:05:22 -080015
16#include "webrtc/base/checks.h"
17
Jon Hjelle67e83d62016-01-06 12:05:22 -080018@implementation RTCSessionDescription
19
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 {
34 NSParameterAssert(sdp.length);
35 if (self = [super init]) {
36 _type = type;
37 _sdp = [sdp copy];
38 }
39 return self;
40}
41
42- (NSString *)description {
hjona2f77982016-03-04 07:09:09 -080043 return [NSString stringWithFormat:@"RTCSessionDescription:\n%@\n%@",
44 [[self class] stringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080045 _sdp];
46}
47
48#pragma mark - Private
49
50- (webrtc::SessionDescriptionInterface *)nativeDescription {
51 webrtc::SdpParseError error;
52
53 webrtc::SessionDescriptionInterface *description =
hjona2f77982016-03-04 07:09:09 -080054 webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080055 _sdp.stdString,
56 &error);
57
58 if (!description) {
59 RTCLogError(@"Failed to create session description: %s\nline: %s",
60 error.description.c_str(),
61 error.line.c_str());
62 }
63
64 return description;
65}
66
67- (instancetype)initWithNativeDescription:
hjonf396f602016-02-11 16:19:06 -080068 (const webrtc::SessionDescriptionInterface *)nativeDescription {
Jon Hjelle67e83d62016-01-06 12:05:22 -080069 NSParameterAssert(nativeDescription);
70 std::string sdp;
71 nativeDescription->ToString(&sdp);
hjona2f77982016-03-04 07:09:09 -080072 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
Jon Hjelle67e83d62016-01-06 12:05:22 -080073
74 return [self initWithType:type
75 sdp:[NSString stringForStdString:sdp]];
76}
77
hjona2f77982016-03-04 07:09:09 -080078+ (std::string)stdStringForType:(RTCSdpType)type {
Jon Hjelle67e83d62016-01-06 12:05:22 -080079 switch (type) {
80 case RTCSdpTypeOffer:
81 return webrtc::SessionDescriptionInterface::kOffer;
82 case RTCSdpTypePrAnswer:
83 return webrtc::SessionDescriptionInterface::kPrAnswer;
84 case RTCSdpTypeAnswer:
85 return webrtc::SessionDescriptionInterface::kAnswer;
86 }
87}
88
hjona2f77982016-03-04 07:09:09 -080089+ (RTCSdpType)typeForStdString:(const std::string &)string {
Jon Hjelle67e83d62016-01-06 12:05:22 -080090 if (string == webrtc::SessionDescriptionInterface::kOffer) {
91 return RTCSdpTypeOffer;
92 } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) {
93 return RTCSdpTypePrAnswer;
94 } else if (string == webrtc::SessionDescriptionInterface::kAnswer) {
95 return RTCSdpTypeAnswer;
96 } else {
97 RTC_NOTREACHED();
tkchinab8f82f2016-01-27 17:50:11 -080098 return RTCSdpTypeOffer;
Jon Hjelle67e83d62016-01-06 12:05:22 -080099 }
100}
101
102@end