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 #include "pc/track_media_info_map.h"
12
13 #include <cstdint>
14 #include <set>
15 #include <type_traits>
16 #include <utility>
17
18 #include "api/media_types.h"
19 #include "api/rtp_parameters.h"
20 #include "media/base/stream_params.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/thread.h"
23
24 namespace webrtc {
25
26 namespace {
27
28 template <typename K, typename V>
FindValueOrNull(const std::map<K,V> & map,const K & key)29 V FindValueOrNull(const std::map<K, V>& map, const K& key) {
30 auto it = map.find(key);
31 return (it != map.end()) ? it->second : nullptr;
32 }
33
34 template <typename K, typename V>
FindAddressOrNull(const std::map<K,V> & map,const K & key)35 const V* FindAddressOrNull(const std::map<K, V>& map, const K& key) {
36 auto it = map.find(key);
37 return (it != map.end()) ? &it->second : nullptr;
38 }
39
GetAudioAndVideoTrackBySsrc(rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders,rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers,std::map<uint32_t,AudioTrackInterface * > * local_audio_track_by_ssrc,std::map<uint32_t,VideoTrackInterface * > * local_video_track_by_ssrc,std::map<uint32_t,AudioTrackInterface * > * remote_audio_track_by_ssrc,std::map<uint32_t,VideoTrackInterface * > * remote_video_track_by_ssrc,AudioTrackInterface ** unsignaled_audio_track,VideoTrackInterface ** unsignaled_video_track)40 void GetAudioAndVideoTrackBySsrc(
41 rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders,
42 rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers,
43 std::map<uint32_t, AudioTrackInterface*>* local_audio_track_by_ssrc,
44 std::map<uint32_t, VideoTrackInterface*>* local_video_track_by_ssrc,
45 std::map<uint32_t, AudioTrackInterface*>* remote_audio_track_by_ssrc,
46 std::map<uint32_t, VideoTrackInterface*>* remote_video_track_by_ssrc,
47 AudioTrackInterface** unsignaled_audio_track,
48 VideoTrackInterface** unsignaled_video_track) {
49 RTC_DCHECK(local_audio_track_by_ssrc->empty());
50 RTC_DCHECK(local_video_track_by_ssrc->empty());
51 RTC_DCHECK(remote_audio_track_by_ssrc->empty());
52 RTC_DCHECK(remote_video_track_by_ssrc->empty());
53 for (const auto& rtp_sender : rtp_senders) {
54 cricket::MediaType media_type = rtp_sender->media_type();
55 MediaStreamTrackInterface* track = rtp_sender->track().get();
56 if (!track) {
57 continue;
58 }
59 // TODO(deadbeef): `ssrc` should be removed in favor of `GetParameters`.
60 uint32_t ssrc = rtp_sender->ssrc();
61 if (ssrc != 0) {
62 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
63 RTC_DCHECK(local_audio_track_by_ssrc->find(ssrc) ==
64 local_audio_track_by_ssrc->end());
65 (*local_audio_track_by_ssrc)[ssrc] =
66 static_cast<AudioTrackInterface*>(track);
67 } else {
68 RTC_DCHECK(local_video_track_by_ssrc->find(ssrc) ==
69 local_video_track_by_ssrc->end());
70 (*local_video_track_by_ssrc)[ssrc] =
71 static_cast<VideoTrackInterface*>(track);
72 }
73 }
74 }
75 for (const auto& rtp_receiver : rtp_receivers) {
76 cricket::MediaType media_type = rtp_receiver->media_type();
77 MediaStreamTrackInterface* track = rtp_receiver->track().get();
78 RTC_DCHECK(track);
79 RtpParameters params = rtp_receiver->GetParameters();
80 for (const RtpEncodingParameters& encoding : params.encodings) {
81 if (!encoding.ssrc) {
82 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
83 *unsignaled_audio_track = static_cast<AudioTrackInterface*>(track);
84 } else {
85 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_VIDEO);
86 *unsignaled_video_track = static_cast<VideoTrackInterface*>(track);
87 }
88 continue;
89 }
90 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
91 RTC_DCHECK(remote_audio_track_by_ssrc->find(*encoding.ssrc) ==
92 remote_audio_track_by_ssrc->end());
93 (*remote_audio_track_by_ssrc)[*encoding.ssrc] =
94 static_cast<AudioTrackInterface*>(track);
95 } else {
96 RTC_DCHECK(remote_video_track_by_ssrc->find(*encoding.ssrc) ==
97 remote_video_track_by_ssrc->end());
98 (*remote_video_track_by_ssrc)[*encoding.ssrc] =
99 static_cast<VideoTrackInterface*>(track);
100 }
101 }
102 }
103 }
104
105 } // namespace
106
107 TrackMediaInfoMap::TrackMediaInfoMap() = default;
108
Initialize(absl::optional<cricket::VoiceMediaInfo> voice_media_info,absl::optional<cricket::VideoMediaInfo> video_media_info,rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders,rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers)109 void TrackMediaInfoMap::Initialize(
110 absl::optional<cricket::VoiceMediaInfo> voice_media_info,
111 absl::optional<cricket::VideoMediaInfo> video_media_info,
112 rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders,
113 rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers) {
114 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
115 RTC_DCHECK(!is_initialized_);
116 is_initialized_ = true;
117 voice_media_info_ = std::move(voice_media_info);
118 video_media_info_ = std::move(video_media_info);
119
120 std::map<uint32_t, AudioTrackInterface*> local_audio_track_by_ssrc;
121 std::map<uint32_t, VideoTrackInterface*> local_video_track_by_ssrc;
122 std::map<uint32_t, AudioTrackInterface*> remote_audio_track_by_ssrc;
123 std::map<uint32_t, VideoTrackInterface*> remote_video_track_by_ssrc;
124 AudioTrackInterface* unsignaled_audio_track = nullptr;
125 VideoTrackInterface* unsignaled_video_track = nullptr;
126 GetAudioAndVideoTrackBySsrc(
127 rtp_senders, rtp_receivers, &local_audio_track_by_ssrc,
128 &local_video_track_by_ssrc, &remote_audio_track_by_ssrc,
129 &remote_video_track_by_ssrc, &unsignaled_audio_track,
130 &unsignaled_video_track);
131
132 for (const auto& sender : rtp_senders) {
133 attachment_id_by_track_[sender->track().get()] = sender->AttachmentId();
134 }
135 for (const auto& receiver : rtp_receivers) {
136 attachment_id_by_track_[receiver->track().get()] = receiver->AttachmentId();
137 }
138
139 if (voice_media_info_.has_value()) {
140 for (auto& sender_info : voice_media_info_->senders) {
141 AudioTrackInterface* associated_track =
142 FindValueOrNull(local_audio_track_by_ssrc, sender_info.ssrc());
143 if (associated_track) {
144 // One sender is associated with at most one track.
145 // One track may be associated with multiple senders.
146 audio_track_by_sender_info_[&sender_info] = associated_track;
147 voice_infos_by_local_track_[associated_track].push_back(&sender_info);
148 }
149 if (sender_info.ssrc() == 0)
150 continue; // Unconnected SSRC. bugs.webrtc.org/8673
151 RTC_CHECK(voice_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0)
152 << "Duplicate voice sender SSRC: " << sender_info.ssrc();
153 voice_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info;
154 }
155 for (auto& receiver_info : voice_media_info_->receivers) {
156 AudioTrackInterface* associated_track =
157 FindValueOrNull(remote_audio_track_by_ssrc, receiver_info.ssrc());
158 if (associated_track) {
159 // One receiver is associated with at most one track, which is uniquely
160 // associated with that receiver.
161 audio_track_by_receiver_info_[&receiver_info] = associated_track;
162 RTC_DCHECK(voice_info_by_remote_track_.find(associated_track) ==
163 voice_info_by_remote_track_.end());
164 voice_info_by_remote_track_[associated_track] = &receiver_info;
165 } else if (unsignaled_audio_track) {
166 audio_track_by_receiver_info_[&receiver_info] = unsignaled_audio_track;
167 voice_info_by_remote_track_[unsignaled_audio_track] = &receiver_info;
168 }
169 RTC_CHECK(voice_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0)
170 << "Duplicate voice receiver SSRC: " << receiver_info.ssrc();
171 voice_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info;
172 }
173 }
174 if (video_media_info_.has_value()) {
175 for (auto& sender_info : video_media_info_->senders) {
176 std::set<uint32_t> ssrcs;
177 ssrcs.insert(sender_info.ssrc());
178 for (auto& ssrc_group : sender_info.ssrc_groups) {
179 for (auto ssrc : ssrc_group.ssrcs) {
180 ssrcs.insert(ssrc);
181 }
182 }
183 for (auto ssrc : ssrcs) {
184 VideoTrackInterface* associated_track =
185 FindValueOrNull(local_video_track_by_ssrc, ssrc);
186 if (associated_track) {
187 // One sender is associated with at most one track.
188 // One track may be associated with multiple senders.
189 video_track_by_sender_info_[&sender_info] = associated_track;
190 video_infos_by_local_track_[associated_track].push_back(&sender_info);
191 break;
192 }
193 }
194 }
195 for (auto& sender_info : video_media_info_->aggregated_senders) {
196 if (sender_info.ssrc() == 0)
197 continue; // Unconnected SSRC. bugs.webrtc.org/8673
198 RTC_DCHECK(video_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0)
199 << "Duplicate video sender SSRC: " << sender_info.ssrc();
200 video_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info;
201 VideoTrackInterface* associated_track =
202 FindValueOrNull(local_video_track_by_ssrc, sender_info.ssrc());
203 if (associated_track) {
204 video_track_by_sender_info_[&sender_info] = associated_track;
205 }
206 }
207 for (auto& receiver_info : video_media_info_->receivers) {
208 VideoTrackInterface* associated_track =
209 FindValueOrNull(remote_video_track_by_ssrc, receiver_info.ssrc());
210 if (associated_track) {
211 // One receiver is associated with at most one track, which is uniquely
212 // associated with that receiver.
213 video_track_by_receiver_info_[&receiver_info] = associated_track;
214 RTC_DCHECK(video_info_by_remote_track_.find(associated_track) ==
215 video_info_by_remote_track_.end());
216 video_info_by_remote_track_[associated_track] = &receiver_info;
217 } else if (unsignaled_video_track) {
218 video_track_by_receiver_info_[&receiver_info] = unsignaled_video_track;
219 video_info_by_remote_track_[unsignaled_video_track] = &receiver_info;
220 }
221 RTC_DCHECK(video_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0)
222 << "Duplicate video receiver SSRC: " << receiver_info.ssrc();
223 video_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info;
224 }
225 }
226 }
227
228 const std::vector<cricket::VoiceSenderInfo*>*
GetVoiceSenderInfos(const AudioTrackInterface & local_audio_track) const229 TrackMediaInfoMap::GetVoiceSenderInfos(
230 const AudioTrackInterface& local_audio_track) const {
231 RTC_DCHECK(is_initialized_);
232 return FindAddressOrNull(voice_infos_by_local_track_, &local_audio_track);
233 }
234
GetVoiceReceiverInfo(const AudioTrackInterface & remote_audio_track) const235 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo(
236 const AudioTrackInterface& remote_audio_track) const {
237 RTC_DCHECK(is_initialized_);
238 return FindValueOrNull(voice_info_by_remote_track_, &remote_audio_track);
239 }
240
241 const std::vector<cricket::VideoSenderInfo*>*
GetVideoSenderInfos(const VideoTrackInterface & local_video_track) const242 TrackMediaInfoMap::GetVideoSenderInfos(
243 const VideoTrackInterface& local_video_track) const {
244 RTC_DCHECK(is_initialized_);
245 return FindAddressOrNull(video_infos_by_local_track_, &local_video_track);
246 }
247
GetVideoReceiverInfo(const VideoTrackInterface & remote_video_track) const248 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo(
249 const VideoTrackInterface& remote_video_track) const {
250 RTC_DCHECK(is_initialized_);
251 return FindValueOrNull(video_info_by_remote_track_, &remote_video_track);
252 }
253
GetVoiceSenderInfoBySsrc(uint32_t ssrc) const254 const cricket::VoiceSenderInfo* TrackMediaInfoMap::GetVoiceSenderInfoBySsrc(
255 uint32_t ssrc) const {
256 RTC_DCHECK(is_initialized_);
257 return FindValueOrNull(voice_info_by_sender_ssrc_, ssrc);
258 }
GetVoiceReceiverInfoBySsrc(uint32_t ssrc) const259 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfoBySsrc(
260 uint32_t ssrc) const {
261 RTC_DCHECK(is_initialized_);
262 return FindValueOrNull(voice_info_by_receiver_ssrc_, ssrc);
263 }
264
GetVideoSenderInfoBySsrc(uint32_t ssrc) const265 const cricket::VideoSenderInfo* TrackMediaInfoMap::GetVideoSenderInfoBySsrc(
266 uint32_t ssrc) const {
267 RTC_DCHECK(is_initialized_);
268 return FindValueOrNull(video_info_by_sender_ssrc_, ssrc);
269 }
270
GetVideoReceiverInfoBySsrc(uint32_t ssrc) const271 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfoBySsrc(
272 uint32_t ssrc) const {
273 RTC_DCHECK(is_initialized_);
274 return FindValueOrNull(video_info_by_receiver_ssrc_, ssrc);
275 }
276
GetAudioTrack(const cricket::VoiceSenderInfo & voice_sender_info) const277 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
278 const cricket::VoiceSenderInfo& voice_sender_info) const {
279 RTC_DCHECK(is_initialized_);
280 return FindValueOrNull(audio_track_by_sender_info_, &voice_sender_info);
281 }
282
GetAudioTrack(const cricket::VoiceReceiverInfo & voice_receiver_info) const283 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
284 const cricket::VoiceReceiverInfo& voice_receiver_info) const {
285 RTC_DCHECK(is_initialized_);
286 return FindValueOrNull(audio_track_by_receiver_info_, &voice_receiver_info);
287 }
288
GetVideoTrack(const cricket::VideoSenderInfo & video_sender_info) const289 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
290 const cricket::VideoSenderInfo& video_sender_info) const {
291 RTC_DCHECK(is_initialized_);
292 return FindValueOrNull(video_track_by_sender_info_, &video_sender_info);
293 }
294
GetVideoTrack(const cricket::VideoReceiverInfo & video_receiver_info) const295 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
296 const cricket::VideoReceiverInfo& video_receiver_info) const {
297 RTC_DCHECK(is_initialized_);
298 return FindValueOrNull(video_track_by_receiver_info_, &video_receiver_info);
299 }
300
GetAttachmentIdByTrack(const MediaStreamTrackInterface * track) const301 absl::optional<int> TrackMediaInfoMap::GetAttachmentIdByTrack(
302 const MediaStreamTrackInterface* track) const {
303 RTC_DCHECK(is_initialized_);
304 auto it = attachment_id_by_track_.find(track);
305 return it != attachment_id_by_track_.end() ? absl::optional<int>(it->second)
306 : absl::nullopt;
307 }
308
309 } // namespace webrtc
310