blob: 3fab185848513b53ab6bbe8190ccfbd6a7ae8524 [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
tkchin9eeb6242016-04-27 01:54:20 -070013#import "WebRTC/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
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070019static NSString * const kARDSignalingMessageTypeKey = @"type";
20static NSString * const kARDTypeValueRemoveCandidates = @"remove-candidates";
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000021
22@implementation ARDSignalingMessage
23
24@synthesize type = _type;
25
26- (instancetype)initWithType:(ARDSignalingMessageType)type {
27 if (self = [super init]) {
28 _type = type;
29 }
30 return self;
31}
32
33- (NSString *)description {
34 return [[NSString alloc] initWithData:[self JSONData]
35 encoding:NSUTF8StringEncoding];
36}
37
38+ (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString {
39 NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString];
40 if (!values) {
tkchinc3f46a92015-07-23 12:50:55 -070041 RTCLogError(@"Error parsing signaling message JSON.");
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000042 return nil;
43 }
44
45 NSString *typeString = values[kARDSignalingMessageTypeKey];
46 ARDSignalingMessage *message = nil;
47 if ([typeString isEqualToString:@"candidate"]) {
hjon79858f82016-03-13 22:08:26 -070048 RTCIceCandidate *candidate =
49 [RTCIceCandidate candidateFromJSONDictionary:values];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000050 message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070051 } else if ([typeString isEqualToString:kARDTypeValueRemoveCandidates]) {
52 RTCLogInfo(@"Received remove-candidates message");
53 NSArray<RTCIceCandidate *> *candidates =
54 [RTCIceCandidate candidatesFromJSONDictionary:values];
55 message = [[ARDICECandidateRemovalMessage alloc]
56 initWithRemovedCandidates:candidates];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000057 } else if ([typeString isEqualToString:@"offer"] ||
58 [typeString isEqualToString:@"answer"]) {
59 RTCSessionDescription *description =
60 [RTCSessionDescription descriptionFromJSONDictionary:values];
61 message =
62 [[ARDSessionDescriptionMessage alloc] initWithDescription:description];
63 } else if ([typeString isEqualToString:@"bye"]) {
64 message = [[ARDByeMessage alloc] init];
65 } else {
tkchinc3f46a92015-07-23 12:50:55 -070066 RTCLogError(@"Unexpected type: %@", typeString);
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000067 }
68 return message;
69}
70
71- (NSData *)JSONData {
72 return nil;
73}
74
75@end
76
77@implementation ARDICECandidateMessage
78
79@synthesize candidate = _candidate;
80
hjon79858f82016-03-13 22:08:26 -070081- (instancetype)initWithCandidate:(RTCIceCandidate *)candidate {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000082 if (self = [super initWithType:kARDSignalingMessageTypeCandidate]) {
83 _candidate = candidate;
84 }
85 return self;
86}
87
88- (NSData *)JSONData {
89 return [_candidate JSONData];
90}
91
92@end
93
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070094@implementation ARDICECandidateRemovalMessage
95
96@synthesize candidates = _candidates;
97
98- (instancetype)initWithRemovedCandidates:(
99 NSArray<RTCIceCandidate *> *)candidates {
100 NSParameterAssert(candidates.count);
101 if (self = [super initWithType:kARDSignalingMessageTypeCandidateRemoval]) {
102 _candidates = candidates;
103 }
104 return self;
105}
106
107- (NSData *)JSONData {
108 return
109 [RTCIceCandidate JSONDataForIceCandidates:_candidates
110 withType:kARDTypeValueRemoveCandidates];
111}
112
113@end
114
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115@implementation ARDSessionDescriptionMessage
116
117@synthesize sessionDescription = _sessionDescription;
118
119- (instancetype)initWithDescription:(RTCSessionDescription *)description {
hjon79858f82016-03-13 22:08:26 -0700120 ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer;
121 RTCSdpType sdpType = description.type;
122 switch (sdpType) {
123 case RTCSdpTypeOffer:
124 messageType = kARDSignalingMessageTypeOffer;
hjonc4ec4a22016-03-14 14:56:47 -0700125 break;
hjon79858f82016-03-13 22:08:26 -0700126 case RTCSdpTypeAnswer:
127 messageType = kARDSignalingMessageTypeAnswer;
hjonc4ec4a22016-03-14 14:56:47 -0700128 break;
hjon79858f82016-03-13 22:08:26 -0700129 case RTCSdpTypePrAnswer:
130 NSAssert(NO, @"Unexpected type: %@",
131 [RTCSessionDescription stringForType:sdpType]);
hjonc4ec4a22016-03-14 14:56:47 -0700132 break;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000133 }
hjon79858f82016-03-13 22:08:26 -0700134 if (self = [super initWithType:messageType]) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000135 _sessionDescription = description;
136 }
137 return self;
138}
139
140- (NSData *)JSONData {
141 return [_sessionDescription JSONData];
142}
143
144@end
145
146@implementation ARDByeMessage
147
148- (instancetype)init {
149 return [super initWithType:kARDSignalingMessageTypeBye];
150}
151
152- (NSData *)JSONData {
153 NSDictionary *message = @{
154 @"type": @"bye"
155 };
156 return [NSJSONSerialization dataWithJSONObject:message
157 options:NSJSONWritingPrettyPrinted
158 error:NULL];
159}
160
161@end