blob: c9bc3f19b0a1b770c4d2aa8cb3f6444dec71be28 [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.org87776a82014-12-09 19:32:35 +00009 */
10
11#import "ARDSignalingMessage.h"
12
hjon79858f82016-03-13 22:08:26 -070013#import "webrtc/base/objc/RTCLogging.h"
tkchinc3f46a92015-07-23 12:50:55 -070014
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000015#import "ARDUtilities.h"
hjon79858f82016-03-13 22:08:26 -070016#import "RTCIceCandidate+JSON.h"
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000017#import "RTCSessionDescription+JSON.h"
18
19static 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) {
tkchinc3f46a92015-07-23 12:50:55 -070040 RTCLogError(@"Error parsing signaling message JSON.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000041 return nil;
42 }
43
44 NSString *typeString = values[kARDSignalingMessageTypeKey];
45 ARDSignalingMessage *message = nil;
46 if ([typeString isEqualToString:@"candidate"]) {
hjon79858f82016-03-13 22:08:26 -070047 RTCIceCandidate *candidate =
48 [RTCIceCandidate candidateFromJSONDictionary:values];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000049 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 {
tkchinc3f46a92015-07-23 12:50:55 -070059 RTCLogError(@"Unexpected type: %@", typeString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000060 }
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
hjon79858f82016-03-13 22:08:26 -070074- (instancetype)initWithCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000075 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 {
hjon79858f82016-03-13 22:08:26 -070092 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.org87776a82014-12-09 19:32:35 +0000102 }
hjon79858f82016-03-13 22:08:26 -0700103 if (self = [super initWithType:messageType]) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000104 _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