blob: edf07f42bb4a7508f7f16898c042ca811ceeacd0 [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;
80class VoETraceWrapper;
81class VoEWrapper;
82class VoiceProcessor;
83class WebRtcSoundclipMedia;
84class WebRtcVoiceMediaChannel;
85
86// WebRtcVoiceEngine is a class to be used with CompositeMediaEngine.
87// It uses the WebRtc VoiceEngine library for audio handling.
88class WebRtcVoiceEngine
89 : public webrtc::VoiceEngineObserver,
90 public webrtc::TraceCallback,
91 public webrtc::VoEMediaProcess {
92 public:
93 WebRtcVoiceEngine();
94 // Dependency injection for testing.
95 WebRtcVoiceEngine(VoEWrapper* voe_wrapper,
96 VoEWrapper* voe_wrapper_sc,
97 VoETraceWrapper* tracing);
98 ~WebRtcVoiceEngine();
99 bool Init(talk_base::Thread* worker_thread);
100 void Terminate();
101
102 int GetCapabilities();
103 VoiceMediaChannel* CreateChannel();
104
105 SoundclipMedia* CreateSoundclip();
106
107 // TODO(pthatcher): Rename to SetOptions and replace the old
108 // flags-based SetOptions.
109 bool SetAudioOptions(const AudioOptions& options);
110 // Eventually, we will replace them with AudioOptions.
111 // In the meantime, we leave this here for backwards compat.
112 bool SetOptions(int flags);
113 // Overrides, when set, take precedence over the options on a
114 // per-option basis. For example, if AGC is set in options and AEC
115 // is set in overrides, AGC and AEC will be both be set. Overrides
116 // can also turn off options. For example, if AGC is set to "on" in
117 // options and AGC is set to "off" in overrides, the result is that
118 // AGC will be off until different overrides are applied or until
119 // the overrides are cleared. Only one set of overrides is present
120 // at a time (they do not "stack"). And when the overrides are
121 // cleared, the media engine's state reverts back to the options set
122 // via SetOptions. This allows us to have both "persistent options"
123 // (the normal options) and "temporary options" (overrides).
124 bool SetOptionOverrides(const AudioOptions& options);
125 bool ClearOptionOverrides();
126 bool SetDelayOffset(int offset);
127 bool SetDevices(const Device* in_device, const Device* out_device);
128 bool GetOutputVolume(int* level);
129 bool SetOutputVolume(int level);
130 int GetInputLevel();
131 bool SetLocalMonitor(bool enable);
132
133 const std::vector<AudioCodec>& codecs();
134 bool FindCodec(const AudioCodec& codec);
135 bool FindWebRtcCodec(const AudioCodec& codec, webrtc::CodecInst* gcodec);
136
137 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
138
139 void SetLogging(int min_sev, const char* filter);
140
141 bool RegisterProcessor(uint32 ssrc,
142 VoiceProcessor* voice_processor,
143 MediaProcessorDirection direction);
144 bool UnregisterProcessor(uint32 ssrc,
145 VoiceProcessor* voice_processor,
146 MediaProcessorDirection direction);
147
148 // Method from webrtc::VoEMediaProcess
149 virtual void Process(int channel,
150 webrtc::ProcessingTypes type,
151 int16_t audio10ms[],
152 int length,
153 int sampling_freq,
154 bool is_stereo);
155
156 // For tracking WebRtc channels. Needed because we have to pause them
157 // all when switching devices.
158 // May only be called by WebRtcVoiceMediaChannel.
159 void RegisterChannel(WebRtcVoiceMediaChannel *channel);
160 void UnregisterChannel(WebRtcVoiceMediaChannel *channel);
161
162 // May only be called by WebRtcSoundclipMedia.
163 void RegisterSoundclip(WebRtcSoundclipMedia *channel);
164 void UnregisterSoundclip(WebRtcSoundclipMedia *channel);
165
166 // Called by WebRtcVoiceMediaChannel to set a gain offset from
167 // the default AGC target level.
168 bool AdjustAgcLevel(int delta);
169
170 VoEWrapper* voe() { return voe_wrapper_.get(); }
171 VoEWrapper* voe_sc() { return voe_wrapper_sc_.get(); }
172 int GetLastEngineError();
173
174 // Set the external ADMs. This can only be called before Init.
175 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm,
176 webrtc::AudioDeviceModule* adm_sc);
177
178 // Check whether the supplied trace should be ignored.
179 bool ShouldIgnoreTrace(const std::string& trace);
180
181 private:
182 typedef std::vector<WebRtcSoundclipMedia *> SoundclipList;
183 typedef std::vector<WebRtcVoiceMediaChannel *> ChannelList;
184 typedef sigslot::
185 signal3<uint32, MediaProcessorDirection, AudioFrame*> FrameSignal;
186
187 void Construct();
188 void ConstructCodecs();
189 bool InitInternal();
190 void SetTraceFilter(int filter);
191 void SetTraceOptions(const std::string& options);
192 // Applies either options or overrides. Every option that is "set"
193 // will be applied. Every option not "set" will be ignored. This
194 // allows us to selectively turn on and off different options easily
195 // at any time.
196 bool ApplyOptions(const AudioOptions& options);
197 virtual void Print(webrtc::TraceLevel level, const char* trace, int length);
198 virtual void CallbackOnError(int channel, int errCode);
199 // Given the device type, name, and id, find device id. Return true and
200 // set the output parameter rtc_id if successful.
201 bool FindWebRtcAudioDeviceId(
202 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id);
203 bool FindChannelAndSsrc(int channel_num,
204 WebRtcVoiceMediaChannel** channel,
205 uint32* ssrc) const;
206 bool FindChannelNumFromSsrc(uint32 ssrc,
207 MediaProcessorDirection direction,
208 int* channel_num);
209 bool ChangeLocalMonitor(bool enable);
210 bool PauseLocalMonitor();
211 bool ResumeLocalMonitor();
212
213 bool UnregisterProcessorChannel(MediaProcessorDirection channel_direction,
214 uint32 ssrc,
215 VoiceProcessor* voice_processor,
216 MediaProcessorDirection processor_direction);
217
218 void StartAecDump(const std::string& filename);
219 void StopAecDump();
220
221 // When a voice processor registers with the engine, it is connected
222 // to either the Rx or Tx signals, based on the direction parameter.
223 // SignalXXMediaFrame will be invoked for every audio packet.
224 FrameSignal SignalRxMediaFrame;
225 FrameSignal SignalTxMediaFrame;
226
227 static const int kDefaultLogSeverity = talk_base::LS_WARNING;
228
229 // The primary instance of WebRtc VoiceEngine.
230 talk_base::scoped_ptr<VoEWrapper> voe_wrapper_;
231 // A secondary instance, for playing out soundclips (on the 'ring' device).
232 talk_base::scoped_ptr<VoEWrapper> voe_wrapper_sc_;
233 talk_base::scoped_ptr<VoETraceWrapper> tracing_;
234 // The external audio device manager
235 webrtc::AudioDeviceModule* adm_;
236 webrtc::AudioDeviceModule* adm_sc_;
237 int log_filter_;
238 std::string log_options_;
239 bool is_dumping_aec_;
240 std::vector<AudioCodec> codecs_;
241 std::vector<RtpHeaderExtension> rtp_header_extensions_;
242 bool desired_local_monitor_enable_;
243 talk_base::scoped_ptr<WebRtcMonitorStream> monitor_;
244 SoundclipList soundclips_;
245 ChannelList channels_;
246 // channels_ can be read from WebRtc callback thread. We need a lock on that
247 // callback as well as the RegisterChannel/UnregisterChannel.
248 talk_base::CriticalSection channels_cs_;
249 webrtc::AgcConfig default_agc_config_;
250 bool initialized_;
251 // See SetOptions and SetOptionOverrides for a description of the
252 // difference between options and overrides.
253 // options_ are the base options, which combined with the
254 // option_overrides_, create the current options being used.
255 // options_ is stored so that when option_overrides_ is cleared, we
256 // can restore the options_ without the option_overrides.
257 AudioOptions options_;
258 AudioOptions option_overrides_;
259
260 // When the media processor registers with the engine, the ssrc is cached
261 // here so that a look up need not be made when the callback is invoked.
262 // This is necessary because the lookup results in mux_channels_cs lock being
263 // held and if a remote participant leaves the hangout at the same time
264 // we hit a deadlock.
265 uint32 tx_processor_ssrc_;
266 uint32 rx_processor_ssrc_;
267
268 talk_base::CriticalSection signal_media_critical_;
269};
270
271// WebRtcMediaChannel is a class that implements the common WebRtc channel
272// functionality.
273template <class T, class E>
274class WebRtcMediaChannel : public T, public webrtc::Transport {
275 public:
276 WebRtcMediaChannel(E *engine, int channel)
277 : engine_(engine), voe_channel_(channel), sequence_number_(-1) {}
278 E *engine() { return engine_; }
279 int voe_channel() const { return voe_channel_; }
280 bool valid() const { return voe_channel_ != -1; }
281
282 protected:
283 // implements Transport interface
284 virtual int SendPacket(int channel, const void *data, int len) {
285 if (!T::network_interface_) {
286 return -1;
287 }
288
289 // We need to store the sequence number to be able to pick up
290 // the same sequence when the device is restarted.
291 // TODO(oja): Remove when WebRtc has fixed the problem.
292 int seq_num;
293 if (!GetRtpSeqNum(data, len, &seq_num)) {
294 return -1;
295 }
296 if (sequence_number() == -1) {
297 LOG(INFO) << "WebRtcVoiceMediaChannel sends first packet seqnum="
298 << seq_num;
299 }
300 sequence_number_ = seq_num;
301
302 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
303 return T::network_interface_->SendPacket(&packet) ? len : -1;
304 }
305 virtual int SendRTCPPacket(int channel, const void *data, int len) {
306 if (!T::network_interface_) {
307 return -1;
308 }
309
310 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
311 return T::network_interface_->SendRtcp(&packet) ? len : -1;
312 }
313 int sequence_number() const {
314 return sequence_number_;
315 }
316
317 private:
318 E *engine_;
319 int voe_channel_;
320 int sequence_number_;
321};
322
323// WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
324// WebRtc Voice Engine.
325class WebRtcVoiceMediaChannel
326 : public WebRtcMediaChannel<VoiceMediaChannel, WebRtcVoiceEngine> {
327 public:
328 explicit WebRtcVoiceMediaChannel(WebRtcVoiceEngine *engine);
329 virtual ~WebRtcVoiceMediaChannel();
330 virtual bool SetOptions(const AudioOptions& options);
331 virtual bool GetOptions(AudioOptions* options) const {
332 *options = options_;
333 return true;
334 }
335 virtual bool SetRecvCodecs(const std::vector<AudioCodec> &codecs);
336 virtual bool SetSendCodecs(const std::vector<AudioCodec> &codecs);
337 virtual bool SetRecvRtpHeaderExtensions(
338 const std::vector<RtpHeaderExtension>& extensions);
339 virtual bool SetSendRtpHeaderExtensions(
340 const std::vector<RtpHeaderExtension>& extensions);
341 virtual bool SetPlayout(bool playout);
342 bool PausePlayout();
343 bool ResumePlayout();
344 virtual bool SetSend(SendFlags send);
345 bool PauseSend();
346 bool ResumeSend();
347 virtual bool AddSendStream(const StreamParams& sp);
348 virtual bool RemoveSendStream(uint32 ssrc);
349 virtual bool AddRecvStream(const StreamParams& sp);
350 virtual bool RemoveRecvStream(uint32 ssrc);
351 virtual bool SetRenderer(uint32 ssrc, AudioRenderer* renderer);
352 virtual bool GetActiveStreams(AudioInfo::StreamList* actives);
353 virtual int GetOutputLevel();
354 virtual int GetTimeSinceLastTyping();
355 virtual void SetTypingDetectionParameters(int time_window,
356 int cost_per_typing, int reporting_threshold, int penalty_decay,
357 int type_event_delay);
358 virtual bool SetOutputScaling(uint32 ssrc, double left, double right);
359 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right);
360
361 virtual bool SetRingbackTone(const char *buf, int len);
362 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
363 virtual bool CanInsertDtmf();
364 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags);
365
366 virtual void OnPacketReceived(talk_base::Buffer* packet);
367 virtual void OnRtcpReceived(talk_base::Buffer* packet);
368 virtual void OnReadyToSend(bool ready) {}
369 virtual bool MuteStream(uint32 ssrc, bool on);
370 virtual bool SetSendBandwidth(bool autobw, int bps);
371 virtual bool GetStats(VoiceMediaInfo* info);
372 // Gets last reported error from WebRtc voice engine. This should be only
373 // called in response a failure.
374 virtual void GetLastMediaError(uint32* ssrc,
375 VoiceMediaChannel::Error* error);
376 bool FindSsrc(int channel_num, uint32* ssrc);
377 void OnError(uint32 ssrc, int error);
378
379 bool sending() const { return send_ != SEND_NOTHING; }
380 int GetReceiveChannelNum(uint32 ssrc);
381 int GetSendChannelNum(uint32 ssrc);
382
383 protected:
384 int GetLastEngineError() { return engine()->GetLastEngineError(); }
385 int GetOutputLevel(int channel);
386 bool GetRedSendCodec(const AudioCodec& red_codec,
387 const std::vector<AudioCodec>& all_codecs,
388 webrtc::CodecInst* send_codec);
389 bool EnableRtcp(int channel);
390 bool ResetRecvCodecs(int channel);
391 bool SetPlayout(int channel, bool playout);
392 static uint32 ParseSsrc(const void* data, size_t len, bool rtcp);
393 static Error WebRtcErrorToChannelError(int err_code);
394
395 private:
396 void SetNack(uint32 ssrc, int channel, bool nack_enabled);
397 bool SetSendCodec(const webrtc::CodecInst& send_codec);
398 bool ChangePlayout(bool playout);
399 bool ChangeSend(SendFlags send);
400
401 typedef std::map<uint32, int> ChannelMap;
402 talk_base::scoped_ptr<WebRtcSoundclipStream> ringback_tone_;
403 std::set<int> ringback_channels_; // channels playing ringback
404 std::vector<AudioCodec> recv_codecs_;
405 talk_base::scoped_ptr<webrtc::CodecInst> send_codec_;
406 AudioOptions options_;
407 bool dtmf_allowed_;
408 bool desired_playout_;
409 bool nack_enabled_;
410 bool playout_;
411 SendFlags desired_send_;
412 SendFlags send_;
413
414 uint32 send_ssrc_;
415 uint32 default_receive_ssrc_;
416 ChannelMap mux_channels_; // for multiple sources
417 // mux_channels_ can be read from WebRtc callback thread. Accesses off the
418 // WebRtc thread must be synchronized with edits on the worker thread. Reads
419 // on the worker thread are ok.
420 //
421 // Do not lock this on the VoE media processor thread; potential for deadlock
422 // exists.
423 mutable talk_base::CriticalSection mux_channels_cs_;
424};
425
426} // namespace cricket
427
428#endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_