webrtc_voice_engine.h 14 KB

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