blob: a5ca29576a0c0315194b00b938efc98e26d3be04 [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
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;
hjonc4ec4a22016-03-14 14:56:47 -070097 break;
hjon79858f82016-03-13 22:08:26 -070098 case RTCSdpTypeAnswer:
99 messageType = kARDSignalingMessageTypeAnswer;
hjonc4ec4a22016-03-14 14:56:47 -0700100 break;
hjon79858f82016-03-13 22:08:26 -0700101 case RTCSdpTypePrAnswer:
102 NSAssert(NO, @"Unexpected type: %@",
103 [RTCSessionDescription stringForType:sdpType]);
hjonc4ec4a22016-03-14 14:56:47 -0700104 break;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000105 }
hjon79858f82016-03-13 22:08:26 -0700106 if (self = [super initWithType:messageType]) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000107 _sessionDescription = description;
108 }
109 return self;
110}
111
112- (NSData *)JSONData {
113 return [_sessionDescription JSONData];
114}
115
116@end
117
118@implementation ARDByeMessage
119
120- (instancetype)init {
121 return [super initWithType:kARDSignalingMessageTypeBye];
122}
123
124- (NSData *)JSONData {
125 NSDictionary *message = @{
126 @"type": @"bye"
127 };
128 return [NSJSONSerialization dataWithJSONObject:message
129 options:NSJSONWritingPrettyPrinted
130 error:NULL];
131}
132
133@end