You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
11 KiB

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