| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2004 Google Inc. | 3 * Copyright 2004 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include <map> | 31 #include <map> |
| 32 #include <set> | 32 #include <set> |
| 33 #include <string> | 33 #include <string> |
| 34 #include <vector> | 34 #include <vector> |
| 35 | 35 |
| 36 #include "talk/media/base/rtputils.h" | 36 #include "talk/media/base/rtputils.h" |
| 37 #include "talk/media/webrtc/webrtccommon.h" | 37 #include "talk/media/webrtc/webrtccommon.h" |
| 38 #include "talk/media/webrtc/webrtcvoe.h" | 38 #include "talk/media/webrtc/webrtcvoe.h" |
| 39 #include "talk/session/media/channel.h" | 39 #include "talk/session/media/channel.h" |
| 40 #include "webrtc/audio_state.h" |
| 40 #include "webrtc/base/buffer.h" | 41 #include "webrtc/base/buffer.h" |
| 41 #include "webrtc/base/byteorder.h" | 42 #include "webrtc/base/byteorder.h" |
| 42 #include "webrtc/base/logging.h" | 43 #include "webrtc/base/logging.h" |
| 43 #include "webrtc/base/scoped_ptr.h" | 44 #include "webrtc/base/scoped_ptr.h" |
| 44 #include "webrtc/base/stream.h" | 45 #include "webrtc/base/stream.h" |
| 45 #include "webrtc/base/thread_checker.h" | 46 #include "webrtc/base/thread_checker.h" |
| 46 #include "webrtc/call.h" | 47 #include "webrtc/call.h" |
| 47 #include "webrtc/common.h" | 48 #include "webrtc/common.h" |
| 48 #include "webrtc/config.h" | 49 #include "webrtc/config.h" |
| 49 | 50 |
| 50 namespace cricket { | 51 namespace cricket { |
| 51 | 52 |
| 52 class AudioDeviceModule; | 53 class AudioDeviceModule; |
| 53 class AudioRenderer; | 54 class AudioRenderer; |
| 54 class VoETraceWrapper; | 55 class VoETraceWrapper; |
| 55 class VoEWrapper; | 56 class VoEWrapper; |
| 56 class WebRtcVoiceMediaChannel; | 57 class WebRtcVoiceMediaChannel; |
| 57 | 58 |
| 58 // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. | 59 // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. |
| 59 // It uses the WebRtc VoiceEngine library for audio handling. | 60 // It uses the WebRtc VoiceEngine library for audio handling. |
| 60 class WebRtcVoiceEngine | 61 class WebRtcVoiceEngine final : public webrtc::TraceCallback { |
| 61 : public webrtc::VoiceEngineObserver, | |
| 62 public webrtc::TraceCallback { | |
| 63 friend class WebRtcVoiceMediaChannel; | 62 friend class WebRtcVoiceMediaChannel; |
| 64 | 63 |
| 65 public: | 64 public: |
| 66 WebRtcVoiceEngine(); | 65 WebRtcVoiceEngine(); |
| 67 // Dependency injection for testing. | 66 // Dependency injection for testing. |
| 68 WebRtcVoiceEngine(VoEWrapper* voe_wrapper, VoETraceWrapper* tracing); | 67 WebRtcVoiceEngine(VoEWrapper* voe_wrapper, |
| 68 VoETraceWrapper* tracing, |
| 69 webrtc::AudioState* audio_state); |
| 69 ~WebRtcVoiceEngine(); | 70 ~WebRtcVoiceEngine(); |
| 70 bool Init(rtc::Thread* worker_thread); | 71 bool Init(rtc::Thread* worker_thread); |
| 71 void Terminate(); | 72 void Terminate(); |
| 72 | 73 |
| 73 webrtc::VoiceEngine* GetVoE() { return voe()->engine(); } | 74 webrtc::AudioState* GetAudioState() { return audio_state_.get(); } |
| 74 VoiceMediaChannel* CreateChannel(webrtc::Call* call, | 75 VoiceMediaChannel* CreateChannel(webrtc::Call* call, |
| 75 const AudioOptions& options); | 76 const AudioOptions& options); |
| 76 | 77 |
| 77 AudioOptions GetOptions() const { return options_; } | 78 AudioOptions GetOptions() const { return options_; } |
| 78 bool SetOptions(const AudioOptions& options); | 79 bool SetOptions(const AudioOptions& options); |
| 79 bool SetDevices(const Device* in_device, const Device* out_device); | 80 bool SetDevices(const Device* in_device, const Device* out_device); |
| 80 bool GetOutputVolume(int* level); | 81 bool GetOutputVolume(int* level); |
| 81 bool SetOutputVolume(int level); | 82 bool SetOutputVolume(int level); |
| 82 int GetInputLevel(); | 83 int GetInputLevel(); |
| 83 | 84 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void SetTraceFilter(int filter); | 127 void SetTraceFilter(int filter); |
| 127 void SetTraceOptions(const std::string& options); | 128 void SetTraceOptions(const std::string& options); |
| 128 // Every option that is "set" will be applied. Every option not "set" will be | 129 // Every option that is "set" will be applied. Every option not "set" will be |
| 129 // ignored. This allows us to selectively turn on and off different options | 130 // ignored. This allows us to selectively turn on and off different options |
| 130 // easily at any time. | 131 // easily at any time. |
| 131 bool ApplyOptions(const AudioOptions& options); | 132 bool ApplyOptions(const AudioOptions& options); |
| 132 | 133 |
| 133 // webrtc::TraceCallback: | 134 // webrtc::TraceCallback: |
| 134 void Print(webrtc::TraceLevel level, const char* trace, int length) override; | 135 void Print(webrtc::TraceLevel level, const char* trace, int length) override; |
| 135 | 136 |
| 136 // webrtc::VoiceEngineObserver: | |
| 137 void CallbackOnError(int channel_id, int errCode) override; | |
| 138 | |
| 139 // Given the device type, name, and id, find device id. Return true and | 137 // Given the device type, name, and id, find device id. Return true and |
| 140 // set the output parameter rtc_id if successful. | 138 // set the output parameter rtc_id if successful. |
| 141 bool FindWebRtcAudioDeviceId( | 139 bool FindWebRtcAudioDeviceId( |
| 142 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id); | 140 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id); |
| 143 | 141 |
| 144 void StartAecDump(const std::string& filename); | 142 void StartAecDump(const std::string& filename); |
| 145 int CreateVoEChannel(); | 143 int CreateVoEChannel(); |
| 146 | 144 |
| 147 static const int kDefaultLogSeverity = rtc::LS_WARNING; | 145 static const int kDefaultLogSeverity = rtc::LS_WARNING; |
| 148 | 146 |
| 149 // The primary instance of WebRtc VoiceEngine. | 147 // The primary instance of WebRtc VoiceEngine. |
| 150 rtc::scoped_ptr<VoEWrapper> voe_wrapper_; | 148 rtc::scoped_ptr<VoEWrapper> voe_wrapper_; |
| 151 rtc::scoped_ptr<VoETraceWrapper> tracing_; | 149 rtc::scoped_ptr<VoETraceWrapper> tracing_; |
| 150 rtc::scoped_ptr<webrtc::AudioState> audio_state_; |
| 152 // The external audio device manager | 151 // The external audio device manager |
| 153 webrtc::AudioDeviceModule* adm_; | 152 webrtc::AudioDeviceModule* adm_ = nullptr; |
| 154 int log_filter_; | 153 int log_filter_; |
| 155 std::string log_options_; | 154 std::string log_options_; |
| 156 bool is_dumping_aec_; | 155 bool is_dumping_aec_ = false; |
| 157 std::vector<AudioCodec> codecs_; | 156 std::vector<AudioCodec> codecs_; |
| 158 std::vector<RtpHeaderExtension> rtp_header_extensions_; | 157 std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 159 std::vector<WebRtcVoiceMediaChannel*> channels_; | 158 std::vector<WebRtcVoiceMediaChannel*> channels_; |
| 160 // channels_ can be read from WebRtc callback thread. We need a lock on that | |
| 161 // callback as well as the RegisterChannel/UnregisterChannel. | |
| 162 rtc::CriticalSection channels_cs_; | |
| 163 webrtc::AgcConfig default_agc_config_; | 159 webrtc::AgcConfig default_agc_config_; |
| 164 | 160 |
| 165 webrtc::Config voe_config_; | 161 webrtc::Config voe_config_; |
| 166 | 162 |
| 167 bool initialized_; | 163 bool initialized_ = false; |
| 168 AudioOptions options_; | 164 AudioOptions options_; |
| 169 | 165 |
| 170 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns | 166 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns |
| 171 // values, and apply them in case they are missing in the audio options. We | 167 // values, and apply them in case they are missing in the audio options. We |
| 172 // need to do this because SetExtraOptions() will revert to defaults for | 168 // need to do this because SetExtraOptions() will revert to defaults for |
| 173 // options which are not provided. | 169 // options which are not provided. |
| 174 Settable<bool> extended_filter_aec_; | 170 Settable<bool> extended_filter_aec_; |
| 175 Settable<bool> delay_agnostic_aec_; | 171 Settable<bool> delay_agnostic_aec_; |
| 176 Settable<bool> experimental_ns_; | 172 Settable<bool> experimental_ns_; |
| 177 | 173 |
| 178 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine); | 174 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine); |
| 179 }; | 175 }; |
| 180 | 176 |
| 181 // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses | 177 // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses |
| 182 // WebRtc Voice Engine. | 178 // WebRtc Voice Engine. |
| 183 class WebRtcVoiceMediaChannel : public VoiceMediaChannel, | 179 class WebRtcVoiceMediaChannel final : public VoiceMediaChannel, |
| 184 public webrtc::Transport { | 180 public webrtc::Transport { |
| 185 public: | 181 public: |
| 186 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, | 182 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, |
| 187 const AudioOptions& options, | 183 const AudioOptions& options, |
| 188 webrtc::Call* call); | 184 webrtc::Call* call); |
| 189 ~WebRtcVoiceMediaChannel() override; | 185 ~WebRtcVoiceMediaChannel() override; |
| 190 | 186 |
| 191 const AudioOptions& options() const { return options_; } | 187 const AudioOptions& options() const { return options_; } |
| 192 | 188 |
| 193 bool SetSendParameters(const AudioSendParameters& params) override; | 189 bool SetSendParameters(const AudioSendParameters& params) override; |
| 194 bool SetRecvParameters(const AudioRecvParameters& params) override; | 190 bool SetRecvParameters(const AudioRecvParameters& params) override; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 rtc_options.packet_id = options.packet_id; | 232 rtc_options.packet_id = options.packet_id; |
| 237 return VoiceMediaChannel::SendPacket(&packet, rtc_options); | 233 return VoiceMediaChannel::SendPacket(&packet, rtc_options); |
| 238 } | 234 } |
| 239 | 235 |
| 240 bool SendRtcp(const uint8_t* data, size_t len) override { | 236 bool SendRtcp(const uint8_t* data, size_t len) override { |
| 241 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, | 237 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 242 kMaxRtpPacketLen); | 238 kMaxRtpPacketLen); |
| 243 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions()); | 239 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions()); |
| 244 } | 240 } |
| 245 | 241 |
| 246 void OnError(int error); | |
| 247 | |
| 248 int GetReceiveChannelId(uint32_t ssrc) const; | 242 int GetReceiveChannelId(uint32_t ssrc) const; |
| 249 int GetSendChannelId(uint32_t ssrc) const; | 243 int GetSendChannelId(uint32_t ssrc) const; |
| 250 | 244 |
| 251 private: | 245 private: |
| 252 bool SetSendCodecs(const std::vector<AudioCodec>& codecs); | 246 bool SetSendCodecs(const std::vector<AudioCodec>& codecs); |
| 253 bool SetSendRtpHeaderExtensions( | 247 bool SetSendRtpHeaderExtensions( |
| 254 const std::vector<RtpHeaderExtension>& extensions); | 248 const std::vector<RtpHeaderExtension>& extensions); |
| 255 bool SetOptions(const AudioOptions& options); | 249 bool SetOptions(const AudioOptions& options); |
| 256 bool SetMaxSendBandwidth(int bps); | 250 bool SetMaxSendBandwidth(int bps); |
| 257 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); | 251 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); |
| 258 bool SetRecvRtpHeaderExtensions( | 252 bool SetRecvRtpHeaderExtensions( |
| 259 const std::vector<RtpHeaderExtension>& extensions); | 253 const std::vector<RtpHeaderExtension>& extensions); |
| 260 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer); | 254 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer); |
| 261 bool MuteStream(uint32_t ssrc, bool mute); | 255 bool MuteStream(uint32_t ssrc, bool mute); |
| 262 | 256 |
| 263 WebRtcVoiceEngine* engine() { return engine_; } | 257 WebRtcVoiceEngine* engine() { return engine_; } |
| 264 int GetLastEngineError() { return engine()->GetLastEngineError(); } | 258 int GetLastEngineError() { return engine()->GetLastEngineError(); } |
| 265 int GetOutputLevel(int channel); | 259 int GetOutputLevel(int channel); |
| 266 bool GetRedSendCodec(const AudioCodec& red_codec, | 260 bool GetRedSendCodec(const AudioCodec& red_codec, |
| 267 const std::vector<AudioCodec>& all_codecs, | 261 const std::vector<AudioCodec>& all_codecs, |
| 268 webrtc::CodecInst* send_codec); | 262 webrtc::CodecInst* send_codec); |
| 269 bool SetPlayout(int channel, bool playout); | 263 bool SetPlayout(int channel, bool playout); |
| 270 static Error WebRtcErrorToChannelError(int err_code); | |
| 271 | 264 |
| 272 typedef int (webrtc::VoERTP_RTCP::* ExtensionSetterFunction)(int, bool, | 265 typedef int (webrtc::VoERTP_RTCP::* ExtensionSetterFunction)(int, bool, |
| 273 unsigned char); | 266 unsigned char); |
| 274 | 267 |
| 275 void SetNack(int channel, bool nack_enabled); | 268 void SetNack(int channel, bool nack_enabled); |
| 276 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); | 269 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); |
| 277 bool ChangePlayout(bool playout); | 270 bool ChangePlayout(bool playout); |
| 278 bool ChangeSend(SendFlags send); | 271 bool ChangeSend(SendFlags send); |
| 279 bool ChangeSend(int channel, SendFlags send); | 272 bool ChangeSend(int channel, SendFlags send); |
| 280 bool ConfigureRecvChannel(int channel); | 273 bool ConfigureRecvChannel(int channel); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 295 | 288 |
| 296 bool SetChannelRecvRtpHeaderExtensions( | 289 bool SetChannelRecvRtpHeaderExtensions( |
| 297 int channel_id, | 290 int channel_id, |
| 298 const std::vector<RtpHeaderExtension>& extensions); | 291 const std::vector<RtpHeaderExtension>& extensions); |
| 299 bool SetChannelSendRtpHeaderExtensions( | 292 bool SetChannelSendRtpHeaderExtensions( |
| 300 int channel_id, | 293 int channel_id, |
| 301 const std::vector<RtpHeaderExtension>& extensions); | 294 const std::vector<RtpHeaderExtension>& extensions); |
| 302 | 295 |
| 303 rtc::ThreadChecker thread_checker_; | 296 rtc::ThreadChecker thread_checker_; |
| 304 | 297 |
| 305 WebRtcVoiceEngine* const engine_; | 298 WebRtcVoiceEngine* const engine_ = nullptr; |
| 306 std::vector<AudioCodec> recv_codecs_; | 299 std::vector<AudioCodec> recv_codecs_; |
| 307 std::vector<AudioCodec> send_codecs_; | 300 std::vector<AudioCodec> send_codecs_; |
| 308 rtc::scoped_ptr<webrtc::CodecInst> send_codec_; | 301 rtc::scoped_ptr<webrtc::CodecInst> send_codec_; |
| 309 bool send_bitrate_setting_; | 302 bool send_bitrate_setting_ = false; |
| 310 int send_bitrate_bps_; | 303 int send_bitrate_bps_ = 0; |
| 311 AudioOptions options_; | 304 AudioOptions options_; |
| 312 bool dtmf_allowed_; | 305 bool dtmf_allowed_ = false; |
| 313 bool desired_playout_; | 306 bool desired_playout_ = false; |
| 314 bool nack_enabled_; | 307 bool nack_enabled_ = false; |
| 315 bool playout_; | 308 bool playout_ = false; |
| 316 bool typing_noise_detected_; | 309 SendFlags desired_send_ = SEND_NOTHING; |
| 317 SendFlags desired_send_; | 310 SendFlags send_ = SEND_NOTHING; |
| 318 SendFlags send_; | 311 webrtc::Call* const call_ = nullptr; |
| 319 webrtc::Call* const call_; | |
| 320 | 312 |
| 321 // SSRC of unsignalled receive stream, or -1 if there isn't one. | 313 // SSRC of unsignalled receive stream, or -1 if there isn't one. |
| 322 int64_t default_recv_ssrc_ = -1; | 314 int64_t default_recv_ssrc_ = -1; |
| 323 // Volume for unsignalled stream, which may be set before the stream exists. | 315 // Volume for unsignalled stream, which may be set before the stream exists. |
| 324 double default_recv_volume_ = 1.0; | 316 double default_recv_volume_ = 1.0; |
| 325 // SSRC to use for RTCP receiver reports; default to 1 in case of no signaled | 317 // SSRC to use for RTCP receiver reports; default to 1 in case of no signaled |
| 326 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740 | 318 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740 |
| 327 uint32_t receiver_reports_ssrc_ = 1; | 319 uint32_t receiver_reports_ssrc_ = 1; |
| 328 | 320 |
| 329 class WebRtcAudioSendStream; | 321 class WebRtcAudioSendStream; |
| 330 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_; | 322 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_; |
| 331 std::vector<RtpHeaderExtension> send_extensions_; | 323 std::vector<RtpHeaderExtension> send_extensions_; |
| 332 | 324 |
| 333 class WebRtcAudioReceiveStream; | 325 class WebRtcAudioReceiveStream; |
| 334 std::map<uint32_t, WebRtcAudioReceiveStream*> receive_channels_; | 326 std::map<uint32_t, WebRtcAudioReceiveStream*> receive_channels_; |
| 335 std::map<uint32_t, webrtc::AudioReceiveStream*> receive_streams_; | 327 std::map<uint32_t, webrtc::AudioReceiveStream*> receive_streams_; |
| 336 std::map<uint32_t, StreamParams> receive_stream_params_; | 328 std::map<uint32_t, StreamParams> receive_stream_params_; |
| 337 // receive_channels_ can be read from WebRtc callback thread. Access from | 329 // receive_channels_ can be read from WebRtc callback thread. Access from |
| 338 // the WebRtc thread must be synchronized with edits on the worker thread. | 330 // the WebRtc thread must be synchronized with edits on the worker thread. |
| 339 // Reads on the worker thread are ok. | 331 // Reads on the worker thread are ok. |
| 340 std::vector<RtpHeaderExtension> receive_extensions_; | 332 std::vector<RtpHeaderExtension> receive_extensions_; |
| 341 std::vector<webrtc::RtpExtension> recv_rtp_extensions_; | 333 std::vector<webrtc::RtpExtension> recv_rtp_extensions_; |
| 342 | 334 |
| 343 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel); | 335 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel); |
| 344 }; | 336 }; |
| 345 | |
| 346 } // namespace cricket | 337 } // namespace cricket |
| 347 | 338 |
| 348 #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ | 339 #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ |
| OLD | NEW |