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.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 24 - (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp { 25 NSParameterAssert(sdp.length); 26 if (self = [super init]) { 27 _type = type; 28 _sdp = [sdp copy]; 29 } 30 return self; 31 } 32 33 - (NSString *)description { 34 return [NSString stringWithFormat:@"RTCSessionDescription:\n%s\n%@", 35 [[self class] stringForType:_type].c_str(), 36 _sdp]; 37 } 38 39 #pragma mark - Private 40 41 - (webrtc::SessionDescriptionInterface *)nativeDescription { 42 webrtc::SdpParseError error; 43 44 webrtc::SessionDescriptionInterface *description = 45 webrtc::CreateSessionDescription([[self class] stringForType:_type], 46 _sdp.stdString, 47 &error); 48 49 if (!description) { 50 RTCLogError(@"Failed to create session description: %s\nline: %s", 51 error.description.c_str(), 52 error.line.c_str()); 53 } 54 55 return description; 56 } 57 58 - (instancetype)initWithNativeDescription: 59 (webrtc::SessionDescriptionInterface *)nativeDescription { 60 NSParameterAssert(nativeDescription); 61 std::string sdp; 62 nativeDescription->ToString(&sdp); 63 RTCSdpType type = [[self class] typeForString:nativeDescription->type()]; 64 65 return [self initWithType:type 66 sdp:[NSString stringForStdString:sdp]]; 67 } 68 69 + (std::string)stringForType:(RTCSdpType)type { 70 switch (type) { 71 case RTCSdpTypeOffer: 72 return webrtc::SessionDescriptionInterface::kOffer; 73 case RTCSdpTypePrAnswer: 74 return webrtc::SessionDescriptionInterface::kPrAnswer; 75 case RTCSdpTypeAnswer: 76 return webrtc::SessionDescriptionInterface::kAnswer; 77 } 78 } 79 80 + (RTCSdpType)typeForString:(const std::string &)string { 81 if (string == webrtc::SessionDescriptionInterface::kOffer) { 82 return RTCSdpTypeOffer; 83 } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) { 84 return RTCSdpTypePrAnswer; 85 } else if (string == webrtc::SessionDescriptionInterface::kAnswer) { 86 return RTCSdpTypeAnswer; 87 } else { 88 RTC_NOTREACHED(); 89 } 90 } 91 92 @end 93