1/* 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+Private.h" 12 13#import "base/RTCLogging.h" 14#import "helpers/NSString+StdString.h" 15 16#include "rtc_base/checks.h" 17 18@implementation RTC_OBJC_TYPE (RTCSessionDescription) 19 20@synthesize type = _type; 21@synthesize sdp = _sdp; 22 23+ (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 33- (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp { 34 if (self = [super init]) { 35 _type = type; 36 _sdp = [sdp copy]; 37 } 38 return self; 39} 40 41- (NSString *)description { 42 return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCSessionDescription):\n%@\n%@", 43 [[self class] stringForType:_type], 44 _sdp]; 45} 46 47#pragma mark - Private 48 49- (std::unique_ptr<webrtc::SessionDescriptionInterface>)nativeDescription { 50 webrtc::SdpParseError error; 51 52 std::unique_ptr<webrtc::SessionDescriptionInterface> description(webrtc::CreateSessionDescription( 53 [[self class] stdStringForType:_type], _sdp.stdString, &error)); 54 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 64- (instancetype)initWithNativeDescription: 65 (const webrtc::SessionDescriptionInterface *)nativeDescription { 66 NSParameterAssert(nativeDescription); 67 std::string sdp; 68 nativeDescription->ToString(&sdp); 69 RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()]; 70 71 return [self initWithType:type 72 sdp:[NSString stringForStdString:sdp]]; 73} 74 75+ (std::string)stdStringForType:(RTCSdpType)type { 76 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; 83 case RTCSdpTypeRollback: 84 return webrtc::SessionDescriptionInterface::kRollback; 85 } 86} 87 88+ (RTCSdpType)typeForStdString:(const std::string &)string { 89 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; 95 } else if (string == webrtc::SessionDescriptionInterface::kRollback) { 96 return RTCSdpTypeRollback; 97 } else { 98 RTC_DCHECK_NOTREACHED(); 99 return RTCSdpTypeOffer; 100 } 101} 102 103@end 104