henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #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.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 41 | #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" |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 46 | #include "webrtc/common.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
| 48 | #if !defined(LIBPEERCONNECTION_LIB) && \ |
| 49 | !defined(LIBPEERCONNECTION_IMPLEMENTATION) |
buildbot@webrtc.org | 6b21b71 | 2014-07-31 15:08:53 +0000 | [diff] [blame] | 50 | // If you hit this, then you've tried to include this header from outside |
| 51 | // the shared library. An instance of this class must only be created from |
| 52 | // within the library that actually implements it. Otherwise use the |
| 53 | // WebRtcMediaEngine to construct an instance. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | #error "Bogus include." |
| 55 | #endif |
| 56 | |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 57 | namespace webrtc { |
| 58 | class VideoEngine; |
| 59 | } |
| 60 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | namespace cricket { |
| 62 | |
| 63 | // WebRtcSoundclipStream is an adapter object that allows a memory stream to be |
| 64 | // passed into WebRtc, and support looping. |
| 65 | class WebRtcSoundclipStream : public webrtc::InStream { |
| 66 | public: |
| 67 | WebRtcSoundclipStream(const char* buf, size_t len) |
| 68 | : mem_(buf, len), loop_(true) { |
| 69 | } |
| 70 | void set_loop(bool loop) { loop_ = loop; } |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 71 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 72 | virtual int Read(void* buf, size_t len) OVERRIDE; |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 73 | virtual int Rewind() OVERRIDE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | rtc::MemoryStream mem_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | bool loop_; |
| 78 | }; |
| 79 | |
| 80 | // WebRtcMonitorStream is used to monitor a stream coming from WebRtc. |
| 81 | // For now we just dump the data. |
| 82 | class WebRtcMonitorStream : public webrtc::OutStream { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 83 | virtual bool Write(const void *buf, size_t len) OVERRIDE { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | class AudioDeviceModule; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 89 | class AudioRenderer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | class VoETraceWrapper; |
| 91 | class VoEWrapper; |
| 92 | class VoiceProcessor; |
| 93 | class WebRtcSoundclipMedia; |
| 94 | class WebRtcVoiceMediaChannel; |
| 95 | |
| 96 | // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. |
| 97 | // It uses the WebRtc VoiceEngine library for audio handling. |
| 98 | class 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 109 | bool Init(rtc::Thread* worker_thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | void Terminate(); |
| 111 | |
| 112 | int GetCapabilities(); |
| 113 | VoiceMediaChannel* CreateChannel(); |
| 114 | |
| 115 | SoundclipMedia* CreateSoundclip(); |
| 116 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 117 | AudioOptions GetOptions() const { return options_; } |
| 118 | bool SetOptions(const AudioOptions& options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | // 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 |
| 155 | virtual void Process(int channel, |
| 156 | webrtc::ProcessingTypes type, |
| 157 | int16_t audio10ms[], |
| 158 | int length, |
| 159 | int sampling_freq, |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 160 | bool is_stereo) OVERRIDE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | |
| 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.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 184 | // Starts AEC dump using existing file. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 185 | bool StartAecDump(rtc::PlatformFile file); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 186 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | // Check whether the supplied trace should be ignored. |
| 188 | bool ShouldIgnoreTrace(const std::string& trace); |
| 189 | |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 190 | // Create a VoiceEngine Channel. |
| 191 | int CreateMediaVoiceChannel(); |
| 192 | int CreateSoundclipVoiceChannel(); |
| 193 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | 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.org | 8038d42 | 2014-11-11 08:38:24 +0000 | [diff] [blame] | 202 | bool GetVoeCodec(int index, webrtc::CodecInst* codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 203 | bool InitInternal(); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 204 | bool EnsureSoundclipEngineInit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | 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.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 212 | |
| 213 | // webrtc::TraceCallback: |
| 214 | virtual void Print(webrtc::TraceLevel level, |
| 215 | const char* trace, |
| 216 | int length) OVERRIDE; |
| 217 | |
| 218 | // webrtc::VoiceEngineObserver: |
| 219 | virtual void CallbackOnError(int channel, int errCode) OVERRIDE; |
| 220 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 221 | // Given the device type, name, and id, find device id. Return true and |
| 222 | // set the output parameter rtc_id if successful. |
| 223 | bool FindWebRtcAudioDeviceId( |
| 224 | bool is_input, const std::string& dev_name, int dev_id, int* rtc_id); |
| 225 | bool FindChannelAndSsrc(int channel_num, |
| 226 | WebRtcVoiceMediaChannel** channel, |
| 227 | uint32* ssrc) const; |
| 228 | bool FindChannelNumFromSsrc(uint32 ssrc, |
| 229 | MediaProcessorDirection direction, |
| 230 | int* channel_num); |
| 231 | bool ChangeLocalMonitor(bool enable); |
| 232 | bool PauseLocalMonitor(); |
| 233 | bool ResumeLocalMonitor(); |
| 234 | |
| 235 | bool UnregisterProcessorChannel(MediaProcessorDirection channel_direction, |
| 236 | uint32 ssrc, |
| 237 | VoiceProcessor* voice_processor, |
| 238 | MediaProcessorDirection processor_direction); |
| 239 | |
| 240 | void StartAecDump(const std::string& filename); |
| 241 | void StopAecDump(); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 242 | int CreateVoiceChannel(VoEWrapper* voe); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | |
| 244 | // When a voice processor registers with the engine, it is connected |
| 245 | // to either the Rx or Tx signals, based on the direction parameter. |
| 246 | // SignalXXMediaFrame will be invoked for every audio packet. |
| 247 | FrameSignal SignalRxMediaFrame; |
| 248 | FrameSignal SignalTxMediaFrame; |
| 249 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 250 | static const int kDefaultLogSeverity = rtc::LS_WARNING; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | |
| 252 | // The primary instance of WebRtc VoiceEngine. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 253 | rtc::scoped_ptr<VoEWrapper> voe_wrapper_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 254 | // A secondary instance, for playing out soundclips (on the 'ring' device). |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 255 | rtc::scoped_ptr<VoEWrapper> voe_wrapper_sc_; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 256 | bool voe_wrapper_sc_initialized_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 257 | rtc::scoped_ptr<VoETraceWrapper> tracing_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 258 | // The external audio device manager |
| 259 | webrtc::AudioDeviceModule* adm_; |
| 260 | webrtc::AudioDeviceModule* adm_sc_; |
| 261 | int log_filter_; |
| 262 | std::string log_options_; |
| 263 | bool is_dumping_aec_; |
| 264 | std::vector<AudioCodec> codecs_; |
| 265 | std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 266 | bool desired_local_monitor_enable_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 267 | rtc::scoped_ptr<WebRtcMonitorStream> monitor_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | SoundclipList soundclips_; |
| 269 | ChannelList channels_; |
| 270 | // channels_ can be read from WebRtc callback thread. We need a lock on that |
| 271 | // callback as well as the RegisterChannel/UnregisterChannel. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 272 | rtc::CriticalSection channels_cs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | webrtc::AgcConfig default_agc_config_; |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 274 | |
| 275 | webrtc::Config voe_config_; |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 276 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 277 | bool initialized_; |
| 278 | // See SetOptions and SetOptionOverrides for a description of the |
| 279 | // difference between options and overrides. |
| 280 | // options_ are the base options, which combined with the |
| 281 | // option_overrides_, create the current options being used. |
| 282 | // options_ is stored so that when option_overrides_ is cleared, we |
| 283 | // can restore the options_ without the option_overrides. |
| 284 | AudioOptions options_; |
| 285 | AudioOptions option_overrides_; |
| 286 | |
| 287 | // When the media processor registers with the engine, the ssrc is cached |
| 288 | // here so that a look up need not be made when the callback is invoked. |
| 289 | // This is necessary because the lookup results in mux_channels_cs lock being |
| 290 | // held and if a remote participant leaves the hangout at the same time |
| 291 | // we hit a deadlock. |
| 292 | uint32 tx_processor_ssrc_; |
| 293 | uint32 rx_processor_ssrc_; |
| 294 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 295 | rtc::CriticalSection signal_media_critical_; |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 296 | |
| 297 | // Cache received experimental_aec and experimental_ns values, and apply them |
| 298 | // in case they are missing in the audio options. We need to do this because |
| 299 | // SetExtraOptions() will revert to defaults for options which are not |
| 300 | // provided. |
| 301 | Settable<bool> experimental_aec_; |
| 302 | Settable<bool> experimental_ns_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | // WebRtcMediaChannel is a class that implements the common WebRtc channel |
| 306 | // functionality. |
| 307 | template <class T, class E> |
| 308 | class WebRtcMediaChannel : public T, public webrtc::Transport { |
| 309 | public: |
| 310 | WebRtcMediaChannel(E *engine, int channel) |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 311 | : engine_(engine), voe_channel_(channel) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | E *engine() { return engine_; } |
| 313 | int voe_channel() const { return voe_channel_; } |
| 314 | bool valid() const { return voe_channel_ != -1; } |
| 315 | |
| 316 | protected: |
| 317 | // implements Transport interface |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 318 | virtual int SendPacket(int channel, const void *data, size_t len) OVERRIDE { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 319 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 320 | return T::SendPacket(&packet) ? static_cast<int>(len) : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 321 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 322 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 323 | virtual int SendRTCPPacket(int channel, |
| 324 | const void* data, |
| 325 | size_t len) OVERRIDE { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 326 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 327 | return T::SendRtcp(&packet) ? static_cast<int>(len) : -1; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 328 | } |
| 329 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 330 | private: |
| 331 | E *engine_; |
| 332 | int voe_channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses |
| 336 | // WebRtc Voice Engine. |
| 337 | class WebRtcVoiceMediaChannel |
| 338 | : public WebRtcMediaChannel<VoiceMediaChannel, WebRtcVoiceEngine> { |
| 339 | public: |
| 340 | explicit WebRtcVoiceMediaChannel(WebRtcVoiceEngine *engine); |
| 341 | virtual ~WebRtcVoiceMediaChannel(); |
| 342 | virtual bool SetOptions(const AudioOptions& options); |
| 343 | virtual bool GetOptions(AudioOptions* options) const { |
| 344 | *options = options_; |
| 345 | return true; |
| 346 | } |
| 347 | virtual bool SetRecvCodecs(const std::vector<AudioCodec> &codecs); |
| 348 | virtual bool SetSendCodecs(const std::vector<AudioCodec> &codecs); |
| 349 | virtual bool SetRecvRtpHeaderExtensions( |
| 350 | const std::vector<RtpHeaderExtension>& extensions); |
| 351 | virtual bool SetSendRtpHeaderExtensions( |
| 352 | const std::vector<RtpHeaderExtension>& extensions); |
| 353 | virtual bool SetPlayout(bool playout); |
| 354 | bool PausePlayout(); |
| 355 | bool ResumePlayout(); |
| 356 | virtual bool SetSend(SendFlags send); |
| 357 | bool PauseSend(); |
| 358 | bool ResumeSend(); |
| 359 | virtual bool AddSendStream(const StreamParams& sp); |
| 360 | virtual bool RemoveSendStream(uint32 ssrc); |
| 361 | virtual bool AddRecvStream(const StreamParams& sp); |
| 362 | virtual bool RemoveRecvStream(uint32 ssrc); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 363 | virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer); |
| 364 | virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | virtual bool GetActiveStreams(AudioInfo::StreamList* actives); |
| 366 | virtual int GetOutputLevel(); |
| 367 | virtual int GetTimeSinceLastTyping(); |
| 368 | virtual void SetTypingDetectionParameters(int time_window, |
| 369 | int cost_per_typing, int reporting_threshold, int penalty_decay, |
| 370 | int type_event_delay); |
| 371 | virtual bool SetOutputScaling(uint32 ssrc, double left, double right); |
| 372 | virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right); |
| 373 | |
| 374 | virtual bool SetRingbackTone(const char *buf, int len); |
| 375 | virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop); |
| 376 | virtual bool CanInsertDtmf(); |
| 377 | virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags); |
| 378 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 379 | virtual void OnPacketReceived(rtc::Buffer* packet, |
| 380 | const rtc::PacketTime& packet_time); |
| 381 | virtual void OnRtcpReceived(rtc::Buffer* packet, |
| 382 | const rtc::PacketTime& packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 383 | virtual void OnReadyToSend(bool ready) {} |
| 384 | virtual bool MuteStream(uint32 ssrc, bool on); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 385 | virtual bool SetMaxSendBandwidth(int bps); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 386 | virtual bool GetStats(VoiceMediaInfo* info); |
| 387 | // Gets last reported error from WebRtc voice engine. This should be only |
| 388 | // called in response a failure. |
| 389 | virtual void GetLastMediaError(uint32* ssrc, |
| 390 | VoiceMediaChannel::Error* error); |
| 391 | bool FindSsrc(int channel_num, uint32* ssrc); |
| 392 | void OnError(uint32 ssrc, int error); |
| 393 | |
| 394 | bool sending() const { return send_ != SEND_NOTHING; } |
| 395 | int GetReceiveChannelNum(uint32 ssrc); |
| 396 | int GetSendChannelNum(uint32 ssrc); |
| 397 | |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 398 | bool SetupSharedBandwidthEstimation(webrtc::VideoEngine* vie, |
| 399 | int vie_channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 400 | 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.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 413 | 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.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 418 | typedef int (webrtc::VoERTP_RTCP::* ExtensionSetterFunction)(int, bool, |
| 419 | unsigned char); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 420 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 421 | void SetNack(int channel, bool nack_enabled); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 422 | void SetNack(const ChannelMap& channels, bool nack_enabled); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 423 | bool SetSendCodec(const webrtc::CodecInst& send_codec); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 424 | bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 425 | bool ChangePlayout(bool playout); |
| 426 | bool ChangeSend(SendFlags send); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 427 | bool ChangeSend(int channel, SendFlags send); |
| 428 | void ConfigureSendChannel(int channel); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 429 | bool ConfigureRecvChannel(int channel); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 430 | bool DeleteChannel(int channel); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 431 | bool InConferenceMode() const { |
| 432 | return options_.conference_mode.GetWithDefaultIfUnset(false); |
| 433 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 434 | bool IsDefaultChannel(int channel_id) const { |
| 435 | return channel_id == voe_channel(); |
| 436 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 437 | bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs); |
minyue@webrtc.org | 2623695 | 2014-10-29 02:27:08 +0000 | [diff] [blame] | 438 | bool SetSendBitrateInternal(int bps); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 439 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 440 | bool SetHeaderExtension(ExtensionSetterFunction setter, int channel_id, |
| 441 | const RtpHeaderExtension* extension); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 442 | bool SetupSharedBweOnChannel(int voe_channel); |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 443 | |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 444 | bool SetChannelRecvRtpHeaderExtensions( |
| 445 | int channel_id, |
| 446 | const std::vector<RtpHeaderExtension>& extensions); |
| 447 | bool SetChannelSendRtpHeaderExtensions( |
| 448 | int channel_id, |
| 449 | const std::vector<RtpHeaderExtension>& extensions); |
| 450 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 451 | rtc::scoped_ptr<WebRtcSoundclipStream> ringback_tone_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | std::set<int> ringback_channels_; // channels playing ringback |
| 453 | std::vector<AudioCodec> recv_codecs_; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 454 | std::vector<AudioCodec> send_codecs_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 455 | rtc::scoped_ptr<webrtc::CodecInst> send_codec_; |
minyue@webrtc.org | 2623695 | 2014-10-29 02:27:08 +0000 | [diff] [blame] | 456 | bool send_bitrate_setting_; |
| 457 | int send_bitrate_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | AudioOptions options_; |
| 459 | bool dtmf_allowed_; |
| 460 | bool desired_playout_; |
| 461 | bool nack_enabled_; |
| 462 | bool playout_; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 463 | bool typing_noise_detected_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | SendFlags desired_send_; |
| 465 | SendFlags send_; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 466 | // shared_bwe_vie_ and shared_bwe_vie_channel_ together identifies a WebRTC |
| 467 | // VideoEngine channel that this voice channel should forward incoming packets |
| 468 | // to for Bandwidth Estimation purposes. |
| 469 | webrtc::VideoEngine* shared_bwe_vie_; |
| 470 | int shared_bwe_vie_channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 472 | // send_channels_ contains the channels which are being used for sending. |
| 473 | // When the default channel (voe_channel) is used for sending, it is |
| 474 | // contained in send_channels_, otherwise not. |
| 475 | ChannelMap send_channels_; |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 476 | std::vector<RtpHeaderExtension> send_extensions_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 477 | uint32 default_receive_ssrc_; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 478 | // Note the default channel (voe_channel()) can reside in both |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 479 | // receive_channels_ and send_channels_ in non-conference mode and in that |
| 480 | // case it will only be there if a non-zero default_receive_ssrc_ is set. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 481 | ChannelMap receive_channels_; // for multiple sources |
| 482 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 485 | // |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 486 | std::vector<RtpHeaderExtension> receive_extensions_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 487 | // Do not lock this on the VoE media processor thread; potential for deadlock |
| 488 | // exists. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 489 | mutable rtc::CriticalSection receive_channels_cs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | } // namespace cricket |
| 493 | |
| 494 | #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ |