blob: b661351e9fc985ed6d15621afe274de8ce1c47e7 [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"
zhihuang38ede132017-06-15 12:52:32 -070027// TODO(zhihuang): Remove unrelated headers once downstream applications stop
28// relying on them; they were previously transitively included by
29// mediachannel.h, which is no longer a dependency of this file.
Niels Möllerc6ce9c52018-05-11 11:15:30 +020030#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020031#include "api/video/video_source_interface.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010032#include "modules/audio_processing/include/audio_processing_statistics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/ratetracker.h"
34#include "rtc_base/refcount.h"
35#include "rtc_base/scoped_ref_ptr.h"
36#include "rtc_base/thread.h"
37#include "rtc_base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039namespace webrtc {
40
41// Generic observer interface.
42class ObserverInterface {
43 public:
44 virtual void OnChanged() = 0;
45
46 protected:
47 virtual ~ObserverInterface() {}
48};
49
50class NotifierInterface {
51 public:
52 virtual void RegisterObserver(ObserverInterface* observer) = 0;
53 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
54
55 virtual ~NotifierInterface() {}
56};
57
deadbeefb10f32f2017-02-08 01:38:21 -080058// Base class for sources. A MediaStreamTrack has an underlying source that
59// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 public NotifierInterface {
62 public:
Yves Gerey665174f2018-06-19 15:03:05 +020063 enum SourceState { kInitializing, kLive, kEnded, kMuted };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65 virtual SourceState state() const = 0;
66
tommi6eca7e32015-12-15 04:27:11 -080067 virtual bool remote() const = 0;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010070 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
deadbeefb10f32f2017-02-08 01:38:21 -080073// C++ version of MediaStreamTrack.
74// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public NotifierInterface {
77 public:
78 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070079 kLive,
80 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 };
82
deadbeeffac06552015-11-25 11:26:01 -080083 static const char kAudioKind[];
84 static const char kVideoKind[];
85
nissefcc640f2016-04-01 01:10:42 -070086 // The kind() method must return kAudioKind only if the object is a
87 // subclass of AudioTrackInterface, and kVideoKind only if the
88 // object is a subclass of VideoTrackInterface. It is typically used
89 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080091
92 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080094
95 // A disabled track will produce silence (if audio) or black frames (if
96 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000099
deadbeefb10f32f2017-02-08 01:38:21 -0800100 // Live or ended. A track will never be live again after becoming ended.
101 virtual TrackState state() const = 0;
102
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000103 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100104 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105};
106
deadbeefb10f32f2017-02-08 01:38:21 -0800107// VideoTrackSourceInterface is a reference counted source used for
108// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700109// VideoTrackSourceInterface is designed to be invoked on the signaling thread
110// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
111// on the worker thread via a VideoTrack. A custom implementation of a source
112// can inherit AdaptedVideoTrackSource instead of directly implementing this
113// interface.
Yves Gerey665174f2018-06-19 15:03:05 +0200114class VideoTrackSourceInterface : public MediaSourceInterface,
115 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100116 public:
nissefcc640f2016-04-01 01:10:42 -0700117 struct Stats {
118 // Original size of captured frame, before video adaptation.
119 int input_width;
120 int input_height;
121 };
perkja3ede6c2016-03-08 01:27:48 +0100122
perkj0d3eef22016-03-09 02:39:17 +0100123 // Indicates that parameters suitable for screencasts should be automatically
124 // applied to RtpSenders.
125 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800126 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100127 // implicit behavior.
128 virtual bool is_screencast() const = 0;
129
Perc0d31e92016-03-31 17:23:39 +0200130 // Indicates that the encoder should denoise video before encoding it.
131 // If it is not set, the default configuration is used which is different
132 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100133 // TODO(perkj): Remove this once denoising is done by the source, and not by
134 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200135 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100136
deadbeefb10f32f2017-02-08 01:38:21 -0800137 // Returns false if no stats are available, e.g, for a remote source, or a
138 // source which has not seen its first frame yet.
139 //
140 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700141 virtual bool GetStats(Stats* stats) = 0;
142
perkja3ede6c2016-03-08 01:27:48 +0100143 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100144 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 01:27:48 +0100145};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
perkj773be362017-07-31 23:22:01 -0700147// VideoTrackInterface is designed to be invoked on the signaling thread except
148// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
149// on the worker thread.
150// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
151// that ensures thread safety and that all methods are called on the right
152// thread.
Yves Gerey665174f2018-06-19 15:03:05 +0200153class VideoTrackInterface : public MediaStreamTrackInterface,
154 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 public:
pbos5214a0a2016-12-16 15:39:11 -0800156 // Video track content hint, used to override the source is_screencast
157 // property.
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200158 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
159 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 15:39:11 -0800160
mbonadei539d1042017-07-10 02:40:49 -0700161 // Register a video sink for this track. Used to connect the track to the
162 // underlying video engine.
163 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
164 const rtc::VideoSinkWants& wants) override {}
165 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
166
perkja3ede6c2016-03-08 01:27:48 +0100167 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100169 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 15:39:11 -0800170 virtual void set_content_hint(ContentHint hint) {}
171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100173 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174};
175
tommi6eca7e32015-12-15 04:27:11 -0800176// Interface for receiving audio data from a AudioTrack.
177class AudioTrackSinkInterface {
178 public:
179 virtual void OnData(const void* audio_data,
180 int bits_per_sample,
181 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800182 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800183 size_t number_of_frames) = 0;
184
185 protected:
186 virtual ~AudioTrackSinkInterface() {}
187};
188
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800190// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000192 public:
193 class AudioObserver {
194 public:
195 virtual void OnSetVolume(double volume) = 0;
196
197 protected:
198 virtual ~AudioObserver() {}
199 };
200
deadbeefb10f32f2017-02-08 01:38:21 -0800201 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
202 // implemented in chromium.
203
204 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100205 // TODO(tommi): This method should be on the track and ideally volume should
206 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000207 virtual void SetVolume(double volume) {}
208
deadbeefb10f32f2017-02-08 01:38:21 -0800209 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000210 virtual void RegisterAudioObserver(AudioObserver* observer) {}
211 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
tommi6eca7e32015-12-15 04:27:11 -0800213 // TODO(tommi): Make pure virtual.
214 virtual void AddSink(AudioTrackSinkInterface* sink) {}
215 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000216};
217
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000218// Interface of the audio processor used by the audio track to collect
219// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000221 public:
Ivo Creusenae026092017-11-20 13:07:16 +0100222 // Deprecated, use AudioProcessorStatistics instead.
223 // TODO(ivoc): Remove this when all implementations have switched to the new
224 // GetStats function. See b/67926135.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000225 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800226 AudioProcessorStats()
227 : typing_noise_detected(false),
228 echo_return_loss(0),
229 echo_return_loss_enhancement(0),
230 echo_delay_median_ms(0),
231 echo_delay_std_ms(0),
ivoc4e477a12017-01-15 08:29:46 -0800232 residual_echo_likelihood(0.0f),
233 residual_echo_likelihood_recent_max(0.0f),
234 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000235 ~AudioProcessorStats() {}
236
237 bool typing_noise_detected;
238 int echo_return_loss;
239 int echo_return_loss_enhancement;
240 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000241 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700242 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800243 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200244 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000245 };
Ivo Creusenae026092017-11-20 13:07:16 +0100246 // This struct maintains the optionality of the stats, and will replace the
247 // regular stats struct when all users have been updated.
248 struct AudioProcessorStatistics {
249 bool typing_noise_detected = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100250 AudioProcessingStats apm_statistics;
Ivo Creusenae026092017-11-20 13:07:16 +0100251 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000252
253 // Get audio processor statistics.
Ivo Creusen21eb9fc2017-12-12 10:45:51 +0100254 virtual void GetStats(AudioProcessorStats* stats);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000255
Ivo Creusenae026092017-11-20 13:07:16 +0100256 // Get audio processor statistics. The |has_remote_tracks| argument should be
257 // set if there are active remote tracks (this would usually be true during
258 // a call). If there are no remote tracks some of the stats will not be set by
259 // the AudioProcessor, because they only make sense if there is at least one
260 // remote track.
261 // TODO(ivoc): Make pure virtual when all implementions are updated.
262 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks);
263
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000264 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100265 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000266};
267
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268class AudioTrackInterface : public MediaStreamTrackInterface {
269 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800270 // TODO(deadbeef): Figure out if the following interface should be const or
271 // not.
Yves Gerey665174f2018-06-19 15:03:05 +0200272 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000274 // Add/Remove a sink that will receive the audio data from the track.
275 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
276 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000277
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000278 // Get the signal level from the audio track.
279 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800280 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
281 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100282 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000283
deadbeef8d60a942017-02-27 14:47:33 -0800284 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000285 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800286 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100287 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000288
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100290 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291};
292
Yves Gerey665174f2018-06-19 15:03:05 +0200293typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
294typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
deadbeefb10f32f2017-02-08 01:38:21 -0800296// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
297//
298// A major difference is that remote audio/video tracks (received by a
299// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
300// the same stream; a session description with the correct "a=msid" attributes
301// must be pushed down.
302//
303// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000304class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 public NotifierInterface {
306 public:
Seth Hampson13b8bad2018-03-13 16:05:28 -0700307 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308
309 virtual AudioTrackVector GetAudioTracks() = 0;
310 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200311 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
312 const std::string& track_id) = 0;
313 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
314 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
316 virtual bool AddTrack(AudioTrackInterface* track) = 0;
317 virtual bool AddTrack(VideoTrackInterface* track) = 0;
318 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
319 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
320
321 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100322 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323};
324
325} // namespace webrtc
326
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200327#endif // API_MEDIASTREAMINTERFACE_H_