blob: d296c7f70e7fa60ae367bef8416f677107021e34 [file] [log] [blame]
hbos1f8239c2017-01-16 04:24:10 -08001/*
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
deadbeef804c1af2017-02-11 19:07:31 -080011#include "webrtc/pc/trackmediainfomap.h"
hbos1f8239c2017-01-16 04:24:10 -080012
13#include <utility>
14
15namespace webrtc {
16
17namespace {
18
deadbeef804c1af2017-02-11 19:07:31 -080019template <typename K, typename V>
hbos1f8239c2017-01-16 04:24:10 -080020V 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
deadbeef804c1af2017-02-11 19:07:31 -080025template <typename K, typename V>
hbos1f8239c2017-01-16 04:24:10 -080026const 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
31void 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 }
deadbeef804c1af2017-02-11 19:07:31 -080049 RTC_DCHECK_EQ(track->kind(), media_type == cricket::MEDIA_TYPE_AUDIO
50 ? MediaStreamTrackInterface::kAudioKind
51 : MediaStreamTrackInterface::kVideoKind);
hbos1f8239c2017-01-16 04:24:10 -080052 // TODO(deadbeef): |ssrc| should be removed in favor of |GetParameters|.
53 uint32_t ssrc = rtp_sender->ssrc();
54 if (ssrc != 0) {
55 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
56 RTC_DCHECK(audio_track_by_ssrc->find(ssrc) ==
57 audio_track_by_ssrc->end());
58 (*audio_track_by_ssrc)[ssrc] = static_cast<AudioTrackInterface*>(track);
59 } else {
60 RTC_DCHECK(video_track_by_ssrc->find(ssrc) ==
61 video_track_by_ssrc->end());
62 (*video_track_by_ssrc)[ssrc] = static_cast<VideoTrackInterface*>(track);
63 }
64 }
65 }
66 for (const rtc::scoped_refptr<RtpReceiverInterface>& rtp_receiver :
67 rtp_receivers) {
68 cricket::MediaType media_type = rtp_receiver->media_type();
69 MediaStreamTrackInterface* track = rtp_receiver->track();
70 RTC_DCHECK(track);
deadbeef804c1af2017-02-11 19:07:31 -080071 RTC_DCHECK_EQ(track->kind(), media_type == cricket::MEDIA_TYPE_AUDIO
72 ? MediaStreamTrackInterface::kAudioKind
73 : MediaStreamTrackInterface::kVideoKind);
hbos1f8239c2017-01-16 04:24:10 -080074 RtpParameters params = rtp_receiver->GetParameters();
75 for (const RtpEncodingParameters& encoding : params.encodings) {
76 if (!encoding.ssrc) {
77 continue;
78 }
79 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
80 RTC_DCHECK(audio_track_by_ssrc->find(*encoding.ssrc) ==
81 audio_track_by_ssrc->end());
82 (*audio_track_by_ssrc)[*encoding.ssrc] =
83 static_cast<AudioTrackInterface*>(track);
84 } else {
85 RTC_DCHECK(video_track_by_ssrc->find(*encoding.ssrc) ==
86 video_track_by_ssrc->end());
87 (*video_track_by_ssrc)[*encoding.ssrc] =
88 static_cast<VideoTrackInterface*>(track);
89 }
90 }
91 }
92}
93
94} // namespace
95
96TrackMediaInfoMap::TrackMediaInfoMap(
97 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,
98 std::unique_ptr<cricket::VideoMediaInfo> video_media_info,
99 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& rtp_senders,
100 const std::vector<rtc::scoped_refptr<RtpReceiverInterface>>& rtp_receivers)
101 : voice_media_info_(std::move(voice_media_info)),
102 video_media_info_(std::move(video_media_info)) {
103 std::map<uint32_t, AudioTrackInterface*> audio_track_by_ssrc;
104 std::map<uint32_t, VideoTrackInterface*> video_track_by_ssrc;
deadbeef804c1af2017-02-11 19:07:31 -0800105 GetAudioAndVideoTrackBySsrc(rtp_senders, rtp_receivers, &audio_track_by_ssrc,
106 &video_track_by_ssrc);
hbos1f8239c2017-01-16 04:24:10 -0800107 if (voice_media_info_) {
108 for (auto& sender_info : voice_media_info_->senders) {
109 AudioTrackInterface* associated_track =
110 FindValueOrNull(audio_track_by_ssrc, sender_info.ssrc());
111 if (associated_track) {
112 // One sender is associated with at most one track.
113 // One track may be associated with multiple senders.
114 audio_track_by_sender_info_[&sender_info] = associated_track;
115 voice_infos_by_local_track_[associated_track].push_back(&sender_info);
116 }
117 }
118 for (auto& receiver_info : voice_media_info_->receivers) {
119 AudioTrackInterface* associated_track =
120 FindValueOrNull(audio_track_by_ssrc, receiver_info.ssrc());
121 if (associated_track) {
122 // One receiver is associated with at most one track, which is uniquely
123 // associated with that receiver.
124 audio_track_by_receiver_info_[&receiver_info] = associated_track;
125 RTC_DCHECK(voice_info_by_remote_track_.find(associated_track) ==
126 voice_info_by_remote_track_.end());
127 voice_info_by_remote_track_[associated_track] = &receiver_info;
128 }
129 }
130 }
131 if (video_media_info_) {
132 for (auto& sender_info : video_media_info_->senders) {
133 VideoTrackInterface* associated_track =
134 FindValueOrNull(video_track_by_ssrc, sender_info.ssrc());
135 if (associated_track) {
136 // One sender is associated with at most one track.
137 // One track may be associated with multiple senders.
138 video_track_by_sender_info_[&sender_info] = associated_track;
139 video_infos_by_local_track_[associated_track].push_back(&sender_info);
140 }
141 }
142 for (auto& receiver_info : video_media_info_->receivers) {
143 VideoTrackInterface* associated_track =
144 FindValueOrNull(video_track_by_ssrc, receiver_info.ssrc());
145 if (associated_track) {
146 // One receiver is associated with at most one track, which is uniquely
147 // associated with that receiver.
148 video_track_by_receiver_info_[&receiver_info] = associated_track;
149 RTC_DCHECK(video_info_by_remote_track_.find(associated_track) ==
150 video_info_by_remote_track_.end());
151 video_info_by_remote_track_[associated_track] = &receiver_info;
152 }
153 }
154 }
155}
156
157const std::vector<cricket::VoiceSenderInfo*>*
158TrackMediaInfoMap::GetVoiceSenderInfos(
159 const AudioTrackInterface& local_audio_track) const {
160 return FindAddressOrNull(voice_infos_by_local_track_, &local_audio_track);
161}
162
163const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo(
164 const AudioTrackInterface& remote_audio_track) const {
165 return FindValueOrNull(voice_info_by_remote_track_, &remote_audio_track);
166}
167
168const std::vector<cricket::VideoSenderInfo*>*
169TrackMediaInfoMap::GetVideoSenderInfos(
170 const VideoTrackInterface& local_video_track) const {
171 return FindAddressOrNull(video_infos_by_local_track_, &local_video_track);
172}
173
174const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo(
175 const VideoTrackInterface& remote_video_track) const {
176 return FindValueOrNull(video_info_by_remote_track_, &remote_video_track);
177}
178
179rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
180 const cricket::VoiceSenderInfo& voice_sender_info) const {
181 return FindValueOrNull(audio_track_by_sender_info_, &voice_sender_info);
182}
183
184rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack(
185 const cricket::VoiceReceiverInfo& voice_receiver_info) const {
186 return FindValueOrNull(audio_track_by_receiver_info_, &voice_receiver_info);
187}
188
189rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
190 const cricket::VideoSenderInfo& video_sender_info) const {
191 return FindValueOrNull(video_track_by_sender_info_, &video_sender_info);
192}
193
194rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack(
195 const cricket::VideoReceiverInfo& video_receiver_info) const {
196 return FindValueOrNull(video_track_by_receiver_info_, &video_receiver_info);
197}
198
199} // namespace webrtc