blob: b8b50b0befe356ba3929ee5db32bbd450f4c5603 [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
36#include "talk/base/buffer.h"
37#include "talk/base/byteorder.h"
38#include "talk/base/logging.h"
39#include "talk/base/scoped_ptr.h"
40#include "talk/base/stream.h"
41#include "talk/media/base/rtputils.h"
42#include "talk/media/webrtc/webrtccommon.h"
43#include "talk/media/webrtc/webrtcexport.h"
44#include "talk/media/webrtc/webrtcvoe.h"
45#include "talk/session/media/channel.h"
46
47#if !defined(LIBPEERCONNECTION_LIB) && \
48 !defined(LIBPEERCONNECTION_IMPLEMENTATION)
49#error "Bogus include."
50#endif
51
52
53namespace cricket {
54
55// WebRtcSoundclipStream is an adapter object that allows a memory stream to be
56// passed into WebRtc, and support looping.
57class WebRtcSoundclipStream : public webrtc::InStream {
58 public:
59 WebRtcSoundclipStream(const char* buf, size_t len)
60 : mem_(buf, len), loop_(true) {
61 }
62 void set_loop(bool loop) { loop_ = loop; }
63 virtual int Read(void* buf, int len);
64 virtual int Rewind();
65
66 private:
67 talk_base::MemoryStream mem_;
68 bool loop_;
69};
70
71// WebRtcMonitorStream is used to monitor a stream coming from WebRtc.
72// For now we just dump the data.
73class WebRtcMonitorStream : public webrtc::OutStream {
74 virtual bool Write(const void *buf, int len) {
75 return true;
76 }
77};
78
79class AudioDeviceModule;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000080class AudioRenderer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081class VoETraceWrapper;
82class VoEWrapper;
83class VoiceProcessor;
84class WebRtcSoundclipMedia;
85class WebRtcVoiceMediaChannel;
86
87// WebRtcVoiceEngine is a class to be used with CompositeMediaEngine.
88// It uses the WebRtc VoiceEngine library for audio handling.
89class WebRtcVoiceEngine
90 : public webrtc::VoiceEngineObserver,
91 public webrtc::TraceCallback,
92 public webrtc::VoEMediaProcess {
93 public:
94 WebRtcVoiceEngine();
95 // Dependency injection for testing.
96 WebRtcVoiceEngine(VoEWrapper* voe_wrapper,
97 VoEWrapper* voe_wrapper_sc,
98 VoETraceWrapper* tracing);
99 ~WebRtcVoiceEngine();
100 bool Init(talk_base::Thread* worker_thread);
101 void Terminate();
102
103 int GetCapabilities();
104 VoiceMediaChannel* CreateChannel();
105
106 SoundclipMedia* CreateSoundclip();
107
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000108 AudioOptions GetOptions() const { return options_; }
109 bool SetOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 // Overrides, when set, take precedence over the options on a
111 // per-option basis. For example, if AGC is set in options and AEC
112 // is set in overrides, AGC and AEC will be both be set. Overrides
113 // can also turn off options. For example, if AGC is set to "on" in
114 // options and AGC is set to "off" in overrides, the result is that
115 // AGC will be off until different overrides are applied or until
116 // the overrides are cleared. Only one set of overrides is present
117 // at a time (they do not "stack"). And when the overrides are
118 // cleared, the media engine's state reverts back to the options set
119 // via SetOptions. This allows us to have both "persistent options"
120 // (the normal options) and "temporary options" (overrides).
121 bool SetOptionOverrides(const AudioOptions& options);
122 bool ClearOptionOverrides();
123 bool SetDelayOffset(int offset);
124 bool SetDevices(const Device* in_device, const Device* out_device);
125 bool GetOutputVolume(int* level);
126 bool SetOutputVolume(int level);
127 int GetInputLevel();
128 bool SetLocalMonitor(bool enable);
129
130 const std::vector<AudioCodec>& codecs();
131 bool FindCodec(const AudioCodec& codec);
132 bool FindWebRtcCodec(const AudioCodec& codec, webrtc::CodecInst* gcodec);
133
134 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
135
136 void SetLogging(int min_sev, const char* filter);
137
138 bool RegisterProcessor(uint32 ssrc,
139 VoiceProcessor* voice_processor,
140 MediaProcessorDirection direction);
141 bool UnregisterProcessor(uint32 ssrc,
142 VoiceProcessor* voice_processor,
143 MediaProcessorDirection direction);
144
145 // Method from webrtc::VoEMediaProcess
146 virtual void Process(int channel,
147 webrtc::ProcessingTypes type,
148 int16_t audio10ms[],
149 int length,
150 int sampling_freq,
151 bool is_stereo);
152
153 // For tracking WebRtc channels. Needed because we have to pause them
154 // all when switching devices.
155 // May only be called by WebRtcVoiceMediaChannel.
156 void RegisterChannel(WebRtcVoiceMediaChannel *channel);
157 void UnregisterChannel(WebRtcVoiceMediaChannel *channel);
158
159 // May only be called by WebRtcSoundclipMedia.
160 void RegisterSoundclip(WebRtcSoundclipMedia *channel);
161 void UnregisterSoundclip(WebRtcSoundclipMedia *channel);
162
163 // Called by WebRtcVoiceMediaChannel to set a gain offset from
164 // the default AGC target level.
165 bool AdjustAgcLevel(int delta);
166
167 VoEWrapper* voe() { return voe_wrapper_.get(); }
168 VoEWrapper* voe_sc() { return voe_wrapper_sc_.get(); }
169 int GetLastEngineError();
170
171 // Set the external ADMs. This can only be called before Init.
172 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm,
173 webrtc::AudioDeviceModule* adm_sc);
174
175 // Check whether the supplied trace should be ignored.
176 bool ShouldIgnoreTrace(const std::string& trace);
177
178 private:
179 typedef std::vector<WebRtcSoundclipMedia *> SoundclipList;
180 typedef std::vector<WebRtcVoiceMediaChannel *> ChannelList;
181 typedef sigslot::
182 signal3<uint32, MediaProcessorDirection, AudioFrame*> FrameSignal;
183
184 void Construct();
185 void ConstructCodecs();
186 bool InitInternal();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000187 bool EnsureSoundclipEngineInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 void SetTraceFilter(int filter);
189 void SetTraceOptions(const std::string& options);
190 // Applies either options or overrides. Every option that is "set"
191 // will be applied. Every option not "set" will be ignored. This
192 // allows us to selectively turn on and off different options easily
193 // at any time.
194 bool ApplyOptions(const AudioOptions& options);
195 virtual void Print(webrtc::TraceLevel level, const char* trace, int length);
196 virtual void CallbackOnError(int channel, int errCode);
197 // Given the device type, name, and id, find device id. Return true and
198 // set the output parameter rtc_id if successful.
199 bool FindWebRtcAudioDeviceId(
200 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id);
201 bool FindChannelAndSsrc(int channel_num,
202 WebRtcVoiceMediaChannel** channel,
203 uint32* ssrc) const;
204 bool FindChannelNumFromSsrc(uint32 ssrc,
205 MediaProcessorDirection direction,
206 int* channel_num);
207 bool ChangeLocalMonitor(bool enable);
208 bool PauseLocalMonitor();
209 bool ResumeLocalMonitor();
210
211 bool UnregisterProcessorChannel(MediaProcessorDirection channel_direction,
212 uint32 ssrc,
213 VoiceProcessor* voice_processor,
214 MediaProcessorDirection processor_direction);
215
216 void StartAecDump(const std::string& filename);
217 void StopAecDump();
218
219 // When a voice processor registers with the engine, it is connected
220 // to either the Rx or Tx signals, based on the direction parameter.
221 // SignalXXMediaFrame will be invoked for every audio packet.
222 FrameSignal SignalRxMediaFrame;
223 FrameSignal SignalTxMediaFrame;
224
225 static const int kDefaultLogSeverity = talk_base::LS_WARNING;
226
227 // The primary instance of WebRtc VoiceEngine.
228 talk_base::scoped_ptr<VoEWrapper> voe_wrapper_;
229 // A secondary instance, for playing out soundclips (on the 'ring' device).
230 talk_base::scoped_ptr<VoEWrapper> voe_wrapper_sc_;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000231 bool voe_wrapper_sc_initialized_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 talk_base::scoped_ptr<VoETraceWrapper> tracing_;
233 // The external audio device manager
234 webrtc::AudioDeviceModule* adm_;
235 webrtc::AudioDeviceModule* adm_sc_;
236 int log_filter_;
237 std::string log_options_;
238 bool is_dumping_aec_;
239 std::vector<AudioCodec> codecs_;
240 std::vector<RtpHeaderExtension> rtp_header_extensions_;
241 bool desired_local_monitor_enable_;
242 talk_base::scoped_ptr<WebRtcMonitorStream> monitor_;
243 SoundclipList soundclips_;
244 ChannelList channels_;
245 // channels_ can be read from WebRtc callback thread. We need a lock on that
246 // callback as well as the RegisterChannel/UnregisterChannel.
247 talk_base::CriticalSection channels_cs_;
248 webrtc::AgcConfig default_agc_config_;
249 bool initialized_;
250 // See SetOptions and SetOptionOverrides for a description of the
251 // difference between options and overrides.
252 // options_ are the base options, which combined with the
253 // option_overrides_, create the current options being used.
254 // options_ is stored so that when option_overrides_ is cleared, we
255 // can restore the options_ without the option_overrides.
256 AudioOptions options_;
257 AudioOptions option_overrides_;
258
259 // When the media processor registers with the engine, the ssrc is cached
260 // here so that a look up need not be made when the callback is invoked.
261 // This is necessary because the lookup results in mux_channels_cs lock being
262 // held and if a remote participant leaves the hangout at the same time
263 // we hit a deadlock.
264 uint32 tx_processor_ssrc_;
265 uint32 rx_processor_ssrc_;
266
267 talk_base::CriticalSection signal_media_critical_;
268};
269
270// WebRtcMediaChannel is a class that implements the common WebRtc channel
271// functionality.
272template <class T, class E>
273class WebRtcMediaChannel : public T, public webrtc::Transport {
274 public:
275 WebRtcMediaChannel(E *engine, int channel)
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000276 : engine_(engine), voe_channel_(channel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 E *engine() { return engine_; }
278 int voe_channel() const { return voe_channel_; }
279 bool valid() const { return voe_channel_ != -1; }
280
281 protected:
282 // implements Transport interface
283 virtual int SendPacket(int channel, const void *data, int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000285 if (!T::SendPacket(&packet)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 return -1;
287 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000288 return len;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000290
291 virtual int SendRTCPPacket(int channel, const void *data, int len) {
292 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
293 return T::SendRtcp(&packet) ? len : -1;
294 }
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 private:
297 E *engine_;
298 int voe_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299};
300
301// WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
302// WebRtc Voice Engine.
303class WebRtcVoiceMediaChannel
304 : public WebRtcMediaChannel<VoiceMediaChannel, WebRtcVoiceEngine> {
305 public:
306 explicit WebRtcVoiceMediaChannel(WebRtcVoiceEngine *engine);
307 virtual ~WebRtcVoiceMediaChannel();
308 virtual bool SetOptions(const AudioOptions& options);
309 virtual bool GetOptions(AudioOptions* options) const {
310 *options = options_;
311 return true;
312 }
313 virtual bool SetRecvCodecs(const std::vector<AudioCodec> &codecs);
314 virtual bool SetSendCodecs(const std::vector<AudioCodec> &codecs);
315 virtual bool SetRecvRtpHeaderExtensions(
316 const std::vector<RtpHeaderExtension>& extensions);
317 virtual bool SetSendRtpHeaderExtensions(
318 const std::vector<RtpHeaderExtension>& extensions);
319 virtual bool SetPlayout(bool playout);
320 bool PausePlayout();
321 bool ResumePlayout();
322 virtual bool SetSend(SendFlags send);
323 bool PauseSend();
324 bool ResumeSend();
325 virtual bool AddSendStream(const StreamParams& sp);
326 virtual bool RemoveSendStream(uint32 ssrc);
327 virtual bool AddRecvStream(const StreamParams& sp);
328 virtual bool RemoveRecvStream(uint32 ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000329 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
330 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 virtual bool GetActiveStreams(AudioInfo::StreamList* actives);
332 virtual int GetOutputLevel();
333 virtual int GetTimeSinceLastTyping();
334 virtual void SetTypingDetectionParameters(int time_window,
335 int cost_per_typing, int reporting_threshold, int penalty_decay,
336 int type_event_delay);
337 virtual bool SetOutputScaling(uint32 ssrc, double left, double right);
338 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right);
339
340 virtual bool SetRingbackTone(const char *buf, int len);
341 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
342 virtual bool CanInsertDtmf();
343 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags);
344
345 virtual void OnPacketReceived(talk_base::Buffer* packet);
346 virtual void OnRtcpReceived(talk_base::Buffer* packet);
347 virtual void OnReadyToSend(bool ready) {}
348 virtual bool MuteStream(uint32 ssrc, bool on);
349 virtual bool SetSendBandwidth(bool autobw, int bps);
350 virtual bool GetStats(VoiceMediaInfo* info);
351 // Gets last reported error from WebRtc voice engine. This should be only
352 // called in response a failure.
353 virtual void GetLastMediaError(uint32* ssrc,
354 VoiceMediaChannel::Error* error);
355 bool FindSsrc(int channel_num, uint32* ssrc);
356 void OnError(uint32 ssrc, int error);
357
358 bool sending() const { return send_ != SEND_NOTHING; }
359 int GetReceiveChannelNum(uint32 ssrc);
360 int GetSendChannelNum(uint32 ssrc);
361
362 protected:
363 int GetLastEngineError() { return engine()->GetLastEngineError(); }
364 int GetOutputLevel(int channel);
365 bool GetRedSendCodec(const AudioCodec& red_codec,
366 const std::vector<AudioCodec>& all_codecs,
367 webrtc::CodecInst* send_codec);
368 bool EnableRtcp(int channel);
369 bool ResetRecvCodecs(int channel);
370 bool SetPlayout(int channel, bool playout);
371 static uint32 ParseSsrc(const void* data, size_t len, bool rtcp);
372 static Error WebRtcErrorToChannelError(int err_code);
373
374 private:
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000375 struct WebRtcVoiceChannelInfo;
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000376 typedef std::map<uint32, WebRtcVoiceChannelInfo> ChannelMap;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000377
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000378 void SetNack(int channel, bool nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000379 void SetNack(const ChannelMap& channels, bool nack_enabled);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 bool SetSendCodec(const webrtc::CodecInst& send_codec);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000381 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 bool ChangePlayout(bool playout);
383 bool ChangeSend(SendFlags send);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000384 bool ChangeSend(int channel, SendFlags send);
385 void ConfigureSendChannel(int channel);
wu@webrtc.org78187522013-10-07 23:32:02 +0000386 bool ConfigureRecvChannel(int channel);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000387 bool DeleteChannel(int channel);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000388 bool InConferenceMode() const {
389 return options_.conference_mode.GetWithDefaultIfUnset(false);
390 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000391 bool IsDefaultChannel(int channel_id) const {
392 return channel_id == voe_channel();
393 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000394 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000395 bool SetSendBandwidthInternal(bool autobw, int bps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 talk_base::scoped_ptr<WebRtcSoundclipStream> ringback_tone_;
398 std::set<int> ringback_channels_; // channels playing ringback
399 std::vector<AudioCodec> recv_codecs_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000400 std::vector<AudioCodec> send_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 talk_base::scoped_ptr<webrtc::CodecInst> send_codec_;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000402 bool send_bw_setting_;
403 bool send_autobw_;
404 int send_bw_bps_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 AudioOptions options_;
406 bool dtmf_allowed_;
407 bool desired_playout_;
408 bool nack_enabled_;
409 bool playout_;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000410 bool typing_noise_detected_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 SendFlags desired_send_;
412 SendFlags send_;
413
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000414 // send_channels_ contains the channels which are being used for sending.
415 // When the default channel (voe_channel) is used for sending, it is
416 // contained in send_channels_, otherwise not.
417 ChannelMap send_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 uint32 default_receive_ssrc_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000419 // Note the default channel (voe_channel()) can reside in both
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000420 // receive_channels_ and send_channels_ in non-conference mode and in that
421 // case it will only be there if a non-zero default_receive_ssrc_ is set.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000422 ChannelMap receive_channels_; // for multiple sources
423 // receive_channels_ can be read from WebRtc callback thread. Access from
424 // the WebRtc thread must be synchronized with edits on the worker thread.
425 // Reads on the worker thread are ok.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 //
427 // Do not lock this on the VoE media processor thread; potential for deadlock
428 // exists.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000429 mutable talk_base::CriticalSection receive_channels_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430};
431
432} // namespace cricket
433
434#endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_