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 | |
| 19 | static NSString const *kARDSignalingMessageTypeKey = @"type"; |
| 20 | |
| 21 | @implementation ARDSignalingMessage |
| 22 | |
| 23 | @synthesize type = _type; |
| 24 | |
| 25 | - (instancetype)initWithType:(ARDSignalingMessageType)type { |
| 26 | if (self = [super init]) { |
| 27 | _type = type; |
| 28 | } |
| 29 | return self; |
| 30 | } |
| 31 | |
| 32 | - (NSString *)description { |
| 33 | return [[NSString alloc] initWithData:[self JSONData] |
| 34 | encoding:NSUTF8StringEncoding]; |
| 35 | } |
| 36 | |
| 37 | + (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString { |
| 38 | NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString]; |
| 39 | if (!values) { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 40 | RTCLogError(@"Error parsing signaling message JSON."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 41 | return nil; |
| 42 | } |
| 43 | |
| 44 | NSString *typeString = values[kARDSignalingMessageTypeKey]; |
| 45 | ARDSignalingMessage *message = nil; |
| 46 | if ([typeString isEqualToString:@"candidate"]) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 47 | RTCIceCandidate *candidate = |
| 48 | [RTCIceCandidate candidateFromJSONDictionary:values]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 49 | message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
| 50 | } else if ([typeString isEqualToString:@"offer"] || |
| 51 | [typeString isEqualToString:@"answer"]) { |
| 52 | RTCSessionDescription *description = |
| 53 | [RTCSessionDescription descriptionFromJSONDictionary:values]; |
| 54 | message = |
| 55 | [[ARDSessionDescriptionMessage alloc] initWithDescription:description]; |
| 56 | } else if ([typeString isEqualToString:@"bye"]) { |
| 57 | message = [[ARDByeMessage alloc] init]; |
| 58 | } else { |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 59 | RTCLogError(@"Unexpected type: %@", typeString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 60 | } |
| 61 | return message; |
| 62 | } |
| 63 | |
| 64 | - (NSData *)JSONData { |
| 65 | return nil; |
| 66 | } |
| 67 | |
| 68 | @end |
| 69 | |
| 70 | @implementation ARDICECandidateMessage |
| 71 | |
| 72 | @synthesize candidate = _candidate; |
| 73 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 74 | - (instancetype)initWithCandidate:(RTCIceCandidate *)candidate { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 75 | if (self = [super initWithType:kARDSignalingMessageTypeCandidate]) { |
| 76 | _candidate = candidate; |
| 77 | } |
| 78 | return self; |
| 79 | } |
| 80 | |
| 81 | - (NSData *)JSONData { |
| 82 | return [_candidate JSONData]; |
| 83 | } |
| 84 | |
| 85 | @end |
| 86 | |
| 87 | @implementation ARDSessionDescriptionMessage |
| 88 | |
| 89 | @synthesize sessionDescription = _sessionDescription; |
| 90 | |
| 91 | - (instancetype)initWithDescription:(RTCSessionDescription *)description { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 92 | ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer; |
| 93 | RTCSdpType sdpType = description.type; |
| 94 | switch (sdpType) { |
| 95 | case RTCSdpTypeOffer: |
| 96 | messageType = kARDSignalingMessageTypeOffer; |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 97 | break; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 98 | case RTCSdpTypeAnswer: |
| 99 | messageType = kARDSignalingMessageTypeAnswer; |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 100 | break; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 101 | case RTCSdpTypePrAnswer: |
| 102 | NSAssert(NO, @"Unexpected type: %@", |
| 103 | [RTCSessionDescription stringForType:sdpType]); |
hjon | c4ec4a2 | 2016-03-14 14:56:47 -0700 | [diff] [blame] | 104 | break; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 105 | } |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 106 | if (self = [super initWithType:messageType]) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 107 | _sessionDescription = description; |
| 108 | } |
| 109 | return self; |
| 110 | } |
| 111 | |
| 112 | - (NSData *)JSONData { |
| 113 | return [_sessionDescription JSONData]; |
| 114 | } |
| 115 | |
| 116 | @end |
| 117 | |
| 118 | @implementation ARDByeMessage |
| 119 | |
| 120 | - (instancetype)init { |
| 121 | return [super initWithType:kARDSignalingMessageTypeBye]; |
| 122 | } |
| 123 | |
| 124 | - (NSData *)JSONData { |
| 125 | NSDictionary *message = @{ |
| 126 | @"type": @"bye" |
| 127 | }; |
| 128 | return [NSJSONSerialization dataWithJSONObject:message |
| 129 | options:NSJSONWritingPrettyPrinted |
| 130 | error:NULL]; |
| 131 | } |
| 132 | |
| 133 | @end |