1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2016 The WebRTC Project Authors. All rights reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_TRACK_MEDIA_INFO_MAP_H_ 12*d9f75844SAndroid Build Coastguard Worker #define PC_TRACK_MEDIA_INFO_MAP_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h> 15*d9f75844SAndroid Build Coastguard Worker 16*d9f75844SAndroid Build Coastguard Worker #include <map> 17*d9f75844SAndroid Build Coastguard Worker #include <memory> 18*d9f75844SAndroid Build Coastguard Worker #include <string> 19*d9f75844SAndroid Build Coastguard Worker #include <vector> 20*d9f75844SAndroid Build Coastguard Worker 21*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 22*d9f75844SAndroid Build Coastguard Worker #include "api/array_view.h" 23*d9f75844SAndroid Build Coastguard Worker #include "api/media_stream_interface.h" 24*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h" 25*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_channel.h" 26*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_receiver.h" 27*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_sender.h" 28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_count.h" 29*d9f75844SAndroid Build Coastguard Worker 30*d9f75844SAndroid Build Coastguard Worker namespace webrtc { 31*d9f75844SAndroid Build Coastguard Worker 32*d9f75844SAndroid Build Coastguard Worker // Audio/video tracks and sender/receiver statistical information are associated 33*d9f75844SAndroid Build Coastguard Worker // with each other based on attachments to RTP senders/receivers. This class 34*d9f75844SAndroid Build Coastguard Worker // maps that relationship, in both directions, so that stats about a track can 35*d9f75844SAndroid Build Coastguard Worker // be retrieved on a per-attachment basis. 36*d9f75844SAndroid Build Coastguard Worker // 37*d9f75844SAndroid Build Coastguard Worker // An RTP sender/receiver sends or receives media for a set of SSRCs. The media 38*d9f75844SAndroid Build Coastguard Worker // comes from an audio/video track that is attached to it. 39*d9f75844SAndroid Build Coastguard Worker // |[Voice/Video][Sender/Receiver]Info| has statistical information for a set of 40*d9f75844SAndroid Build Coastguard Worker // SSRCs. Looking at the RTP senders and receivers uncovers the track <-> info 41*d9f75844SAndroid Build Coastguard Worker // relationships, which this class does. 42*d9f75844SAndroid Build Coastguard Worker // 43*d9f75844SAndroid Build Coastguard Worker // In the spec, "track" attachment stats have been made obsolete, and in Unified 44*d9f75844SAndroid Build Coastguard Worker // Plan there is just one sender and one receiver per transceiver, so we may be 45*d9f75844SAndroid Build Coastguard Worker // able to simplify/delete this class. 46*d9f75844SAndroid Build Coastguard Worker // TODO(https://crbug.com/webrtc/14175): Simplify or delete this class when 47*d9f75844SAndroid Build Coastguard Worker // "track" stats have been deleted. 48*d9f75844SAndroid Build Coastguard Worker // TODO(https://crbug.com/webrtc/13528): Simplify or delete this class when 49*d9f75844SAndroid Build Coastguard Worker // Plan B is gone from the native library (already gone for Chrome). 50*d9f75844SAndroid Build Coastguard Worker class TrackMediaInfoMap { 51*d9f75844SAndroid Build Coastguard Worker public: 52*d9f75844SAndroid Build Coastguard Worker TrackMediaInfoMap(); 53*d9f75844SAndroid Build Coastguard Worker 54*d9f75844SAndroid Build Coastguard Worker // Takes ownership of the "infos". Does not affect the lifetime of the senders 55*d9f75844SAndroid Build Coastguard Worker // or receivers, but TrackMediaInfoMap will keep their associated tracks alive 56*d9f75844SAndroid Build Coastguard Worker // through reference counting until the map is destroyed. 57*d9f75844SAndroid Build Coastguard Worker void Initialize( 58*d9f75844SAndroid Build Coastguard Worker absl::optional<cricket::VoiceMediaInfo> voice_media_info, 59*d9f75844SAndroid Build Coastguard Worker absl::optional<cricket::VideoMediaInfo> video_media_info, 60*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<rtc::scoped_refptr<RtpSenderInternal>> rtp_senders, 61*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<rtc::scoped_refptr<RtpReceiverInternal>> rtp_receivers); 62*d9f75844SAndroid Build Coastguard Worker voice_media_info()63*d9f75844SAndroid Build Coastguard Worker const absl::optional<cricket::VoiceMediaInfo>& voice_media_info() const { 64*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(is_initialized_); 65*d9f75844SAndroid Build Coastguard Worker return voice_media_info_; 66*d9f75844SAndroid Build Coastguard Worker } video_media_info()67*d9f75844SAndroid Build Coastguard Worker const absl::optional<cricket::VideoMediaInfo>& video_media_info() const { 68*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(is_initialized_); 69*d9f75844SAndroid Build Coastguard Worker return video_media_info_; 70*d9f75844SAndroid Build Coastguard Worker } 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard Worker const std::vector<cricket::VoiceSenderInfo*>* GetVoiceSenderInfos( 73*d9f75844SAndroid Build Coastguard Worker const AudioTrackInterface& local_audio_track) const; 74*d9f75844SAndroid Build Coastguard Worker const cricket::VoiceReceiverInfo* GetVoiceReceiverInfo( 75*d9f75844SAndroid Build Coastguard Worker const AudioTrackInterface& remote_audio_track) const; 76*d9f75844SAndroid Build Coastguard Worker const std::vector<cricket::VideoSenderInfo*>* GetVideoSenderInfos( 77*d9f75844SAndroid Build Coastguard Worker const VideoTrackInterface& local_video_track) const; 78*d9f75844SAndroid Build Coastguard Worker const cricket::VideoReceiverInfo* GetVideoReceiverInfo( 79*d9f75844SAndroid Build Coastguard Worker const VideoTrackInterface& remote_video_track) const; 80*d9f75844SAndroid Build Coastguard Worker 81*d9f75844SAndroid Build Coastguard Worker const cricket::VoiceSenderInfo* GetVoiceSenderInfoBySsrc(uint32_t ssrc) const; 82*d9f75844SAndroid Build Coastguard Worker const cricket::VoiceReceiverInfo* GetVoiceReceiverInfoBySsrc( 83*d9f75844SAndroid Build Coastguard Worker uint32_t ssrc) const; 84*d9f75844SAndroid Build Coastguard Worker const cricket::VideoSenderInfo* GetVideoSenderInfoBySsrc(uint32_t ssrc) const; 85*d9f75844SAndroid Build Coastguard Worker const cricket::VideoReceiverInfo* GetVideoReceiverInfoBySsrc( 86*d9f75844SAndroid Build Coastguard Worker uint32_t ssrc) const; 87*d9f75844SAndroid Build Coastguard Worker 88*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack( 89*d9f75844SAndroid Build Coastguard Worker const cricket::VoiceSenderInfo& voice_sender_info) const; 90*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack( 91*d9f75844SAndroid Build Coastguard Worker const cricket::VoiceReceiverInfo& voice_receiver_info) const; 92*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack( 93*d9f75844SAndroid Build Coastguard Worker const cricket::VideoSenderInfo& video_sender_info) const; 94*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack( 95*d9f75844SAndroid Build Coastguard Worker const cricket::VideoReceiverInfo& video_receiver_info) const; 96*d9f75844SAndroid Build Coastguard Worker 97*d9f75844SAndroid Build Coastguard Worker // TODO(hta): Remove this function, and redesign the callers not to need it. 98*d9f75844SAndroid Build Coastguard Worker // It is not going to work if a track is attached multiple times, and 99*d9f75844SAndroid Build Coastguard Worker // it is not going to work if a received track is attached as a sending 100*d9f75844SAndroid Build Coastguard Worker // track (loopback). 101*d9f75844SAndroid Build Coastguard Worker absl::optional<int> GetAttachmentIdByTrack( 102*d9f75844SAndroid Build Coastguard Worker const MediaStreamTrackInterface* track) const; 103*d9f75844SAndroid Build Coastguard Worker 104*d9f75844SAndroid Build Coastguard Worker private: 105*d9f75844SAndroid Build Coastguard Worker bool is_initialized_ = false; 106*d9f75844SAndroid Build Coastguard Worker absl::optional<cricket::VoiceMediaInfo> voice_media_info_; 107*d9f75844SAndroid Build Coastguard Worker absl::optional<cricket::VideoMediaInfo> video_media_info_; 108*d9f75844SAndroid Build Coastguard Worker // These maps map tracks (identified by a pointer) to their corresponding info 109*d9f75844SAndroid Build Coastguard Worker // object of the correct kind. One track can map to multiple info objects. 110*d9f75844SAndroid Build Coastguard Worker // Known tracks are guaranteed to be alive because they are also stored as 111*d9f75844SAndroid Build Coastguard Worker // entries in the reverse maps below. 112*d9f75844SAndroid Build Coastguard Worker std::map<const AudioTrackInterface*, std::vector<cricket::VoiceSenderInfo*>> 113*d9f75844SAndroid Build Coastguard Worker voice_infos_by_local_track_; 114*d9f75844SAndroid Build Coastguard Worker std::map<const AudioTrackInterface*, cricket::VoiceReceiverInfo*> 115*d9f75844SAndroid Build Coastguard Worker voice_info_by_remote_track_; 116*d9f75844SAndroid Build Coastguard Worker std::map<const VideoTrackInterface*, std::vector<cricket::VideoSenderInfo*>> 117*d9f75844SAndroid Build Coastguard Worker video_infos_by_local_track_; 118*d9f75844SAndroid Build Coastguard Worker std::map<const VideoTrackInterface*, cricket::VideoReceiverInfo*> 119*d9f75844SAndroid Build Coastguard Worker video_info_by_remote_track_; 120*d9f75844SAndroid Build Coastguard Worker // These maps map info objects to their corresponding tracks. They are always 121*d9f75844SAndroid Build Coastguard Worker // the inverse of the maps above. One info object always maps to only one 122*d9f75844SAndroid Build Coastguard Worker // track. The use of scoped_refptr<> here ensures the tracks outlive 123*d9f75844SAndroid Build Coastguard Worker // TrackMediaInfoMap. 124*d9f75844SAndroid Build Coastguard Worker std::map<const cricket::VoiceSenderInfo*, 125*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface>> 126*d9f75844SAndroid Build Coastguard Worker audio_track_by_sender_info_; 127*d9f75844SAndroid Build Coastguard Worker std::map<const cricket::VoiceReceiverInfo*, 128*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface>> 129*d9f75844SAndroid Build Coastguard Worker audio_track_by_receiver_info_; 130*d9f75844SAndroid Build Coastguard Worker std::map<const cricket::VideoSenderInfo*, 131*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface>> 132*d9f75844SAndroid Build Coastguard Worker video_track_by_sender_info_; 133*d9f75844SAndroid Build Coastguard Worker std::map<const cricket::VideoReceiverInfo*, 134*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface>> 135*d9f75844SAndroid Build Coastguard Worker video_track_by_receiver_info_; 136*d9f75844SAndroid Build Coastguard Worker // Map of tracks to attachment IDs. 137*d9f75844SAndroid Build Coastguard Worker // Necessary because senders and receivers live on the signaling thread, 138*d9f75844SAndroid Build Coastguard Worker // but the attachment IDs are needed while building stats on the networking 139*d9f75844SAndroid Build Coastguard Worker // thread, so we can't look them up in the senders/receivers without 140*d9f75844SAndroid Build Coastguard Worker // thread jumping. 141*d9f75844SAndroid Build Coastguard Worker std::map<const MediaStreamTrackInterface*, int> attachment_id_by_track_; 142*d9f75844SAndroid Build Coastguard Worker // These maps map SSRCs to the corresponding voice or video info objects. 143*d9f75844SAndroid Build Coastguard Worker std::map<uint32_t, cricket::VoiceSenderInfo*> voice_info_by_sender_ssrc_; 144*d9f75844SAndroid Build Coastguard Worker std::map<uint32_t, cricket::VoiceReceiverInfo*> voice_info_by_receiver_ssrc_; 145*d9f75844SAndroid Build Coastguard Worker std::map<uint32_t, cricket::VideoSenderInfo*> video_info_by_sender_ssrc_; 146*d9f75844SAndroid Build Coastguard Worker std::map<uint32_t, cricket::VideoReceiverInfo*> video_info_by_receiver_ssrc_; 147*d9f75844SAndroid Build Coastguard Worker }; 148*d9f75844SAndroid Build Coastguard Worker 149*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc 150*d9f75844SAndroid Build Coastguard Worker 151*d9f75844SAndroid Build Coastguard Worker #endif // PC_TRACK_MEDIA_INFO_MAP_H_ 152