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.

176 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2004 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. #include "media/base/media_engine.h"
  11. #include <stddef.h>
  12. #include <cstdint>
  13. #include <string>
  14. #include <utility>
  15. #include "absl/algorithm/container.h"
  16. #include "api/video/video_bitrate_allocation.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/string_encode.h"
  19. namespace cricket {
  20. RtpCapabilities::RtpCapabilities() = default;
  21. RtpCapabilities::~RtpCapabilities() = default;
  22. webrtc::RtpParameters CreateRtpParametersWithOneEncoding() {
  23. webrtc::RtpParameters parameters;
  24. webrtc::RtpEncodingParameters encoding;
  25. parameters.encodings.push_back(encoding);
  26. return parameters;
  27. }
  28. webrtc::RtpParameters CreateRtpParametersWithEncodings(StreamParams sp) {
  29. std::vector<uint32_t> primary_ssrcs;
  30. sp.GetPrimarySsrcs(&primary_ssrcs);
  31. size_t encoding_count = primary_ssrcs.size();
  32. std::vector<webrtc::RtpEncodingParameters> encodings(encoding_count);
  33. for (size_t i = 0; i < encodings.size(); ++i) {
  34. encodings[i].ssrc = primary_ssrcs[i];
  35. }
  36. const std::vector<RidDescription>& rids = sp.rids();
  37. RTC_DCHECK(rids.size() == 0 || rids.size() == encoding_count);
  38. for (size_t i = 0; i < rids.size(); ++i) {
  39. encodings[i].rid = rids[i].rid;
  40. }
  41. webrtc::RtpParameters parameters;
  42. parameters.encodings = encodings;
  43. parameters.rtcp.cname = sp.cname;
  44. return parameters;
  45. }
  46. webrtc::RTCError CheckRtpParametersValues(
  47. const webrtc::RtpParameters& rtp_parameters) {
  48. using webrtc::RTCErrorType;
  49. for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) {
  50. if (rtp_parameters.encodings[i].bitrate_priority <= 0) {
  51. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE,
  52. "Attempted to set RtpParameters bitrate_priority to "
  53. "an invalid number. bitrate_priority must be > 0.");
  54. }
  55. if (rtp_parameters.encodings[i].scale_resolution_down_by &&
  56. *rtp_parameters.encodings[i].scale_resolution_down_by < 1.0) {
  57. LOG_AND_RETURN_ERROR(
  58. RTCErrorType::INVALID_RANGE,
  59. "Attempted to set RtpParameters scale_resolution_down_by to an "
  60. "invalid number. scale_resolution_down_by must be >= 1.0");
  61. }
  62. if (rtp_parameters.encodings[i].min_bitrate_bps &&
  63. rtp_parameters.encodings[i].max_bitrate_bps) {
  64. if (*rtp_parameters.encodings[i].max_bitrate_bps <
  65. *rtp_parameters.encodings[i].min_bitrate_bps) {
  66. LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_RANGE,
  67. "Attempted to set RtpParameters min bitrate "
  68. "larger than max bitrate.");
  69. }
  70. }
  71. if (rtp_parameters.encodings[i].num_temporal_layers) {
  72. if (*rtp_parameters.encodings[i].num_temporal_layers < 1 ||
  73. *rtp_parameters.encodings[i].num_temporal_layers >
  74. webrtc::kMaxTemporalStreams) {
  75. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE,
  76. "Attempted to set RtpParameters "
  77. "num_temporal_layers to an invalid number.");
  78. }
  79. }
  80. if (i > 0 && (rtp_parameters.encodings[i].num_temporal_layers !=
  81. rtp_parameters.encodings[i - 1].num_temporal_layers)) {
  82. LOG_AND_RETURN_ERROR(
  83. RTCErrorType::INVALID_MODIFICATION,
  84. "Attempted to set RtpParameters num_temporal_layers "
  85. "at encoding layer i: " +
  86. rtc::ToString(i) +
  87. " to a different value than other encoding layers.");
  88. }
  89. }
  90. return webrtc::RTCError::OK();
  91. }
  92. webrtc::RTCError CheckRtpParametersInvalidModificationAndValues(
  93. const webrtc::RtpParameters& old_rtp_parameters,
  94. const webrtc::RtpParameters& rtp_parameters) {
  95. using webrtc::RTCErrorType;
  96. if (rtp_parameters.encodings.size() != old_rtp_parameters.encodings.size()) {
  97. LOG_AND_RETURN_ERROR(
  98. RTCErrorType::INVALID_MODIFICATION,
  99. "Attempted to set RtpParameters with different encoding count");
  100. }
  101. if (rtp_parameters.rtcp != old_rtp_parameters.rtcp) {
  102. LOG_AND_RETURN_ERROR(
  103. RTCErrorType::INVALID_MODIFICATION,
  104. "Attempted to set RtpParameters with modified RTCP parameters");
  105. }
  106. if (rtp_parameters.header_extensions !=
  107. old_rtp_parameters.header_extensions) {
  108. LOG_AND_RETURN_ERROR(
  109. RTCErrorType::INVALID_MODIFICATION,
  110. "Attempted to set RtpParameters with modified header extensions");
  111. }
  112. if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings,
  113. [](const webrtc::RtpEncodingParameters& encoding1,
  114. const webrtc::RtpEncodingParameters& encoding2) {
  115. return encoding1.rid == encoding2.rid;
  116. })) {
  117. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
  118. "Attempted to change RID values in the encodings.");
  119. }
  120. if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings,
  121. [](const webrtc::RtpEncodingParameters& encoding1,
  122. const webrtc::RtpEncodingParameters& encoding2) {
  123. return encoding1.ssrc == encoding2.ssrc;
  124. })) {
  125. LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
  126. "Attempted to set RtpParameters with modified SSRC");
  127. }
  128. return CheckRtpParametersValues(rtp_parameters);
  129. }
  130. CompositeMediaEngine::CompositeMediaEngine(
  131. std::unique_ptr<VoiceEngineInterface> voice_engine,
  132. std::unique_ptr<VideoEngineInterface> video_engine)
  133. : voice_engine_(std::move(voice_engine)),
  134. video_engine_(std::move(video_engine)) {}
  135. CompositeMediaEngine::~CompositeMediaEngine() = default;
  136. bool CompositeMediaEngine::Init() {
  137. voice().Init();
  138. return true;
  139. }
  140. VoiceEngineInterface& CompositeMediaEngine::voice() {
  141. return *voice_engine_.get();
  142. }
  143. VideoEngineInterface& CompositeMediaEngine::video() {
  144. return *video_engine_.get();
  145. }
  146. const VoiceEngineInterface& CompositeMediaEngine::voice() const {
  147. return *voice_engine_.get();
  148. }
  149. const VideoEngineInterface& CompositeMediaEngine::video() const {
  150. return *video_engine_.get();
  151. }
  152. } // namespace cricket