blob: c8c6da2701c6f909f0b98e3d97d24b6c260ff2ea [file] [log] [blame]
hbos1f8239c2017-01-16 12:24:101/*
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 Anton10542f22019-01-11 17:11:0011#ifndef PC_TRACK_MEDIA_INFO_MAP_H_
12#define PC_TRACK_MEDIA_INFO_MAP_H_
hbos1f8239c2017-01-16 12:24:1013
Harald Alvestrand5761e7b2021-01-29 14:45:0814#include <stdint.h>
15
hbos1f8239c2017-01-16 12:24:1016#include <map>
17#include <memory>
Steve Anton5dfde182018-02-06 18:34:4018#include <string>
hbos1f8239c2017-01-16 12:24:1019#include <vector>
20
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0022#include "api/media_stream_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0024#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"
hbos1f8239c2017-01-16 12:24:1028
29namespace 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.
41class TrackMediaInfoMap {
42 public:
43 TrackMediaInfoMap(
44 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info,
45 std::unique_ptr<cricket::VideoMediaInfo> video_media_info,
Steve Anton57858b32018-02-15 23:19:5046 const std::vector<rtc::scoped_refptr<RtpSenderInternal>>& rtp_senders,
47 const std::vector<rtc::scoped_refptr<RtpReceiverInternal>>&
hbos1f8239c2017-01-16 12:24:1048 rtp_receivers);
49
50 const cricket::VoiceMediaInfo* voice_media_info() const {
51 return voice_media_info_.get();
52 }
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
Harald Alvestrand89061872018-01-02 13:08:3466 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
hbos1f8239c2017-01-16 12:24:1073 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
Harald Alvestrandc72af932018-01-11 16:18:1982 // 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).
Danil Chapovalov66cadcc2018-06-19 14:47:4386 absl::optional<int> GetAttachmentIdByTrack(
Harald Alvestrandc72af932018-01-11 16:18:1987 const MediaStreamTrackInterface* track) const;
88
hbos1f8239c2017-01-16 12:24:1089 private:
Danil Chapovalov66cadcc2018-06-19 14:47:4390 absl::optional<std::string> voice_mid_;
91 absl::optional<std::string> video_mid_;
hbos1f8239c2017-01-16 12:24:1092 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*,
deadbeef804c1af2017-02-12 03:07:31108 rtc::scoped_refptr<AudioTrackInterface>>
109 audio_track_by_sender_info_;
hbos1f8239c2017-01-16 12:24:10110 std::map<const cricket::VoiceReceiverInfo*,
deadbeef804c1af2017-02-12 03:07:31111 rtc::scoped_refptr<AudioTrackInterface>>
112 audio_track_by_receiver_info_;
hbos1f8239c2017-01-16 12:24:10113 std::map<const cricket::VideoSenderInfo*,
deadbeef804c1af2017-02-12 03:07:31114 rtc::scoped_refptr<VideoTrackInterface>>
115 video_track_by_sender_info_;
hbos1f8239c2017-01-16 12:24:10116 std::map<const cricket::VideoReceiverInfo*,
deadbeef804c1af2017-02-12 03:07:31117 rtc::scoped_refptr<VideoTrackInterface>>
118 video_track_by_receiver_info_;
Harald Alvestrandc72af932018-01-11 16:18:19119 // 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_;
Harald Alvestrand89061872018-01-02 13:08:34125 // 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_;
hbos1f8239c2017-01-16 12:24:10130};
131
132} // namespace webrtc
133
Steve Anton10542f22019-01-11 17:11:00134#endif // PC_TRACK_MEDIA_INFO_MAP_H_