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 <string>
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(const std::vector<rtc::scoped_refptr<RtpSenderInternal>> & rtp_senders,const std::vector<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     const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders,
42     const std::vector<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();
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();
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 
TrackMediaInfoMap(std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,std::unique_ptr<cricket::VideoMediaInfo> video_media_info,const std::vector<rtc::scoped_refptr<RtpSenderInternal>> & rtp_senders,const std::vector<rtc::scoped_refptr<RtpReceiverInternal>> & rtp_receivers)107 TrackMediaInfoMap::TrackMediaInfoMap(
108     std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,
109     std::unique_ptr<cricket::VideoMediaInfo> video_media_info,
110     const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders,
111     const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>& rtp_receivers)
112     : voice_media_info_(std::move(voice_media_info)),
113       video_media_info_(std::move(video_media_info)) {
114   rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
115 
116   std::map<uint32_t, AudioTrackInterface*> local_audio_track_by_ssrc;
117   std::map<uint32_t, VideoTrackInterface*> local_video_track_by_ssrc;
118   std::map<uint32_t, AudioTrackInterface*> remote_audio_track_by_ssrc;
119   std::map<uint32_t, VideoTrackInterface*> remote_video_track_by_ssrc;
120   AudioTrackInterface* unsignaled_audio_track = nullptr;
121   VideoTrackInterface* unsignaled_video_track = nullptr;
122   GetAudioAndVideoTrackBySsrc(
123       rtp_senders, rtp_receivers, &local_audio_track_by_ssrc,
124       &local_video_track_by_ssrc, &remote_audio_track_by_ssrc,
125       &remote_video_track_by_ssrc, &unsignaled_audio_track,
126       &unsignaled_video_track);
127 
128   for (const auto& sender : rtp_senders) {
129     attachment_id_by_track_[sender->track()] = sender->AttachmentId();
130   }
131   for (const auto& receiver : rtp_receivers) {
132     attachment_id_by_track_[receiver->track()] = receiver->AttachmentId();
133   }
134 
135   if (voice_media_info_) {
136     for (auto& sender_info : voice_media_info_->senders) {
137       AudioTrackInterface* associated_track =
138           FindValueOrNull(local_audio_track_by_ssrc, sender_info.ssrc());
139       if (associated_track) {
140         // One sender is associated with at most one track.
141         // One track may be associated with multiple senders.
142         audio_track_by_sender_info_[&sender_info] = associated_track;
143         voice_infos_by_local_track_[associated_track].push_back(&sender_info);
144       }
145       if (sender_info.ssrc() == 0)
146         continue;  // Unconnected SSRC. bugs.webrtc.org/8673
147       RTC_CHECK(voice_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0)
148           << "Duplicate voice sender SSRC: " << sender_info.ssrc();
149       voice_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info;
150     }
151     for (auto& receiver_info : voice_media_info_->receivers) {
152       AudioTrackInterface* associated_track =
153           FindValueOrNull(remote_audio_track_by_ssrc, receiver_info.ssrc());
154       if (associated_track) {
155         // One receiver is associated with at most one track, which is uniquely
156         // associated with that receiver.
157         audio_track_by_receiver_info_[&receiver_info] = associated_track;
158         RTC_DCHECK(voice_info_by_remote_track_.find(associated_track) ==
159                    voice_info_by_remote_track_.end());
160         voice_info_by_remote_track_[associated_track] = &receiver_info;
161       } else if (unsignaled_audio_track) {
162         audio_track_by_receiver_info_[&receiver_info] = unsignaled_audio_track;
163         voice_info_by_remote_track_[unsignaled_audio_track] = &receiver_info;
164       }
165       RTC_CHECK(voice_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0)
166           << "Duplicate voice receiver SSRC: " << receiver_info.ssrc();
167       voice_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info;
168     }
169   }
170   if (video_media_info_) {
171     for (auto& sender_info : video_media_info_->senders) {
172       std::set<uint32_t> ssrcs;
173       ssrcs.insert(sender_info.ssrc());
174       for (auto& ssrc_group : sender_info.ssrc_groups) {
175         for (auto ssrc : ssrc_group.ssrcs) {
176           ssrcs.insert(ssrc);
177         }
178       }
179       for (auto ssrc : ssrcs) {
180         VideoTrackInterface* associated_track =
181             FindValueOrNull(local_video_track_by_ssrc, ssrc);
182         if (associated_track) {
183           // One sender is associated with at most one track.
184           // One track may be associated with multiple senders.
185           video_track_by_sender_info_[&sender_info] = associated_track;
186           video_infos_by_local_track_[associated_track].push_back(&sender_info);
187           break;
188         }
189       }
190     }
191     for (auto& sender_info : video_media_info_->aggregated_senders) {
192       if (sender_info.ssrc() == 0)
193         continue;  // Unconnected SSRC. bugs.webrtc.org/8673
194       RTC_DCHECK(video_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0)
195           << "Duplicate video sender SSRC: " << sender_info.ssrc();
196       video_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info;
197       VideoTrackInterface* associated_track =
198           FindValueOrNull(local_video_track_by_ssrc, sender_info.ssrc());
199       if (associated_track) {
200         video_track_by_sender_info_[&sender_info] = associated_track;
201       }
202     }
203     for (auto& receiver_info : video_media_info_->receivers) {
204       VideoTrackInterface* associated_track =
205           FindValueOrNull(remote_video_track_by_ssrc, receiver_info.ssrc());
206       if (associated_track) {
207         // One receiver is associated with at most one track, which is uniquely
208         // associated with that receiver.
209         video_track_by_receiver_info_[&receiver_info] = associated_track;
210         RTC_DCHECK(video_info_by_remote_track_.find(associated_track) ==
211                    video_info_by_remote_track_.end());
212         video_info_by_remote_track_[associated_track] = &receiver_info;
213       } else if (unsignaled_video_track) {
214         video_track_by_receiver_info_[&receiver_info] = unsignaled_video_track;
215         video_info_by_remote_track_[unsignaled_video_track] = &receiver_info;
216       }
217       RTC_DCHECK(video_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0)
218           << "Duplicate video receiver SSRC: " << receiver_info.ssrc();
219       video_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info;
220     }
221   }
222 }
223 
224 const std::vector<cricket::VoiceSenderInfo*>*
GetVoiceSenderInfos(const AudioTrackInterface & local_audio_track) const225 TrackMediaInfoMap::GetVoiceSenderInfos(
226     const AudioTrackInterface& local_audio_track) const {
227   return FindAddressOrNull(voice_infos_by_local_track_, &local_audio_track);
228 }
229 
GetVoiceReceiverInfo(const AudioTrackInterface & remote_audio_track) const230 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo(
231     const AudioTrackInterface& remote_audio_track) const {
232   return FindValueOrNull(voice_info_by_remote_track_, &remote_audio_track);
233 }
234 
235 const std::vector<cricket::VideoSenderInfo*>*
GetVideoSenderInfos(const VideoTrackInterface & local_video_track) const236 TrackMediaInfoMap::GetVideoSenderInfos(
237     const VideoTrackInterface& local_video_track) const {
238   return FindAddressOrNull(video_infos_by_local_track_, &local_video_track);
239 }
240 
GetVideoReceiverInfo(const VideoTrackInterface & remote_video_track) const241 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo(
242     const VideoTrackInterface& remote_video_track) const {
243   return FindValueOrNull(video_info_by_remote_track_, &remote_video_track);
244 }
245 
GetVoiceSenderInfoBySsrc(uint32_t ssrc) const246 const cricket::VoiceSenderInfo* TrackMediaInfoMap::GetVoiceSenderInfoBySsrc(
247     uint32_t ssrc) const {
248   return FindValueOrNull(voice_info_by_sender_ssrc_, ssrc);
249 }
GetVoiceReceiverInfoBySsrc(uint32_t ssrc) const250 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfoBySsrc(
251     uint32_t ssrc) const {
252   return FindValueOrNull(voice_info_by_receiver_ssrc_, ssrc);
253 }
254 
GetVideoSenderInfoBySsrc(uint32_t ssrc) const255 const cricket::VideoSenderInfo* TrackMediaInfoMap::GetVideoSenderInfoBySsrc(
256     uint32_t ssrc) const {
257   return FindValueOrNull(video_info_by_sender_ssrc_, ssrc);
258 }
259 
GetVideoReceiverInfoBySsrc(uint32_t ssrc) const260 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfoBySsrc(
261     uint32_t ssrc) const {
262   return FindValueOrNull(video_info_by_receiver_ssrc_, ssrc);
263 }
264 
GetAudioTrack(const cricket::VoiceSenderInfo & voice_sender_info) const265 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
266     const cricket::VoiceSenderInfo& voice_sender_info) const {
267   return FindValueOrNull(audio_track_by_sender_info_, &voice_sender_info);
268 }
269 
GetAudioTrack(const cricket::VoiceReceiverInfo & voice_receiver_info) const270 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
271     const cricket::VoiceReceiverInfo& voice_receiver_info) const {
272   return FindValueOrNull(audio_track_by_receiver_info_, &voice_receiver_info);
273 }
274 
GetVideoTrack(const cricket::VideoSenderInfo & video_sender_info) const275 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
276     const cricket::VideoSenderInfo& video_sender_info) const {
277   return FindValueOrNull(video_track_by_sender_info_, &video_sender_info);
278 }
279 
GetVideoTrack(const cricket::VideoReceiverInfo & video_receiver_info) const280 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
281     const cricket::VideoReceiverInfo& video_receiver_info) const {
282   return FindValueOrNull(video_track_by_receiver_info_, &video_receiver_info);
283 }
284 
GetAttachmentIdByTrack(const MediaStreamTrackInterface * track) const285 absl::optional<int> TrackMediaInfoMap::GetAttachmentIdByTrack(
286     const MediaStreamTrackInterface* track) const {
287   auto it = attachment_id_by_track_.find(track);
288   return it != attachment_id_by_track_.end() ? absl::optional<int>(it->second)
289                                              : absl::nullopt;
290 }
291 
292 }  // namespace webrtc
293