webrtc_voice_engine.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2004 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. #ifndef MEDIA_ENGINE_WEBRTC_VOICE_ENGINE_H_
  11. #define MEDIA_ENGINE_WEBRTC_VOICE_ENGINE_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "api/audio_codecs/audio_encoder_factory.h"
  17. #include "api/scoped_refptr.h"
  18. #include "api/task_queue/task_queue_factory.h"
  19. #include "api/transport/rtp/rtp_source.h"
  20. #include "call/audio_state.h"
  21. #include "call/call.h"
  22. #include "media/base/media_engine.h"
  23. #include "media/base/rtp_utils.h"
  24. #include "rtc_base/buffer.h"
  25. #include "rtc_base/constructor_magic.h"
  26. #include "rtc_base/network_route.h"
  27. #include "rtc_base/task_queue.h"
  28. #include "rtc_base/thread_checker.h"
  29. namespace cricket {
  30. class AudioDeviceModule;
  31. class AudioMixer;
  32. class AudioSource;
  33. class WebRtcVoiceMediaChannel;
  34. // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine.
  35. // It uses the WebRtc VoiceEngine library for audio handling.
  36. class WebRtcVoiceEngine final : public VoiceEngineInterface {
  37. friend class WebRtcVoiceMediaChannel;
  38. public:
  39. WebRtcVoiceEngine(
  40. webrtc::TaskQueueFactory* task_queue_factory,
  41. webrtc::AudioDeviceModule* adm,
  42. const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
  43. const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
  44. rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
  45. rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing);
  46. ~WebRtcVoiceEngine() override;
  47. // Does initialization that needs to occur on the worker thread.
  48. void Init() override;
  49. rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const override;
  50. VoiceMediaChannel* CreateMediaChannel(
  51. webrtc::Call* call,
  52. const MediaConfig& config,
  53. const AudioOptions& options,
  54. const webrtc::CryptoOptions& crypto_options) override;
  55. const std::vector<AudioCodec>& send_codecs() const override;
  56. const std::vector<AudioCodec>& recv_codecs() const override;
  57. std::vector<webrtc::RtpHeaderExtensionCapability> GetRtpHeaderExtensions()
  58. const override;
  59. // For tracking WebRtc channels. Needed because we have to pause them
  60. // all when switching devices.
  61. // May only be called by WebRtcVoiceMediaChannel.
  62. void RegisterChannel(WebRtcVoiceMediaChannel* channel);
  63. void UnregisterChannel(WebRtcVoiceMediaChannel* channel);
  64. // Starts AEC dump using an existing file. A maximum file size in bytes can be
  65. // specified. When the maximum file size is reached, logging is stopped and
  66. // the file is closed. If max_size_bytes is set to <= 0, no limit will be
  67. // used.
  68. bool StartAecDump(webrtc::FileWrapper file, int64_t max_size_bytes) override;
  69. // Stops AEC dump.
  70. void StopAecDump() override;
  71. private:
  72. // Every option that is "set" will be applied. Every option not "set" will be
  73. // ignored. This allows us to selectively turn on and off different options
  74. // easily at any time.
  75. bool ApplyOptions(const AudioOptions& options);
  76. int CreateVoEChannel();
  77. webrtc::TaskQueueFactory* const task_queue_factory_;
  78. std::unique_ptr<rtc::TaskQueue> low_priority_worker_queue_;
  79. webrtc::AudioDeviceModule* adm();
  80. webrtc::AudioProcessing* apm() const;
  81. webrtc::AudioState* audio_state();
  82. std::vector<AudioCodec> CollectCodecs(
  83. const std::vector<webrtc::AudioCodecSpec>& specs) const;
  84. rtc::ThreadChecker signal_thread_checker_;
  85. rtc::ThreadChecker worker_thread_checker_;
  86. // The audio device module.
  87. rtc::scoped_refptr<webrtc::AudioDeviceModule> adm_;
  88. rtc::scoped_refptr<webrtc::AudioEncoderFactory> encoder_factory_;
  89. rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory_;
  90. rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer_;
  91. // The audio processing module.
  92. rtc::scoped_refptr<webrtc::AudioProcessing> apm_;
  93. // The primary instance of WebRtc VoiceEngine.
  94. rtc::scoped_refptr<webrtc::AudioState> audio_state_;
  95. std::vector<AudioCodec> send_codecs_;
  96. std::vector<AudioCodec> recv_codecs_;
  97. std::vector<WebRtcVoiceMediaChannel*> channels_;
  98. bool is_dumping_aec_ = false;
  99. bool initialized_ = false;
  100. // Cache experimental_ns and apply in case they are missing in the audio
  101. // options. We need to do this because SetExtraOptions() will revert to
  102. // defaults for options which are not provided.
  103. absl::optional<bool> experimental_ns_;
  104. // Jitter buffer settings for new streams.
  105. size_t audio_jitter_buffer_max_packets_ = 200;
  106. bool audio_jitter_buffer_fast_accelerate_ = false;
  107. int audio_jitter_buffer_min_delay_ms_ = 0;
  108. bool audio_jitter_buffer_enable_rtx_handling_ = false;
  109. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceEngine);
  110. };
  111. // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
  112. // WebRtc Voice Engine.
  113. class WebRtcVoiceMediaChannel final : public VoiceMediaChannel,
  114. public webrtc::Transport {
  115. public:
  116. WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
  117. const MediaConfig& config,
  118. const AudioOptions& options,
  119. const webrtc::CryptoOptions& crypto_options,
  120. webrtc::Call* call);
  121. ~WebRtcVoiceMediaChannel() override;
  122. const AudioOptions& options() const { return options_; }
  123. bool SetSendParameters(const AudioSendParameters& params) override;
  124. bool SetRecvParameters(const AudioRecvParameters& params) override;
  125. webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
  126. webrtc::RTCError SetRtpSendParameters(
  127. uint32_t ssrc,
  128. const webrtc::RtpParameters& parameters) override;
  129. webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
  130. webrtc::RtpParameters GetDefaultRtpReceiveParameters() const override;
  131. void SetPlayout(bool playout) override;
  132. void SetSend(bool send) override;
  133. bool SetAudioSend(uint32_t ssrc,
  134. bool enable,
  135. const AudioOptions* options,
  136. AudioSource* source) override;
  137. bool AddSendStream(const StreamParams& sp) override;
  138. bool RemoveSendStream(uint32_t ssrc) override;
  139. bool AddRecvStream(const StreamParams& sp) override;
  140. bool RemoveRecvStream(uint32_t ssrc) override;
  141. void ResetUnsignaledRecvStream() override;
  142. // E2EE Frame API
  143. // Set a frame decryptor to a particular ssrc that will intercept all
  144. // incoming audio payloads and attempt to decrypt them before forwarding the
  145. // result.
  146. void SetFrameDecryptor(uint32_t ssrc,
  147. rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
  148. frame_decryptor) override;
  149. // Set a frame encryptor to a particular ssrc that will intercept all
  150. // outgoing audio payloads frames and attempt to encrypt them and forward the
  151. // result to the packetizer.
  152. void SetFrameEncryptor(uint32_t ssrc,
  153. rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
  154. frame_encryptor) override;
  155. bool SetOutputVolume(uint32_t ssrc, double volume) override;
  156. // Applies the new volume to current and future unsignaled streams.
  157. bool SetDefaultOutputVolume(double volume) override;
  158. bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
  159. absl::optional<int> GetBaseMinimumPlayoutDelayMs(
  160. uint32_t ssrc) const override;
  161. bool CanInsertDtmf() override;
  162. bool InsertDtmf(uint32_t ssrc, int event, int duration) override;
  163. void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
  164. int64_t packet_time_us) override;
  165. void OnNetworkRouteChanged(const std::string& transport_name,
  166. const rtc::NetworkRoute& network_route) override;
  167. void OnReadyToSend(bool ready) override;
  168. bool GetStats(VoiceMediaInfo* info) override;
  169. // Set the audio sink for an existing stream.
  170. void SetRawAudioSink(
  171. uint32_t ssrc,
  172. std::unique_ptr<webrtc::AudioSinkInterface> sink) override;
  173. // Will set the audio sink on the latest unsignaled stream, future or
  174. // current. Only one stream at a time will use the sink.
  175. void SetDefaultRawAudioSink(
  176. std::unique_ptr<webrtc::AudioSinkInterface> sink) override;
  177. std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
  178. // Sets a frame transformer between encoder and packetizer, to transform
  179. // encoded frames before sending them out the network.
  180. void SetEncoderToPacketizerFrameTransformer(
  181. uint32_t ssrc,
  182. rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
  183. override;
  184. void SetDepacketizerToDecoderFrameTransformer(
  185. uint32_t ssrc,
  186. rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
  187. override;
  188. // implements Transport interface
  189. bool SendRtp(const uint8_t* data,
  190. size_t len,
  191. const webrtc::PacketOptions& options) override {
  192. rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen);
  193. rtc::PacketOptions rtc_options;
  194. rtc_options.packet_id = options.packet_id;
  195. if (DscpEnabled()) {
  196. rtc_options.dscp = PreferredDscp();
  197. }
  198. rtc_options.info_signaled_after_sent.included_in_feedback =
  199. options.included_in_feedback;
  200. rtc_options.info_signaled_after_sent.included_in_allocation =
  201. options.included_in_allocation;
  202. return VoiceMediaChannel::SendPacket(&packet, rtc_options);
  203. }
  204. bool SendRtcp(const uint8_t* data, size_t len) override {
  205. rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen);
  206. rtc::PacketOptions rtc_options;
  207. if (DscpEnabled()) {
  208. rtc_options.dscp = PreferredDscp();
  209. }
  210. return VoiceMediaChannel::SendRtcp(&packet, rtc_options);
  211. }
  212. private:
  213. bool SetOptions(const AudioOptions& options);
  214. bool SetRecvCodecs(const std::vector<AudioCodec>& codecs);
  215. bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
  216. bool SetLocalSource(uint32_t ssrc, AudioSource* source);
  217. bool MuteStream(uint32_t ssrc, bool mute);
  218. WebRtcVoiceEngine* engine() { return engine_; }
  219. void ChangePlayout(bool playout);
  220. int CreateVoEChannel();
  221. bool DeleteVoEChannel(int channel);
  222. bool SetMaxSendBitrate(int bps);
  223. void SetupRecording();
  224. // Check if 'ssrc' is an unsignaled stream, and if so mark it as not being
  225. // unsignaled anymore (i.e. it is now removed, or signaled), and return true.
  226. bool MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc);
  227. rtc::ThreadChecker worker_thread_checker_;
  228. WebRtcVoiceEngine* const engine_ = nullptr;
  229. std::vector<AudioCodec> send_codecs_;
  230. // TODO(kwiberg): decoder_map_ and recv_codecs_ store the exact same
  231. // information, in slightly different formats. Eliminate recv_codecs_.
  232. std::map<int, webrtc::SdpAudioFormat> decoder_map_;
  233. std::vector<AudioCodec> recv_codecs_;
  234. int max_send_bitrate_bps_ = 0;
  235. AudioOptions options_;
  236. absl::optional<int> dtmf_payload_type_;
  237. int dtmf_payload_freq_ = -1;
  238. bool recv_transport_cc_enabled_ = false;
  239. bool recv_nack_enabled_ = false;
  240. bool desired_playout_ = false;
  241. bool playout_ = false;
  242. bool send_ = false;
  243. webrtc::Call* const call_ = nullptr;
  244. const MediaConfig::Audio audio_config_;
  245. // Queue of unsignaled SSRCs; oldest at the beginning.
  246. std::vector<uint32_t> unsignaled_recv_ssrcs_;
  247. // This is a stream param that comes from the remote description, but wasn't
  248. // signaled with any a=ssrc lines. It holds the information that was signaled
  249. // before the unsignaled receive stream is created when the first packet is
  250. // received.
  251. StreamParams unsignaled_stream_params_;
  252. // Volume for unsignaled streams, which may be set before the stream exists.
  253. double default_recv_volume_ = 1.0;
  254. // Delay for unsignaled streams, which may be set before the stream exists.
  255. int default_recv_base_minimum_delay_ms_ = 0;
  256. // Sink for latest unsignaled stream - may be set before the stream exists.
  257. std::unique_ptr<webrtc::AudioSinkInterface> default_sink_;
  258. // Default SSRC to use for RTCP receiver reports in case of no signaled
  259. // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740
  260. // and https://code.google.com/p/chromium/issues/detail?id=547661
  261. uint32_t receiver_reports_ssrc_ = 0xFA17FA17u;
  262. class WebRtcAudioSendStream;
  263. std::map<uint32_t, WebRtcAudioSendStream*> send_streams_;
  264. std::vector<webrtc::RtpExtension> send_rtp_extensions_;
  265. std::string mid_;
  266. class WebRtcAudioReceiveStream;
  267. std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_;
  268. std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
  269. absl::optional<webrtc::AudioSendStream::Config::SendCodecSpec>
  270. send_codec_spec_;
  271. // TODO(kwiberg): Per-SSRC codec pair IDs?
  272. const webrtc::AudioCodecPairId codec_pair_id_ =
  273. webrtc::AudioCodecPairId::Create();
  274. // Per peer connection crypto options that last for the lifetime of the peer
  275. // connection.
  276. const webrtc::CryptoOptions crypto_options_;
  277. // Unsignaled streams have an option to have a frame decryptor set on them.
  278. rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
  279. unsignaled_frame_decryptor_;
  280. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel);
  281. };
  282. } // namespace cricket
  283. #endif // MEDIA_ENGINE_WEBRTC_VOICE_ENGINE_H_