blob: 2eb2f31d4f6182fe0ab103140b097ad79f06fa06 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains interfaces for MediaStream, MediaTrack and MediaSource.
12// These interfaces are used for implementing MediaStream and MediaTrack as
13// defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
14// interfaces must be used only with PeerConnection. PeerConnectionManager
15// interface provides the factory methods to create MediaStream and MediaTracks.
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#ifndef API_MEDIASTREAMINTERFACE_H_
18#define API_MEDIASTREAMINTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
pbos9baddf22017-01-02 06:44:41 -080020#include <stddef.h>
21
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022#include <string>
23#include <vector>
24
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020025#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020027#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020028#include "api/video/video_source_interface.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010029#include "modules/audio_processing/include/audio_processing_statistics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/refcount.h"
31#include "rtc_base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033namespace webrtc {
34
35// Generic observer interface.
36class ObserverInterface {
37 public:
38 virtual void OnChanged() = 0;
39
40 protected:
41 virtual ~ObserverInterface() {}
42};
43
44class NotifierInterface {
45 public:
46 virtual void RegisterObserver(ObserverInterface* observer) = 0;
47 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
48
49 virtual ~NotifierInterface() {}
50};
51
deadbeefb10f32f2017-02-08 01:38:21 -080052// Base class for sources. A MediaStreamTrack has an underlying source that
53// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 public NotifierInterface {
56 public:
Yves Gerey665174f2018-06-19 15:03:05 +020057 enum SourceState { kInitializing, kLive, kEnded, kMuted };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59 virtual SourceState state() const = 0;
60
tommi6eca7e32015-12-15 04:27:11 -080061 virtual bool remote() const = 0;
62
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010064 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065};
66
deadbeefb10f32f2017-02-08 01:38:21 -080067// C++ version of MediaStreamTrack.
68// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 public NotifierInterface {
71 public:
72 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070073 kLive,
74 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 };
76
deadbeeffac06552015-11-25 11:26:01 -080077 static const char kAudioKind[];
78 static const char kVideoKind[];
79
nissefcc640f2016-04-01 01:10:42 -070080 // The kind() method must return kAudioKind only if the object is a
81 // subclass of AudioTrackInterface, and kVideoKind only if the
82 // object is a subclass of VideoTrackInterface. It is typically used
83 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080085
86 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080088
89 // A disabled track will produce silence (if audio) or black frames (if
90 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000093
deadbeefb10f32f2017-02-08 01:38:21 -080094 // Live or ended. A track will never be live again after becoming ended.
95 virtual TrackState state() const = 0;
96
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000097 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010098 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099};
100
deadbeefb10f32f2017-02-08 01:38:21 -0800101// VideoTrackSourceInterface is a reference counted source used for
102// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700103// VideoTrackSourceInterface is designed to be invoked on the signaling thread
104// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
105// on the worker thread via a VideoTrack. A custom implementation of a source
106// can inherit AdaptedVideoTrackSource instead of directly implementing this
107// interface.
Yves Gerey665174f2018-06-19 15:03:05 +0200108class VideoTrackSourceInterface : public MediaSourceInterface,
109 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100110 public:
nissefcc640f2016-04-01 01:10:42 -0700111 struct Stats {
112 // Original size of captured frame, before video adaptation.
113 int input_width;
114 int input_height;
115 };
perkja3ede6c2016-03-08 01:27:48 +0100116
perkj0d3eef22016-03-09 02:39:17 +0100117 // Indicates that parameters suitable for screencasts should be automatically
118 // applied to RtpSenders.
119 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800120 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100121 // implicit behavior.
122 virtual bool is_screencast() const = 0;
123
Perc0d31e92016-03-31 17:23:39 +0200124 // Indicates that the encoder should denoise video before encoding it.
125 // If it is not set, the default configuration is used which is different
126 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100127 // TODO(perkj): Remove this once denoising is done by the source, and not by
128 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200129 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100130
deadbeefb10f32f2017-02-08 01:38:21 -0800131 // Returns false if no stats are available, e.g, for a remote source, or a
132 // source which has not seen its first frame yet.
133 //
134 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700135 virtual bool GetStats(Stats* stats) = 0;
136
perkja3ede6c2016-03-08 01:27:48 +0100137 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100138 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 01:27:48 +0100139};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
perkj773be362017-07-31 23:22:01 -0700141// VideoTrackInterface is designed to be invoked on the signaling thread except
142// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
143// on the worker thread.
144// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
145// that ensures thread safety and that all methods are called on the right
146// thread.
Yves Gerey665174f2018-06-19 15:03:05 +0200147class VideoTrackInterface : public MediaStreamTrackInterface,
148 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 public:
pbos5214a0a2016-12-16 15:39:11 -0800150 // Video track content hint, used to override the source is_screencast
151 // property.
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200152 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
153 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 15:39:11 -0800154
mbonadei539d1042017-07-10 02:40:49 -0700155 // Register a video sink for this track. Used to connect the track to the
156 // underlying video engine.
157 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
158 const rtc::VideoSinkWants& wants) override {}
159 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
160
perkja3ede6c2016-03-08 01:27:48 +0100161 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100163 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 15:39:11 -0800164 virtual void set_content_hint(ContentHint hint) {}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100167 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168};
169
tommi6eca7e32015-12-15 04:27:11 -0800170// Interface for receiving audio data from a AudioTrack.
171class AudioTrackSinkInterface {
172 public:
173 virtual void OnData(const void* audio_data,
174 int bits_per_sample,
175 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800176 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800177 size_t number_of_frames) = 0;
178
179 protected:
180 virtual ~AudioTrackSinkInterface() {}
181};
182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800184// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000186 public:
187 class AudioObserver {
188 public:
189 virtual void OnSetVolume(double volume) = 0;
190
191 protected:
192 virtual ~AudioObserver() {}
193 };
194
deadbeefb10f32f2017-02-08 01:38:21 -0800195 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
196 // implemented in chromium.
197
198 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100199 // TODO(tommi): This method should be on the track and ideally volume should
200 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000201 virtual void SetVolume(double volume) {}
202
deadbeefb10f32f2017-02-08 01:38:21 -0800203 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000204 virtual void RegisterAudioObserver(AudioObserver* observer) {}
205 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
tommi6eca7e32015-12-15 04:27:11 -0800207 // TODO(tommi): Make pure virtual.
208 virtual void AddSink(AudioTrackSinkInterface* sink) {}
209 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000210};
211
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000212// Interface of the audio processor used by the audio track to collect
213// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000215 public:
Ivo Creusenae026092017-11-20 13:07:16 +0100216 // Deprecated, use AudioProcessorStatistics instead.
217 // TODO(ivoc): Remove this when all implementations have switched to the new
218 // GetStats function. See b/67926135.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000219 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800220 AudioProcessorStats()
221 : typing_noise_detected(false),
222 echo_return_loss(0),
223 echo_return_loss_enhancement(0),
224 echo_delay_median_ms(0),
225 echo_delay_std_ms(0),
ivoc4e477a12017-01-15 08:29:46 -0800226 residual_echo_likelihood(0.0f),
227 residual_echo_likelihood_recent_max(0.0f),
228 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000229 ~AudioProcessorStats() {}
230
231 bool typing_noise_detected;
232 int echo_return_loss;
233 int echo_return_loss_enhancement;
234 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000235 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700236 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800237 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200238 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000239 };
Ivo Creusenae026092017-11-20 13:07:16 +0100240 // This struct maintains the optionality of the stats, and will replace the
241 // regular stats struct when all users have been updated.
242 struct AudioProcessorStatistics {
243 bool typing_noise_detected = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100244 AudioProcessingStats apm_statistics;
Ivo Creusenae026092017-11-20 13:07:16 +0100245 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000246
247 // Get audio processor statistics.
Ivo Creusen21eb9fc2017-12-12 10:45:51 +0100248 virtual void GetStats(AudioProcessorStats* stats);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000249
Ivo Creusenae026092017-11-20 13:07:16 +0100250 // Get audio processor statistics. The |has_remote_tracks| argument should be
251 // set if there are active remote tracks (this would usually be true during
252 // a call). If there are no remote tracks some of the stats will not be set by
253 // the AudioProcessor, because they only make sense if there is at least one
254 // remote track.
255 // TODO(ivoc): Make pure virtual when all implementions are updated.
256 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks);
257
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000258 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100259 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000260};
261
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262class AudioTrackInterface : public MediaStreamTrackInterface {
263 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800264 // TODO(deadbeef): Figure out if the following interface should be const or
265 // not.
Yves Gerey665174f2018-06-19 15:03:05 +0200266 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000268 // Add/Remove a sink that will receive the audio data from the track.
269 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
270 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000271
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000272 // Get the signal level from the audio track.
273 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800274 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
275 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100276 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000277
deadbeef8d60a942017-02-27 14:47:33 -0800278 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000279 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800280 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100281 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100284 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285};
286
Yves Gerey665174f2018-06-19 15:03:05 +0200287typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
288typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289
deadbeefb10f32f2017-02-08 01:38:21 -0800290// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
291//
292// A major difference is that remote audio/video tracks (received by a
293// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
294// the same stream; a session description with the correct "a=msid" attributes
295// must be pushed down.
296//
297// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 public NotifierInterface {
300 public:
Seth Hampson13b8bad2018-03-13 16:05:28 -0700301 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
303 virtual AudioTrackVector GetAudioTracks() = 0;
304 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200305 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
306 const std::string& track_id) = 0;
307 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
308 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309
310 virtual bool AddTrack(AudioTrackInterface* track) = 0;
311 virtual bool AddTrack(VideoTrackInterface* track) = 0;
312 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
313 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
314
315 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100316 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317};
318
319} // namespace webrtc
320
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200321#endif // API_MEDIASTREAMINTERFACE_H_