track_media_info_map.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2016 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. #ifndef PC_TRACK_MEDIA_INFO_MAP_H_
  11. #define PC_TRACK_MEDIA_INFO_MAP_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "api/media_stream_interface.h"
  17. #include "media/base/media_channel.h"
  18. #include "pc/rtp_receiver.h"
  19. #include "pc/rtp_sender.h"
  20. #include "rtc_base/ref_count.h"
  21. namespace webrtc {
  22. // Audio/video tracks and sender/receiver statistical information are associated
  23. // with each other based on attachments to RTP senders/receivers. This class
  24. // maps that relationship, in both directions, so that stats about a track can
  25. // be retrieved on a per-attachment basis.
  26. //
  27. // An RTP sender/receiver sends or receives media for a set of SSRCs. The media
  28. // comes from an audio/video track that is attached to it.
  29. // |[Voice/Video][Sender/Receiver]Info| has statistical information for a set of
  30. // SSRCs. Looking at the RTP senders and receivers uncovers the track <-> info
  31. // relationships, which this class does.
  32. class TrackMediaInfoMap {
  33. public:
  34. TrackMediaInfoMap(
  35. std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,
  36. std::unique_ptr<cricket::VideoMediaInfo> video_media_info,
  37. const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders,
  38. const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>&
  39. rtp_receivers);
  40. const cricket::VoiceMediaInfo* voice_media_info() const {
  41. return voice_media_info_.get();
  42. }
  43. const cricket::VideoMediaInfo* video_media_info() const {
  44. return video_media_info_.get();
  45. }
  46. const std::vector<cricket::VoiceSenderInfo*>* GetVoiceSenderInfos(
  47. const AudioTrackInterface& local_audio_track) const;
  48. const cricket::VoiceReceiverInfo* GetVoiceReceiverInfo(
  49. const AudioTrackInterface& remote_audio_track) const;
  50. const std::vector<cricket::VideoSenderInfo*>* GetVideoSenderInfos(
  51. const VideoTrackInterface& local_video_track) const;
  52. const cricket::VideoReceiverInfo* GetVideoReceiverInfo(
  53. const VideoTrackInterface& remote_video_track) const;
  54. const cricket::VoiceSenderInfo* GetVoiceSenderInfoBySsrc(uint32_t ssrc) const;
  55. const cricket::VoiceReceiverInfo* GetVoiceReceiverInfoBySsrc(
  56. uint32_t ssrc) const;
  57. const cricket::VideoSenderInfo* GetVideoSenderInfoBySsrc(uint32_t ssrc) const;
  58. const cricket::VideoReceiverInfo* GetVideoReceiverInfoBySsrc(
  59. uint32_t ssrc) const;
  60. rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
  61. const cricket::VoiceSenderInfo& voice_sender_info) const;
  62. rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
  63. const cricket::VoiceReceiverInfo& voice_receiver_info) const;
  64. rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
  65. const cricket::VideoSenderInfo& video_sender_info) const;
  66. rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
  67. const cricket::VideoReceiverInfo& video_receiver_info) const;
  68. // TODO(hta): Remove this function, and redesign the callers not to need it.
  69. // It is not going to work if a track is attached multiple times, and
  70. // it is not going to work if a received track is attached as a sending
  71. // track (loopback).
  72. absl::optional<int> GetAttachmentIdByTrack(
  73. const MediaStreamTrackInterface* track) const;
  74. private:
  75. absl::optional<std::string> voice_mid_;
  76. absl::optional<std::string> video_mid_;
  77. std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info_;
  78. std::unique_ptr<cricket::VideoMediaInfo> video_media_info_;
  79. // These maps map tracks (identified by a pointer) to their corresponding info
  80. // object of the correct kind. One track can map to multiple info objects.
  81. std::map<const AudioTrackInterface*, std::vector<cricket::VoiceSenderInfo*>>
  82. voice_infos_by_local_track_;
  83. std::map<const AudioTrackInterface*, cricket::VoiceReceiverInfo*>
  84. voice_info_by_remote_track_;
  85. std::map<const VideoTrackInterface*, std::vector<cricket::VideoSenderInfo*>>
  86. video_infos_by_local_track_;
  87. std::map<const VideoTrackInterface*, cricket::VideoReceiverInfo*>
  88. video_info_by_remote_track_;
  89. // These maps map info objects to their corresponding tracks. They are always
  90. // the inverse of the maps above. One info object always maps to only one
  91. // track.
  92. std::map<const cricket::VoiceSenderInfo*,
  93. rtc::scoped_refptr<AudioTrackInterface>>
  94. audio_track_by_sender_info_;
  95. std::map<const cricket::VoiceReceiverInfo*,
  96. rtc::scoped_refptr<AudioTrackInterface>>
  97. audio_track_by_receiver_info_;
  98. std::map<const cricket::VideoSenderInfo*,
  99. rtc::scoped_refptr<VideoTrackInterface>>
  100. video_track_by_sender_info_;
  101. std::map<const cricket::VideoReceiverInfo*,
  102. rtc::scoped_refptr<VideoTrackInterface>>
  103. video_track_by_receiver_info_;
  104. // Map of tracks to attachment IDs.
  105. // Necessary because senders and receivers live on the signaling thread,
  106. // but the attachment IDs are needed while building stats on the networking
  107. // thread, so we can't look them up in the senders/receivers without
  108. // thread jumping.
  109. std::map<const MediaStreamTrackInterface*, int> attachment_id_by_track_;
  110. // These maps map SSRCs to the corresponding voice or video info objects.
  111. std::map<uint32_t, cricket::VoiceSenderInfo*> voice_info_by_sender_ssrc_;
  112. std::map<uint32_t, cricket::VoiceReceiverInfo*> voice_info_by_receiver_ssrc_;
  113. std::map<uint32_t, cricket::VideoSenderInfo*> video_info_by_sender_ssrc_;
  114. std::map<uint32_t, cricket::VideoReceiverInfo*> video_info_by_receiver_ssrc_;
  115. };
  116. } // namespace webrtc
  117. #endif // PC_TRACK_MEDIA_INFO_MAP_H_