blob: f9388068778350f491dc185962f6498dea908eeb [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"
38#include "talk/media/webrtc/webrtcexport.h"
39#include "talk/media/webrtc/webrtcvoe.h"
40#include "talk/session/media/channel.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000041#include "webrtc/base/buffer.h"
42#include "webrtc/base/byteorder.h"
43#include "webrtc/base/logging.h"
44#include "webrtc/base/scoped_ptr.h"
45#include "webrtc/base/stream.h"
Fredrik Solenberg4b60c732015-05-07 14:07:48 +020046#include "webrtc/base/thread_checker.h"
47#include "webrtc/call.h"
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000048#include "webrtc/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50#if !defined(LIBPEERCONNECTION_LIB) && \
51 !defined(LIBPEERCONNECTION_IMPLEMENTATION)
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +000052// If you hit this, then you've tried to include this header from outside
53// the shared library. An instance of this class must only be created from
54// within the library that actually implements it. Otherwise use the
55// WebRtcMediaEngine to construct an instance.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056#error "Bogus include."
57#endif
58
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000059namespace webrtc {
60class VideoEngine;
61}
62
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063namespace cricket {
64
65// WebRtcSoundclipStream is an adapter object that allows a memory stream to be
66// passed into WebRtc, and support looping.
67class WebRtcSoundclipStream : public webrtc::InStream {
68 public:
69 WebRtcSoundclipStream(const char* buf, size_t len)
70 : mem_(buf, len), loop_(true) {
71 }
72 void set_loop(bool loop) { loop_ = loop; }
xians@webrtc.org3cefbc92014-10-10 09:42:53 +000073
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 int Read(void* buf, size_t len) override;
75 int Rewind() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 rtc::MemoryStream mem_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 bool loop_;
80};
81
82// WebRtcMonitorStream is used to monitor a stream coming from WebRtc.
83// For now we just dump the data.
84class WebRtcMonitorStream : public webrtc::OutStream {
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000085 bool Write(const void* buf, size_t len) override { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
88class AudioDeviceModule;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000089class AudioRenderer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090class VoETraceWrapper;
91class VoEWrapper;
92class VoiceProcessor;
93class WebRtcSoundclipMedia;
94class WebRtcVoiceMediaChannel;
95
96// WebRtcVoiceEngine is a class to be used with CompositeMediaEngine.
97// It uses the WebRtc VoiceEngine library for audio handling.
98class WebRtcVoiceEngine
99 : public webrtc::VoiceEngineObserver,
100 public webrtc::TraceCallback,
101 public webrtc::VoEMediaProcess {
102 public:
103 WebRtcVoiceEngine();
104 // Dependency injection for testing.
105 WebRtcVoiceEngine(VoEWrapper* voe_wrapper,
106 VoEWrapper* voe_wrapper_sc,
107 VoETraceWrapper* tracing);
108 ~WebRtcVoiceEngine();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 bool Init(rtc::Thread* worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 void Terminate();
111
112 int GetCapabilities();
113 VoiceMediaChannel* CreateChannel();
114
115 SoundclipMedia* CreateSoundclip();
116
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000117 AudioOptions GetOptions() const { return options_; }
118 bool SetOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 // Overrides, when set, take precedence over the options on a
120 // per-option basis. For example, if AGC is set in options and AEC
121 // is set in overrides, AGC and AEC will be both be set. Overrides
122 // can also turn off options. For example, if AGC is set to "on" in
123 // options and AGC is set to "off" in overrides, the result is that
124 // AGC will be off until different overrides are applied or until
125 // the overrides are cleared. Only one set of overrides is present
126 // at a time (they do not "stack"). And when the overrides are
127 // cleared, the media engine's state reverts back to the options set
128 // via SetOptions. This allows us to have both "persistent options"
129 // (the normal options) and "temporary options" (overrides).
130 bool SetOptionOverrides(const AudioOptions& options);
131 bool ClearOptionOverrides();
132 bool SetDelayOffset(int offset);
133 bool SetDevices(const Device* in_device, const Device* out_device);
134 bool GetOutputVolume(int* level);
135 bool SetOutputVolume(int level);
136 int GetInputLevel();
137 bool SetLocalMonitor(bool enable);
138
139 const std::vector<AudioCodec>& codecs();
140 bool FindCodec(const AudioCodec& codec);
141 bool FindWebRtcCodec(const AudioCodec& codec, webrtc::CodecInst* gcodec);
142
143 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const;
144
145 void SetLogging(int min_sev, const char* filter);
146
147 bool RegisterProcessor(uint32 ssrc,
148 VoiceProcessor* voice_processor,
149 MediaProcessorDirection direction);
150 bool UnregisterProcessor(uint32 ssrc,
151 VoiceProcessor* voice_processor,
152 MediaProcessorDirection direction);
153
154 // Method from webrtc::VoEMediaProcess
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000155 void Process(int channel,
156 webrtc::ProcessingTypes type,
157 int16_t audio10ms[],
158 int length,
159 int sampling_freq,
160 bool is_stereo) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
162 // For tracking WebRtc channels. Needed because we have to pause them
163 // all when switching devices.
164 // May only be called by WebRtcVoiceMediaChannel.
165 void RegisterChannel(WebRtcVoiceMediaChannel *channel);
166 void UnregisterChannel(WebRtcVoiceMediaChannel *channel);
167
168 // May only be called by WebRtcSoundclipMedia.
169 void RegisterSoundclip(WebRtcSoundclipMedia *channel);
170 void UnregisterSoundclip(WebRtcSoundclipMedia *channel);
171
172 // Called by WebRtcVoiceMediaChannel to set a gain offset from
173 // the default AGC target level.
174 bool AdjustAgcLevel(int delta);
175
176 VoEWrapper* voe() { return voe_wrapper_.get(); }
177 VoEWrapper* voe_sc() { return voe_wrapper_sc_.get(); }
178 int GetLastEngineError();
179
180 // Set the external ADMs. This can only be called before Init.
181 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm,
182 webrtc::AudioDeviceModule* adm_sc);
183
wu@webrtc.orga9890802013-12-13 00:21:03 +0000184 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000185 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 // Check whether the supplied trace should be ignored.
188 bool ShouldIgnoreTrace(const std::string& trace);
189
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000190 // Create a VoiceEngine Channel.
191 int CreateMediaVoiceChannel();
192 int CreateSoundclipVoiceChannel();
193
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 private:
195 typedef std::vector<WebRtcSoundclipMedia *> SoundclipList;
196 typedef std::vector<WebRtcVoiceMediaChannel *> ChannelList;
197 typedef sigslot::
198 signal3<uint32, MediaProcessorDirection, AudioFrame*> FrameSignal;
199
200 void Construct();
201 void ConstructCodecs();
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000202 bool GetVoeCodec(int index, webrtc::CodecInst* codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 bool InitInternal();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000204 bool EnsureSoundclipEngineInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 void SetTraceFilter(int filter);
206 void SetTraceOptions(const std::string& options);
207 // Applies either options or overrides. Every option that is "set"
208 // will be applied. Every option not "set" will be ignored. This
209 // allows us to selectively turn on and off different options easily
210 // at any time.
211 bool ApplyOptions(const AudioOptions& options);
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000212
213 // webrtc::TraceCallback:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000214 void Print(webrtc::TraceLevel level, const char* trace, int length) override;
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000215
216 // webrtc::VoiceEngineObserver:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000217 void CallbackOnError(int channel, int errCode) override;
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000218
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 // Given the device type, name, and id, find device id. Return true and
220 // set the output parameter rtc_id if successful.
221 bool FindWebRtcAudioDeviceId(
222 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id);
223 bool FindChannelAndSsrc(int channel_num,
224 WebRtcVoiceMediaChannel** channel,
225 uint32* ssrc) const;
226 bool FindChannelNumFromSsrc(uint32 ssrc,
227 MediaProcessorDirection direction,
228 int* channel_num);
229 bool ChangeLocalMonitor(bool enable);
230 bool PauseLocalMonitor();
231 bool ResumeLocalMonitor();
232
233 bool UnregisterProcessorChannel(MediaProcessorDirection channel_direction,
234 uint32 ssrc,
235 VoiceProcessor* voice_processor,
236 MediaProcessorDirection processor_direction);
237
238 void StartAecDump(const std::string& filename);
239 void StopAecDump();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000240 int CreateVoiceChannel(VoEWrapper* voe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
242 // When a voice processor registers with the engine, it is connected
243 // to either the Rx or Tx signals, based on the direction parameter.
244 // SignalXXMediaFrame will be invoked for every audio packet.
245 FrameSignal SignalRxMediaFrame;
246 FrameSignal SignalTxMediaFrame;
247
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000248 static const int kDefaultLogSeverity = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
250 // The primary instance of WebRtc VoiceEngine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000251 rtc::scoped_ptr<VoEWrapper> voe_wrapper_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 // A secondary instance, for playing out soundclips (on the 'ring' device).
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 rtc::scoped_ptr<VoEWrapper> voe_wrapper_sc_;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000254 bool voe_wrapper_sc_initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 rtc::scoped_ptr<VoETraceWrapper> tracing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 // The external audio device manager
257 webrtc::AudioDeviceModule* adm_;
258 webrtc::AudioDeviceModule* adm_sc_;
259 int log_filter_;
260 std::string log_options_;
261 bool is_dumping_aec_;
262 std::vector<AudioCodec> codecs_;
263 std::vector<RtpHeaderExtension> rtp_header_extensions_;
264 bool desired_local_monitor_enable_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 rtc::scoped_ptr<WebRtcMonitorStream> monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 SoundclipList soundclips_;
267 ChannelList channels_;
268 // channels_ can be read from WebRtc callback thread. We need a lock on that
269 // callback as well as the RegisterChannel/UnregisterChannel.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270 rtc::CriticalSection channels_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 webrtc::AgcConfig default_agc_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000272
273 webrtc::Config voe_config_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 bool initialized_;
276 // See SetOptions and SetOptionOverrides for a description of the
277 // difference between options and overrides.
278 // options_ are the base options, which combined with the
279 // option_overrides_, create the current options being used.
280 // options_ is stored so that when option_overrides_ is cleared, we
281 // can restore the options_ without the option_overrides.
282 AudioOptions options_;
283 AudioOptions option_overrides_;
284
285 // When the media processor registers with the engine, the ssrc is cached
286 // here so that a look up need not be made when the callback is invoked.
287 // This is necessary because the lookup results in mux_channels_cs lock being
288 // held and if a remote participant leaves the hangout at the same time
289 // we hit a deadlock.
290 uint32 tx_processor_ssrc_;
291 uint32 rx_processor_ssrc_;
292
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::CriticalSection signal_media_critical_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000294
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100295 // Cache received experimental_aec, delay_agnostic_aec and experimental_ns
296 // values, and apply them in case they are missing in the audio options. We
297 // need to do this because SetExtraOptions() will revert to defaults for
298 // options which are not provided.
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000299 Settable<bool> experimental_aec_;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100300 Settable<bool> delay_agnostic_aec_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000301 Settable<bool> experimental_ns_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302};
303
304// WebRtcMediaChannel is a class that implements the common WebRtc channel
305// functionality.
306template <class T, class E>
307class WebRtcMediaChannel : public T, public webrtc::Transport {
308 public:
309 WebRtcMediaChannel(E *engine, int channel)
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000310 : engine_(engine), voe_channel_(channel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 E *engine() { return engine_; }
312 int voe_channel() const { return voe_channel_; }
313 bool valid() const { return voe_channel_ != -1; }
314
315 protected:
316 // implements Transport interface
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000317 int SendPacket(int channel, const void* data, size_t len) override {
Karl Wiberg94784372015-04-20 14:03:07 +0200318 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
319 kMaxRtpPacketLen);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000320 return T::SendPacket(&packet) ? static_cast<int>(len) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000322
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000323 int SendRTCPPacket(int channel, const void* data, size_t len) override {
Karl Wiberg94784372015-04-20 14:03:07 +0200324 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len,
325 kMaxRtpPacketLen);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000326 return T::SendRtcp(&packet) ? static_cast<int>(len) : -1;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000327 }
328
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 private:
330 E *engine_;
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000331 const int voe_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332};
333
334// WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses
335// WebRtc Voice Engine.
336class WebRtcVoiceMediaChannel
337 : public WebRtcMediaChannel<VoiceMediaChannel, WebRtcVoiceEngine> {
338 public:
339 explicit WebRtcVoiceMediaChannel(WebRtcVoiceEngine *engine);
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200340 ~WebRtcVoiceMediaChannel() override;
341 bool SetOptions(const AudioOptions& options) override;
342 bool GetOptions(AudioOptions* options) const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 *options = options_;
344 return true;
345 }
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200346 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) override;
347 bool SetSendCodecs(const std::vector<AudioCodec>& codecs) override;
348 bool SetRecvRtpHeaderExtensions(
349 const std::vector<RtpHeaderExtension>& extensions) override;
350 bool SetSendRtpHeaderExtensions(
351 const std::vector<RtpHeaderExtension>& extensions) override;
352 bool SetPlayout(bool playout) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 bool PausePlayout();
354 bool ResumePlayout();
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200355 bool SetSend(SendFlags send) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 bool PauseSend();
357 bool ResumeSend();
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200358 bool AddSendStream(const StreamParams& sp) override;
359 bool RemoveSendStream(uint32 ssrc) override;
360 bool AddRecvStream(const StreamParams& sp) override;
361 bool RemoveRecvStream(uint32 ssrc) override;
362 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) override;
363 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) override;
364 bool GetActiveStreams(AudioInfo::StreamList* actives) override;
365 int GetOutputLevel() override;
366 int GetTimeSinceLastTyping() override;
367 void SetTypingDetectionParameters(int time_window,
368 int cost_per_typing,
369 int reporting_threshold,
370 int penalty_decay,
371 int type_event_delay) override;
372 bool SetOutputScaling(uint32 ssrc, double left, double right) override;
373 bool GetOutputScaling(uint32 ssrc, double* left, double* right) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200375 bool SetRingbackTone(const char* buf, int len) override;
376 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) override;
377 bool CanInsertDtmf() override;
378 bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200380 void OnPacketReceived(rtc::Buffer* packet,
381 const rtc::PacketTime& packet_time) override;
382 void OnRtcpReceived(rtc::Buffer* packet,
383 const rtc::PacketTime& packet_time) override;
384 void OnReadyToSend(bool ready) override {}
385 bool MuteStream(uint32 ssrc, bool on) override;
386 bool SetMaxSendBandwidth(int bps) override;
387 bool GetStats(VoiceMediaInfo* info) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 // Gets last reported error from WebRtc voice engine. This should be only
389 // called in response a failure.
Fredrik Solenbergaaf8ff22015-05-07 16:05:53 +0200390 void GetLastMediaError(uint32* ssrc,
391 VoiceMediaChannel::Error* error) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 bool FindSsrc(int channel_num, uint32* ssrc);
393 void OnError(uint32 ssrc, int error);
394
395 bool sending() const { return send_ != SEND_NOTHING; }
396 int GetReceiveChannelNum(uint32 ssrc);
397 int GetSendChannelNum(uint32 ssrc);
398
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200399 void SetCall(webrtc::Call* call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 protected:
401 int GetLastEngineError() { return engine()->GetLastEngineError(); }
402 int GetOutputLevel(int channel);
403 bool GetRedSendCodec(const AudioCodec& red_codec,
404 const std::vector<AudioCodec>& all_codecs,
405 webrtc::CodecInst* send_codec);
406 bool EnableRtcp(int channel);
407 bool ResetRecvCodecs(int channel);
408 bool SetPlayout(int channel, bool playout);
409 static uint32 ParseSsrc(const void* data, size_t len, bool rtcp);
410 static Error WebRtcErrorToChannelError(int err_code);
411
412 private:
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000413 class WebRtcVoiceChannelRenderer;
414 // Map of ssrc to WebRtcVoiceChannelRenderer object. A new object of
415 // WebRtcVoiceChannelRenderer will be created for every new stream and
416 // will be destroyed when the stream goes away.
417 typedef std::map<uint32, WebRtcVoiceChannelRenderer*> ChannelMap;
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000418 typedef int (webrtc::VoERTP_RTCP::* ExtensionSetterFunction)(int, bool,
419 unsigned char);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000420
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000421 void SetNack(int channel, bool nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000422 void SetNack(const ChannelMap& channels, bool nack_enabled);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 bool SetSendCodec(const webrtc::CodecInst& send_codec);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000424 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 bool ChangePlayout(bool playout);
426 bool ChangeSend(SendFlags send);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000427 bool ChangeSend(int channel, SendFlags send);
428 void ConfigureSendChannel(int channel);
wu@webrtc.org78187522013-10-07 23:32:02 +0000429 bool ConfigureRecvChannel(int channel);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000430 bool DeleteChannel(int channel);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000431 bool InConferenceMode() const {
432 return options_.conference_mode.GetWithDefaultIfUnset(false);
433 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000434 bool IsDefaultChannel(int channel_id) const {
435 return channel_id == voe_channel();
436 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000437 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs);
minyue@webrtc.org26236952014-10-29 02:27:08 +0000438 bool SetSendBitrateInternal(int bps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000440 bool SetHeaderExtension(ExtensionSetterFunction setter, int channel_id,
441 const RtpHeaderExtension* extension);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200442 void TryAddAudioRecvStream(uint32 ssrc);
443 void TryRemoveAudioRecvStream(uint32 ssrc);
444
buildbot@webrtc.org150835e2014-05-06 15:54:38 +0000445 bool SetChannelRecvRtpHeaderExtensions(
446 int channel_id,
447 const std::vector<RtpHeaderExtension>& extensions);
448 bool SetChannelSendRtpHeaderExtensions(
449 int channel_id,
450 const std::vector<RtpHeaderExtension>& extensions);
451
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200452 rtc::ThreadChecker thread_checker_;
453
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 rtc::scoped_ptr<WebRtcSoundclipStream> ringback_tone_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 std::set<int> ringback_channels_; // channels playing ringback
456 std::vector<AudioCodec> recv_codecs_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000457 std::vector<AudioCodec> send_codecs_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 rtc::scoped_ptr<webrtc::CodecInst> send_codec_;
minyue@webrtc.org26236952014-10-29 02:27:08 +0000459 bool send_bitrate_setting_;
460 int send_bitrate_bps_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 AudioOptions options_;
462 bool dtmf_allowed_;
463 bool desired_playout_;
464 bool nack_enabled_;
465 bool playout_;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000466 bool typing_noise_detected_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 SendFlags desired_send_;
468 SendFlags send_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200469 webrtc::Call* call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000471 // send_channels_ contains the channels which are being used for sending.
472 // When the default channel (voe_channel) is used for sending, it is
473 // contained in send_channels_, otherwise not.
474 ChannelMap send_channels_;
buildbot@webrtc.org150835e2014-05-06 15:54:38 +0000475 std::vector<RtpHeaderExtension> send_extensions_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 uint32 default_receive_ssrc_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000477 // Note the default channel (voe_channel()) can reside in both
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000478 // receive_channels_ and send_channels_ in non-conference mode and in that
479 // case it will only be there if a non-zero default_receive_ssrc_ is set.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000480 ChannelMap receive_channels_; // for multiple sources
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200481 std::map<uint32, webrtc::AudioReceiveStream*> receive_streams_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000482 // receive_channels_ can be read from WebRtc callback thread. Access from
483 // the WebRtc thread must be synchronized with edits on the worker thread.
484 // Reads on the worker thread are ok.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 //
buildbot@webrtc.org150835e2014-05-06 15:54:38 +0000486 std::vector<RtpHeaderExtension> receive_extensions_;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200487 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 // Do not lock this on the VoE media processor thread; potential for deadlock
490 // exists.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 mutable rtc::CriticalSection receive_channels_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492};
493
494} // namespace cricket
495
496#endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_