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.

236 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2011 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/stream_params.h"
  11. #include <stdint.h>
  12. #include <list>
  13. #include "absl/algorithm/container.h"
  14. #include "api/array_view.h"
  15. #include "rtc_base/strings/string_builder.h"
  16. namespace cricket {
  17. namespace {
  18. void AppendSsrcs(rtc::ArrayView<const uint32_t> ssrcs,
  19. rtc::SimpleStringBuilder* sb) {
  20. *sb << "ssrcs:[";
  21. const char* delimiter = "";
  22. for (uint32_t ssrc : ssrcs) {
  23. *sb << delimiter << ssrc;
  24. delimiter = ",";
  25. }
  26. *sb << "]";
  27. }
  28. void AppendSsrcGroups(rtc::ArrayView<const SsrcGroup> ssrc_groups,
  29. rtc::SimpleStringBuilder* sb) {
  30. *sb << "ssrc_groups:";
  31. const char* delimiter = "";
  32. for (const SsrcGroup& ssrc_group : ssrc_groups) {
  33. *sb << delimiter << ssrc_group.ToString();
  34. delimiter = ",";
  35. }
  36. }
  37. void AppendStreamIds(rtc::ArrayView<const std::string> stream_ids,
  38. rtc::SimpleStringBuilder* sb) {
  39. *sb << "stream_ids:";
  40. const char* delimiter = "";
  41. for (const std::string& stream_id : stream_ids) {
  42. *sb << delimiter << stream_id;
  43. delimiter = ",";
  44. }
  45. }
  46. void AppendRids(rtc::ArrayView<const RidDescription> rids,
  47. rtc::SimpleStringBuilder* sb) {
  48. *sb << "rids:[";
  49. const char* delimiter = "";
  50. for (const RidDescription& rid : rids) {
  51. *sb << delimiter << rid.rid;
  52. delimiter = ",";
  53. }
  54. *sb << "]";
  55. }
  56. } // namespace
  57. const char kFecSsrcGroupSemantics[] = "FEC";
  58. const char kFecFrSsrcGroupSemantics[] = "FEC-FR";
  59. const char kFidSsrcGroupSemantics[] = "FID";
  60. const char kSimSsrcGroupSemantics[] = "SIM";
  61. bool GetStream(const StreamParamsVec& streams,
  62. const StreamSelector& selector,
  63. StreamParams* stream_out) {
  64. const StreamParams* found = GetStream(streams, selector);
  65. if (found && stream_out)
  66. *stream_out = *found;
  67. return found != nullptr;
  68. }
  69. SsrcGroup::SsrcGroup(const std::string& usage,
  70. const std::vector<uint32_t>& ssrcs)
  71. : semantics(usage), ssrcs(ssrcs) {}
  72. SsrcGroup::SsrcGroup(const SsrcGroup&) = default;
  73. SsrcGroup::SsrcGroup(SsrcGroup&&) = default;
  74. SsrcGroup::~SsrcGroup() = default;
  75. SsrcGroup& SsrcGroup::operator=(const SsrcGroup&) = default;
  76. SsrcGroup& SsrcGroup::operator=(SsrcGroup&&) = default;
  77. bool SsrcGroup::has_semantics(const std::string& semantics_in) const {
  78. return (semantics == semantics_in && ssrcs.size() > 0);
  79. }
  80. std::string SsrcGroup::ToString() const {
  81. char buf[1024];
  82. rtc::SimpleStringBuilder sb(buf);
  83. sb << "{";
  84. sb << "semantics:" << semantics << ";";
  85. AppendSsrcs(ssrcs, &sb);
  86. sb << "}";
  87. return sb.str();
  88. }
  89. StreamParams::StreamParams() = default;
  90. StreamParams::StreamParams(const StreamParams&) = default;
  91. StreamParams::StreamParams(StreamParams&&) = default;
  92. StreamParams::~StreamParams() = default;
  93. StreamParams& StreamParams::operator=(const StreamParams&) = default;
  94. StreamParams& StreamParams::operator=(StreamParams&&) = default;
  95. bool StreamParams::operator==(const StreamParams& other) const {
  96. return (groupid == other.groupid && id == other.id && ssrcs == other.ssrcs &&
  97. ssrc_groups == other.ssrc_groups && cname == other.cname &&
  98. stream_ids_ == other.stream_ids_ &&
  99. // RIDs are not required to be in the same order for equality.
  100. absl::c_is_permutation(rids_, other.rids_));
  101. }
  102. std::string StreamParams::ToString() const {
  103. char buf[2 * 1024];
  104. rtc::SimpleStringBuilder sb(buf);
  105. sb << "{";
  106. if (!groupid.empty()) {
  107. sb << "groupid:" << groupid << ";";
  108. }
  109. if (!id.empty()) {
  110. sb << "id:" << id << ";";
  111. }
  112. AppendSsrcs(ssrcs, &sb);
  113. sb << ";";
  114. AppendSsrcGroups(ssrc_groups, &sb);
  115. sb << ";";
  116. if (!cname.empty()) {
  117. sb << "cname:" << cname << ";";
  118. }
  119. AppendStreamIds(stream_ids_, &sb);
  120. sb << ";";
  121. if (!rids_.empty()) {
  122. AppendRids(rids_, &sb);
  123. sb << ";";
  124. }
  125. sb << "}";
  126. return sb.str();
  127. }
  128. void StreamParams::GenerateSsrcs(int num_layers,
  129. bool generate_fid,
  130. bool generate_fec_fr,
  131. rtc::UniqueRandomIdGenerator* ssrc_generator) {
  132. RTC_DCHECK_GE(num_layers, 0);
  133. RTC_DCHECK(ssrc_generator);
  134. std::vector<uint32_t> primary_ssrcs;
  135. for (int i = 0; i < num_layers; ++i) {
  136. uint32_t ssrc = ssrc_generator->GenerateId();
  137. primary_ssrcs.push_back(ssrc);
  138. add_ssrc(ssrc);
  139. }
  140. if (num_layers > 1) {
  141. SsrcGroup simulcast(kSimSsrcGroupSemantics, primary_ssrcs);
  142. ssrc_groups.push_back(simulcast);
  143. }
  144. if (generate_fid) {
  145. for (uint32_t ssrc : primary_ssrcs) {
  146. AddFidSsrc(ssrc, ssrc_generator->GenerateId());
  147. }
  148. }
  149. if (generate_fec_fr) {
  150. for (uint32_t ssrc : primary_ssrcs) {
  151. AddFecFrSsrc(ssrc, ssrc_generator->GenerateId());
  152. }
  153. }
  154. }
  155. void StreamParams::GetPrimarySsrcs(std::vector<uint32_t>* ssrcs) const {
  156. const SsrcGroup* sim_group = get_ssrc_group(kSimSsrcGroupSemantics);
  157. if (sim_group == NULL) {
  158. ssrcs->push_back(first_ssrc());
  159. } else {
  160. ssrcs->insert(ssrcs->end(), sim_group->ssrcs.begin(),
  161. sim_group->ssrcs.end());
  162. }
  163. }
  164. void StreamParams::GetFidSsrcs(const std::vector<uint32_t>& primary_ssrcs,
  165. std::vector<uint32_t>* fid_ssrcs) const {
  166. for (uint32_t primary_ssrc : primary_ssrcs) {
  167. uint32_t fid_ssrc;
  168. if (GetFidSsrc(primary_ssrc, &fid_ssrc)) {
  169. fid_ssrcs->push_back(fid_ssrc);
  170. }
  171. }
  172. }
  173. bool StreamParams::AddSecondarySsrc(const std::string& semantics,
  174. uint32_t primary_ssrc,
  175. uint32_t secondary_ssrc) {
  176. if (!has_ssrc(primary_ssrc)) {
  177. return false;
  178. }
  179. ssrcs.push_back(secondary_ssrc);
  180. ssrc_groups.push_back(SsrcGroup(semantics, {primary_ssrc, secondary_ssrc}));
  181. return true;
  182. }
  183. bool StreamParams::GetSecondarySsrc(const std::string& semantics,
  184. uint32_t primary_ssrc,
  185. uint32_t* secondary_ssrc) const {
  186. for (const SsrcGroup& ssrc_group : ssrc_groups) {
  187. if (ssrc_group.has_semantics(semantics) && ssrc_group.ssrcs.size() >= 2 &&
  188. ssrc_group.ssrcs[0] == primary_ssrc) {
  189. *secondary_ssrc = ssrc_group.ssrcs[1];
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. std::vector<std::string> StreamParams::stream_ids() const {
  196. return stream_ids_;
  197. }
  198. void StreamParams::set_stream_ids(const std::vector<std::string>& stream_ids) {
  199. stream_ids_ = stream_ids;
  200. }
  201. std::string StreamParams::first_stream_id() const {
  202. return stream_ids_.empty() ? "" : stream_ids_[0];
  203. }
  204. } // namespace cricket