blob: af89e9df2fb23534b743bef9276ce18e38c1454d [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 Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020028#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020029#include "api/video/video_source_interface.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010030#include "modules/audio_processing/include/audio_processing_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/ref_count.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034namespace webrtc {
35
36// Generic observer interface.
37class ObserverInterface {
38 public:
39 virtual void OnChanged() = 0;
40
41 protected:
42 virtual ~ObserverInterface() {}
43};
44
45class NotifierInterface {
46 public:
47 virtual void RegisterObserver(ObserverInterface* observer) = 0;
48 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
49
50 virtual ~NotifierInterface() {}
51};
52
deadbeefb10f32f2017-02-08 01:38:21 -080053// Base class for sources. A MediaStreamTrack has an underlying source that
54// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public NotifierInterface {
57 public:
Yves Gerey665174f2018-06-19 15:03:05 +020058 enum SourceState { kInitializing, kLive, kEnded, kMuted };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60 virtual SourceState state() const = 0;
61
tommi6eca7e32015-12-15 04:27:11 -080062 virtual bool remote() const = 0;
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010065 ~MediaSourceInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066};
67
deadbeefb10f32f2017-02-08 01:38:21 -080068// C++ version of MediaStreamTrack.
69// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 public NotifierInterface {
72 public:
73 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070074 kLive,
75 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 };
77
deadbeeffac06552015-11-25 11:26:01 -080078 static const char kAudioKind[];
79 static const char kVideoKind[];
80
nissefcc640f2016-04-01 01:10:42 -070081 // The kind() method must return kAudioKind only if the object is a
82 // subclass of AudioTrackInterface, and kVideoKind only if the
83 // object is a subclass of VideoTrackInterface. It is typically used
84 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080086
87 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080089
90 // A disabled track will produce silence (if audio) or black frames (if
91 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000094
deadbeefb10f32f2017-02-08 01:38:21 -080095 // Live or ended. A track will never be live again after becoming ended.
96 virtual TrackState state() const = 0;
97
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000098 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010099 ~MediaStreamTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100};
101
deadbeefb10f32f2017-02-08 01:38:21 -0800102// VideoTrackSourceInterface is a reference counted source used for
103// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700104// VideoTrackSourceInterface is designed to be invoked on the signaling thread
105// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
106// on the worker thread via a VideoTrack. A custom implementation of a source
107// can inherit AdaptedVideoTrackSource instead of directly implementing this
108// interface.
Yves Gerey665174f2018-06-19 15:03:05 +0200109class VideoTrackSourceInterface : public MediaSourceInterface,
110 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100111 public:
nissefcc640f2016-04-01 01:10:42 -0700112 struct Stats {
113 // Original size of captured frame, before video adaptation.
114 int input_width;
115 int input_height;
116 };
perkja3ede6c2016-03-08 01:27:48 +0100117
perkj0d3eef22016-03-09 02:39:17 +0100118 // Indicates that parameters suitable for screencasts should be automatically
119 // applied to RtpSenders.
120 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800121 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100122 // implicit behavior.
123 virtual bool is_screencast() const = 0;
124
Perc0d31e92016-03-31 17:23:39 +0200125 // Indicates that the encoder should denoise video before encoding it.
126 // If it is not set, the default configuration is used which is different
127 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100128 // TODO(perkj): Remove this once denoising is done by the source, and not by
129 // the encoder.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200130 virtual absl::optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100131
deadbeefb10f32f2017-02-08 01:38:21 -0800132 // Returns false if no stats are available, e.g, for a remote source, or a
133 // source which has not seen its first frame yet.
134 //
135 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700136 virtual bool GetStats(Stats* stats) = 0;
137
perkja3ede6c2016-03-08 01:27:48 +0100138 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100139 ~VideoTrackSourceInterface() override = default;
perkja3ede6c2016-03-08 01:27:48 +0100140};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
perkj773be362017-07-31 23:22:01 -0700142// VideoTrackInterface is designed to be invoked on the signaling thread except
143// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
144// on the worker thread.
145// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
146// that ensures thread safety and that all methods are called on the right
147// thread.
Yves Gerey665174f2018-06-19 15:03:05 +0200148class VideoTrackInterface : public MediaStreamTrackInterface,
149 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 public:
pbos5214a0a2016-12-16 15:39:11 -0800151 // Video track content hint, used to override the source is_screencast
152 // property.
Harald Alvestrandc19ab072018-06-18 08:53:10 +0200153 // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
154 enum class ContentHint { kNone, kFluid, kDetailed, kText };
pbos5214a0a2016-12-16 15:39:11 -0800155
mbonadei539d1042017-07-10 02:40:49 -0700156 // Register a video sink for this track. Used to connect the track to the
157 // underlying video engine.
158 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
159 const rtc::VideoSinkWants& wants) override {}
160 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
161
perkja3ede6c2016-03-08 01:27:48 +0100162 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100164 virtual ContentHint content_hint() const;
pbos5214a0a2016-12-16 15:39:11 -0800165 virtual void set_content_hint(ContentHint hint) {}
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100168 ~VideoTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169};
170
tommi6eca7e32015-12-15 04:27:11 -0800171// Interface for receiving audio data from a AudioTrack.
172class AudioTrackSinkInterface {
173 public:
174 virtual void OnData(const void* audio_data,
175 int bits_per_sample,
176 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800177 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800178 size_t number_of_frames) = 0;
179
180 protected:
181 virtual ~AudioTrackSinkInterface() {}
182};
183
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800185// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000187 public:
188 class AudioObserver {
189 public:
190 virtual void OnSetVolume(double volume) = 0;
191
192 protected:
193 virtual ~AudioObserver() {}
194 };
195
deadbeefb10f32f2017-02-08 01:38:21 -0800196 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
197 // implemented in chromium.
198
199 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100200 // TODO(tommi): This method should be on the track and ideally volume should
201 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000202 virtual void SetVolume(double volume) {}
203
deadbeefb10f32f2017-02-08 01:38:21 -0800204 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000205 virtual void RegisterAudioObserver(AudioObserver* observer) {}
206 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207
tommi6eca7e32015-12-15 04:27:11 -0800208 // TODO(tommi): Make pure virtual.
209 virtual void AddSink(AudioTrackSinkInterface* sink) {}
210 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
Piotr (Peter) Slatala95ca6e12018-11-13 07:57:07 -0800211
212 // Returns options for the AudioSource.
213 // (for some of the settings this approach is broken, e.g. setting
214 // audio network adaptation on the source is the wrong layer of abstraction).
215 virtual const cricket::AudioOptions options() const;
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 struct AudioProcessorStatistics {
223 bool typing_noise_detected = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100224 AudioProcessingStats apm_statistics;
Ivo Creusenae026092017-11-20 13:07:16 +0100225 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000226
Ivo Creusenae026092017-11-20 13:07:16 +0100227 // Get audio processor statistics. The |has_remote_tracks| argument should be
228 // set if there are active remote tracks (this would usually be true during
229 // a call). If there are no remote tracks some of the stats will not be set by
230 // the AudioProcessor, because they only make sense if there is at least one
231 // remote track.
Sam Zackrisson28127632018-11-01 11:37:15 +0100232 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
Ivo Creusenae026092017-11-20 13:07:16 +0100233
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000234 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100235 ~AudioProcessorInterface() override = default;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000236};
237
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238class AudioTrackInterface : public MediaStreamTrackInterface {
239 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800240 // TODO(deadbeef): Figure out if the following interface should be const or
241 // not.
Yves Gerey665174f2018-06-19 15:03:05 +0200242 virtual AudioSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000244 // Add/Remove a sink that will receive the audio data from the track.
245 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
246 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000247
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000248 // Get the signal level from the audio track.
249 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800250 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
251 // virtual after it's implemented in chromium.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100252 virtual bool GetSignalLevel(int* level);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000253
deadbeef8d60a942017-02-27 14:47:33 -0800254 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000255 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800256 // TODO(deadbeef): Make the interface pure virtual.
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100257 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100260 ~AudioTrackInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261};
262
Yves Gerey665174f2018-06-19 15:03:05 +0200263typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
264typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265
deadbeefb10f32f2017-02-08 01:38:21 -0800266// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
267//
268// A major difference is that remote audio/video tracks (received by a
269// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
270// the same stream; a session description with the correct "a=msid" attributes
271// must be pushed down.
272//
273// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 public NotifierInterface {
276 public:
Seth Hampson13b8bad2018-03-13 16:05:28 -0700277 virtual std::string id() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
279 virtual AudioTrackVector GetAudioTracks() = 0;
280 virtual VideoTrackVector GetVideoTracks() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200281 virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
282 const std::string& track_id) = 0;
283 virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
284 const std::string& track_id) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285
286 virtual bool AddTrack(AudioTrackInterface* track) = 0;
287 virtual bool AddTrack(VideoTrackInterface* track) = 0;
288 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
289 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
290
291 protected:
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100292 ~MediaStreamInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293};
294
295} // namespace webrtc
296
Steve Anton10542f22019-01-11 09:11:00 -0800297#endif // API_MEDIA_STREAM_INTERFACE_H_