blob: bd4a2c0292db6efc36df9b059c0458ac4f9ebf4d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
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 12:30:4114// interfaces must be used only with PeerConnection.
henrike@webrtc.org28e20752013-07-10 00:45:3615
Steve Anton10542f22019-01-11 17:11:0016#ifndef API_MEDIA_STREAM_INTERFACE_H_
17#define API_MEDIA_STREAM_INTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:3618
pbos9baddf22017-01-02 14:44:4119#include <stddef.h>
20
henrike@webrtc.org28e20752013-07-10 00:45:3621#include <string>
22#include <vector>
23
Danil Chapovalov0bc58cf2018-06-21 11:32:5624#include "absl/types/optional.h"
Piotr (Peter) Slatala95ca6e12018-11-13 15:57:0725#include "api/audio_options.h"
Mirko Bonadeid9708072019-01-25 19:26:4826#include "api/scoped_refptr.h"
Markus Handell9982efa2019-11-21 10:56:5027#include "api/video/recordable_encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 09:15:3029#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 12:09:3130#include "api/video/video_source_interface.h"
Ivo Creusen56d460902017-11-24 16:29:5931#include "modules/audio_processing/include/audio_processing_statistics.h"
Steve Anton10542f22019-01-11 17:11:0032#include "rtc_base/ref_count.h"
Mirko Bonadei66e76792019-04-02 09:33:5933#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:3634
henrike@webrtc.org28e20752013-07-10 00:45:3635namespace 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 09:38:2154// 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 09:33:5956class RTC_EXPORT MediaSourceInterface : public rtc::RefCountInterface,
57 public NotifierInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3658 public:
Yves Gerey665174f2018-06-19 13:03:0559 enum SourceState { kInitializing, kLive, kEnded, kMuted };
henrike@webrtc.org28e20752013-07-10 00:45:3660
61 virtual SourceState state() const = 0;
62
tommi6eca7e32015-12-15 12:27:1163 virtual bool remote() const = 0;
64
henrike@webrtc.org28e20752013-07-10 00:45:3665 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:3166 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:3667};
68
deadbeefb10f32f2017-02-08 09:38:2169// C++ version of MediaStreamTrack.
70// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
Mirko Bonadei66e76792019-04-02 09:33:5971class RTC_EXPORT MediaStreamTrackInterface : public rtc::RefCountInterface,
72 public NotifierInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3673 public:
74 enum TrackState {
perkjc8f952d2016-03-23 07:33:5675 kLive,
76 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:3677 };
78
Niels Möller6dcd4dc2019-08-26 08:45:2879 static const char* const kAudioKind;
80 static const char* const kVideoKind;
deadbeeffac06552015-11-25 19:26:0181
nissefcc640f2016-04-01 08:10:4282 // 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:3686 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 09:38:2187
88 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:3689 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 09:38:2190
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:3693 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:3694 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:2195
deadbeefb10f32f2017-02-08 09:38:2196 // 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:2199 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31100 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36101};
102
deadbeefb10f32f2017-02-08 09:38:21103// VideoTrackSourceInterface is a reference counted source used for
104// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-08-01 06:22:01105// 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 13:03:05110class VideoTrackSourceInterface : public MediaSourceInterface,
111 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 00:27:48112 public:
nissefcc640f2016-04-01 08:10:42113 struct Stats {
114 // Original size of captured frame, before video adaptation.
115 int input_width;
116 int input_height;
117 };
perkja3ede6c2016-03-08 00:27:48118
perkj0d3eef22016-03-09 01:39:17119 // 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 09:38:21122 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 01:39:17123 // implicit behavior.
124 virtual bool is_screencast() const = 0;
125
Perc0d31e92016-03-31 15:23:39126 // 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 01:39:17129 // TODO(perkj): Remove this once denoising is done by the source, and not by
130 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 11:32:56131 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 00:27:48132
deadbeefb10f32f2017-02-08 09:38:21133 // 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 08:10:42137 virtual bool GetStats(Stats* stats) = 0;
138
Markus Handell9982efa2019-11-21 10:56:50139 // Returns true if encoded output can be enabled in the source.
Markus Handell6efc14b2020-05-05 18:11:13140 virtual bool SupportsEncodedOutput() const = 0;
Markus Handell9982efa2019-11-21 10:56:50141
142 // Reliably cause a key frame to be generated in encoded output.
143 // TODO(bugs.webrtc.org/11115): find optimal naming.
Markus Handell6efc14b2020-05-05 18:11:13144 virtual void GenerateKeyFrame() = 0;
Markus Handell9982efa2019-11-21 10:56:50145
146 // Add an encoded video sink to the source and additionally cause
147 // a key frame to be generated from the source. The sink will be
148 // invoked from a decoder queue.
149 // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
150 // adapts.
151 virtual void AddEncodedSink(
Markus Handell6efc14b2020-05-05 18:11:13152 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
Markus Handell9982efa2019-11-21 10:56:50153
154 // Removes an encoded video sink from the source.
Markus Handell9982efa2019-11-21 10:56:50155 virtual void RemoveEncodedSink(
Markus Handell6efc14b2020-05-05 18:11:13156 rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
Markus Handell9982efa2019-11-21 10:56:50157
perkja3ede6c2016-03-08 00:27:48158 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31159 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 00:27:48160};
henrike@webrtc.org28e20752013-07-10 00:45:36161
perkj773be362017-08-01 06:22:01162// VideoTrackInterface is designed to be invoked on the signaling thread except
163// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
164// on the worker thread.
165// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
166// that ensures thread safety and that all methods are called on the right
167// thread.
Mirko Bonadei35214fc2019-09-23 12:54:28168class RTC_EXPORT VideoTrackInterface
169 : public MediaStreamTrackInterface,
170 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36171 public:
pbos5214a0a2016-12-16 23:39:11172 // Video track content hint, used to override the source is_screencast
173 // property.
Harald Alvestrandc19ab072018-06-18 06:53:10174 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
175 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 23:39:11176
mbonadei539d1042017-07-10 09:40:49177 // Register a video sink for this track. Used to connect the track to the
178 // underlying video engine.
179 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
180 const rtc::VideoSinkWants& wants) override {}
181 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
182
perkja3ede6c2016-03-08 00:27:48183 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36184
Danil Chapovalov2a5ce2b2018-02-07 08:38:31185 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 23:39:11186 virtual void set_content_hint(ContentHint hint) {}
187
henrike@webrtc.org28e20752013-07-10 00:45:36188 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31189 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36190};
191
tommi6eca7e32015-12-15 12:27:11192// Interface for receiving audio data from a AudioTrack.
193class AudioTrackSinkInterface {
194 public:
195 virtual void OnData(const void* audio_data,
196 int bits_per_sample,
197 int sample_rate,
Peter Kasting69558702016-01-13 00:26:35198 size_t number_of_channels,
Minyue Li99d6d812020-01-29 09:25:12199 size_t number_of_frames) {
200 RTC_NOTREACHED() << "This method must be overridden, or not used.";
201 }
202
203 // In this method, |absolute_capture_timestamp_ms|, when available, is
204 // supposed to deliver the timestamp when this audio frame was originally
205 // captured. This timestamp MUST be based on the same clock as
206 // rtc::TimeMillis().
207 virtual void OnData(const void* audio_data,
208 int bits_per_sample,
209 int sample_rate,
210 size_t number_of_channels,
211 size_t number_of_frames,
212 absl::optional<int64_t> absolute_capture_timestamp_ms) {
213 // TODO(bugs.webrtc.org/10739): Deprecate the old OnData and make this one
214 // pure virtual.
215 return OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
216 number_of_frames);
217 }
tommi6eca7e32015-12-15 12:27:11218
219 protected:
220 virtual ~AudioTrackSinkInterface() {}
221};
222
henrike@webrtc.org28e20752013-07-10 00:45:36223// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 09:38:21224// The same source can be used by multiple AudioTracks.
Mirko Bonadei66e76792019-04-02 09:33:59225class RTC_EXPORT AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49226 public:
227 class AudioObserver {
228 public:
229 virtual void OnSetVolume(double volume) = 0;
230
231 protected:
232 virtual ~AudioObserver() {}
233 };
234
deadbeefb10f32f2017-02-08 09:38:21235 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
236 // implemented in chromium.
237
238 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 00:37:01239 // TODO(tommi): This method should be on the track and ideally volume should
240 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49241 virtual void SetVolume(double volume) {}
242
deadbeefb10f32f2017-02-08 09:38:21243 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49244 virtual void RegisterAudioObserver(AudioObserver* observer) {}
245 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36246
tommi6eca7e32015-12-15 12:27:11247 // TODO(tommi): Make pure virtual.
248 virtual void AddSink(AudioTrackSinkInterface* sink) {}
249 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
Piotr (Peter) Slatala95ca6e12018-11-13 15:57:07250
251 // Returns options for the AudioSource.
252 // (for some of the settings this approach is broken, e.g. setting
253 // audio network adaptation on the source is the wrong layer of abstraction).
254 virtual const cricket::AudioOptions options() const;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16255};
256
henrike@webrtc.org40b3b682014-03-03 18:30:11257// Interface of the audio processor used by the audio track to collect
258// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52259class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11260 public:
Ivo Creusenae0260962017-11-20 12:07:16261 struct AudioProcessorStatistics {
262 bool typing_noise_detected = false;
Ivo Creusen56d460902017-11-24 16:29:59263 AudioProcessingStats apm_statistics;
Ivo Creusenae0260962017-11-20 12:07:16264 };
henrike@webrtc.org40b3b682014-03-03 18:30:11265
Ivo Creusenae0260962017-11-20 12:07:16266 // Get audio processor statistics. The |has_remote_tracks| argument should be
267 // set if there are active remote tracks (this would usually be true during
268 // a call). If there are no remote tracks some of the stats will not be set by
269 // the AudioProcessor, because they only make sense if there is at least one
270 // remote track.
Sam Zackrisson28127632018-11-01 10:37:15271 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
Ivo Creusenae0260962017-11-20 12:07:16272
henrike@webrtc.org40b3b682014-03-03 18:30:11273 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31274 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11275};
276
Mirko Bonadei35214fc2019-09-23 12:54:28277class RTC_EXPORT AudioTrackInterface : public MediaStreamTrackInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36278 public:
deadbeefb10f32f2017-02-08 09:38:21279 // TODO(deadbeef): Figure out if the following interface should be const or
280 // not.
Yves Gerey665174f2018-06-19 13:03:05281 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36282
henrike@webrtc.org40b3b682014-03-03 18:30:11283 // Add/Remove a sink that will receive the audio data from the track.
284 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
285 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16286
henrike@webrtc.org40b3b682014-03-03 18:30:11287 // Get the signal level from the audio track.
288 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 09:38:21289 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
290 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 08:38:31291 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11292
deadbeef8d60a942017-02-27 22:47:33293 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11294 // does not have any processor.
deadbeefb10f32f2017-02-08 09:38:21295 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 08:38:31296 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11297
henrike@webrtc.org28e20752013-07-10 00:45:36298 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31299 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36300};
301
Yves Gerey665174f2018-06-19 13:03:05302typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
303typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36304
deadbeefb10f32f2017-02-08 09:38:21305// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
306//
307// A major difference is that remote audio/video tracks (received by a
308// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
309// the same stream; a session description with the correct "a=msid" attributes
310// must be pushed down.
311//
312// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52313class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36314 public NotifierInterface {
315 public:
Seth Hampson13b8bad2018-03-13 23:05:28316 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36317
318 virtual AudioTrackVector GetAudioTracks() = 0;
319 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 13:03:05320 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
321 const std::string& track_id) = 0;
322 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
323 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36324
325 virtual bool AddTrack(AudioTrackInterface* track) = 0;
326 virtual bool AddTrack(VideoTrackInterface* track) = 0;
327 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
328 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
329
330 protected:
Danil Chapovalov2a5ce2b2018-02-07 08:38:31331 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36332};
333
334} // namespace webrtc
335
Steve Anton10542f22019-01-11 17:11:00336#endif // API_MEDIA_STREAM_INTERFACE_H_