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