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 #ifndef PC_TRACK_MEDIA_INFO_MAP_H_
12 #define PC_TRACK_MEDIA_INFO_MAP_H_
13 
14 #include <stdint.h>
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "absl/types/optional.h"
22 #include "api/media_stream_interface.h"
23 #include "api/scoped_refptr.h"
24 #include "media/base/media_channel.h"
25 #include "pc/rtp_receiver.h"
26 #include "pc/rtp_sender.h"
27 #include "rtc_base/ref_count.h"
28 
29 namespace webrtc {
30 
31 // Audio/video tracks and sender/receiver statistical information are associated
32 // with each other based on attachments to RTP senders/receivers. This class
33 // maps that relationship, in both directions, so that stats about a track can
34 // be retrieved on a per-attachment basis.
35 //
36 // An RTP sender/receiver sends or receives media for a set of SSRCs. The media
37 // comes from an audio/video track that is attached to it.
38 // |[Voice/Video][Sender/Receiver]Info| has statistical information for a set of
39 // SSRCs. Looking at the RTP senders and receivers uncovers the track <-> info
40 // relationships, which this class does.
41 class TrackMediaInfoMap {
42  public:
43   TrackMediaInfoMap(
44       std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,
45       std::unique_ptr<cricket::VideoMediaInfo> video_media_info,
46       const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders,
47       const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>&
48           rtp_receivers);
49 
voice_media_info()50   const cricket::VoiceMediaInfo* voice_media_info() const {
51     return voice_media_info_.get();
52   }
video_media_info()53   const cricket::VideoMediaInfo* video_media_info() const {
54     return video_media_info_.get();
55   }
56 
57   const std::vector<cricket::VoiceSenderInfo*>* GetVoiceSenderInfos(
58       const AudioTrackInterface& local_audio_track) const;
59   const cricket::VoiceReceiverInfo* GetVoiceReceiverInfo(
60       const AudioTrackInterface& remote_audio_track) const;
61   const std::vector<cricket::VideoSenderInfo*>* GetVideoSenderInfos(
62       const VideoTrackInterface& local_video_track) const;
63   const cricket::VideoReceiverInfo* GetVideoReceiverInfo(
64       const VideoTrackInterface& remote_video_track) const;
65 
66   const cricket::VoiceSenderInfo* GetVoiceSenderInfoBySsrc(uint32_t ssrc) const;
67   const cricket::VoiceReceiverInfo* GetVoiceReceiverInfoBySsrc(
68       uint32_t ssrc) const;
69   const cricket::VideoSenderInfo* GetVideoSenderInfoBySsrc(uint32_t ssrc) const;
70   const cricket::VideoReceiverInfo* GetVideoReceiverInfoBySsrc(
71       uint32_t ssrc) const;
72 
73   rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
74       const cricket::VoiceSenderInfo& voice_sender_info) const;
75   rtc::scoped_refptr<AudioTrackInterface> GetAudioTrack(
76       const cricket::VoiceReceiverInfo& voice_receiver_info) const;
77   rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
78       const cricket::VideoSenderInfo& video_sender_info) const;
79   rtc::scoped_refptr<VideoTrackInterface> GetVideoTrack(
80       const cricket::VideoReceiverInfo& video_receiver_info) const;
81 
82   // TODO(hta): Remove this function, and redesign the callers not to need it.
83   // It is not going to work if a track is attached multiple times, and
84   // it is not going to work if a received track is attached as a sending
85   // track (loopback).
86   absl::optional<int> GetAttachmentIdByTrack(
87       const MediaStreamTrackInterface* track) const;
88 
89  private:
90   absl::optional<std::string> voice_mid_;
91   absl::optional<std::string> video_mid_;
92   std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info_;
93   std::unique_ptr<cricket::VideoMediaInfo> video_media_info_;
94   // These maps map tracks (identified by a pointer) to their corresponding info
95   // object of the correct kind. One track can map to multiple info objects.
96   std::map<const AudioTrackInterface*, std::vector<cricket::VoiceSenderInfo*>>
97       voice_infos_by_local_track_;
98   std::map<const AudioTrackInterface*, cricket::VoiceReceiverInfo*>
99       voice_info_by_remote_track_;
100   std::map<const VideoTrackInterface*, std::vector<cricket::VideoSenderInfo*>>
101       video_infos_by_local_track_;
102   std::map<const VideoTrackInterface*, cricket::VideoReceiverInfo*>
103       video_info_by_remote_track_;
104   // These maps map info objects to their corresponding tracks. They are always
105   // the inverse of the maps above. One info object always maps to only one
106   // track.
107   std::map<const cricket::VoiceSenderInfo*,
108            rtc::scoped_refptr<AudioTrackInterface>>
109       audio_track_by_sender_info_;
110   std::map<const cricket::VoiceReceiverInfo*,
111            rtc::scoped_refptr<AudioTrackInterface>>
112       audio_track_by_receiver_info_;
113   std::map<const cricket::VideoSenderInfo*,
114            rtc::scoped_refptr<VideoTrackInterface>>
115       video_track_by_sender_info_;
116   std::map<const cricket::VideoReceiverInfo*,
117            rtc::scoped_refptr<VideoTrackInterface>>
118       video_track_by_receiver_info_;
119   // Map of tracks to attachment IDs.
120   // Necessary because senders and receivers live on the signaling thread,
121   // but the attachment IDs are needed while building stats on the networking
122   // thread, so we can't look them up in the senders/receivers without
123   // thread jumping.
124   std::map<const MediaStreamTrackInterface*, int> attachment_id_by_track_;
125   // These maps map SSRCs to the corresponding voice or video info objects.
126   std::map<uint32_t, cricket::VoiceSenderInfo*> voice_info_by_sender_ssrc_;
127   std::map<uint32_t, cricket::VoiceReceiverInfo*> voice_info_by_receiver_ssrc_;
128   std::map<uint32_t, cricket::VideoSenderInfo*> video_info_by_sender_ssrc_;
129   std::map<uint32_t, cricket::VideoReceiverInfo*> video_info_by_receiver_ssrc_;
130 };
131 
132 }  // namespace webrtc
133 
134 #endif  // PC_TRACK_MEDIA_INFO_MAP_H_
135