blob: 049c0f5b0a68dfdad293ed8aa7126baeceb62dd7 [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
Mirko Bonadei19640aa2020-10-19 16:12:43 +020013#import "sdk/objc/base/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"]) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020048 RTC_OBJC_TYPE(RTCIceCandidate) *candidate =
49 [RTC_OBJC_TYPE(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");
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020053 NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *candidates =
54 [RTC_OBJC_TYPE(RTCIceCandidate) candidatesFromJSONDictionary:values];
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070055 message = [[ARDICECandidateRemovalMessage alloc]
56 initWithRemovedCandidates:candidates];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000057 } else if ([typeString isEqualToString:@"offer"] ||
58 [typeString isEqualToString:@"answer"]) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020059 RTC_OBJC_TYPE(RTCSessionDescription) *description =
60 [RTC_OBJC_TYPE(RTCSessionDescription) descriptionFromJSONDictionary:values];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000061 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
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020081- (instancetype)initWithCandidate:(RTC_OBJC_TYPE(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
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020098- (instancetype)initWithRemovedCandidates:(NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidates {
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070099 NSParameterAssert(candidates.count);
100 if (self = [super initWithType:kARDSignalingMessageTypeCandidateRemoval]) {
101 _candidates = candidates;
102 }
103 return self;
104}
105
106- (NSData *)JSONData {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200107 return [RTC_OBJC_TYPE(RTCIceCandidate) JSONDataForIceCandidates:_candidates
108 withType:kARDTypeValueRemoveCandidates];
Honghai Zhangda2ba4d2016-05-23 11:53:14 -0700109}
110
111@end
112
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000113@implementation ARDSessionDescriptionMessage
114
115@synthesize sessionDescription = _sessionDescription;
116
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200117- (instancetype)initWithDescription:(RTC_OBJC_TYPE(RTCSessionDescription) *)description {
hjon79858f82016-03-13 22:08:26 -0700118 ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer;
119 RTCSdpType sdpType = description.type;
120 switch (sdpType) {
121 case RTCSdpTypeOffer:
122 messageType = kARDSignalingMessageTypeOffer;
hjonc4ec4a22016-03-14 14:56:47 -0700123 break;
hjon79858f82016-03-13 22:08:26 -0700124 case RTCSdpTypeAnswer:
125 messageType = kARDSignalingMessageTypeAnswer;
hjonc4ec4a22016-03-14 14:56:47 -0700126 break;
hjon79858f82016-03-13 22:08:26 -0700127 case RTCSdpTypePrAnswer:
Philipp Hancke5152ea52020-09-09 14:58:32 +0200128 case RTCSdpTypeRollback:
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200129 NSAssert(
130 NO, @"Unexpected type: %@", [RTC_OBJC_TYPE(RTCSessionDescription) stringForType:sdpType]);
hjonc4ec4a22016-03-14 14:56:47 -0700131 break;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000132 }
hjon79858f82016-03-13 22:08:26 -0700133 if (self = [super initWithType:messageType]) {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000134 _sessionDescription = description;
135 }
136 return self;
137}
138
139- (NSData *)JSONData {
140 return [_sessionDescription JSONData];
141}
142
143@end
144
145@implementation ARDByeMessage
146
147- (instancetype)init {
148 return [super initWithType:kARDSignalingMessageTypeBye];
149}
150
151- (NSData *)JSONData {
152 NSDictionary *message = @{
153 @"type": @"bye"
154 };
155 return [NSJSONSerialization dataWithJSONObject:message
156 options:NSJSONWritingPrettyPrinted
157 error:NULL];
158}
159
160@end