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" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | #include "talk/media/webrtc/webrtcvoe.h" |
| 39 | #include "talk/session/media/channel.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 40 | #include "webrtc/base/buffer.h" |
| 41 | #include "webrtc/base/byteorder.h" |
| 42 | #include "webrtc/base/logging.h" |
| 43 | #include "webrtc/base/scoped_ptr.h" |
| 44 | #include "webrtc/base/stream.h" |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 45 | #include "webrtc/base/thread_checker.h" |
| 46 | #include "webrtc/call.h" |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 47 | #include "webrtc/common.h" |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 48 | #include "webrtc/config.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 50 | namespace webrtc { |
| 51 | class VideoEngine; |
| 52 | } |
| 53 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | namespace cricket { |
| 55 | |
| 56 | // WebRtcSoundclipStream is an adapter object that allows a memory stream to be |
| 57 | // passed into WebRtc, and support looping. |
| 58 | class WebRtcSoundclipStream : public webrtc::InStream { |
| 59 | public: |
| 60 | WebRtcSoundclipStream(const char* buf, size_t len) |
| 61 | : mem_(buf, len), loop_(true) { |
| 62 | } |
| 63 | void set_loop(bool loop) { loop_ = loop; } |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 64 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 65 | int Read(void* buf, size_t len) override; |
| 66 | int Rewind() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | rtc::MemoryStream mem_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 { |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 76 | bool Write(const void* buf, size_t len) override { return true; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class AudioDeviceModule; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 80 | class AudioRenderer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | class VoETraceWrapper; |
| 82 | class VoEWrapper; |
| 83 | class VoiceProcessor; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | class WebRtcVoiceMediaChannel; |
| 85 | |
| 86 | // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. |
| 87 | // It uses the WebRtc VoiceEngine library for audio handling. |
| 88 | class WebRtcVoiceEngine |
| 89 | : public webrtc::VoiceEngineObserver, |
| 90 | public webrtc::TraceCallback, |
| 91 | public webrtc::VoEMediaProcess { |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 92 | friend class WebRtcVoiceMediaChannel; |
| 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | public: |
| 95 | WebRtcVoiceEngine(); |
| 96 | // Dependency injection for testing. |
Fredrik Solenberg | ccb49e7 | 2015-05-19 11:37:56 +0200 | [diff] [blame] | 97 | WebRtcVoiceEngine(VoEWrapper* voe_wrapper, VoETraceWrapper* tracing); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | ~WebRtcVoiceEngine(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 99 | bool Init(rtc::Thread* worker_thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | void Terminate(); |
| 101 | |
| 102 | int GetCapabilities(); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 103 | webrtc::VoiceEngine* GetVoE() { return voe()->engine(); } |
| 104 | VoiceMediaChannel* CreateChannel(webrtc::Call* call, |
| 105 | const AudioOptions& options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 107 | AudioOptions GetOptions() const { return options_; } |
| 108 | bool SetOptions(const AudioOptions& options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | bool SetDelayOffset(int offset); |
| 110 | bool SetDevices(const Device* in_device, const Device* out_device); |
| 111 | bool GetOutputVolume(int* level); |
| 112 | bool SetOutputVolume(int level); |
| 113 | int GetInputLevel(); |
| 114 | bool SetLocalMonitor(bool enable); |
| 115 | |
| 116 | const std::vector<AudioCodec>& codecs(); |
| 117 | bool FindCodec(const AudioCodec& codec); |
| 118 | bool FindWebRtcCodec(const AudioCodec& codec, webrtc::CodecInst* gcodec); |
| 119 | |
| 120 | const std::vector<RtpHeaderExtension>& rtp_header_extensions() const; |
| 121 | |
| 122 | void SetLogging(int min_sev, const char* filter); |
| 123 | |
| 124 | bool RegisterProcessor(uint32 ssrc, |
| 125 | VoiceProcessor* voice_processor, |
| 126 | MediaProcessorDirection direction); |
| 127 | bool UnregisterProcessor(uint32 ssrc, |
| 128 | VoiceProcessor* voice_processor, |
| 129 | MediaProcessorDirection direction); |
| 130 | |
| 131 | // Method from webrtc::VoEMediaProcess |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 132 | void Process(int channel, |
| 133 | webrtc::ProcessingTypes type, |
| 134 | int16_t audio10ms[], |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 135 | size_t length, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 136 | int sampling_freq, |
| 137 | bool is_stereo) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | |
| 139 | // For tracking WebRtc channels. Needed because we have to pause them |
| 140 | // all when switching devices. |
| 141 | // May only be called by WebRtcVoiceMediaChannel. |
| 142 | void RegisterChannel(WebRtcVoiceMediaChannel *channel); |
| 143 | void UnregisterChannel(WebRtcVoiceMediaChannel *channel); |
| 144 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | // Called by WebRtcVoiceMediaChannel to set a gain offset from |
| 146 | // the default AGC target level. |
| 147 | bool AdjustAgcLevel(int delta); |
| 148 | |
| 149 | VoEWrapper* voe() { return voe_wrapper_.get(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | int GetLastEngineError(); |
| 151 | |
Fredrik Solenberg | ccb49e7 | 2015-05-19 11:37:56 +0200 | [diff] [blame] | 152 | // Set the external ADM. This can only be called before Init. |
| 153 | bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 155 | // Starts AEC dump using existing file. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 156 | bool StartAecDump(rtc::PlatformFile file); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 157 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | // Check whether the supplied trace should be ignored. |
| 159 | bool ShouldIgnoreTrace(const std::string& trace); |
| 160 | |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 161 | // Create a VoiceEngine Channel. |
| 162 | int CreateMediaVoiceChannel(); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 163 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | private: |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 165 | typedef std::vector<WebRtcVoiceMediaChannel*> ChannelList; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | typedef sigslot:: |
| 167 | signal3<uint32, MediaProcessorDirection, AudioFrame*> FrameSignal; |
| 168 | |
| 169 | void Construct(); |
| 170 | void ConstructCodecs(); |
henrik.lundin@webrtc.org | 8038d42 | 2014-11-11 08:38:24 +0000 | [diff] [blame] | 171 | bool GetVoeCodec(int index, webrtc::CodecInst* codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | bool InitInternal(); |
| 173 | void SetTraceFilter(int filter); |
| 174 | void SetTraceOptions(const std::string& options); |
| 175 | // Applies either options or overrides. Every option that is "set" |
| 176 | // will be applied. Every option not "set" will be ignored. This |
| 177 | // allows us to selectively turn on and off different options easily |
| 178 | // at any time. |
| 179 | bool ApplyOptions(const AudioOptions& options); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 180 | // Overrides, when set, take precedence over the options on a |
| 181 | // per-option basis. For example, if AGC is set in options and AEC |
| 182 | // is set in overrides, AGC and AEC will be both be set. Overrides |
| 183 | // can also turn off options. For example, if AGC is set to "on" in |
| 184 | // options and AGC is set to "off" in overrides, the result is that |
| 185 | // AGC will be off until different overrides are applied or until |
| 186 | // the overrides are cleared. Only one set of overrides is present |
| 187 | // at a time (they do not "stack"). And when the overrides are |
| 188 | // cleared, the media engine's state reverts back to the options set |
| 189 | // via SetOptions. This allows us to have both "persistent options" |
| 190 | // (the normal options) and "temporary options" (overrides). |
| 191 | bool SetOptionOverrides(const AudioOptions& options); |
| 192 | bool ClearOptionOverrides(); |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 193 | |
| 194 | // webrtc::TraceCallback: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 195 | void Print(webrtc::TraceLevel level, const char* trace, int length) override; |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 196 | |
| 197 | // webrtc::VoiceEngineObserver: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 198 | void CallbackOnError(int channel, int errCode) override; |
xians@webrtc.org | 3cefbc9 | 2014-10-10 09:42:53 +0000 | [diff] [blame] | 199 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | // Given the device type, name, and id, find device id. Return true and |
| 201 | // set the output parameter rtc_id if successful. |
| 202 | bool FindWebRtcAudioDeviceId( |
| 203 | bool is_input, const std::string& dev_name, int dev_id, int* rtc_id); |
| 204 | bool FindChannelAndSsrc(int channel_num, |
| 205 | WebRtcVoiceMediaChannel** channel, |
| 206 | uint32* ssrc) const; |
| 207 | bool FindChannelNumFromSsrc(uint32 ssrc, |
| 208 | MediaProcessorDirection direction, |
| 209 | int* channel_num); |
| 210 | bool ChangeLocalMonitor(bool enable); |
| 211 | bool PauseLocalMonitor(); |
| 212 | bool ResumeLocalMonitor(); |
| 213 | |
| 214 | bool UnregisterProcessorChannel(MediaProcessorDirection channel_direction, |
| 215 | uint32 ssrc, |
| 216 | VoiceProcessor* voice_processor, |
| 217 | MediaProcessorDirection processor_direction); |
| 218 | |
| 219 | void StartAecDump(const std::string& filename); |
| 220 | void StopAecDump(); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 221 | int CreateVoiceChannel(VoEWrapper* voe); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | |
| 223 | // When a voice processor registers with the engine, it is connected |
| 224 | // to either the Rx or Tx signals, based on the direction parameter. |
| 225 | // SignalXXMediaFrame will be invoked for every audio packet. |
| 226 | FrameSignal SignalRxMediaFrame; |
| 227 | FrameSignal SignalTxMediaFrame; |
| 228 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 229 | static const int kDefaultLogSeverity = rtc::LS_WARNING; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | |
| 231 | // The primary instance of WebRtc VoiceEngine. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 232 | rtc::scoped_ptr<VoEWrapper> voe_wrapper_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 233 | rtc::scoped_ptr<VoETraceWrapper> tracing_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | // The external audio device manager |
| 235 | webrtc::AudioDeviceModule* adm_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 236 | int log_filter_; |
| 237 | std::string log_options_; |
| 238 | bool is_dumping_aec_; |
| 239 | std::vector<AudioCodec> codecs_; |
| 240 | std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 241 | bool desired_local_monitor_enable_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 242 | rtc::scoped_ptr<WebRtcMonitorStream> monitor_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | ChannelList channels_; |
| 244 | // channels_ can be read from WebRtc callback thread. We need a lock on that |
| 245 | // callback as well as the RegisterChannel/UnregisterChannel. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 246 | rtc::CriticalSection channels_cs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | webrtc::AgcConfig default_agc_config_; |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 248 | |
| 249 | webrtc::Config voe_config_; |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 250 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | bool initialized_; |
| 252 | // See SetOptions and SetOptionOverrides for a description of the |
| 253 | // difference between options and overrides. |
| 254 | // options_ are the base options, which combined with the |
| 255 | // option_overrides_, create the current options being used. |
| 256 | // options_ is stored so that when option_overrides_ is cleared, we |
| 257 | // can restore the options_ without the option_overrides. |
| 258 | AudioOptions options_; |
| 259 | AudioOptions option_overrides_; |
| 260 | |
| 261 | // When the media processor registers with the engine, the ssrc is cached |
| 262 | // here so that a look up need not be made when the callback is invoked. |
| 263 | // This is necessary because the lookup results in mux_channels_cs lock being |
| 264 | // held and if a remote participant leaves the hangout at the same time |
| 265 | // we hit a deadlock. |
| 266 | uint32 tx_processor_ssrc_; |
| 267 | uint32 rx_processor_ssrc_; |
| 268 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 269 | rtc::CriticalSection signal_media_critical_; |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 270 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 271 | // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 272 | // values, and apply them in case they are missing in the audio options. We |
| 273 | // need to do this because SetExtraOptions() will revert to defaults for |
| 274 | // options which are not provided. |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 275 | Settable<bool> extended_filter_aec_; |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 276 | Settable<bool> delay_agnostic_aec_; |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 277 | Settable<bool> experimental_ns_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses |
| 281 | // WebRtc Voice Engine. |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 282 | class WebRtcVoiceMediaChannel : public VoiceMediaChannel, |
| 283 | public webrtc::Transport { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 284 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 285 | WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, |
| 286 | const AudioOptions& options, |
| 287 | webrtc::Call* call); |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 288 | ~WebRtcVoiceMediaChannel() override; |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 289 | |
| 290 | int voe_channel() const { return voe_channel_; } |
| 291 | bool valid() const { return voe_channel_ != -1; } |
solenberg | 66f4339 | 2015-09-09 01:36:22 -0700 | [diff] [blame] | 292 | const AudioOptions& options() const { return options_; } |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 293 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 294 | bool SetSendParameters(const AudioSendParameters& params) override; |
| 295 | bool SetRecvParameters(const AudioRecvParameters& params) override; |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 296 | bool SetPlayout(bool playout) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 297 | bool PausePlayout(); |
| 298 | bool ResumePlayout(); |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 299 | bool SetSend(SendFlags send) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 300 | bool PauseSend(); |
| 301 | bool ResumeSend(); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 302 | bool SetAudioSend(uint32 ssrc, bool mute, const AudioOptions* options, |
| 303 | AudioRenderer* renderer) override; |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 304 | bool AddSendStream(const StreamParams& sp) override; |
| 305 | bool RemoveSendStream(uint32 ssrc) override; |
| 306 | bool AddRecvStream(const StreamParams& sp) override; |
| 307 | bool RemoveRecvStream(uint32 ssrc) override; |
| 308 | bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) override; |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 309 | bool GetActiveStreams(AudioInfo::StreamList* actives) override; |
| 310 | int GetOutputLevel() override; |
| 311 | int GetTimeSinceLastTyping() override; |
| 312 | void SetTypingDetectionParameters(int time_window, |
| 313 | int cost_per_typing, |
| 314 | int reporting_threshold, |
| 315 | int penalty_decay, |
| 316 | int type_event_delay) override; |
| 317 | bool SetOutputScaling(uint32 ssrc, double left, double right) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 318 | |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 319 | bool SetRingbackTone(const char* buf, int len) override; |
| 320 | bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) override; |
| 321 | bool CanInsertDtmf() override; |
| 322 | bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 323 | |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 324 | void OnPacketReceived(rtc::Buffer* packet, |
| 325 | const rtc::PacketTime& packet_time) override; |
| 326 | void OnRtcpReceived(rtc::Buffer* packet, |
| 327 | const rtc::PacketTime& packet_time) override; |
| 328 | void OnReadyToSend(bool ready) override {} |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 329 | bool GetStats(VoiceMediaInfo* info) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 330 | // Gets last reported error from WebRtc voice engine. This should be only |
| 331 | // called in response a failure. |
Fredrik Solenberg | aaf8ff2 | 2015-05-07 16:05:53 +0200 | [diff] [blame] | 332 | void GetLastMediaError(uint32* ssrc, |
| 333 | VoiceMediaChannel::Error* error) override; |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 334 | |
| 335 | // implements Transport interface |
| 336 | int SendPacket(int channel, const void* data, size_t len) override { |
| 337 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 338 | kMaxRtpPacketLen); |
| 339 | return VoiceMediaChannel::SendPacket(&packet) ? static_cast<int>(len) : -1; |
| 340 | } |
| 341 | |
| 342 | int SendRTCPPacket(int channel, const void* data, size_t len) override { |
| 343 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 344 | kMaxRtpPacketLen); |
| 345 | return VoiceMediaChannel::SendRtcp(&packet) ? static_cast<int>(len) : -1; |
| 346 | } |
| 347 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 348 | bool FindSsrc(int channel_num, uint32* ssrc); |
| 349 | void OnError(uint32 ssrc, int error); |
| 350 | |
| 351 | bool sending() const { return send_ != SEND_NOTHING; } |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 352 | int GetReceiveChannelNum(uint32 ssrc) const; |
| 353 | int GetSendChannelNum(uint32 ssrc) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 354 | |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 355 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 356 | bool SetSendCodecs(const std::vector<AudioCodec>& codecs); |
| 357 | bool SetSendRtpHeaderExtensions( |
| 358 | const std::vector<RtpHeaderExtension>& extensions); |
| 359 | bool SetOptions(const AudioOptions& options); |
| 360 | bool SetMaxSendBandwidth(int bps); |
| 361 | bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); |
| 362 | bool SetRecvRtpHeaderExtensions( |
| 363 | const std::vector<RtpHeaderExtension>& extensions); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 364 | bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer); |
| 365 | bool MuteStream(uint32 ssrc, bool mute); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 366 | |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 367 | WebRtcVoiceEngine* engine() { return engine_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 368 | int GetLastEngineError() { return engine()->GetLastEngineError(); } |
| 369 | int GetOutputLevel(int channel); |
| 370 | bool GetRedSendCodec(const AudioCodec& red_codec, |
| 371 | const std::vector<AudioCodec>& all_codecs, |
| 372 | webrtc::CodecInst* send_codec); |
| 373 | bool EnableRtcp(int channel); |
| 374 | bool ResetRecvCodecs(int channel); |
| 375 | bool SetPlayout(int channel, bool playout); |
| 376 | static uint32 ParseSsrc(const void* data, size_t len, bool rtcp); |
| 377 | static Error WebRtcErrorToChannelError(int err_code); |
| 378 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 379 | class WebRtcVoiceChannelRenderer; |
| 380 | // Map of ssrc to WebRtcVoiceChannelRenderer object. A new object of |
| 381 | // WebRtcVoiceChannelRenderer will be created for every new stream and |
| 382 | // will be destroyed when the stream goes away. |
| 383 | typedef std::map<uint32, WebRtcVoiceChannelRenderer*> ChannelMap; |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 384 | typedef int (webrtc::VoERTP_RTCP::* ExtensionSetterFunction)(int, bool, |
| 385 | unsigned char); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 386 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 387 | void SetNack(int channel, bool nack_enabled); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 388 | void SetNack(const ChannelMap& channels, bool nack_enabled); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 389 | bool SetSendCodec(const webrtc::CodecInst& send_codec); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 390 | bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 391 | bool ChangePlayout(bool playout); |
| 392 | bool ChangeSend(SendFlags send); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 393 | bool ChangeSend(int channel, SendFlags send); |
| 394 | void ConfigureSendChannel(int channel); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 395 | bool ConfigureRecvChannel(int channel); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 396 | bool DeleteChannel(int channel); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 397 | bool InConferenceMode() const { |
| 398 | return options_.conference_mode.GetWithDefaultIfUnset(false); |
| 399 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 400 | bool IsDefaultChannel(int channel_id) const { |
| 401 | return channel_id == voe_channel(); |
| 402 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 403 | bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs); |
minyue@webrtc.org | 2623695 | 2014-10-29 02:27:08 +0000 | [diff] [blame] | 404 | bool SetSendBitrateInternal(int bps); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 405 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 406 | bool SetHeaderExtension(ExtensionSetterFunction setter, int channel_id, |
| 407 | const RtpHeaderExtension* extension); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 408 | void RecreateAudioReceiveStreams(); |
| 409 | void AddAudioReceiveStream(uint32 ssrc); |
| 410 | void RemoveAudioReceiveStream(uint32 ssrc); |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 411 | bool SetRecvCodecsInternal(const std::vector<AudioCodec>& new_codecs); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 412 | |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 413 | bool SetChannelRecvRtpHeaderExtensions( |
| 414 | int channel_id, |
| 415 | const std::vector<RtpHeaderExtension>& extensions); |
| 416 | bool SetChannelSendRtpHeaderExtensions( |
| 417 | int channel_id, |
| 418 | const std::vector<RtpHeaderExtension>& extensions); |
| 419 | |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 420 | rtc::ThreadChecker thread_checker_; |
| 421 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 422 | WebRtcVoiceEngine* const engine_; |
Fredrik Solenberg | e444a3d | 2015-05-07 16:42:08 +0200 | [diff] [blame] | 423 | const int voe_channel_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 424 | rtc::scoped_ptr<WebRtcSoundclipStream> ringback_tone_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 425 | std::set<int> ringback_channels_; // channels playing ringback |
| 426 | std::vector<AudioCodec> recv_codecs_; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 427 | std::vector<AudioCodec> send_codecs_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 428 | rtc::scoped_ptr<webrtc::CodecInst> send_codec_; |
minyue@webrtc.org | 2623695 | 2014-10-29 02:27:08 +0000 | [diff] [blame] | 429 | bool send_bitrate_setting_; |
| 430 | int send_bitrate_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 431 | AudioOptions options_; |
| 432 | bool dtmf_allowed_; |
| 433 | bool desired_playout_; |
| 434 | bool nack_enabled_; |
| 435 | bool playout_; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 436 | bool typing_noise_detected_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 437 | SendFlags desired_send_; |
| 438 | SendFlags send_; |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 439 | webrtc::Call* const call_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 440 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 441 | // send_channels_ contains the channels which are being used for sending. |
| 442 | // When the default channel (voe_channel) is used for sending, it is |
| 443 | // contained in send_channels_, otherwise not. |
| 444 | ChannelMap send_channels_; |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 445 | std::vector<RtpHeaderExtension> send_extensions_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 446 | uint32 default_receive_ssrc_; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 447 | // Note the default channel (voe_channel()) can reside in both |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 448 | // receive_channels_ and send_channels_ in non-conference mode and in that |
| 449 | // 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] | 450 | ChannelMap receive_channels_; // for multiple sources |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 451 | std::map<uint32, webrtc::AudioReceiveStream*> receive_streams_; |
pbos | 8fc7fa7 | 2015-07-15 08:02:58 -0700 | [diff] [blame] | 452 | std::map<uint32, StreamParams> receive_stream_params_; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 453 | // receive_channels_ can be read from WebRtc callback thread. Access from |
| 454 | // the WebRtc thread must be synchronized with edits on the worker thread. |
| 455 | // Reads on the worker thread are ok. |
buildbot@webrtc.org | 150835e | 2014-05-06 15:54:38 +0000 | [diff] [blame] | 456 | std::vector<RtpHeaderExtension> receive_extensions_; |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 457 | std::vector<webrtc::RtpExtension> recv_rtp_extensions_; |
| 458 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 459 | // Do not lock this on the VoE media processor thread; potential for deadlock |
| 460 | // exists. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 461 | mutable rtc::CriticalSection receive_channels_cs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | } // namespace cricket |
| 465 | |
| 466 | #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ |