blob: 4d49a2eada980f63256033245226345ac25b0a0a [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00004 *
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
tkchinc3f46a92015-07-23 12:50:55 -070030#import "RTCLogging.h"
31
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000032#import "ARDUtilities.h"
33#import "RTCICECandidate+JSON.h"
34#import "RTCSessionDescription+JSON.h"
35
36static NSString const *kARDSignalingMessageTypeKey = @"type";
37
38@implementation ARDSignalingMessage
39
40@synthesize type = _type;
41
42- (instancetype)initWithType:(ARDSignalingMessageType)type {
43 if (self = [super init]) {
44 _type = type;
45 }
46 return self;
47}
48
49- (NSString *)description {
50 return [[NSString alloc] initWithData:[self JSONData]
51 encoding:NSUTF8StringEncoding];
52}
53
54+ (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString {
55 NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString];
56 if (!values) {
tkchinc3f46a92015-07-23 12:50:55 -070057 RTCLogError(@"Error parsing signaling message JSON.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000058 return nil;
59 }
60
61 NSString *typeString = values[kARDSignalingMessageTypeKey];
62 ARDSignalingMessage *message = nil;
63 if ([typeString isEqualToString:@"candidate"]) {
64 RTCICECandidate *candidate =
65 [RTCICECandidate candidateFromJSONDictionary:values];
66 message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
67 } else if ([typeString isEqualToString:@"offer"] ||
68 [typeString isEqualToString:@"answer"]) {
69 RTCSessionDescription *description =
70 [RTCSessionDescription descriptionFromJSONDictionary:values];
71 message =
72 [[ARDSessionDescriptionMessage alloc] initWithDescription:description];
73 } else if ([typeString isEqualToString:@"bye"]) {
74 message = [[ARDByeMessage alloc] init];
75 } else {
tkchinc3f46a92015-07-23 12:50:55 -070076 RTCLogError(@"Unexpected type: %@", typeString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000077 }
78 return message;
79}
80
81- (NSData *)JSONData {
82 return nil;
83}
84
85@end
86
87@implementation ARDICECandidateMessage
88
89@synthesize candidate = _candidate;
90
91- (instancetype)initWithCandidate:(RTCICECandidate *)candidate {
92 if (self = [super initWithType:kARDSignalingMessageTypeCandidate]) {
93 _candidate = candidate;
94 }
95 return self;
96}
97
98- (NSData *)JSONData {
99 return [_candidate JSONData];
100}
101
102@end
103
104@implementation ARDSessionDescriptionMessage
105
106@synthesize sessionDescription = _sessionDescription;
107
108- (instancetype)initWithDescription:(RTCSessionDescription *)description {
109 ARDSignalingMessageType type = kARDSignalingMessageTypeOffer;
110 NSString *typeString = description.type;
111 if ([typeString isEqualToString:@"offer"]) {
112 type = kARDSignalingMessageTypeOffer;
113 } else if ([typeString isEqualToString:@"answer"]) {
114 type = kARDSignalingMessageTypeAnswer;
115 } else {
116 NSAssert(NO, @"Unexpected type: %@", typeString);
117 }
118 if (self = [super initWithType:type]) {
119 _sessionDescription = description;
120 }
121 return self;
122}
123
124- (NSData *)JSONData {
125 return [_sessionDescription JSONData];
126}
127
128@end
129
130@implementation ARDByeMessage
131
132- (instancetype)init {
133 return [super initWithType:kARDSignalingMessageTypeBye];
134}
135
136- (NSData *)JSONData {
137 NSDictionary *message = @{
138 @"type": @"bye"
139 };
140 return [NSJSONSerialization dataWithJSONObject:message
141 options:NSJSONWritingPrettyPrinted
142 error:NULL];
143}
144
145@end