音视频编解码器的ID列表image.png

Codec ID,就是PayloadType类型。
因为每种CodecID必须是只对应视频或者音频的编解码类型。如果PayloadType类型一样,则无法区分是视频还是音频,所以音频和视频不能同时使用102,103,104 其中一个Codec ID 。

设置编解码器ID

image.png

MergeCodecs

h:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\pc\media_session.cc

  1. // Adds all codecs from |reference_codecs| to |offered_codecs| that don't
  2. // already exist in |offered_codecs| and ensure the payload types don't
  3. // collide.
  4. template <class C>
  5. static void MergeCodecs(const std::vector<C>& reference_codecs,
  6. std::vector<C>* offered_codecs,
  7. UsedPayloadTypes* used_pltypes) {
  8. // Add all new codecs that are not RTX codecs.
  9. for (const C& reference_codec : reference_codecs) {
  10. if (!IsRtxCodec(reference_codec) &&
  11. !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  12. reference_codec, nullptr)) {
  13. C codec = reference_codec;
  14. used_pltypes->FindAndSetIdUsed(&codec);
  15. offered_codecs->push_back(codec);
  16. }
  17. }
  18. // Add all new RTX codecs.
  19. for (const C& reference_codec : reference_codecs) {
  20. if (IsRtxCodec(reference_codec) &&
  21. !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  22. reference_codec, nullptr)) {
  23. C rtx_codec = reference_codec;
  24. const C* associated_codec =
  25. GetAssociatedCodec(reference_codecs, rtx_codec);
  26. if (!associated_codec) {
  27. continue;
  28. }
  29. // Find a codec in the offered list that matches the reference codec.
  30. // Its payload type may be different than the reference codec.
  31. C matching_codec;
  32. if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  33. *associated_codec, &matching_codec)) {
  34. RTC_LOG(LS_WARNING)
  35. << "Couldn't find matching " << associated_codec->name << " codec.";
  36. continue;
  37. }
  38. rtx_codec.params[kCodecParamAssociatedPayloadType] =
  39. rtc::ToString(matching_codec.id);
  40. used_pltypes->FindAndSetIdUsed(&rtx_codec);
  41. offered_codecs->push_back(rtx_codec);
  42. }
  43. }
  44. }

FindAndSetIdUsed

H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\pc\used_ids.h

  1. // Loops through all Id in |ids| and changes its id if it is
  2. // already in use by another IdStruct. Call this methods with all Id
  3. // in a session description to make sure no duplicate ids exists.
  4. // Note that typename Id must be a type of IdStruct.
  5. template <typename Id>
  6. void FindAndSetIdUsed(std::vector<Id>* ids) {
  7. for (const Id& id : *ids) {
  8. FindAndSetIdUsed(&id);
  9. }
  10. }
  11. // Finds and sets an unused id if the |idstruct| id is already in use.
  12. void FindAndSetIdUsed(IdStruct* idstruct) {
  13. const int original_id = idstruct->id;
  14. int new_id = idstruct->id;
  15. if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
  16. // If the original id is not in range - this is an id that can't be
  17. // dynamically changed.
  18. return;
  19. }
  20. if (IsIdUsed(original_id)) {
  21. new_id = FindUnusedId();
  22. RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
  23. << original_id << " to " << new_id;
  24. idstruct->id = new_id;
  25. }
  26. SetIdUsed(new_id);
  27. }

编解码器ID范围

image.png

编解码器获取新的ID算法

image.png

结论

image.png
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\pc\session_description.h
SessionDescription