blob: 94c1a3f2d98e20b0c991d4ad2d913992458de0b9 [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
11#import "RTCSessionDescription.h"
12
13#include "webrtc/base/checks.h"
14
15#import "webrtc/api/objc/RTCSessionDescription+Private.h"
16#import "webrtc/base/objc/NSString+StdString.h"
17#import "webrtc/base/objc/RTCLogging.h"
18
19@implementation RTCSessionDescription
20
21@synthesize type = _type;
22@synthesize sdp = _sdp;
23
hjona2f77982016-03-04 07:09:09 -080024+ (NSString *)stringForType:(RTCSdpType)type {
25 std::string string = [[self class] stdStringForType:type];
26 return [NSString stringForStdString:string];
27}
28
29+ (RTCSdpType)typeForString:(NSString *)string {
30 std::string typeString = string.stdString;
31 return [[self class] typeForStdString:typeString];
32}
33
Jon Hjelle67e83d62016-01-06 12:05:22 -080034- (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
35 NSParameterAssert(sdp.length);
36 if (self = [super init]) {
37 _type = type;
38 _sdp = [sdp copy];
39 }
40 return self;
41}
42
43- (NSString *)description {
hjona2f77982016-03-04 07:09:09 -080044 return [NSString stringWithFormat:@"RTCSessionDescription:\n%@\n%@",
45 [[self class] stringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080046 _sdp];
47}
48
49#pragma mark - Private
50
51- (webrtc::SessionDescriptionInterface *)nativeDescription {
52 webrtc::SdpParseError error;
53
54 webrtc::SessionDescriptionInterface *description =
hjona2f77982016-03-04 07:09:09 -080055 webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
Jon Hjelle67e83d62016-01-06 12:05:22 -080056 _sdp.stdString,
57 &error);
58
59 if (!description) {
60 RTCLogError(@"Failed to create session description: %s\nline: %s",
61 error.description.c_str(),
62 error.line.c_str());
63 }
64
65 return description;
66}
67
68- (instancetype)initWithNativeDescription:
hjonf396f602016-02-11 16:19:06 -080069 (const webrtc::SessionDescriptionInterface *)nativeDescription {
Jon Hjelle67e83d62016-01-06 12:05:22 -080070 NSParameterAssert(nativeDescription);
71 std::string sdp;
72 nativeDescription->ToString(&sdp);
hjona2f77982016-03-04 07:09:09 -080073 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
Jon Hjelle67e83d62016-01-06 12:05:22 -080074
75 return [self initWithType:type
76 sdp:[NSString stringForStdString:sdp]];
77}
78
hjona2f77982016-03-04 07:09:09 -080079+ (std::string)stdStringForType:(RTCSdpType)type {
Jon Hjelle67e83d62016-01-06 12:05:22 -080080 switch (type) {
81 case RTCSdpTypeOffer:
82 return webrtc::SessionDescriptionInterface::kOffer;
83 case RTCSdpTypePrAnswer:
84 return webrtc::SessionDescriptionInterface::kPrAnswer;
85 case RTCSdpTypeAnswer:
86 return webrtc::SessionDescriptionInterface::kAnswer;
87 }
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;
97 } else {
98 RTC_NOTREACHED();
tkchinab8f82f2016-01-27 17:50:11 -080099 return RTCSdpTypeOffer;
Jon Hjelle67e83d62016-01-06 12:05:22 -0800100 }
101}
102
103@end