xref: /aosp_15_r20/external/webrtc/pc/track_media_info_map.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef PC_TRACK_MEDIA_INFO_MAP_H_
12 #define PC_TRACK_MEDIA_INFO_MAP_H_
13 
14 #include <stdint.h>
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "absl/types/optional.h"
22 #include "api/array_view.h"
23 #include "api/media_stream_interface.h"
24 #include "api/scoped_refptr.h"
25 #include "media/base/media_channel.h"
26 #include "pc/rtp_receiver.h"
27 #include "pc/rtp_sender.h"
28 #include "rtc_base/ref_count.h"
29 
30 namespace webrtc {
31 
32 // Audio/video tracks and sender/receiver statistical information are associated
33 // with each other based on attachments to RTP senders/receivers. This class
34 // maps that relationship, in both directions, so that stats about a track can
35 // be retrieved on a per-attachment basis.
36 //
37 // An RTP sender/receiver sends or receives media for a set of SSRCs. The media
38 // comes from an audio/video track that is attached to it.
39 // |[Voice/Video][Sender/Receiver]Info| has statistical information for a set of
40 // SSRCs. Looking at the RTP senders and receivers uncovers the track <-> info
41 // relationships, which this class does.
42 //
43 // In the spec, "track" attachment stats have been made obsolete, and in Unified
44 // Plan there is just one sender and one receiver per transceiver, so we may be
45 // able to simplify/delete this class.
46 // TODO(https://crbug.com/webrtc/14175): Simplify or delete this class when
47 // "track" stats have been deleted.
48 // TODO(https://crbug.com/webrtc/13528): Simplify or delete this class when
49 // Plan B is gone from the native library (already gone for Chrome).
50 class TrackMediaInfoMap {
51  public:
52   TrackMediaInfoMap();
53 
54   // Takes ownership of the "infos". Does not affect the lifetime of the senders
55   // or receivers, but TrackMediaInfoMap will keep their associated tracks alive
56   // through reference counting until the map is destroyed.
57   void Initialize(
58       absl::optional<cricket::VoiceMediaInfo> voice_media_info,
59       absl::optional<cricket::VideoMediaInfo> video_media_info,
60       rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders,
61       rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers);
62 
voice_media_info()63   const absl::optional<cricket::VoiceMediaInfo>& voice_media_info() const {
64     RTC_DCHECK(is_initialized_);
65     return voice_media_info_;
66   }
video_media_info()67   const absl::optional<cricket::VideoMediaInfo>& video_media_info() const {
68     RTC_DCHECK(is_initialized_);
69     return video_media_info_;
70   }
71 
72   const std::vector<cricket::VoiceSenderInfo*>* GetVoiceSenderInfos(
73       const AudioTrackInterface& local_audio_track) const;
74   const cricket::VoiceReceiverInfo* GetVoiceReceiverInfo(
75       const AudioTrackInterface& remote_audio_track) const;
76   const std::vector<cricket::VideoSenderInfo*>* GetVideoSenderInfos(
77       const VideoTrackInterface& local_video_track) const;
78   const cricket::VideoReceiverInfo* GetVideoReceiverInfo(
79       const VideoTrackInterface& remote_video_track) const;
80 
81   const cricket::VoiceSenderInfo* GetVoiceSenderInfoBySsrc(uint32_t ssrc) const;
82   const cricket::VoiceReceiverInfo* GetVoiceReceiverInfoBySsrc(
83       uint32_t ssrc) const;
84   const cricket::VideoSenderInfo* GetVideoSenderInfoBySsrc(uint32_t ssrc) const;
85   const cricket::VideoReceiverInfo* GetVideoReceiverInfoBySsrc(
86       uint32_t ssrc) const;
87 
88   rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
89       const cricket::VoiceSenderInfo& voice_sender_info) const;
90   rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
91       const cricket::VoiceReceiverInfo& voice_receiver_info) const;
92   rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
93       const cricket::VideoSenderInfo& video_sender_info) const;
94   rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
95       const cricket::VideoReceiverInfo& video_receiver_info) const;
96 
97   // TODO(hta): Remove this function, and redesign the callers not to need it.
98   // It is not going to work if a track is attached multiple times, and
99   // it is not going to work if a received track is attached as a sending
100   // track (loopback).
101   absl::optional<int> GetAttachmentIdByTrack(
102       const MediaStreamTrackInterface* track) const;
103 
104  private:
105   bool is_initialized_ = false;
106   absl::optional<cricket::VoiceMediaInfo> voice_media_info_;
107   absl::optional<cricket::VideoMediaInfo> video_media_info_;
108   // These maps map tracks (identified by a pointer) to their corresponding info
109   // object of the correct kind. One track can map to multiple info objects.
110   // Known tracks are guaranteed to be alive because they are also stored as
111   // entries in the reverse maps below.
112   std::map<const AudioTrackInterface*, std::vector<cricket::VoiceSenderInfo*>>
113       voice_infos_by_local_track_;
114   std::map<const AudioTrackInterface*, cricket::VoiceReceiverInfo*>
115       voice_info_by_remote_track_;
116   std::map<const VideoTrackInterface*, std::vector<cricket::VideoSenderInfo*>>
117       video_infos_by_local_track_;
118   std::map<const VideoTrackInterface*, cricket::VideoReceiverInfo*>
119       video_info_by_remote_track_;
120   // These maps map info objects to their corresponding tracks. They are always
121   // the inverse of the maps above. One info object always maps to only one
122   // track. The use of scoped_refptr<> here ensures the tracks outlive
123   // TrackMediaInfoMap.
124   std::map<const cricket::VoiceSenderInfo*,
125            rtc::scoped_refptr<AudioTrackInterface>>
126       audio_track_by_sender_info_;
127   std::map<const cricket::VoiceReceiverInfo*,
128            rtc::scoped_refptr<AudioTrackInterface>>
129       audio_track_by_receiver_info_;
130   std::map<const cricket::VideoSenderInfo*,
131            rtc::scoped_refptr<VideoTrackInterface>>
132       video_track_by_sender_info_;
133   std::map<const cricket::VideoReceiverInfo*,
134            rtc::scoped_refptr<VideoTrackInterface>>
135       video_track_by_receiver_info_;
136   // Map of tracks to attachment IDs.
137   // Necessary because senders and receivers live on the signaling thread,
138   // but the attachment IDs are needed while building stats on the networking
139   // thread, so we can't look them up in the senders/receivers without
140   // thread jumping.
141   std::map<const MediaStreamTrackInterface*, int> attachment_id_by_track_;
142   // These maps map SSRCs to the corresponding voice or video info objects.
143   std::map<uint32_t, cricket::VoiceSenderInfo*> voice_info_by_sender_ssrc_;
144   std::map<uint32_t, cricket::VoiceReceiverInfo*> voice_info_by_receiver_ssrc_;
145   std::map<uint32_t, cricket::VideoSenderInfo*> video_info_by_sender_ssrc_;
146   std::map<uint32_t, cricket::VideoReceiverInfo*> video_info_by_receiver_ssrc_;
147 };
148 
149 }  // namespace webrtc
150 
151 #endif  // PC_TRACK_MEDIA_INFO_MAP_H_
152