blob: 57c9439efca75fa89603062ed8dea429e4cd51a1 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public:
solenberg26c8c912015-11-27 04:00:25 -080061 // Exposed for the WVoE/MC unit test.
62 static bool ToCodecInst(const AudioCodec& in, webrtc::CodecInst* out);
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 WebRtcVoiceEngine();
65 // Dependency injection for testing.
solenbergbd138382015-11-20 16:08:07 -080066 explicit WebRtcVoiceEngine(VoEWrapper* voe_wrapper);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 ~WebRtcVoiceEngine();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068 bool Init(rtc::Thread* worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 void Terminate();
70
solenberg566ef242015-11-06 15:34:49 -080071 rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020072 VoiceMediaChannel* CreateChannel(webrtc::Call* call,
73 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000075 AudioOptions GetOptions() const { return options_; }
76 bool SetOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 bool SetDevices(const Device* in_device, const Device* out_device);
78 bool GetOutputVolume(int* level);
79 bool SetOutputVolume(int level);
80 int GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
82 const std::vector<AudioCodec>& codecs();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
84
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 // For tracking WebRtc channels. Needed because we have to pause them
86 // all when switching devices.
87 // May only be called by WebRtcVoiceMediaChannel.
solenberg63b34542015-09-29 06:06:31 -070088 void RegisterChannel(WebRtcVoiceMediaChannel* channel);
89 void UnregisterChannel(WebRtcVoiceMediaChannel* channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 // Called by WebRtcVoiceMediaChannel to set a gain offset from
92 // the default AGC target level.
93 bool AdjustAgcLevel(int delta);
94
95 VoEWrapper* voe() { return voe_wrapper_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 int GetLastEngineError();
97
Fredrik Solenbergccb49e72015-05-19 11:37:56 +020098 // Set the external ADM. This can only be called before Init.
99 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
wu@webrtc.orga9890802013-12-13 00:21:03 +0000101 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000103
ivoc797ef122015-10-22 03:25:41 -0700104 // Stops AEC dump.
105 void StopAecDump();
106
ivoc112a3d82015-10-16 02:22:18 -0700107 // Starts recording an RtcEventLog using an existing file until 10 minutes
108 // pass or the StopRtcEventLog function is called.
109 bool StartRtcEventLog(rtc::PlatformFile file);
110
111 // Stops recording the RtcEventLog.
112 void StopRtcEventLog();
113
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 void Construct();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 bool InitInternal();
solenberg63b34542015-09-29 06:06:31 -0700117 // Every option that is "set" will be applied. Every option not "set" will be
118 // ignored. This allows us to selectively turn on and off different options
119 // easily at any time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 bool ApplyOptions(const AudioOptions& options);
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000121
122 // webrtc::TraceCallback:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 void Print(webrtc::TraceLevel level, const char* trace, int length) override;
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Given the device type, name, and id, find device id. Return true and
126 // set the output parameter rtc_id if successful.
127 bool FindWebRtcAudioDeviceId(
128 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 void StartAecDump(const std::string& filename);
solenberg0a617e22015-10-20 15:49:38 -0700131 int CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
solenberg566ef242015-11-06 15:34:49 -0800133 rtc::ThreadChecker signal_thread_checker_;
134 rtc::ThreadChecker worker_thread_checker_;
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 // The primary instance of WebRtc VoiceEngine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 rtc::scoped_ptr<VoEWrapper> voe_wrapper_;
solenberg566ef242015-11-06 15:34:49 -0800138 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 // The external audio device manager
solenberg566ef242015-11-06 15:34:49 -0800140 webrtc::AudioDeviceModule* adm_ = nullptr;
solenberg566ef242015-11-06 15:34:49 -0800141 bool is_dumping_aec_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 std::vector<AudioCodec> codecs_;
143 std::vector<RtpHeaderExtension> rtp_header_extensions_;
solenberg63b34542015-09-29 06:06:31 -0700144 std::vector<WebRtcVoiceMediaChannel*> channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 webrtc::AgcConfig default_agc_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000146
147 webrtc::Config voe_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000148
solenberg566ef242015-11-06 15:34:49 -0800149 bool initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 AudioOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
Henrik Lundin441f6342015-06-09 16:03:13 +0200152 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100153 // values, and apply them in case they are missing in the audio options. We
154 // need to do this because SetExtraOptions() will revert to defaults for
155 // options which are not provided.
Karl Wibergbe579832015-11-10 22:34:18 +0100156 rtc::Optional<bool> extended_filter_aec_;
157 rtc::Optional<bool> delay_agnostic_aec_;
158 rtc::Optional<bool> experimental_ns_;
solenbergc96df772015-10-21 13:01:53 -0700159
160 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161};
162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163// WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
164// WebRtc Voice Engine.
solenberg566ef242015-11-06 15:34:49 -0800165class WebRtcVoiceMediaChannel final : public VoiceMediaChannel,
166 public webrtc::Transport {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 public:
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200168 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
169 const AudioOptions& options,
170 webrtc::Call* call);
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200171 ~WebRtcVoiceMediaChannel() override;
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200172
solenberg66f43392015-09-09 01:36:22 -0700173 const AudioOptions& options() const { return options_; }
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200174
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700175 bool SetSendParameters(const AudioSendParameters& params) override;
176 bool SetRecvParameters(const AudioRecvParameters& params) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200177 bool SetPlayout(bool playout) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 bool PausePlayout();
179 bool ResumePlayout();
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200180 bool SetSend(SendFlags send) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 bool PauseSend();
182 bool ResumeSend();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200183 bool SetAudioSend(uint32_t ssrc,
184 bool enable,
185 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700186 AudioRenderer* renderer) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200187 bool AddSendStream(const StreamParams& sp) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 bool RemoveSendStream(uint32_t ssrc) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200189 bool AddRecvStream(const StreamParams& sp) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200190 bool RemoveRecvStream(uint32_t ssrc) override;
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200191 bool GetActiveStreams(AudioInfo::StreamList* actives) override;
192 int GetOutputLevel() override;
193 int GetTimeSinceLastTyping() override;
194 void SetTypingDetectionParameters(int time_window,
195 int cost_per_typing,
196 int reporting_threshold,
197 int penalty_decay,
198 int type_event_delay) override;
solenberg4bac9c52015-10-09 02:32:53 -0700199 bool SetOutputVolume(uint32_t ssrc, double volume) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200201 bool CanInsertDtmf() override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200202 bool InsertDtmf(uint32_t ssrc, int event, int duration, int flags) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200204 void OnPacketReceived(rtc::Buffer* packet,
205 const rtc::PacketTime& packet_time) override;
206 void OnRtcpReceived(rtc::Buffer* packet,
207 const rtc::PacketTime& packet_time) override;
208 void OnReadyToSend(bool ready) override {}
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200209 bool GetStats(VoiceMediaInfo* info) override;
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200210
211 // implements Transport interface
stefan1d8a5062015-10-02 03:39:33 -0700212 bool SendRtp(const uint8_t* data,
213 size_t len,
214 const webrtc::PacketOptions& options) override {
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200215 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
216 kMaxRtpPacketLen);
stefanc1aeaf02015-10-15 07:26:07 -0700217 rtc::PacketOptions rtc_options;
218 rtc_options.packet_id = options.packet_id;
219 return VoiceMediaChannel::SendPacket(&packet, rtc_options);
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200220 }
221
pbos2d566682015-09-28 09:59:31 -0700222 bool SendRtcp(const uint8_t* data, size_t len) override {
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200223 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
224 kMaxRtpPacketLen);
stefanc1aeaf02015-10-15 07:26:07 -0700225 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions());
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200226 }
227
Peter Boström0c4e06b2015-10-07 12:23:21 +0200228 int GetReceiveChannelId(uint32_t ssrc) const;
229 int GetSendChannelId(uint32_t ssrc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200231 private:
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200232 bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200233 bool SetOptions(const AudioOptions& options);
234 bool SetMaxSendBandwidth(int bps);
235 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200236 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer);
237 bool MuteStream(uint32_t ssrc, bool mute);
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200238
Fredrik Solenberge444a3d2015-05-07 16:42:08 +0200239 WebRtcVoiceEngine* engine() { return engine_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 int GetLastEngineError() { return engine()->GetLastEngineError(); }
241 int GetOutputLevel(int channel);
242 bool GetRedSendCodec(const AudioCodec& red_codec,
243 const std::vector<AudioCodec>& all_codecs,
244 webrtc::CodecInst* send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 bool SetPlayout(int channel, bool playout);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000246 void SetNack(int channel, bool nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000247 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 bool ChangePlayout(bool playout);
249 bool ChangeSend(SendFlags send);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000250 bool ChangeSend(int channel, SendFlags send);
solenberg0a617e22015-10-20 15:49:38 -0700251 int CreateVoEChannel();
solenberg7add0582015-11-20 09:59:34 -0800252 bool DeleteVoEChannel(int channel);
solenberg1ac56142015-10-13 03:58:19 -0700253 bool IsDefaultRecvStream(uint32_t ssrc) {
254 return default_recv_ssrc_ == static_cast<int64_t>(ssrc);
255 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000256 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs);
minyue@webrtc.org26236952014-10-29 02:27:08 +0000257 bool SetSendBitrateInternal(int bps);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200258
solenberg566ef242015-11-06 15:34:49 -0800259 rtc::ThreadChecker worker_thread_checker_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200260
solenberg566ef242015-11-06 15:34:49 -0800261 WebRtcVoiceEngine* const engine_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 std::vector<AudioCodec> recv_codecs_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000263 std::vector<AudioCodec> send_codecs_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 rtc::scoped_ptr<webrtc::CodecInst> send_codec_;
solenberg566ef242015-11-06 15:34:49 -0800265 bool send_bitrate_setting_ = false;
266 int send_bitrate_bps_ = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 AudioOptions options_;
solenberg566ef242015-11-06 15:34:49 -0800268 bool dtmf_allowed_ = false;
269 bool desired_playout_ = false;
270 bool nack_enabled_ = false;
271 bool playout_ = false;
272 SendFlags desired_send_ = SEND_NOTHING;
273 SendFlags send_ = SEND_NOTHING;
274 webrtc::Call* const call_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275
solenberg1ac56142015-10-13 03:58:19 -0700276 // SSRC of unsignalled receive stream, or -1 if there isn't one.
277 int64_t default_recv_ssrc_ = -1;
278 // Volume for unsignalled stream, which may be set before the stream exists.
279 double default_recv_volume_ = 1.0;
solenberg8093d542015-11-12 06:02:30 -0800280 // Default SSRC to use for RTCP receiver reports in case of no signaled
solenberg0a617e22015-10-20 15:49:38 -0700281 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740
solenberg8093d542015-11-12 06:02:30 -0800282 // and https://code.google.com/p/chromium/issues/detail?id=547661
283 uint32_t receiver_reports_ssrc_ = 0xFA17FA17u;
solenberg1ac56142015-10-13 03:58:19 -0700284
solenbergc96df772015-10-21 13:01:53 -0700285 class WebRtcAudioSendStream;
286 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_;
solenberg3a941542015-11-16 07:34:50 -0800287 std::vector<webrtc::RtpExtension> send_rtp_extensions_;
solenbergc96df772015-10-21 13:01:53 -0700288
289 class WebRtcAudioReceiveStream;
solenberg7add0582015-11-20 09:59:34 -0800290 std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200291 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
solenbergc96df772015-10-21 13:01:53 -0700292
293 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295} // namespace cricket
296
297#endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_