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