tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2014 Google Inc. |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "ARDSignalingMessage.h" |
| 29 | |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 30 | #import "ARDLogging.h" |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 31 | #import "ARDUtilities.h" |
| 32 | #import "RTCICECandidate+JSON.h" |
| 33 | #import "RTCSessionDescription+JSON.h" |
| 34 | |
| 35 | static NSString const *kARDSignalingMessageTypeKey = @"type"; |
| 36 | |
| 37 | @implementation ARDSignalingMessage |
| 38 | |
| 39 | @synthesize type = _type; |
| 40 | |
| 41 | - (instancetype)initWithType:(ARDSignalingMessageType)type { |
| 42 | if (self = [super init]) { |
| 43 | _type = type; |
| 44 | } |
| 45 | return self; |
| 46 | } |
| 47 | |
| 48 | - (NSString *)description { |
| 49 | return [[NSString alloc] initWithData:[self JSONData] |
| 50 | encoding:NSUTF8StringEncoding]; |
| 51 | } |
| 52 | |
| 53 | + (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString { |
| 54 | NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString]; |
| 55 | if (!values) { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 56 | ARDLog(@"Error parsing signaling message JSON."); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 57 | return nil; |
| 58 | } |
| 59 | |
| 60 | NSString *typeString = values[kARDSignalingMessageTypeKey]; |
| 61 | ARDSignalingMessage *message = nil; |
| 62 | if ([typeString isEqualToString:@"candidate"]) { |
| 63 | RTCICECandidate *candidate = |
| 64 | [RTCICECandidate candidateFromJSONDictionary:values]; |
| 65 | message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate]; |
| 66 | } else if ([typeString isEqualToString:@"offer"] || |
| 67 | [typeString isEqualToString:@"answer"]) { |
| 68 | RTCSessionDescription *description = |
| 69 | [RTCSessionDescription descriptionFromJSONDictionary:values]; |
| 70 | message = |
| 71 | [[ARDSessionDescriptionMessage alloc] initWithDescription:description]; |
| 72 | } else if ([typeString isEqualToString:@"bye"]) { |
| 73 | message = [[ARDByeMessage alloc] init]; |
| 74 | } else { |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 75 | ARDLog(@"Unexpected type: %@", typeString); |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 76 | } |
| 77 | return message; |
| 78 | } |
| 79 | |
| 80 | - (NSData *)JSONData { |
| 81 | return nil; |
| 82 | } |
| 83 | |
| 84 | @end |
| 85 | |
| 86 | @implementation ARDICECandidateMessage |
| 87 | |
| 88 | @synthesize candidate = _candidate; |
| 89 | |
| 90 | - (instancetype)initWithCandidate:(RTCICECandidate *)candidate { |
| 91 | if (self = [super initWithType:kARDSignalingMessageTypeCandidate]) { |
| 92 | _candidate = candidate; |
| 93 | } |
| 94 | return self; |
| 95 | } |
| 96 | |
| 97 | - (NSData *)JSONData { |
| 98 | return [_candidate JSONData]; |
| 99 | } |
| 100 | |
| 101 | @end |
| 102 | |
| 103 | @implementation ARDSessionDescriptionMessage |
| 104 | |
| 105 | @synthesize sessionDescription = _sessionDescription; |
| 106 | |
| 107 | - (instancetype)initWithDescription:(RTCSessionDescription *)description { |
| 108 | ARDSignalingMessageType type = kARDSignalingMessageTypeOffer; |
| 109 | NSString *typeString = description.type; |
| 110 | if ([typeString isEqualToString:@"offer"]) { |
| 111 | type = kARDSignalingMessageTypeOffer; |
| 112 | } else if ([typeString isEqualToString:@"answer"]) { |
| 113 | type = kARDSignalingMessageTypeAnswer; |
| 114 | } else { |
| 115 | NSAssert(NO, @"Unexpected type: %@", typeString); |
| 116 | } |
| 117 | if (self = [super initWithType:type]) { |
| 118 | _sessionDescription = description; |
| 119 | } |
| 120 | return self; |
| 121 | } |
| 122 | |
| 123 | - (NSData *)JSONData { |
| 124 | return [_sessionDescription JSONData]; |
| 125 | } |
| 126 | |
| 127 | @end |
| 128 | |
| 129 | @implementation ARDByeMessage |
| 130 | |
| 131 | - (instancetype)init { |
| 132 | return [super initWithType:kARDSignalingMessageTypeBye]; |
| 133 | } |
| 134 | |
| 135 | - (NSData *)JSONData { |
| 136 | NSDictionary *message = @{ |
| 137 | @"type": @"bye" |
| 138 | }; |
| 139 | return [NSJSONSerialization dataWithJSONObject:message |
| 140 | options:NSJSONWritingPrettyPrinted |
| 141 | error:NULL]; |
| 142 | } |
| 143 | |
| 144 | @end |