blob: 79d463d788fac25c074bcce65385731792b6bede [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
Niels Möllere942b142019-09-17 14:30:41 +020014// interfaces must be used only with PeerConnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
Steve Anton10542f22019-01-11 09:11:00 -080016#ifndef API_MEDIA_STREAM_INTERFACE_H_
17#define API_MEDIA_STREAM_INTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
pbos9baddf22017-01-02 06:44:41 -080019#include <stddef.h>
20
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <string>
22#include <vector>
23
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020024#include "absl/types/optional.h"
Piotr (Peter) Slatala95ca6e12018-11-13 07:57:07 -080025#include "api/audio_options.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010026#include "api/scoped_refptr.h"
Markus Handell9982efa2019-11-21 11:56:50 +010027#include "api/video/recordable_encoded_frame.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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010066 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067};
68
deadbeefb10f32f2017-02-08 01:38:21 -080069// C++ version of MediaStreamTrack.
70// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
Mirko Bonadei66e76792019-04-02 11:33:59 +020071class RTC_EXPORT MediaStreamTrackInterface : public rtc::RefCountInterface,
72 public NotifierInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 public:
74 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070075 kLive,
76 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 };
78
Niels Möller6dcd4dc2019-08-26 10:45:28 +020079 static const char* const kAudioKind;
80 static const char* const kVideoKind;
deadbeeffac06552015-11-25 11:26:01 -080081
nissefcc640f2016-04-01 01:10:42 -070082 // The kind() method must return kAudioKind only if the object is a
83 // subclass of AudioTrackInterface, and kVideoKind only if the
84 // object is a subclass of VideoTrackInterface. It is typically used
85 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080087
88 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080090
91 // A disabled track will produce silence (if audio) or black frames (if
92 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000095
deadbeefb10f32f2017-02-08 01:38:21 -080096 // Live or ended. A track will never be live again after becoming ended.
97 virtual TrackState state() const = 0;
98
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000099 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100100 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101};
102
deadbeefb10f32f2017-02-08 01:38:21 -0800103// VideoTrackSourceInterface is a reference counted source used for
104// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700105// VideoTrackSourceInterface is designed to be invoked on the signaling thread
106// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
107// on the worker thread via a VideoTrack. A custom implementation of a source
108// can inherit AdaptedVideoTrackSource instead of directly implementing this
109// interface.
Yves Gerey665174f2018-06-19 15:03:05 +0200110class VideoTrackSourceInterface : public MediaSourceInterface,
111 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100112 public:
nissefcc640f2016-04-01 01:10:42 -0700113 struct Stats {
114 // Original size of captured frame, before video adaptation.
115 int input_width;
116 int input_height;
117 };
perkja3ede6c2016-03-08 01:27:48 +0100118
perkj0d3eef22016-03-09 02:39:17 +0100119 // Indicates that parameters suitable for screencasts should be automatically
120 // applied to RtpSenders.
121 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800122 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100123 // implicit behavior.
124 virtual bool is_screencast() const = 0;
125
Perc0d31e92016-03-31 17:23:39 +0200126 // Indicates that the encoder should denoise video before encoding it.
127 // If it is not set, the default configuration is used which is different
128 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100129 // TODO(perkj): Remove this once denoising is done by the source, and not by
130 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200131 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100132
deadbeefb10f32f2017-02-08 01:38:21 -0800133 // Returns false if no stats are available, e.g, for a remote source, or a
134 // source which has not seen its first frame yet.
135 //
136 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700137 virtual bool GetStats(Stats* stats) = 0;
138
Markus Handell9982efa2019-11-21 11:56:50 +0100139 // Returns true if encoded output can be enabled in the source.
140 // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
141 // adapts.
142 virtual bool SupportsEncodedOutput() const { return false; }
143
144 // Reliably cause a key frame to be generated in encoded output.
145 // TODO(bugs.webrtc.org/11115): find optimal naming.
146 // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
147 // adapts.
148 virtual void GenerateKeyFrame() {}
149
150 // Add an encoded video sink to the source and additionally cause
151 // a key frame to be generated from the source. The sink will be
152 // invoked from a decoder queue.
153 // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
154 // adapts.
155 virtual void AddEncodedSink(
156 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {}
157
158 // Removes an encoded video sink from the source.
159 // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
160 // adapts.
161 virtual void RemoveEncodedSink(
162 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) {}
163
perkja3ede6c2016-03-08 01:27:48 +0100164 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100165 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 01:27:48 +0100166};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
perkj773be362017-07-31 23:22:01 -0700168// VideoTrackInterface is designed to be invoked on the signaling thread except
169// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
170// on the worker thread.
171// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
172// that ensures thread safety and that all methods are called on the right
173// thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200174class RTC_EXPORT VideoTrackInterface
175 : public MediaStreamTrackInterface,
176 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 public:
pbos5214a0a2016-12-16 15:39:11 -0800178 // Video track content hint, used to override the source is_screencast
179 // property.
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200180 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
181 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 15:39:11 -0800182
mbonadei539d1042017-07-10 02:40:49 -0700183 // Register a video sink for this track. Used to connect the track to the
184 // underlying video engine.
185 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
186 const rtc::VideoSinkWants& wants) override {}
187 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
188
perkja3ede6c2016-03-08 01:27:48 +0100189 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100191 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 15:39:11 -0800192 virtual void set_content_hint(ContentHint hint) {}
193
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100195 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196};
197
tommi6eca7e32015-12-15 04:27:11 -0800198// Interface for receiving audio data from a AudioTrack.
199class AudioTrackSinkInterface {
200 public:
201 virtual void OnData(const void* audio_data,
202 int bits_per_sample,
203 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800204 size_t number_of_channels,
Minyue Li99d6d812020-01-29 10:25:12 +0100205 size_t number_of_frames) {
206 RTC_NOTREACHED() << "This method must be overridden, or not used.";
207 }
208
209 // In this method, |absolute_capture_timestamp_ms|, when available, is
210 // supposed to deliver the timestamp when this audio frame was originally
211 // captured. This timestamp MUST be based on the same clock as
212 // rtc::TimeMillis().
213 virtual void OnData(const void* audio_data,
214 int bits_per_sample,
215 int sample_rate,
216 size_t number_of_channels,
217 size_t number_of_frames,
218 absl::optional<int64_t> absolute_capture_timestamp_ms) {
219 // TODO(bugs.webrtc.org/10739): Deprecate the old OnData and make this one
220 // pure virtual.
221 return OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
222 number_of_frames);
223 }
tommi6eca7e32015-12-15 04:27:11 -0800224
225 protected:
226 virtual ~AudioTrackSinkInterface() {}
227};
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800230// The same source can be used by multiple AudioTracks.
Mirko Bonadei66e76792019-04-02 11:33:59 +0200231class RTC_EXPORT AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000232 public:
233 class AudioObserver {
234 public:
235 virtual void OnSetVolume(double volume) = 0;
236
237 protected:
238 virtual ~AudioObserver() {}
239 };
240
deadbeefb10f32f2017-02-08 01:38:21 -0800241 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
242 // implemented in chromium.
243
244 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100245 // TODO(tommi): This method should be on the track and ideally volume should
246 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000247 virtual void SetVolume(double volume) {}
248
deadbeefb10f32f2017-02-08 01:38:21 -0800249 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000250 virtual void RegisterAudioObserver(AudioObserver* observer) {}
251 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
tommi6eca7e32015-12-15 04:27:11 -0800253 // TODO(tommi): Make pure virtual.
254 virtual void AddSink(AudioTrackSinkInterface* sink) {}
255 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
Piotr (Peter) Slatala95ca6e12018-11-13 07:57:07 -0800256
257 // Returns options for the AudioSource.
258 // (for some of the settings this approach is broken, e.g. setting
259 // audio network adaptation on the source is the wrong layer of abstraction).
260 virtual const cricket::AudioOptions options() const;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000261};
262
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000263// Interface of the audio processor used by the audio track to collect
264// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000266 public:
Ivo Creusenae026092017-11-20 13:07:16 +0100267 struct AudioProcessorStatistics {
268 bool typing_noise_detected = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100269 AudioProcessingStats apm_statistics;
Ivo Creusenae026092017-11-20 13:07:16 +0100270 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000271
Ivo Creusenae026092017-11-20 13:07:16 +0100272 // Get audio processor statistics. The |has_remote_tracks| argument should be
273 // set if there are active remote tracks (this would usually be true during
274 // a call). If there are no remote tracks some of the stats will not be set by
275 // the AudioProcessor, because they only make sense if there is at least one
276 // remote track.
Sam Zackrisson28127632018-11-01 11:37:15 +0100277 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
Ivo Creusenae026092017-11-20 13:07:16 +0100278
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000279 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100280 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000281};
282
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200283class RTC_EXPORT AudioTrackInterface : public MediaStreamTrackInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800285 // TODO(deadbeef): Figure out if the following interface should be const or
286 // not.
Yves Gerey665174f2018-06-19 15:03:05 +0200287 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000289 // Add/Remove a sink that will receive the audio data from the track.
290 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
291 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000292
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000293 // Get the signal level from the audio track.
294 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800295 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
296 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100297 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000298
deadbeef8d60a942017-02-27 14:47:33 -0800299 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000300 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800301 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100302 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000303
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100305 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306};
307
Yves Gerey665174f2018-06-19 15:03:05 +0200308typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
309typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310
deadbeefb10f32f2017-02-08 01:38:21 -0800311// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
312//
313// A major difference is that remote audio/video tracks (received by a
314// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
315// the same stream; a session description with the correct "a=msid" attributes
316// must be pushed down.
317//
318// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000319class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 public NotifierInterface {
321 public:
Seth Hampson13b8bad2018-03-13 16:05:28 -0700322 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323
324 virtual AudioTrackVector GetAudioTracks() = 0;
325 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200326 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
327 const std::string& track_id) = 0;
328 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
329 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330
331 virtual bool AddTrack(AudioTrackInterface* track) = 0;
332 virtual bool AddTrack(VideoTrackInterface* track) = 0;
333 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
334 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
335
336 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100337 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338};
339
340} // namespace webrtc
341
Steve Anton10542f22019-01-11 09:11:00 -0800342#endif // API_MEDIA_STREAM_INTERFACE_H_