media_stream_interface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * 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.
  9. */
  10. // This file contains interfaces for MediaStream, MediaTrack and MediaSource.
  11. // These interfaces are used for implementing MediaStream and MediaTrack as
  12. // defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
  13. // interfaces must be used only with PeerConnection.
  14. #ifndef API_MEDIA_STREAM_INTERFACE_H_
  15. #define API_MEDIA_STREAM_INTERFACE_H_
  16. #include <stddef.h>
  17. #include <string>
  18. #include <vector>
  19. #include "absl/types/optional.h"
  20. #include "api/audio_options.h"
  21. #include "api/scoped_refptr.h"
  22. #include "api/video/recordable_encoded_frame.h"
  23. #include "api/video/video_frame.h"
  24. #include "api/video/video_sink_interface.h"
  25. #include "api/video/video_source_interface.h"
  26. #include "modules/audio_processing/include/audio_processing_statistics.h"
  27. #include "rtc_base/ref_count.h"
  28. #include "rtc_base/system/rtc_export.h"
  29. namespace webrtc {
  30. // Generic observer interface.
  31. class ObserverInterface {
  32. public:
  33. virtual void OnChanged() = 0;
  34. protected:
  35. virtual ~ObserverInterface() {}
  36. };
  37. class NotifierInterface {
  38. public:
  39. virtual void RegisterObserver(ObserverInterface* observer) = 0;
  40. virtual void UnregisterObserver(ObserverInterface* observer) = 0;
  41. virtual ~NotifierInterface() {}
  42. };
  43. // Base class for sources. A MediaStreamTrack has an underlying source that
  44. // provides media. A source can be shared by multiple tracks.
  45. class RTC_EXPORT MediaSourceInterface : public rtc::RefCountInterface,
  46. public NotifierInterface {
  47. public:
  48. enum SourceState { kInitializing, kLive, kEnded, kMuted };
  49. virtual SourceState state() const = 0;
  50. virtual bool remote() const = 0;
  51. protected:
  52. ~MediaSourceInterface() override = default;
  53. };
  54. // C++ version of MediaStreamTrack.
  55. // See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
  56. class RTC_EXPORT MediaStreamTrackInterface : public rtc::RefCountInterface,
  57. public NotifierInterface {
  58. public:
  59. enum TrackState {
  60. kLive,
  61. kEnded,
  62. };
  63. static const char* const kAudioKind;
  64. static const char* const kVideoKind;
  65. // The kind() method must return kAudioKind only if the object is a
  66. // subclass of AudioTrackInterface, and kVideoKind only if the
  67. // object is a subclass of VideoTrackInterface. It is typically used
  68. // to protect a static_cast<> to the corresponding subclass.
  69. virtual std::string kind() const = 0;
  70. // Track identifier.
  71. virtual std::string id() const = 0;
  72. // A disabled track will produce silence (if audio) or black frames (if
  73. // video). Can be disabled and re-enabled.
  74. virtual bool enabled() const = 0;
  75. virtual bool set_enabled(bool enable) = 0;
  76. // Live or ended. A track will never be live again after becoming ended.
  77. virtual TrackState state() const = 0;
  78. protected:
  79. ~MediaStreamTrackInterface() override = default;
  80. };
  81. // VideoTrackSourceInterface is a reference counted source used for
  82. // VideoTracks. The same source can be used by multiple VideoTracks.
  83. // VideoTrackSourceInterface is designed to be invoked on the signaling thread
  84. // except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
  85. // on the worker thread via a VideoTrack. A custom implementation of a source
  86. // can inherit AdaptedVideoTrackSource instead of directly implementing this
  87. // interface.
  88. class VideoTrackSourceInterface : public MediaSourceInterface,
  89. public rtc::VideoSourceInterface<VideoFrame> {
  90. public:
  91. struct Stats {
  92. // Original size of captured frame, before video adaptation.
  93. int input_width;
  94. int input_height;
  95. };
  96. // Indicates that parameters suitable for screencasts should be automatically
  97. // applied to RtpSenders.
  98. // TODO(perkj): Remove these once all known applications have moved to
  99. // explicitly setting suitable parameters for screencasts and don't need this
  100. // implicit behavior.
  101. virtual bool is_screencast() const = 0;
  102. // Indicates that the encoder should denoise video before encoding it.
  103. // If it is not set, the default configuration is used which is different
  104. // depending on video codec.
  105. // TODO(perkj): Remove this once denoising is done by the source, and not by
  106. // the encoder.
  107. virtual absl::optional<bool> needs_denoising() const = 0;
  108. // Returns false if no stats are available, e.g, for a remote source, or a
  109. // source which has not seen its first frame yet.
  110. //
  111. // Implementation should avoid blocking.
  112. virtual bool GetStats(Stats* stats) = 0;
  113. // Returns true if encoded output can be enabled in the source.
  114. virtual bool SupportsEncodedOutput() const = 0;
  115. // Reliably cause a key frame to be generated in encoded output.
  116. // TODO(bugs.webrtc.org/11115): find optimal naming.
  117. virtual void GenerateKeyFrame() = 0;
  118. // Add an encoded video sink to the source and additionally cause
  119. // a key frame to be generated from the source. The sink will be
  120. // invoked from a decoder queue.
  121. // TODO(bugs.webrtc.org/11114): make pure virtual once downstream project
  122. // adapts.
  123. virtual void AddEncodedSink(
  124. rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
  125. // Removes an encoded video sink from the source.
  126. virtual void RemoveEncodedSink(
  127. rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
  128. protected:
  129. ~VideoTrackSourceInterface() override = default;
  130. };
  131. // VideoTrackInterface is designed to be invoked on the signaling thread except
  132. // for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
  133. // on the worker thread.
  134. // PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
  135. // that ensures thread safety and that all methods are called on the right
  136. // thread.
  137. class RTC_EXPORT VideoTrackInterface
  138. : public MediaStreamTrackInterface,
  139. public rtc::VideoSourceInterface<VideoFrame> {
  140. public:
  141. // Video track content hint, used to override the source is_screencast
  142. // property.
  143. // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
  144. enum class ContentHint { kNone, kFluid, kDetailed, kText };
  145. // Register a video sink for this track. Used to connect the track to the
  146. // underlying video engine.
  147. void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
  148. const rtc::VideoSinkWants& wants) override {}
  149. void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
  150. virtual VideoTrackSourceInterface* GetSource() const = 0;
  151. virtual ContentHint content_hint() const;
  152. virtual void set_content_hint(ContentHint hint) {}
  153. protected:
  154. ~VideoTrackInterface() override = default;
  155. };
  156. // Interface for receiving audio data from a AudioTrack.
  157. class AudioTrackSinkInterface {
  158. public:
  159. virtual void OnData(const void* audio_data,
  160. int bits_per_sample,
  161. int sample_rate,
  162. size_t number_of_channels,
  163. size_t number_of_frames) {
  164. RTC_NOTREACHED() << "This method must be overridden, or not used.";
  165. }
  166. // In this method, |absolute_capture_timestamp_ms|, when available, is
  167. // supposed to deliver the timestamp when this audio frame was originally
  168. // captured. This timestamp MUST be based on the same clock as
  169. // rtc::TimeMillis().
  170. virtual void OnData(const void* audio_data,
  171. int bits_per_sample,
  172. int sample_rate,
  173. size_t number_of_channels,
  174. size_t number_of_frames,
  175. absl::optional<int64_t> absolute_capture_timestamp_ms) {
  176. // TODO(bugs.webrtc.org/10739): Deprecate the old OnData and make this one
  177. // pure virtual.
  178. return OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
  179. number_of_frames);
  180. }
  181. protected:
  182. virtual ~AudioTrackSinkInterface() {}
  183. };
  184. // AudioSourceInterface is a reference counted source used for AudioTracks.
  185. // The same source can be used by multiple AudioTracks.
  186. class RTC_EXPORT AudioSourceInterface : public MediaSourceInterface {
  187. public:
  188. class AudioObserver {
  189. public:
  190. virtual void OnSetVolume(double volume) = 0;
  191. protected:
  192. virtual ~AudioObserver() {}
  193. };
  194. // TODO(deadbeef): Makes all the interfaces pure virtual after they're
  195. // implemented in chromium.
  196. // Sets the volume of the source. |volume| is in the range of [0, 10].
  197. // TODO(tommi): This method should be on the track and ideally volume should
  198. // be applied in the track in a way that does not affect clones of the track.
  199. virtual void SetVolume(double volume) {}
  200. // Registers/unregisters observers to the audio source.
  201. virtual void RegisterAudioObserver(AudioObserver* observer) {}
  202. virtual void UnregisterAudioObserver(AudioObserver* observer) {}
  203. // TODO(tommi): Make pure virtual.
  204. virtual void AddSink(AudioTrackSinkInterface* sink) {}
  205. virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
  206. // Returns options for the AudioSource.
  207. // (for some of the settings this approach is broken, e.g. setting
  208. // audio network adaptation on the source is the wrong layer of abstraction).
  209. virtual const cricket::AudioOptions options() const;
  210. };
  211. // Interface of the audio processor used by the audio track to collect
  212. // statistics.
  213. class AudioProcessorInterface : public rtc::RefCountInterface {
  214. public:
  215. struct AudioProcessorStatistics {
  216. bool typing_noise_detected = false;
  217. AudioProcessingStats apm_statistics;
  218. };
  219. // Get audio processor statistics. The |has_remote_tracks| argument should be
  220. // set if there are active remote tracks (this would usually be true during
  221. // a call). If there are no remote tracks some of the stats will not be set by
  222. // the AudioProcessor, because they only make sense if there is at least one
  223. // remote track.
  224. virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
  225. protected:
  226. ~AudioProcessorInterface() override = default;
  227. };
  228. class RTC_EXPORT AudioTrackInterface : public MediaStreamTrackInterface {
  229. public:
  230. // TODO(deadbeef): Figure out if the following interface should be const or
  231. // not.
  232. virtual AudioSourceInterface* GetSource() const = 0;
  233. // Add/Remove a sink that will receive the audio data from the track.
  234. virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
  235. virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
  236. // Get the signal level from the audio track.
  237. // Return true on success, otherwise false.
  238. // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
  239. // virtual after it's implemented in chromium.
  240. virtual bool GetSignalLevel(int* level);
  241. // Get the audio processor used by the audio track. Return null if the track
  242. // does not have any processor.
  243. // TODO(deadbeef): Make the interface pure virtual.
  244. virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
  245. protected:
  246. ~AudioTrackInterface() override = default;
  247. };
  248. typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > AudioTrackVector;
  249. typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > VideoTrackVector;
  250. // C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
  251. //
  252. // A major difference is that remote audio/video tracks (received by a
  253. // PeerConnection/RtpReceiver) are not synchronized simply by adding them to
  254. // the same stream; a session description with the correct "a=msid" attributes
  255. // must be pushed down.
  256. //
  257. // Thus, this interface acts as simply a container for tracks.
  258. class MediaStreamInterface : public rtc::RefCountInterface,
  259. public NotifierInterface {
  260. public:
  261. virtual std::string id() const = 0;
  262. virtual AudioTrackVector GetAudioTracks() = 0;
  263. virtual VideoTrackVector GetVideoTracks() = 0;
  264. virtual rtc::scoped_refptr<AudioTrackInterface> FindAudioTrack(
  265. const std::string& track_id) = 0;
  266. virtual rtc::scoped_refptr<VideoTrackInterface> FindVideoTrack(
  267. const std::string& track_id) = 0;
  268. virtual bool AddTrack(AudioTrackInterface* track) = 0;
  269. virtual bool AddTrack(VideoTrackInterface* track) = 0;
  270. virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
  271. virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
  272. protected:
  273. ~MediaStreamInterface() override = default;
  274. };
  275. } // namespace webrtc
  276. #endif // API_MEDIA_STREAM_INTERFACE_H_