hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/track_media_info_map.h" |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 12 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 13 | #include <string> |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
| 19 | |
deadbeef | 804c1af | 2017-02-11 19:07:31 -0800 | [diff] [blame] | 20 | template <typename K, typename V> |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 21 | V FindValueOrNull(const std::map<K, V>& map, const K& key) { |
| 22 | auto it = map.find(key); |
| 23 | return (it != map.end()) ? it->second : nullptr; |
| 24 | } |
| 25 | |
deadbeef | 804c1af | 2017-02-11 19:07:31 -0800 | [diff] [blame] | 26 | template <typename K, typename V> |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 27 | const V* FindAddressOrNull(const std::map<K, V>& map, const K& key) { |
| 28 | auto it = map.find(key); |
| 29 | return (it != map.end()) ? &it->second : nullptr; |
| 30 | } |
| 31 | |
| 32 | void GetAudioAndVideoTrackBySsrc( |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 33 | const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders, |
| 34 | const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>& rtp_receivers, |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 35 | std::map<uint32_t, AudioTrackInterface*>* local_audio_track_by_ssrc, |
| 36 | std::map<uint32_t, VideoTrackInterface*>* local_video_track_by_ssrc, |
| 37 | std::map<uint32_t, AudioTrackInterface*>* remote_audio_track_by_ssrc, |
zhihuang | f816493 | 2017-05-19 13:09:47 -0700 | [diff] [blame] | 38 | std::map<uint32_t, VideoTrackInterface*>* remote_video_track_by_ssrc, |
| 39 | AudioTrackInterface** unsignaled_audio_track, |
| 40 | VideoTrackInterface** unsignaled_video_track) { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 41 | RTC_DCHECK(local_audio_track_by_ssrc->empty()); |
| 42 | RTC_DCHECK(local_video_track_by_ssrc->empty()); |
| 43 | RTC_DCHECK(remote_audio_track_by_ssrc->empty()); |
| 44 | RTC_DCHECK(remote_video_track_by_ssrc->empty()); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 45 | // TODO(hbos): RTP senders/receivers uses a proxy to the signaling thread, and |
| 46 | // our sender/receiver implementations invokes on the worker thread. (This |
| 47 | // means one thread jump if on signaling thread and two thread jumps if on any |
| 48 | // other threads). Is there a way to avoid thread jump(s) on a per |
| 49 | // sender/receiver, per method basis? |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 50 | for (auto rtp_sender : rtp_senders) { |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 51 | cricket::MediaType media_type = rtp_sender->media_type(); |
| 52 | MediaStreamTrackInterface* track = rtp_sender->track(); |
| 53 | if (!track) { |
| 54 | continue; |
| 55 | } |
deadbeef | 804c1af | 2017-02-11 19:07:31 -0800 | [diff] [blame] | 56 | RTC_DCHECK_EQ(track->kind(), media_type == cricket::MEDIA_TYPE_AUDIO |
| 57 | ? MediaStreamTrackInterface::kAudioKind |
| 58 | : MediaStreamTrackInterface::kVideoKind); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 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) { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 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); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 67 | } else { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 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); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 75 | for (auto rtp_receiver : rtp_receivers) { |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 76 | cricket::MediaType media_type = rtp_receiver->media_type(); |
| 77 | MediaStreamTrackInterface* track = rtp_receiver->track(); |
| 78 | RTC_DCHECK(track); |
deadbeef | 804c1af | 2017-02-11 19:07:31 -0800 | [diff] [blame] | 79 | RTC_DCHECK_EQ(track->kind(), media_type == cricket::MEDIA_TYPE_AUDIO |
| 80 | ? MediaStreamTrackInterface::kAudioKind |
| 81 | : MediaStreamTrackInterface::kVideoKind); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 82 | RtpParameters params = rtp_receiver->GetParameters(); |
| 83 | for (const RtpEncodingParameters& encoding : params.encodings) { |
| 84 | if (!encoding.ssrc) { |
zhihuang | f816493 | 2017-05-19 13:09:47 -0700 | [diff] [blame] | 85 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 86 | *unsignaled_audio_track = static_cast<AudioTrackInterface*>(track); |
| 87 | } else { |
| 88 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_VIDEO); |
| 89 | *unsignaled_video_track = static_cast<VideoTrackInterface*>(track); |
| 90 | } |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 94 | RTC_DCHECK(remote_audio_track_by_ssrc->find(*encoding.ssrc) == |
| 95 | remote_audio_track_by_ssrc->end()); |
| 96 | (*remote_audio_track_by_ssrc)[*encoding.ssrc] = |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 97 | static_cast<AudioTrackInterface*>(track); |
| 98 | } else { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 99 | RTC_DCHECK(remote_video_track_by_ssrc->find(*encoding.ssrc) == |
| 100 | remote_video_track_by_ssrc->end()); |
| 101 | (*remote_video_track_by_ssrc)[*encoding.ssrc] = |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 102 | static_cast<VideoTrackInterface*>(track); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | |
| 110 | TrackMediaInfoMap::TrackMediaInfoMap( |
| 111 | std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info, |
| 112 | std::unique_ptr<cricket::VideoMediaInfo> video_media_info, |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 113 | const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders, |
| 114 | const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>& rtp_receivers) |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 115 | : voice_media_info_(std::move(voice_media_info)), |
| 116 | video_media_info_(std::move(video_media_info)) { |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 117 | std::map<uint32_t, AudioTrackInterface*> local_audio_track_by_ssrc; |
| 118 | std::map<uint32_t, VideoTrackInterface*> local_video_track_by_ssrc; |
| 119 | std::map<uint32_t, AudioTrackInterface*> remote_audio_track_by_ssrc; |
| 120 | std::map<uint32_t, VideoTrackInterface*> remote_video_track_by_ssrc; |
zhihuang | f816493 | 2017-05-19 13:09:47 -0700 | [diff] [blame] | 121 | AudioTrackInterface* unsignaled_audio_track = nullptr; |
| 122 | VideoTrackInterface* unsignaled_video_track = nullptr; |
| 123 | GetAudioAndVideoTrackBySsrc( |
| 124 | rtp_senders, rtp_receivers, &local_audio_track_by_ssrc, |
| 125 | &local_video_track_by_ssrc, &remote_audio_track_by_ssrc, |
| 126 | &remote_video_track_by_ssrc, &unsignaled_audio_track, |
| 127 | &unsignaled_video_track); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 128 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 129 | for (auto sender : rtp_senders) { |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 130 | attachment_id_by_track_[sender->track()] = sender->AttachmentId(); |
| 131 | } |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 132 | for (auto receiver : rtp_receivers) { |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 133 | attachment_id_by_track_[receiver->track()] = receiver->AttachmentId(); |
| 134 | } |
| 135 | |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 136 | if (voice_media_info_) { |
| 137 | for (auto& sender_info : voice_media_info_->senders) { |
| 138 | AudioTrackInterface* associated_track = |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 139 | FindValueOrNull(local_audio_track_by_ssrc, sender_info.ssrc()); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 140 | if (associated_track) { |
| 141 | // One sender is associated with at most one track. |
| 142 | // One track may be associated with multiple senders. |
| 143 | audio_track_by_sender_info_[&sender_info] = associated_track; |
| 144 | voice_infos_by_local_track_[associated_track].push_back(&sender_info); |
| 145 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 146 | if (sender_info.ssrc() == 0) |
| 147 | continue; // Unconnected SSRC. bugs.webrtc.org/8673 |
| 148 | RTC_CHECK(voice_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0) |
| 149 | << "Duplicate voice sender SSRC: " << sender_info.ssrc(); |
| 150 | voice_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 151 | } |
| 152 | for (auto& receiver_info : voice_media_info_->receivers) { |
| 153 | AudioTrackInterface* associated_track = |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 154 | FindValueOrNull(remote_audio_track_by_ssrc, receiver_info.ssrc()); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 155 | if (associated_track) { |
| 156 | // One receiver is associated with at most one track, which is uniquely |
| 157 | // associated with that receiver. |
| 158 | audio_track_by_receiver_info_[&receiver_info] = associated_track; |
| 159 | RTC_DCHECK(voice_info_by_remote_track_.find(associated_track) == |
| 160 | voice_info_by_remote_track_.end()); |
| 161 | voice_info_by_remote_track_[associated_track] = &receiver_info; |
zhihuang | f816493 | 2017-05-19 13:09:47 -0700 | [diff] [blame] | 162 | } else if (unsignaled_audio_track) { |
| 163 | audio_track_by_receiver_info_[&receiver_info] = unsignaled_audio_track; |
| 164 | voice_info_by_remote_track_[unsignaled_audio_track] = &receiver_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 165 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 166 | RTC_CHECK(voice_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0) |
| 167 | << "Duplicate voice receiver SSRC: " << receiver_info.ssrc(); |
| 168 | voice_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | if (video_media_info_) { |
| 172 | for (auto& sender_info : video_media_info_->senders) { |
| 173 | VideoTrackInterface* associated_track = |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 174 | FindValueOrNull(local_video_track_by_ssrc, sender_info.ssrc()); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 175 | if (associated_track) { |
| 176 | // One sender is associated with at most one track. |
| 177 | // One track may be associated with multiple senders. |
| 178 | video_track_by_sender_info_[&sender_info] = associated_track; |
| 179 | video_infos_by_local_track_[associated_track].push_back(&sender_info); |
| 180 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 181 | if (sender_info.ssrc() == 0) |
| 182 | continue; // Unconnected SSRC. bugs.webrtc.org/8673 |
| 183 | RTC_DCHECK(video_info_by_sender_ssrc_.count(sender_info.ssrc()) == 0) |
| 184 | << "Duplicate video sender SSRC: " << sender_info.ssrc(); |
| 185 | video_info_by_sender_ssrc_[sender_info.ssrc()] = &sender_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 186 | } |
| 187 | for (auto& receiver_info : video_media_info_->receivers) { |
| 188 | VideoTrackInterface* associated_track = |
hbos | fe90ad1 | 2017-02-20 02:05:13 -0800 | [diff] [blame] | 189 | FindValueOrNull(remote_video_track_by_ssrc, receiver_info.ssrc()); |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 190 | if (associated_track) { |
| 191 | // One receiver is associated with at most one track, which is uniquely |
| 192 | // associated with that receiver. |
| 193 | video_track_by_receiver_info_[&receiver_info] = associated_track; |
| 194 | RTC_DCHECK(video_info_by_remote_track_.find(associated_track) == |
| 195 | video_info_by_remote_track_.end()); |
| 196 | video_info_by_remote_track_[associated_track] = &receiver_info; |
zhihuang | f816493 | 2017-05-19 13:09:47 -0700 | [diff] [blame] | 197 | } else if (unsignaled_video_track) { |
| 198 | video_track_by_receiver_info_[&receiver_info] = unsignaled_video_track; |
| 199 | video_info_by_remote_track_[unsignaled_video_track] = &receiver_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 200 | } |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 201 | RTC_DCHECK(video_info_by_receiver_ssrc_.count(receiver_info.ssrc()) == 0) |
| 202 | << "Duplicate video receiver SSRC: " << receiver_info.ssrc(); |
| 203 | video_info_by_receiver_ssrc_[receiver_info.ssrc()] = &receiver_info; |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | const std::vector<cricket::VoiceSenderInfo*>* |
| 209 | TrackMediaInfoMap::GetVoiceSenderInfos( |
| 210 | const AudioTrackInterface& local_audio_track) const { |
| 211 | return FindAddressOrNull(voice_infos_by_local_track_, &local_audio_track); |
| 212 | } |
| 213 | |
| 214 | const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfo( |
| 215 | const AudioTrackInterface& remote_audio_track) const { |
| 216 | return FindValueOrNull(voice_info_by_remote_track_, &remote_audio_track); |
| 217 | } |
| 218 | |
| 219 | const std::vector<cricket::VideoSenderInfo*>* |
| 220 | TrackMediaInfoMap::GetVideoSenderInfos( |
| 221 | const VideoTrackInterface& local_video_track) const { |
| 222 | return FindAddressOrNull(video_infos_by_local_track_, &local_video_track); |
| 223 | } |
| 224 | |
| 225 | const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfo( |
| 226 | const VideoTrackInterface& remote_video_track) const { |
| 227 | return FindValueOrNull(video_info_by_remote_track_, &remote_video_track); |
| 228 | } |
| 229 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 230 | const cricket::VoiceSenderInfo* TrackMediaInfoMap::GetVoiceSenderInfoBySsrc( |
| 231 | uint32_t ssrc) const { |
| 232 | return FindValueOrNull(voice_info_by_sender_ssrc_, ssrc); |
| 233 | } |
| 234 | const cricket::VoiceReceiverInfo* TrackMediaInfoMap::GetVoiceReceiverInfoBySsrc( |
| 235 | uint32_t ssrc) const { |
| 236 | return FindValueOrNull(voice_info_by_receiver_ssrc_, ssrc); |
| 237 | } |
| 238 | |
| 239 | const cricket::VideoSenderInfo* TrackMediaInfoMap::GetVideoSenderInfoBySsrc( |
| 240 | uint32_t ssrc) const { |
| 241 | return FindValueOrNull(video_info_by_sender_ssrc_, ssrc); |
| 242 | } |
| 243 | |
| 244 | const cricket::VideoReceiverInfo* TrackMediaInfoMap::GetVideoReceiverInfoBySsrc( |
| 245 | uint32_t ssrc) const { |
| 246 | return FindValueOrNull(video_info_by_receiver_ssrc_, ssrc); |
| 247 | } |
| 248 | |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 249 | rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( |
| 250 | const cricket::VoiceSenderInfo& voice_sender_info) const { |
| 251 | return FindValueOrNull(audio_track_by_sender_info_, &voice_sender_info); |
| 252 | } |
| 253 | |
| 254 | rtc::scoped_refptr<AudioTrackInterface> TrackMediaInfoMap::GetAudioTrack( |
| 255 | const cricket::VoiceReceiverInfo& voice_receiver_info) const { |
| 256 | return FindValueOrNull(audio_track_by_receiver_info_, &voice_receiver_info); |
| 257 | } |
| 258 | |
| 259 | rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( |
| 260 | const cricket::VideoSenderInfo& video_sender_info) const { |
| 261 | return FindValueOrNull(video_track_by_sender_info_, &video_sender_info); |
| 262 | } |
| 263 | |
| 264 | rtc::scoped_refptr<VideoTrackInterface> TrackMediaInfoMap::GetVideoTrack( |
| 265 | const cricket::VideoReceiverInfo& video_receiver_info) const { |
| 266 | return FindValueOrNull(video_track_by_receiver_info_, &video_receiver_info); |
| 267 | } |
| 268 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 269 | absl::optional<int> TrackMediaInfoMap::GetAttachmentIdByTrack( |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 270 | const MediaStreamTrackInterface* track) const { |
| 271 | auto it = attachment_id_by_track_.find(track); |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 272 | return it != attachment_id_by_track_.end() ? absl::optional<int>(it->second) |
| 273 | : absl::nullopt; |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 274 | } |
| 275 | |
hbos | 1f8239c | 2017-01-16 04:24:10 -0800 | [diff] [blame] | 276 | } // namespace webrtc |