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 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 13 | #import "webrtc/base/objc/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; |
| 97 | case RTCSdpTypeAnswer: |
| 98 | messageType = kARDSignalingMessageTypeAnswer; |
| 99 | case RTCSdpTypePrAnswer: |
| 100 | NSAssert(NO, @"Unexpected type: %@", |
| 101 | [RTCSessionDescription stringForType:sdpType]); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 102 | } |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 103 | if (self = [super initWithType:messageType]) { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 104 | _sessionDescription = description; |
| 105 | } |
| 106 | return self; |
| 107 | } |
| 108 | |
| 109 | - (NSData *)JSONData { |
| 110 | return [_sessionDescription JSONData]; |
| 111 | } |
| 112 | |
| 113 | @end |
| 114 | |
| 115 | @implementation ARDByeMessage |
| 116 | |
| 117 | - (instancetype)init { |
| 118 | return [super initWithType:kARDSignalingMessageTypeBye]; |
| 119 | } |
| 120 | |
| 121 | - (NSData *)JSONData { |
| 122 | NSDictionary *message = @{ |
| 123 | @"type": @"bye" |
| 124 | }; |
| 125 | return [NSJSONSerialization dataWithJSONObject:message |
| 126 | options:NSJSONWritingPrettyPrinted |
| 127 | error:NULL]; |
| 128 | } |
| 129 | |
| 130 | @end |