blob: 9c7f7bb525be79d0883b06c2750cc9f80a5431c7 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_WEBRTCVOICEENGINE_H_
29#define TALK_MEDIA_WEBRTCVOICEENGINE_H_
30
31#include <map>
32#include <set>
33#include <string>
34#include <vector>
35
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/media/base/rtputils.h"
37#include "talk/media/webrtc/webrtccommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/media/webrtc/webrtcvoe.h"
39#include "talk/session/media/channel.h"
solenberg566ef242015-11-06 15:34:49 -080040#include "webrtc/audio_state.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000041#include "webrtc/base/buffer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "webrtc/base/scoped_ptr.h"
43#include "webrtc/base/stream.h"
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020044#include "webrtc/base/thread_checker.h"
45#include "webrtc/call.h"
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000046#include "webrtc/common.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020047#include "webrtc/config.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049namespace cricket {
50
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051class AudioDeviceModule;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000052class AudioRenderer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053class VoEWrapper;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054class WebRtcVoiceMediaChannel;
55
56// WebRtcVoiceEngine is a class to be used with CompositeMediaEngine.
57// It uses the WebRtc VoiceEngine library for audio handling.
solenberg566ef242015-11-06 15:34:49 -080058class WebRtcVoiceEngine final : public webrtc::TraceCallback {
Jelena Marusicc28a8962015-05-29 15:05:44 +020059 friend class WebRtcVoiceMediaChannel;
60
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 public:
62 WebRtcVoiceEngine();
63 // Dependency injection for testing.
solenbergbd138382015-11-20 16:08:07 -080064 explicit WebRtcVoiceEngine(VoEWrapper* voe_wrapper);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 ~WebRtcVoiceEngine();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 bool Init(rtc::Thread* worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 void Terminate();
68
solenberg566ef242015-11-06 15:34:49 -080069 rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020070 VoiceMediaChannel* CreateChannel(webrtc::Call* call,
71 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000073 AudioOptions GetOptions() const { return options_; }
74 bool SetOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 bool SetDevices(const Device* in_device, const Device* out_device);
76 bool GetOutputVolume(int* level);
77 bool SetOutputVolume(int level);
78 int GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
80 const std::vector<AudioCodec>& codecs();
81 bool FindCodec(const AudioCodec& codec);
82 bool FindWebRtcCodec(const AudioCodec& codec, webrtc::CodecInst* gcodec);
83
84 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
85
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 // For tracking WebRtc channels. Needed because we have to pause them
87 // all when switching devices.
88 // May only be called by WebRtcVoiceMediaChannel.
solenberg63b34542015-09-29 06:06:31 -070089 void RegisterChannel(WebRtcVoiceMediaChannel* channel);
90 void UnregisterChannel(WebRtcVoiceMediaChannel* channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 // Called by WebRtcVoiceMediaChannel to set a gain offset from
93 // the default AGC target level.
94 bool AdjustAgcLevel(int delta);
95
96 VoEWrapper* voe() { return voe_wrapper_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 int GetLastEngineError();
98
Fredrik Solenbergccb49e72015-05-19 11:37:56 +020099 // Set the external ADM. This can only be called before Init.
100 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
wu@webrtc.orga9890802013-12-13 00:21:03 +0000102 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000104
ivoc797ef122015-10-22 03:25:41 -0700105 // Stops AEC dump.
106 void StopAecDump();
107
ivoc112a3d82015-10-16 02:22:18 -0700108 // Starts recording an RtcEventLog using an existing file until 10 minutes
109 // pass or the StopRtcEventLog function is called.
110 bool StartRtcEventLog(rtc::PlatformFile file);
111
112 // Stops recording the RtcEventLog.
113 void StopRtcEventLog();
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 void Construct();
117 void ConstructCodecs();
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000118 bool GetVoeCodec(int index, webrtc::CodecInst* codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 bool InitInternal();
solenberg63b34542015-09-29 06:06:31 -0700120 // Every option that is "set" will be applied. Every option not "set" will be
121 // ignored. This allows us to selectively turn on and off different options
122 // easily at any time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 bool ApplyOptions(const AudioOptions& options);
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000124
125 // webrtc::TraceCallback:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 void Print(webrtc::TraceLevel level, const char* trace, int length) override;
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 // Given the device type, name, and id, find device id. Return true and
129 // set the output parameter rtc_id if successful.
130 bool FindWebRtcAudioDeviceId(
131 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
133 void StartAecDump(const std::string& filename);
solenberg0a617e22015-10-20 15:49:38 -0700134 int CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
solenberg566ef242015-11-06 15:34:49 -0800136 rtc::ThreadChecker signal_thread_checker_;
137 rtc::ThreadChecker worker_thread_checker_;
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 // The primary instance of WebRtc VoiceEngine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 rtc::scoped_ptr<VoEWrapper> voe_wrapper_;
solenberg566ef242015-11-06 15:34:49 -0800141 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 // The external audio device manager
solenberg566ef242015-11-06 15:34:49 -0800143 webrtc::AudioDeviceModule* adm_ = nullptr;
solenberg566ef242015-11-06 15:34:49 -0800144 bool is_dumping_aec_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 std::vector<AudioCodec> codecs_;
146 std::vector<RtpHeaderExtension> rtp_header_extensions_;
solenberg63b34542015-09-29 06:06:31 -0700147 std::vector<WebRtcVoiceMediaChannel*> channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 webrtc::AgcConfig default_agc_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000149
150 webrtc::Config voe_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000151
solenberg566ef242015-11-06 15:34:49 -0800152 bool initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 AudioOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
Henrik Lundin441f6342015-06-09 16:03:13 +0200155 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100156 // values, and apply them in case they are missing in the audio options. We
157 // need to do this because SetExtraOptions() will revert to defaults for
158 // options which are not provided.
Karl Wibergbe579832015-11-10 22:34:18 +0100159 rtc::Optional<bool> extended_filter_aec_;
160 rtc::Optional<bool> delay_agnostic_aec_;
161 rtc::Optional<bool> experimental_ns_;
solenbergc96df772015-10-21 13:01:53 -0700162
163 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164};
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166// WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
167// WebRtc Voice Engine.
solenberg566ef242015-11-06 15:34:49 -0800168class WebRtcVoiceMediaChannel final : public VoiceMediaChannel,
169 public webrtc::Transport {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 public:
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200171 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
172 const AudioOptions& options,
173 webrtc::Call* call);
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200174 ~WebRtcVoiceMediaChannel() override;
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200175
solenberg66f43392015-09-09 01:36:22 -0700176 const AudioOptions& options() const { return options_; }
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200177
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700178 bool SetSendParameters(const AudioSendParameters& params) override;
179 bool SetRecvParameters(const AudioRecvParameters& params) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200180 bool SetPlayout(bool playout) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 bool PausePlayout();
182 bool ResumePlayout();
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200183 bool SetSend(SendFlags send) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 bool PauseSend();
185 bool ResumeSend();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200186 bool SetAudioSend(uint32_t ssrc,
187 bool enable,
188 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700189 AudioRenderer* renderer) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200190 bool AddSendStream(const StreamParams& sp) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200191 bool RemoveSendStream(uint32_t ssrc) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200192 bool AddRecvStream(const StreamParams& sp) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200193 bool RemoveRecvStream(uint32_t ssrc) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200194 bool GetActiveStreams(AudioInfo::StreamList* actives) override;
195 int GetOutputLevel() override;
196 int GetTimeSinceLastTyping() override;
197 void SetTypingDetectionParameters(int time_window,
198 int cost_per_typing,
199 int reporting_threshold,
200 int penalty_decay,
201 int type_event_delay) override;
solenberg4bac9c52015-10-09 02:32:53 -0700202 bool SetOutputVolume(uint32_t ssrc, double volume) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200204 bool CanInsertDtmf() override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200205 bool InsertDtmf(uint32_t ssrc, int event, int duration, int flags) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200207 void OnPacketReceived(rtc::Buffer* packet,
208 const rtc::PacketTime& packet_time) override;
209 void OnRtcpReceived(rtc::Buffer* packet,
210 const rtc::PacketTime& packet_time) override;
211 void OnReadyToSend(bool ready) override {}
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200212 bool GetStats(VoiceMediaInfo* info) override;
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200213
214 // implements Transport interface
stefan1d8a5062015-10-02 03:39:33 -0700215 bool SendRtp(const uint8_t* data,
216 size_t len,
217 const webrtc::PacketOptions& options) override {
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200218 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
219 kMaxRtpPacketLen);
stefanc1aeaf02015-10-15 07:26:07 -0700220 rtc::PacketOptions rtc_options;
221 rtc_options.packet_id = options.packet_id;
222 return VoiceMediaChannel::SendPacket(&packet, rtc_options);
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200223 }
224
pbos2d566682015-09-28 09:59:31 -0700225 bool SendRtcp(const uint8_t* data, size_t len) override {
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200226 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
227 kMaxRtpPacketLen);
stefanc1aeaf02015-10-15 07:26:07 -0700228 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions());
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200229 }
230
Peter Boström0c4e06b2015-10-07 12:23:21 +0200231 int GetReceiveChannelId(uint32_t ssrc) const;
232 int GetSendChannelId(uint32_t ssrc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200234 private:
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200235 bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200236 bool SetOptions(const AudioOptions& options);
237 bool SetMaxSendBandwidth(int bps);
238 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200239 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer);
240 bool MuteStream(uint32_t ssrc, bool mute);
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200241
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200242 WebRtcVoiceEngine* engine() { return engine_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 int GetLastEngineError() { return engine()->GetLastEngineError(); }
244 int GetOutputLevel(int channel);
245 bool GetRedSendCodec(const AudioCodec& red_codec,
246 const std::vector<AudioCodec>& all_codecs,
247 webrtc::CodecInst* send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 bool SetPlayout(int channel, bool playout);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000249 void SetNack(int channel, bool nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000250 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 bool ChangePlayout(bool playout);
252 bool ChangeSend(SendFlags send);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000253 bool ChangeSend(int channel, SendFlags send);
solenberg0a617e22015-10-20 15:49:38 -0700254 int CreateVoEChannel();
solenberg7add0582015-11-20 09:59:34 -0800255 bool DeleteVoEChannel(int channel);
solenberg1ac56142015-10-13 03:58:19 -0700256 bool IsDefaultRecvStream(uint32_t ssrc) {
257 return default_recv_ssrc_ == static_cast<int64_t>(ssrc);
258 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000259 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs);
minyue@webrtc.org26236952014-10-29 02:27:08 +0000260 bool SetSendBitrateInternal(int bps);
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200261 bool SetRecvCodecsInternal(const std::vector<AudioCodec>& new_codecs);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200262
solenberg566ef242015-11-06 15:34:49 -0800263 rtc::ThreadChecker worker_thread_checker_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200264
solenberg566ef242015-11-06 15:34:49 -0800265 WebRtcVoiceEngine* const engine_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 std::vector<AudioCodec> recv_codecs_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000267 std::vector<AudioCodec> send_codecs_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 rtc::scoped_ptr<webrtc::CodecInst> send_codec_;
solenberg566ef242015-11-06 15:34:49 -0800269 bool send_bitrate_setting_ = false;
270 int send_bitrate_bps_ = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 AudioOptions options_;
solenberg566ef242015-11-06 15:34:49 -0800272 bool dtmf_allowed_ = false;
273 bool desired_playout_ = false;
274 bool nack_enabled_ = false;
275 bool playout_ = false;
276 SendFlags desired_send_ = SEND_NOTHING;
277 SendFlags send_ = SEND_NOTHING;
278 webrtc::Call* const call_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279
solenberg1ac56142015-10-13 03:58:19 -0700280 // SSRC of unsignalled receive stream, or -1 if there isn't one.
281 int64_t default_recv_ssrc_ = -1;
282 // Volume for unsignalled stream, which may be set before the stream exists.
283 double default_recv_volume_ = 1.0;
solenberg8093d542015-11-12 06:02:30 -0800284 // Default SSRC to use for RTCP receiver reports in case of no signaled
solenberg0a617e22015-10-20 15:49:38 -0700285 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740
solenberg8093d542015-11-12 06:02:30 -0800286 // and https://code.google.com/p/chromium/issues/detail?id=547661
287 uint32_t receiver_reports_ssrc_ = 0xFA17FA17u;
solenberg1ac56142015-10-13 03:58:19 -0700288
solenbergc96df772015-10-21 13:01:53 -0700289 class WebRtcAudioSendStream;
290 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_;
solenberg3a941542015-11-16 07:34:50 -0800291 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
solenbergc96df772015-10-21 13:01:53 -0700292
293 class WebRtcAudioReceiveStream;
solenberg7add0582015-11-20 09:59:34 -0800294 std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200295 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
solenbergc96df772015-10-21 13:01:53 -0700296
297 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299} // namespace cricket
300
301#endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_