tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #import "ARDSignalingMessage.h" |
| 12 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #import "WebRTC/RTCLogging.h" |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 14 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 15 | #import "ARDUtilities.h" |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 16 | #import "RTCIceCandidate+JSON.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 17 | #import "RTCSessionDescription+JSON.h" |
| 18 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 19 | static NSString * const kARDSignalingMessageTypeKey = @"type"; |
| 20 | static NSString * const kARDTypeValueRemoveCandidates = @"remove-candidates"; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 41 | RTCLogError(@"Error parsing signaling message JSON."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 42 | return nil; |
| 43 | } |
| 44 | |
| 45 | NSString *typeString = values[kARDSignalingMessageTypeKey]; |
| 46 | ARDSignalingMessage *message = nil; |
| 47 | if ([typeString isEqualToString:@"candidate"]) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 48 | RTCIceCandidate *candidate = |
| 49 | [RTCIceCandidate candidateFromJSONDictionary:values]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 50 | message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 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]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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 { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 66 | RTCLogError(@"Unexpected type: %@", typeString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 81 | - (instancetype)initWithCandidate:(RTCIceCandidate *)candidate { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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 | |
Honghai Zhang | da2ba4d | 2016-05-23 11:53:14 -0700 | [diff] [blame] | 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 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 115 | @implementation ARDSessionDescriptionMessage |
| 116 | |
| 117 | @synthesize sessionDescription = _sessionDescription; |
| 118 | |
| 119 | - (instancetype)initWithDescription:(RTCSessionDescription *)description { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 120 | ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer; |
| 121 | RTCSdpType sdpType = description.type; |
| 122 | switch (sdpType) { |
| 123 | case RTCSdpTypeOffer: |
| 124 | messageType = kARDSignalingMessageTypeOffer; |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 125 | break; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 126 | case RTCSdpTypeAnswer: |
| 127 | messageType = kARDSignalingMessageTypeAnswer; |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 128 | break; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 129 | case RTCSdpTypePrAnswer: |
| 130 | NSAssert(NO, @"Unexpected type: %@", |
| 131 | [RTCSessionDescription stringForType:sdpType]); |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 132 | break; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 133 | } |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 134 | if (self = [super initWithType:messageType]) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 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 |