ARDSignalingMessage.m (4817B)
1 /* 2 * Copyright 2014 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 "ARDSignalingMessage.h" 12 13 #import "sdk/objc/base/RTCLogging.h" 14 15 #import "ARDUtilities.h" 16 #import "RTCIceCandidate+JSON.h" 17 #import "RTCSessionDescription+JSON.h" 18 19 static NSString *const kARDSignalingMessageTypeKey = @"type"; 20 static NSString *const kARDTypeValueRemoveCandidates = @"remove-candidates"; 21 22 @implementation ARDSignalingMessage 23 24 @synthesize type = _type; 25 26 - (instancetype)initWithType:(ARDSignalingMessageType)type { 27 self = [super init]; 28 if (self) { 29 _type = type; 30 } 31 return self; 32 } 33 34 - (NSString *)description { 35 return [[NSString alloc] initWithData:[self JSONData] 36 encoding:NSUTF8StringEncoding]; 37 } 38 39 + (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString { 40 NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString]; 41 if (!values) { 42 RTCLogError(@"Error parsing signaling message JSON."); 43 return nil; 44 } 45 46 NSString *typeString = values[kARDSignalingMessageTypeKey]; 47 ARDSignalingMessage *message = nil; 48 if ([typeString isEqualToString:@"candidate"]) { 49 RTC_OBJC_TYPE(RTCIceCandidate) *candidate = 50 [RTC_OBJC_TYPE(RTCIceCandidate) candidateFromJSONDictionary:values]; 51 message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; 52 } else if ([typeString isEqualToString:kARDTypeValueRemoveCandidates]) { 53 RTCLogInfo(@"Received remove-candidates message"); 54 NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *candidates = 55 [RTC_OBJC_TYPE(RTCIceCandidate) candidatesFromJSONDictionary:values]; 56 message = [[ARDICECandidateRemovalMessage alloc] 57 initWithRemovedCandidates:candidates]; 58 } else if ([typeString isEqualToString:@"offer"] || 59 [typeString isEqualToString:@"answer"]) { 60 RTC_OBJC_TYPE(RTCSessionDescription) *description = 61 [RTC_OBJC_TYPE(RTCSessionDescription) 62 descriptionFromJSONDictionary:values]; 63 message = 64 [[ARDSessionDescriptionMessage alloc] initWithDescription:description]; 65 } else if ([typeString isEqualToString:@"bye"]) { 66 message = [[ARDByeMessage alloc] init]; 67 } else { 68 RTCLogError(@"Unexpected type: %@", typeString); 69 } 70 return message; 71 } 72 73 - (NSData *)JSONData { 74 return nil; 75 } 76 77 @end 78 79 @implementation ARDICECandidateMessage 80 81 @synthesize candidate = _candidate; 82 83 - (instancetype)initWithCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)candidate { 84 self = [super initWithType:kARDSignalingMessageTypeCandidate]; 85 if (self) { 86 _candidate = candidate; 87 } 88 return self; 89 } 90 91 - (NSData *)JSONData { 92 return [_candidate JSONData]; 93 } 94 95 @end 96 97 @implementation ARDICECandidateRemovalMessage 98 99 @synthesize candidates = _candidates; 100 101 - (instancetype)initWithRemovedCandidates: 102 (NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidates { 103 NSParameterAssert(candidates.count); 104 self = [super initWithType:kARDSignalingMessageTypeCandidateRemoval]; 105 if (self) { 106 _candidates = candidates; 107 } 108 return self; 109 } 110 111 - (NSData *)JSONData { 112 return [RTC_OBJC_TYPE(RTCIceCandidate) 113 JSONDataForIceCandidates:_candidates 114 withType:kARDTypeValueRemoveCandidates]; 115 } 116 117 @end 118 119 @implementation ARDSessionDescriptionMessage 120 121 @synthesize sessionDescription = _sessionDescription; 122 123 - (instancetype)initWithDescription: 124 (RTC_OBJC_TYPE(RTCSessionDescription) *)description { 125 ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer; 126 RTCSdpType sdpType = description.type; 127 switch (sdpType) { 128 case RTCSdpTypeOffer: 129 messageType = kARDSignalingMessageTypeOffer; 130 break; 131 case RTCSdpTypeAnswer: 132 messageType = kARDSignalingMessageTypeAnswer; 133 break; 134 case RTCSdpTypePrAnswer: 135 case RTCSdpTypeRollback: 136 NSAssert(NO, 137 @"Unexpected type: %@", 138 [RTC_OBJC_TYPE(RTCSessionDescription) stringForType:sdpType]); 139 break; 140 } 141 self = [super initWithType:messageType]; 142 if (self) { 143 _sessionDescription = description; 144 } 145 return self; 146 } 147 148 - (NSData *)JSONData { 149 return [_sessionDescription JSONData]; 150 } 151 152 @end 153 154 @implementation ARDByeMessage 155 156 - (instancetype)init { 157 return [super initWithType:kARDSignalingMessageTypeBye]; 158 } 159 160 - (NSData *)JSONData { 161 NSDictionary *message = @{@"type" : @"bye"}; 162 return [NSJSONSerialization dataWithJSONObject:message 163 options:NSJSONWritingPrettyPrinted 164 error:NULL]; 165 } 166 167 @end