VoIPCallMessageDecoder.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "VoIPCallMessageDecoder.h"
  21. #import <WebRTC/RTCSessionDescription.h>
  22. #ifdef DEBUG
  23. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  24. #else
  25. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  26. #endif
  27. @implementation VoIPCallMessageDecoder
  28. #pragma mark - Public functions
  29. + (instancetype)messageDecoder {
  30. VoIPCallMessageDecoder *decoder = [[VoIPCallMessageDecoder alloc] init];
  31. return decoder;
  32. }
  33. - (VoIPCallOfferMessage *)decodeVoIPCallOfferFromBox:(BoxVoIPCallOfferMessage *)boxMessage {
  34. return [self decodeVoIPCallOfferMessage:boxMessage];
  35. }
  36. - (VoIPCallAnswerMessage *)decodeVoIPCallAnswerFromBox:(BoxVoIPCallAnswerMessage *)boxMessage {
  37. return [self decodeVoIPCallAnswerMessage:boxMessage];
  38. }
  39. - (VoIPCallHangupMessage *)decodeVoIPCallHangupFromBox:(BoxVoIPCallHangupMessage *)boxMessage contact:(Contact *)contact {
  40. return [self decodeVoIPCallHangupMessage:boxMessage contact:contact];
  41. }
  42. - (VoIPCallRingingMessage *)decodeVoIPCallRingingFromBox:(BoxVoIPCallRingingMessage *)boxMessage contact:(Contact *)contact {
  43. return [self decodeVoIPCallRingingMessage:boxMessage contact:contact];
  44. }
  45. - (VoIPCallIceCandidatesMessage *)decodeVoIPCallIceCandidatesFromBox:(BoxVoIPCallIceCandidatesMessage *)boxMessage {
  46. return [self decodeVoIPCallIceCandidates:boxMessage];
  47. }
  48. #pragma mark - Private functions
  49. - (VoIPCallOfferMessage *)decodeVoIPCallOfferMessage:(AbstractMessage *)boxMessage {
  50. NSData *jsonData;
  51. if ([boxMessage isKindOfClass:[BoxVoIPCallOfferMessage class]]) {
  52. jsonData = ((BoxVoIPCallAnswerMessage *)boxMessage).jsonData;
  53. } else {
  54. DDLogError(@"VoIP call decode: invalid message type");
  55. return nil;
  56. }
  57. VoIPCallOfferMessage *message = [self parseJsonVoIPCallOfferMessage:jsonData];
  58. return message;
  59. }
  60. - (VoIPCallAnswerMessage *)decodeVoIPCallAnswerMessage:(AbstractMessage *)boxMessage {
  61. NSData *jsonData;
  62. if ([boxMessage isKindOfClass:[BoxVoIPCallAnswerMessage class]]) {
  63. jsonData = ((BoxVoIPCallAnswerMessage *)boxMessage).jsonData;
  64. } else {
  65. DDLogError(@"VoIP call decode: invalid message type");
  66. return nil;
  67. }
  68. VoIPCallAnswerMessage *message = [self parseJsonVoIPCallAnswerMessage:jsonData];
  69. return message;
  70. }
  71. - (VoIPCallHangupMessage *)decodeVoIPCallHangupMessage:(AbstractMessage *)boxMessage contact:(Contact *)contact {
  72. NSData *jsonData;
  73. if ([boxMessage isKindOfClass:[BoxVoIPCallHangupMessage class]]) {
  74. jsonData = ((BoxVoIPCallHangupMessage *)boxMessage).jsonData;
  75. } else {
  76. DDLogError(@"VoIP call decode: invalid message type");
  77. return nil;
  78. }
  79. VoIPCallHangupMessage *message = [self parseJsonVoIPCallHangupMessage:jsonData contact:contact];
  80. return message;
  81. }
  82. - (VoIPCallRingingMessage *)decodeVoIPCallRingingMessage:(AbstractMessage *)boxMessage contact:(Contact *)contact {
  83. NSData *jsonData;
  84. if ([boxMessage isKindOfClass:[BoxVoIPCallRingingMessage class]]) {
  85. jsonData = ((BoxVoIPCallRingingMessage *)boxMessage).jsonData;
  86. } else {
  87. DDLogError(@"VoIP call decode: invalid message type");
  88. return nil;
  89. }
  90. VoIPCallRingingMessage *message = [self parseJsonVoIPCallRingingMessage:jsonData contact:contact];
  91. return message;
  92. }
  93. - (VoIPCallIceCandidatesMessage *)decodeVoIPCallIceCandidates:(AbstractMessage *)boxMessage {
  94. NSData *jsonData;
  95. if ([boxMessage isKindOfClass:[BoxVoIPCallIceCandidatesMessage class]]) {
  96. jsonData = ((BoxVoIPCallIceCandidatesMessage *)boxMessage).jsonData;
  97. } else {
  98. DDLogError(@"VoIP call decode: invalid message type");
  99. return nil;
  100. }
  101. VoIPCallIceCandidatesMessage *message = [self parseJsonVoIPCallIceCandidatesMessage:jsonData];
  102. return message;
  103. }
  104. - (VoIPCallAnswerMessage *)parseJsonVoIPCallAnswerMessage:(NSData *)jsonData {
  105. NSError *error;
  106. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  107. if (json == nil) {
  108. DDLogError(@"Error parsing ballot json data %@, %@", error, [error userInfo]);
  109. return nil;
  110. }
  111. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage answerFromJSONDictionary:json];
  112. return message;
  113. }
  114. - (VoIPCallHangupMessage *)parseJsonVoIPCallHangupMessage:(NSData *)jsonData contact:(Contact *)contact {
  115. if (jsonData.bytes == 0) {
  116. return [VoIPCallHangupMessage hangupFromJSONDictionary:nil contact:contact];
  117. }
  118. NSError *error;
  119. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  120. if (json == nil) {
  121. DDLogError(@"Error parsing ballot json data %@, %@", error, [error userInfo]);
  122. return nil;
  123. }
  124. VoIPCallHangupMessage *message = [VoIPCallHangupMessage hangupFromJSONDictionary:json contact:contact];
  125. return message;
  126. }
  127. - (VoIPCallRingingMessage *)parseJsonVoIPCallRingingMessage:(NSData *)jsonData contact:(Contact *)contact {
  128. if (jsonData.bytes == 0) {
  129. return [VoIPCallRingingMessage ringingFromJSONDictionary:nil contact:contact];
  130. }
  131. NSError *error;
  132. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  133. if (json == nil) {
  134. DDLogError(@"Error parsing ballot json data %@, %@", error, [error userInfo]);
  135. return nil;
  136. }
  137. VoIPCallRingingMessage *message = [VoIPCallRingingMessage ringingFromJSONDictionary:json contact:contact];
  138. return message;
  139. }
  140. - (VoIPCallOfferMessage *)parseJsonVoIPCallOfferMessage:(NSData *)jsonData {
  141. NSError *error;
  142. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  143. if (json == nil) {
  144. DDLogError(@"Error parsing ballot json data %@, %@", error, [error userInfo]);
  145. return nil;
  146. }
  147. VoIPCallOfferMessage *message = [VoIPCallOfferMessage offerFromJSONDictionary:json];
  148. return message;
  149. }
  150. - (VoIPCallIceCandidatesMessage *)parseJsonVoIPCallIceCandidatesMessage:(NSData *)jsonData {
  151. NSError *error;
  152. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  153. if (json == nil) {
  154. DDLogError(@"Error parsing ballot json data %@, %@", error, [error userInfo]);
  155. return nil;
  156. }
  157. VoIPCallIceCandidatesMessage *message = [VoIPCallIceCandidatesMessage iceCandidatesWithDictionary:json];
  158. return message;
  159. }
  160. @end