OLD | NEW |
| (Empty) |
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 "webrtc/api/trackmediainfomap.h" | |
12 | |
13 #include <utility> | |
14 | |
15 namespace webrtc { | |
16 | |
17 namespace { | |
18 | |
19 template<typename K, typename V> | |
20 V FindValueOrNull(const std::map<K, V>& map, const K& key) { | |
21 auto it = map.find(key); | |
22 return (it != map.end()) ? it->second : nullptr; | |
23 } | |
24 | |
25 template<typename K, typename V> | |
26 const V* FindAddressOrNull(const std::map<K, V>& map, const K& key) { | |
27 auto it = map.find(key); | |
28 return (it != map.end()) ? &it->second : nullptr; | |
29 } | |
30 | |
31 void GetAudioAndVideoTrackBySsrc( | |
32 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders, | |
33 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers, | |
34 std::map<uint32_t, AudioTrackInterface*>* audio_track_by_ssrc, | |
35 std::map<uint32_t, VideoTrackInterface*>* video_track_by_ssrc) { | |
36 RTC_DCHECK(audio_track_by_ssrc->empty()); | |
37 RTC_DCHECK(video_track_by_ssrc->empty()); | |
38 // TODO(hbos): RTP senders/receivers uses a proxy to the signaling thread, and | |
39 // our sender/receiver implementations invokes on the worker thread. (This | |
40 // means one thread jump if on signaling thread and two thread jumps if on any | |
41 // other threads). Is there a way to avoid thread jump(s) on a per | |
42 // sender/receiver, per method basis? | |
43 for (const rtc::scoped_refptr<RtpSenderInterface>& rtp_sender : rtp_senders) { | |
44 cricket::MediaType media_type = rtp_sender->media_type(); | |
45 MediaStreamTrackInterface* track = rtp_sender->track(); | |
46 if (!track) { | |
47 continue; | |
48 } | |
49 RTC_DCHECK_EQ(track->kind(), | |
50 media_type == cricket::MEDIA_TYPE_AUDIO | |
51 ? MediaStreamTrackInterface::kAudioKind | |
52 : MediaStreamTrackInterface::kVideoKind); | |
53 // TODO(deadbeef): |ssrc| should be removed in favor of |GetParameters|. | |
54 uint32_t ssrc = rtp_sender->ssrc(); | |
55 if (ssrc != 0) { | |
56 if (media_type == cricket::MEDIA_TYPE_AUDIO) { | |
57 RTC_DCHECK(audio_track_by_ssrc->find(ssrc) == | |
58 audio_track_by_ssrc->end()); | |
59 (*audio_track_by_ssrc)[ssrc] = static_cast<AudioTrackInterface*>(track); | |
60 } else { | |
61 RTC_DCHECK(video_track_by_ssrc->find(ssrc) == | |
62 video_track_by_ssrc->end()); | |
63 (*video_track_by_ssrc)[ssrc] = static_cast<VideoTrackInterface*>(track); | |
64 } | |
65 } | |
66 } | |
67 for (const rtc::scoped_refptr<RtpReceiverInterface>& rtp_receiver : | |
68 rtp_receivers) { | |
69 cricket::MediaType media_type = rtp_receiver->media_type(); | |
70 MediaStreamTrackInterface* track = rtp_receiver->track(); | |
71 RTC_DCHECK(track); | |
72 RTC_DCHECK_EQ(track->kind(), | |
73 media_type == cricket::MEDIA_TYPE_AUDIO | |
74 ? MediaStreamTrackInterface::kAudioKind | |
75 : MediaStreamTrackInterface::kVideoKind); | |
76 RtpParameters params = rtp_receiver->GetParameters(); | |
77 for (const RtpEncodingParameters& encoding : params.encodings) { | |
78 if (!encoding.ssrc) { | |
79 continue; | |
80 } | |
81 if (media_type == cricket::MEDIA_TYPE_AUDIO) { | |
82 RTC_DCHECK(audio_track_by_ssrc->find(*encoding.ssrc) == | |
83 audio_track_by_ssrc->end()); | |
84 (*audio_track_by_ssrc)[*encoding.ssrc] = | |
85 static_cast<AudioTrackInterface*>(track); | |
86 } else { | |
87 RTC_DCHECK(video_track_by_ssrc->find(*encoding.ssrc) == | |
88 video_track_by_ssrc->end()); | |
89 (*video_track_by_ssrc)[*encoding.ssrc] = | |
90 static_cast<VideoTrackInterface*>(track); | |
91 } | |
92 } | |
93 } | |
94 } | |
95 | |
96 } // namespace | |
97 | |
98 TrackMediaInfoMap::TrackMediaInfoMap( | |
99 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info, | |
100 std::unique_ptr<cricket::VideoMediaInfo> video_media_info, | |
101 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders, | |
102 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers) | |
103 : voice_media_info_(std::move(voice_media_info)), | |
104 video_media_info_(std::move(video_media_info)) { | |
105 std::map<uint32_t, AudioTrackInterface*> audio_track_by_ssrc; | |
106 std::map<uint32_t, VideoTrackInterface*> video_track_by_ssrc; | |
107 GetAudioAndVideoTrackBySsrc( | |
108 rtp_senders, rtp_receivers, &audio_track_by_ssrc, &video_track_by_ssrc); | |
109 if (voice_media_info_) { | |
110 for (auto& sender_info : voice_media_info_->senders) { | |
111 AudioTrackInterface* associated_track = | |
112 FindValueOrNull(audio_track_by_ssrc, sender_info.ssrc()); | |
113 if (associated_track) { | |
114 // One sender is associated with at most one track. | |
115 // One track may be associated with multiple senders. | |
116 audio_track_by_sender_info_[&sender_info] = associated_track; | |
117 voice_infos_by_local_track_[associated_track].push_back(&sender_info); | |
118 } | |
119 } | |
120 for (auto& receiver_info : voice_media_info_->receivers) { | |
121 AudioTrackInterface* associated_track = | |
122 FindValueOrNull(audio_track_by_ssrc, receiver_info.ssrc()); | |
123 if (associated_track) { | |
124 // One receiver is associated with at most one track, which is uniquely | |
125 // associated with that receiver. | |
126 audio_track_by_receiver_info_[&receiver_info] = associated_track; | |
127 RTC_DCHECK(voice_info_by_remote_track_.find(associated_track) == | |
128 voice_info_by_remote_track_.end()); | |
129 voice_info_by_remote_track_[associated_track] = &receiver_info; | |
130 } | |
131 } | |
132 } | |
133 if (video_media_info_) { | |
134 for (auto& sender_info : video_media_info_->senders) { | |
135 VideoTrackInterface* associated_track = | |
136 FindValueOrNull(video_track_by_ssrc, sender_info.ssrc()); | |
137 if (associated_track) { | |
138 // One sender is associated with at most one track. | |
139 // One track may be associated with multiple senders. | |
140 video_track_by_sender_info_[&sender_info] = associated_track; | |
141 video_infos_by_local_track_[associated_track].push_back(&sender_info); | |
142 } | |
143 } | |
144 for (auto& receiver_info : video_media_info_->receivers) { | |
145 VideoTrackInterface* associated_track = | |
146 FindValueOrNull(video_track_by_ssrc, receiver_info.ssrc()); | |
147 if (associated_track) { | |
148 // One receiver is associated with at most one track, which is uniquely | |
149 // associated with that receiver. | |
150 video_track_by_receiver_info_[&receiver_info] = associated_track; | |
151 RTC_DCHECK(video_info_by_remote_track_.find(associated_track) == | |
152 video_info_by_remote_track_.end()); | |
153 video_info_by_remote_track_[associated_track] = &receiver_info; | |
154 } | |
155 } | |
156 } | |
157 } | |
158 | |
159 const std::vector<cricket::VoiceSenderInfo*>* | |
160 TrackMediaInfoMap::GetVoiceSenderInfos( | |
161 const AudioTrackInterface& local_audio_track) const { | |
162 return FindAddressOrNull(voice_infos_by_local_track_, &local_audio_track); | |
163 } | |
164 | |
165 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo( | |
166 const AudioTrackInterface& remote_audio_track) const { | |
167 return FindValueOrNull(voice_info_by_remote_track_, &remote_audio_track); | |
168 } | |
169 | |
170 const std::vector<cricket::VideoSenderInfo*>* | |
171 TrackMediaInfoMap::GetVideoSenderInfos( | |
172 const VideoTrackInterface& local_video_track) const { | |
173 return FindAddressOrNull(video_infos_by_local_track_, &local_video_track); | |
174 } | |
175 | |
176 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo( | |
177 const VideoTrackInterface& remote_video_track) const { | |
178 return FindValueOrNull(video_info_by_remote_track_, &remote_video_track); | |
179 } | |
180 | |
181 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( | |
182 const cricket::VoiceSenderInfo& voice_sender_info) const { | |
183 return FindValueOrNull(audio_track_by_sender_info_, &voice_sender_info); | |
184 } | |
185 | |
186 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( | |
187 const cricket::VoiceReceiverInfo& voice_receiver_info) const { | |
188 return FindValueOrNull(audio_track_by_receiver_info_, &voice_receiver_info); | |
189 } | |
190 | |
191 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( | |
192 const cricket::VideoSenderInfo& video_sender_info) const { | |
193 return FindValueOrNull(video_track_by_sender_info_, &video_sender_info); | |
194 } | |
195 | |
196 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( | |
197 const cricket::VideoReceiverInfo& video_receiver_info) const { | |
198 return FindValueOrNull(video_track_by_receiver_info_, &video_receiver_info); | |
199 } | |
200 | |
201 } // namespace webrtc | |
OLD | NEW |