You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2014 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * 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.
  9. */
  10. #import "ARDSignalingMessage.h"
  11. #import <WebRTC/RTCLogging.h>
  12. #import "ARDUtilities.h"
  13. #import "RTCIceCandidate+JSON.h"
  14. #import "RTCSessionDescription+JSON.h"
  15. static NSString * const kARDSignalingMessageTypeKey = @"type";
  16. static NSString * const kARDTypeValueRemoveCandidates = @"remove-candidates";
  17. @implementation ARDSignalingMessage
  18. @synthesize type = _type;
  19. - (instancetype)initWithType:(ARDSignalingMessageType)type {
  20. if (self = [super init]) {
  21. _type = type;
  22. }
  23. return self;
  24. }
  25. - (NSString *)description {
  26. return [[NSString alloc] initWithData:[self JSONData]
  27. encoding:NSUTF8StringEncoding];
  28. }
  29. + (ARDSignalingMessage *)messageFromJSONString:(NSString *)jsonString {
  30. NSDictionary *values = [NSDictionary dictionaryWithJSONString:jsonString];
  31. if (!values) {
  32. RTCLogError(@"Error parsing signaling message JSON.");
  33. return nil;
  34. }
  35. NSString *typeString = values[kARDSignalingMessageTypeKey];
  36. ARDSignalingMessage *message = nil;
  37. if ([typeString isEqualToString:@"candidate"]) {
  38. RTCIceCandidate *candidate =
  39. [RTCIceCandidate candidateFromJSONDictionary:values];
  40. message = [[ARDICECandidateMessage alloc] initWithCandidate:candidate];
  41. } else if ([typeString isEqualToString:kARDTypeValueRemoveCandidates]) {
  42. RTCLogInfo(@"Received remove-candidates message");
  43. NSArray<RTCIceCandidate *> *candidates =
  44. [RTCIceCandidate candidatesFromJSONDictionary:values];
  45. message = [[ARDICECandidateRemovalMessage alloc]
  46. initWithRemovedCandidates:candidates];
  47. } else if ([typeString isEqualToString:@"offer"] ||
  48. [typeString isEqualToString:@"answer"]) {
  49. RTCSessionDescription *description =
  50. [RTCSessionDescription descriptionFromJSONDictionary:values];
  51. message =
  52. [[ARDSessionDescriptionMessage alloc] initWithDescription:description];
  53. } else if ([typeString isEqualToString:@"bye"]) {
  54. message = [[ARDByeMessage alloc] init];
  55. } else {
  56. RTCLogError(@"Unexpected type: %@", typeString);
  57. }
  58. return message;
  59. }
  60. - (NSData *)JSONData {
  61. return nil;
  62. }
  63. @end
  64. @implementation ARDICECandidateMessage
  65. @synthesize candidate = _candidate;
  66. - (instancetype)initWithCandidate:(RTCIceCandidate *)candidate {
  67. if (self = [super initWithType:kARDSignalingMessageTypeCandidate]) {
  68. _candidate = candidate;
  69. }
  70. return self;
  71. }
  72. - (NSData *)JSONData {
  73. return [_candidate JSONData];
  74. }
  75. @end
  76. @implementation ARDICECandidateRemovalMessage
  77. @synthesize candidates = _candidates;
  78. - (instancetype)initWithRemovedCandidates:(
  79. NSArray<RTCIceCandidate *> *)candidates {
  80. NSParameterAssert(candidates.count);
  81. if (self = [super initWithType:kARDSignalingMessageTypeCandidateRemoval]) {
  82. _candidates = candidates;
  83. }
  84. return self;
  85. }
  86. - (NSData *)JSONData {
  87. return
  88. [RTCIceCandidate JSONDataForIceCandidates:_candidates
  89. withType:kARDTypeValueRemoveCandidates];
  90. }
  91. @end
  92. @implementation ARDSessionDescriptionMessage
  93. @synthesize sessionDescription = _sessionDescription;
  94. - (instancetype)initWithDescription:(RTCSessionDescription *)description {
  95. ARDSignalingMessageType messageType = kARDSignalingMessageTypeOffer;
  96. RTCSdpType sdpType = description.type;
  97. switch (sdpType) {
  98. case RTCSdpTypeOffer:
  99. messageType = kARDSignalingMessageTypeOffer;
  100. break;
  101. case RTCSdpTypeAnswer:
  102. messageType = kARDSignalingMessageTypeAnswer;
  103. break;
  104. case RTCSdpTypePrAnswer:
  105. NSAssert(NO, @"Unexpected type: %@",
  106. [RTCSessionDescription stringForType:sdpType]);
  107. break;
  108. }
  109. if (self = [super initWithType:messageType]) {
  110. _sessionDescription = description;
  111. }
  112. return self;
  113. }
  114. - (NSData *)JSONData {
  115. return [_sessionDescription JSONData];
  116. }
  117. @end
  118. @implementation ARDByeMessage
  119. - (instancetype)init {
  120. return [super initWithType:kARDSignalingMessageTypeBye];
  121. }
  122. - (NSData *)JSONData {
  123. NSDictionary *message = @{
  124. @"type": @"bye"
  125. };
  126. return [NSJSONSerialization dataWithJSONObject:message
  127. options:NSJSONWritingPrettyPrinted
  128. error:NULL];
  129. }
  130. @end