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.

102 lines
3.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 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 "RTCSessionDescription+Private.h"
  11. #import "base/RTCLogging.h"
  12. #import "helpers/NSString+StdString.h"
  13. #include "rtc_base/checks.h"
  14. @implementation RTCSessionDescription
  15. @synthesize type = _type;
  16. @synthesize sdp = _sdp;
  17. + (NSString *)stringForType:(RTCSdpType)type {
  18. std::string string = [[self class] stdStringForType:type];
  19. return [NSString stringForStdString:string];
  20. }
  21. + (RTCSdpType)typeForString:(NSString *)string {
  22. std::string typeString = string.stdString;
  23. return [[self class] typeForStdString:typeString];
  24. }
  25. - (instancetype)initWithType:(RTCSdpType)type sdp:(NSString *)sdp {
  26. NSParameterAssert(sdp.length);
  27. if (self = [super init]) {
  28. _type = type;
  29. _sdp = [sdp copy];
  30. }
  31. return self;
  32. }
  33. - (NSString *)description {
  34. return [NSString stringWithFormat:@"RTCSessionDescription:\n%@\n%@",
  35. [[self class] stringForType:_type],
  36. _sdp];
  37. }
  38. #pragma mark - Private
  39. - (webrtc::SessionDescriptionInterface *)nativeDescription {
  40. webrtc::SdpParseError error;
  41. webrtc::SessionDescriptionInterface *description =
  42. webrtc::CreateSessionDescription([[self class] stdStringForType:_type],
  43. _sdp.stdString,
  44. &error);
  45. if (!description) {
  46. RTCLogError(@"Failed to create session description: %s\nline: %s",
  47. error.description.c_str(),
  48. error.line.c_str());
  49. }
  50. return description;
  51. }
  52. - (instancetype)initWithNativeDescription:
  53. (const webrtc::SessionDescriptionInterface *)nativeDescription {
  54. NSParameterAssert(nativeDescription);
  55. std::string sdp;
  56. nativeDescription->ToString(&sdp);
  57. RTCSdpType type = [[self class] typeForStdString:nativeDescription->type()];
  58. return [self initWithType:type
  59. sdp:[NSString stringForStdString:sdp]];
  60. }
  61. + (std::string)stdStringForType:(RTCSdpType)type {
  62. switch (type) {
  63. case RTCSdpTypeOffer:
  64. return webrtc::SessionDescriptionInterface::kOffer;
  65. case RTCSdpTypePrAnswer:
  66. return webrtc::SessionDescriptionInterface::kPrAnswer;
  67. case RTCSdpTypeAnswer:
  68. return webrtc::SessionDescriptionInterface::kAnswer;
  69. }
  70. }
  71. + (RTCSdpType)typeForStdString:(const std::string &)string {
  72. if (string == webrtc::SessionDescriptionInterface::kOffer) {
  73. return RTCSdpTypeOffer;
  74. } else if (string == webrtc::SessionDescriptionInterface::kPrAnswer) {
  75. return RTCSdpTypePrAnswer;
  76. } else if (string == webrtc::SessionDescriptionInterface::kAnswer) {
  77. return RTCSdpTypeAnswer;
  78. } else {
  79. RTC_NOTREACHED();
  80. return RTCSdpTypeOffer;
  81. }
  82. }
  83. @end