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