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 T> | |
20 T* CheckedTrackCast(MediaStreamTrackInterface* track); | |
21 | |
22 template<> | |
23 AudioTrackInterface* CheckedTrackCast(MediaStreamTrackInterface* track) { | |
24 RTC_DCHECK_EQ(track->kind(), MediaStreamTrackInterface::kAudioKind); | |
25 return static_cast<AudioTrackInterface*>(track); | |
26 } | |
27 | |
28 template<> | |
29 VideoTrackInterface* CheckedTrackCast(MediaStreamTrackInterface* track) { | |
30 RTC_DCHECK_EQ(track->kind(), MediaStreamTrackInterface::kVideoKind); | |
31 return static_cast<VideoTrackInterface*>(track); | |
32 } | |
33 | |
34 // |T|: |AudioTrackInterface| or |VideoTrackInterface|, must match with | |
35 // |media_type|. | |
36 template<typename T> | |
37 std::map<uint32_t, T*> GetSsrcToTrack( | |
38 cricket::MediaType media_type, | |
39 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders, | |
40 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& | |
41 rtp_receivers) { | |
42 std::map<uint32_t, T*> ssrc_to_track; | |
43 for (const rtc::scoped_refptr<RtpSenderInterface>& rtp_sender : rtp_senders) { | |
44 if (rtp_sender->media_type() != media_type || !rtp_sender->track()) { | |
45 continue; | |
46 } | |
47 // TODO(deadbeef): |ssrc| should be removed in favor of |GetParameters|. | |
48 if (rtp_sender->ssrc() != 0) { | |
49 RTC_DCHECK(ssrc_to_track.find(rtp_sender->ssrc()) == ssrc_to_track.end()); | |
50 ssrc_to_track[rtp_sender->ssrc()] = CheckedTrackCast<T>( | |
pthatcher1
2017/01/13 00:10:29
Do you really need all this templating complexity?
hbos
2017/01/13 13:58:23
Done. I also merged it into one function that gets
| |
51 rtp_sender->track()); | |
52 } | |
53 } | |
54 for (const rtc::scoped_refptr<RtpReceiverInterface>& rtp_receiver : | |
55 rtp_receivers) { | |
56 if (rtp_receiver->media_type() != media_type) { | |
57 continue; | |
58 } | |
59 RTC_DCHECK(rtp_receiver->track()); | |
60 // TODO(hbos): This uses a proxy to the signaling thread, and our | |
61 // implementation invokes on the worker thread. (This means one thread jump | |
62 // if on signaling thread and two thread jumps if on any other threads). Is | |
63 // there a way to avoid thread jumps per receivers (and same for senders | |
64 // when switching to |rtp_sender->GetParameters()|)? | |
65 RtpParameters params = rtp_receiver->GetParameters(); | |
66 for (const RtpEncodingParameters& encoding : params.encodings) { | |
67 if (!encoding.ssrc) { | |
68 continue; | |
69 } | |
70 RTC_DCHECK(ssrc_to_track.find(*encoding.ssrc) == ssrc_to_track.end()); | |
71 ssrc_to_track[*encoding.ssrc] = CheckedTrackCast<T>( | |
72 rtp_receiver->track()); | |
73 } | |
74 } | |
75 return ssrc_to_track; | |
76 } | |
77 | |
78 // Template in order to support both the audio and video case. | |
79 template<typename TrackType, typename InfoType> | |
80 TrackType* GetAssociatedTrack( | |
pthatcher1
2017/01/13 00:10:29
You could have just made this FindOrNull and have
hbos
2017/01/13 13:58:23
Done.
| |
81 const std::map<uint32_t, TrackType*>& ssrc_to_track, | |
82 const InfoType& info) { | |
83 typename std::map<uint32_t, TrackType*>::const_iterator it = | |
pthatcher1
2017/01/13 00:10:29
I think an "auto it =" would work well here.
hbos
2017/01/13 13:58:23
Done.
| |
84 ssrc_to_track.find(info.ssrc()); | |
85 if (it == ssrc_to_track.end()) { | |
86 return nullptr; | |
87 } | |
88 return it->second; | |
89 } | |
90 | |
91 // Template in order to support both the audio and video case. | |
92 template<typename TrackType, | |
93 typename MediaInfoType, | |
94 typename SenderInfoToTrackType, | |
95 typename LocalTrackToInfosType, | |
96 typename ReceiverInfoToTrackType, | |
97 typename RemoteTrackToInfosType> | |
pthatcher1
2017/01/13 00:10:29
I'd personally prefer just duplicating the logic t
hbos
2017/01/13 13:58:23
Done.
| |
98 void MapTracksAndInfos( | |
99 const std::map<uint32_t, TrackType*>& ssrc_to_track, | |
100 MediaInfoType* media_info, | |
101 SenderInfoToTrackType* sender_info_to_track, | |
102 LocalTrackToInfosType* local_track_to_infos, | |
103 ReceiverInfoToTrackType* receiver_info_to_track, | |
104 RemoteTrackToInfosType* remote_track_to_info) { | |
105 for (auto& sender_info : media_info->senders) { | |
106 TrackType* associated_track = GetAssociatedTrack<TrackType>( | |
107 ssrc_to_track, sender_info); | |
108 if (associated_track) { | |
109 // One sender is associated with at most one track. | |
110 // One track may be associated with multiple senders. | |
111 (*sender_info_to_track)[&sender_info] = associated_track; | |
112 (*local_track_to_infos)[associated_track].push_back(&sender_info); | |
113 } | |
114 } | |
pthatcher1
2017/01/13 00:10:29
I might make sense write a generic "invert map" me
hbos
2017/01/13 13:58:23
I skipped this one because I think it would increa
| |
115 for (auto& receiver_info : media_info->receivers) { | |
116 TrackType* associated_track = GetAssociatedTrack<TrackType>( | |
117 ssrc_to_track, receiver_info); | |
118 if (associated_track) { | |
119 // One receiver is associated with at most one track, which is uniquely | |
120 // associated with that receiver. | |
121 (*receiver_info_to_track)[&receiver_info] = associated_track; | |
122 RTC_DCHECK(remote_track_to_info->find(associated_track) == | |
123 remote_track_to_info->end()); | |
124 (*remote_track_to_info)[associated_track] = &receiver_info; | |
125 } | |
126 } | |
127 } | |
128 | |
129 } // namespace | |
130 | |
131 TrackMediaInfoMap::TrackMediaInfoMap( | |
132 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info, | |
133 std::unique_ptr<cricket::VideoMediaInfo> video_media_info, | |
134 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders, | |
135 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers) | |
136 : voice_media_info_(std::move(voice_media_info)), | |
137 video_media_info_(std::move(video_media_info)) { | |
138 if (voice_media_info_) { | |
139 std::map<uint32_t, AudioTrackInterface*> ssrc_to_audio_track = | |
140 GetSsrcToTrack<AudioTrackInterface>( | |
141 cricket::MEDIA_TYPE_AUDIO, rtp_senders, rtp_receivers); | |
142 MapTracksAndInfos<AudioTrackInterface>(ssrc_to_audio_track, | |
143 voice_media_info_.get(), | |
144 &voice_sender_info_to_track_, | |
145 &local_audio_track_to_infos_, | |
146 &voice_receiver_info_to_track_, | |
147 &remote_audio_track_to_info_); | |
148 } | |
149 if (video_media_info_) { | |
150 std::map<uint32_t, VideoTrackInterface*> ssrc_to_video_track = | |
151 GetSsrcToTrack<VideoTrackInterface>( | |
152 cricket::MEDIA_TYPE_VIDEO, rtp_senders, rtp_receivers); | |
153 MapTracksAndInfos<VideoTrackInterface>(ssrc_to_video_track, | |
154 video_media_info_.get(), | |
155 &video_sender_info_to_track_, | |
156 &local_video_track_to_infos_, | |
157 &video_receiver_info_to_track_, | |
158 &remote_video_track_to_info_); | |
159 } | |
160 } | |
161 | |
162 const std::vector<cricket::VoiceSenderInfo*>* | |
163 TrackMediaInfoMap::GetVoiceSenderInfos( | |
164 const AudioTrackInterface& local_audio_track) const { | |
165 auto it = local_audio_track_to_infos_.find(&local_audio_track); | |
166 if (it == local_audio_track_to_infos_.end()) { | |
167 return nullptr; | |
168 } | |
169 return &it->second; | |
170 } | |
171 | |
172 const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo( | |
173 const AudioTrackInterface& remote_audio_track) const { | |
174 auto it = remote_audio_track_to_info_.find(&remote_audio_track); | |
175 if (it == remote_audio_track_to_info_.end()) { | |
176 return nullptr; | |
177 } | |
178 return it->second; | |
179 } | |
180 | |
181 const std::vector<cricket::VideoSenderInfo*>* | |
182 TrackMediaInfoMap::GetVideoSenderInfos( | |
183 const VideoTrackInterface& local_video_track) const { | |
184 auto it = local_video_track_to_infos_.find(&local_video_track); | |
185 if (it == local_video_track_to_infos_.end()) { | |
186 return nullptr; | |
187 } | |
188 return &it->second; | |
189 } | |
190 | |
191 const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo( | |
192 const VideoTrackInterface& remote_video_track) const { | |
193 auto it =remote_video_track_to_info_.find(&remote_video_track); | |
194 if (it == remote_video_track_to_info_.end()) { | |
195 return nullptr; | |
196 } | |
197 return it->second; | |
198 } | |
199 | |
200 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( | |
201 const cricket::VoiceSenderInfo& voice_sender_info) const { | |
202 auto it = voice_sender_info_to_track_.find(&voice_sender_info); | |
203 if (it == voice_sender_info_to_track_.end()) { | |
204 return nullptr; | |
205 } | |
206 return it->second; | |
207 } | |
208 | |
209 rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( | |
210 const cricket::VoiceReceiverInfo& voice_receiver_info) const { | |
211 auto it = voice_receiver_info_to_track_.find(&voice_receiver_info); | |
212 if (it == voice_receiver_info_to_track_.end()) { | |
213 return nullptr; | |
214 } | |
215 return it->second; | |
216 } | |
217 | |
218 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( | |
219 const cricket::VideoSenderInfo& video_sender_info) const { | |
220 auto it = video_sender_info_to_track_.find(&video_sender_info); | |
221 if (it == video_sender_info_to_track_.end()) { | |
222 return nullptr; | |
223 } | |
224 return it->second; | |
225 } | |
226 | |
227 rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( | |
228 const cricket::VideoReceiverInfo& video_receiver_info) const { | |
229 auto it = video_receiver_info_to_track_.find(&video_receiver_info); | |
230 if (it == video_receiver_info_to_track_.end()) { | |
231 return nullptr; | |
232 } | |
233 return it->second; | |
pthatcher1
2017/01/13 00:10:29
It's curious that you templatized the "return null
hbos
2017/01/13 13:58:23
Done. Generic FindValueOrNull / FindAddressOrNull
| |
234 } | |
235 | |
236 } // namespace webrtc | |
OLD | NEW |