blob: ccacb4ca56a2ac42653721f1295805b9240e1356 [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
Steve Anton10542f22019-01-11 09:11:00 -080017#ifndef API_MEDIA_STREAM_INTERFACE_H_
18#define API_MEDIA_STREAM_INTERFACE_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"
Piotr (Peter) Slatala95ca6e12018-11-13 07:57:07 -080026#include "api/audio_options.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010027#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020029#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020030#include "api/video/video_source_interface.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010031#include "modules/audio_processing/include/audio_processing_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/ref_count.h"
Mirko Bonadei66e76792019-04-02 11:33:59 +020033#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035namespace webrtc {
36
37// Generic observer interface.
38class ObserverInterface {
39 public:
40 virtual void OnChanged() = 0;
41
42 protected:
43 virtual ~ObserverInterface() {}
44};
45
46class NotifierInterface {
47 public:
48 virtual void RegisterObserver(ObserverInterface* observer) = 0;
49 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
50
51 virtual ~NotifierInterface() {}
52};
53
deadbeefb10f32f2017-02-08 01:38:21 -080054// Base class for sources. A MediaStreamTrack has an underlying source that
55// provides media. A source can be shared by multiple tracks.
Mirko Bonadei66e76792019-04-02 11:33:59 +020056class RTC_EXPORT MediaSourceInterface : public rtc::RefCountInterface,
57 public NotifierInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 public:
Yves Gerey665174f2018-06-19 15:03:05 +020059 enum SourceState { kInitializing, kLive, kEnded, kMuted };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
61 virtual SourceState state() const = 0;
62
tommi6eca7e32015-12-15 04:27:11 -080063 virtual bool remote() const = 0;
64
Ruslan Burakov493a6502019-02-27 15:32:48 +010065 // Sets the minimum latency of the remote source until audio playout. Actual
66 // observered latency may differ depending on the source. |latency| is in the
67 // range of [0.0, 10.0] seconds.
68 // TODO(kuddai) make pure virtual once not only remote tracks support latency.
69 virtual void SetLatency(double latency) {}
70 virtual double GetLatency() const;
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010073 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074};
75
deadbeefb10f32f2017-02-08 01:38:21 -080076// C++ version of MediaStreamTrack.
77// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
Mirko Bonadei66e76792019-04-02 11:33:59 +020078class RTC_EXPORT MediaStreamTrackInterface : public rtc::RefCountInterface,
79 public NotifierInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 public:
81 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070082 kLive,
83 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 };
85
deadbeeffac06552015-11-25 11:26:01 -080086 static const char kAudioKind[];
87 static const char kVideoKind[];
88
nissefcc640f2016-04-01 01:10:42 -070089 // The kind() method must return kAudioKind only if the object is a
90 // subclass of AudioTrackInterface, and kVideoKind only if the
91 // object is a subclass of VideoTrackInterface. It is typically used
92 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080094
95 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080097
98 // A disabled track will produce silence (if audio) or black frames (if
99 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000102
deadbeefb10f32f2017-02-08 01:38:21 -0800103 // Live or ended. A track will never be live again after becoming ended.
104 virtual TrackState state() const = 0;
105
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000106 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100107 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
deadbeefb10f32f2017-02-08 01:38:21 -0800110// VideoTrackSourceInterface is a reference counted source used for
111// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700112// VideoTrackSourceInterface is designed to be invoked on the signaling thread
113// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
114// on the worker thread via a VideoTrack. A custom implementation of a source
115// can inherit AdaptedVideoTrackSource instead of directly implementing this
116// interface.
Yves Gerey665174f2018-06-19 15:03:05 +0200117class VideoTrackSourceInterface : public MediaSourceInterface,
118 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100119 public:
nissefcc640f2016-04-01 01:10:42 -0700120 struct Stats {
121 // Original size of captured frame, before video adaptation.
122 int input_width;
123 int input_height;
124 };
perkja3ede6c2016-03-08 01:27:48 +0100125
perkj0d3eef22016-03-09 02:39:17 +0100126 // Indicates that parameters suitable for screencasts should be automatically
127 // applied to RtpSenders.
128 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800129 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100130 // implicit behavior.
131 virtual bool is_screencast() const = 0;
132
Perc0d31e92016-03-31 17:23:39 +0200133 // Indicates that the encoder should denoise video before encoding it.
134 // If it is not set, the default configuration is used which is different
135 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100136 // TODO(perkj): Remove this once denoising is done by the source, and not by
137 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200138 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100139
deadbeefb10f32f2017-02-08 01:38:21 -0800140 // Returns false if no stats are available, e.g, for a remote source, or a
141 // source which has not seen its first frame yet.
142 //
143 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700144 virtual bool GetStats(Stats* stats) = 0;
145
perkja3ede6c2016-03-08 01:27:48 +0100146 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100147 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 01:27:48 +0100148};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
perkj773be362017-07-31 23:22:01 -0700150// VideoTrackInterface is designed to be invoked on the signaling thread except
151// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
152// on the worker thread.
153// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
154// that ensures thread safety and that all methods are called on the right
155// thread.
Yves Gerey665174f2018-06-19 15:03:05 +0200156class VideoTrackInterface : public MediaStreamTrackInterface,
157 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 public:
pbos5214a0a2016-12-16 15:39:11 -0800159 // Video track content hint, used to override the source is_screencast
160 // property.
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200161 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
162 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 15:39:11 -0800163
mbonadei539d1042017-07-10 02:40:49 -0700164 // Register a video sink for this track. Used to connect the track to the
165 // underlying video engine.
166 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
167 const rtc::VideoSinkWants& wants) override {}
168 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
169
perkja3ede6c2016-03-08 01:27:48 +0100170 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100172 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 15:39:11 -0800173 virtual void set_content_hint(ContentHint hint) {}
174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100176 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177};
178
tommi6eca7e32015-12-15 04:27:11 -0800179// Interface for receiving audio data from a AudioTrack.
180class AudioTrackSinkInterface {
181 public:
182 virtual void OnData(const void* audio_data,
183 int bits_per_sample,
184 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800185 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800186 size_t number_of_frames) = 0;
187
188 protected:
189 virtual ~AudioTrackSinkInterface() {}
190};
191
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800193// The same source can be used by multiple AudioTracks.
Mirko Bonadei66e76792019-04-02 11:33:59 +0200194class RTC_EXPORT AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000195 public:
196 class AudioObserver {
197 public:
198 virtual void OnSetVolume(double volume) = 0;
199
200 protected:
201 virtual ~AudioObserver() {}
202 };
203
deadbeefb10f32f2017-02-08 01:38:21 -0800204 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
205 // implemented in chromium.
206
207 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100208 // TODO(tommi): This method should be on the track and ideally volume should
209 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000210 virtual void SetVolume(double volume) {}
211
deadbeefb10f32f2017-02-08 01:38:21 -0800212 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000213 virtual void RegisterAudioObserver(AudioObserver* observer) {}
214 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
tommi6eca7e32015-12-15 04:27:11 -0800216 // TODO(tommi): Make pure virtual.
217 virtual void AddSink(AudioTrackSinkInterface* sink) {}
218 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
Piotr (Peter) Slatala95ca6e12018-11-13 07:57:07 -0800219
220 // Returns options for the AudioSource.
221 // (for some of the settings this approach is broken, e.g. setting
222 // audio network adaptation on the source is the wrong layer of abstraction).
223 virtual const cricket::AudioOptions options() const;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000224};
225
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000226// Interface of the audio processor used by the audio track to collect
227// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000228class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000229 public:
Ivo Creusenae026092017-11-20 13:07:16 +0100230 struct AudioProcessorStatistics {
231 bool typing_noise_detected = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100232 AudioProcessingStats apm_statistics;
Ivo Creusenae026092017-11-20 13:07:16 +0100233 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000234
Ivo Creusenae026092017-11-20 13:07:16 +0100235 // Get audio processor statistics. The |has_remote_tracks| argument should be
236 // set if there are active remote tracks (this would usually be true during
237 // a call). If there are no remote tracks some of the stats will not be set by
238 // the AudioProcessor, because they only make sense if there is at least one
239 // remote track.
Sam Zackrisson28127632018-11-01 11:37:15 +0100240 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
Ivo Creusenae026092017-11-20 13:07:16 +0100241
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000242 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100243 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000244};
245
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246class AudioTrackInterface : public MediaStreamTrackInterface {
247 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800248 // TODO(deadbeef): Figure out if the following interface should be const or
249 // not.
Yves Gerey665174f2018-06-19 15:03:05 +0200250 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000252 // Add/Remove a sink that will receive the audio data from the track.
253 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
254 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000255
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000256 // Get the signal level from the audio track.
257 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800258 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
259 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100260 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000261
deadbeef8d60a942017-02-27 14:47:33 -0800262 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000263 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800264 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100265 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000266
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100268 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269};
270
Yves Gerey665174f2018-06-19 15:03:05 +0200271typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
272typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
deadbeefb10f32f2017-02-08 01:38:21 -0800274// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
275//
276// A major difference is that remote audio/video tracks (received by a
277// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
278// the same stream; a session description with the correct "a=msid" attributes
279// must be pushed down.
280//
281// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 public NotifierInterface {
284 public:
Seth Hampson13b8bad2018-03-13 16:05:28 -0700285 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
287 virtual AudioTrackVector GetAudioTracks() = 0;
288 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200289 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
290 const std::string& track_id) = 0;
291 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
292 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293
294 virtual bool AddTrack(AudioTrackInterface* track) = 0;
295 virtual bool AddTrack(VideoTrackInterface* track) = 0;
296 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
297 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
298
299 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100300 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301};
302
303} // namespace webrtc
304
Steve Anton10542f22019-01-11 09:11:00 -0800305#endif // API_MEDIA_STREAM_INTERFACE_H_