Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: webrtc/audio/audio_send_stream.cc

Issue 2405183002: Moving WebRtcVoiceMediaChannel::SendSetCodec to AudioSendStream. (Closed)
Patch Set: final change Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/audio/audio_send_stream.h ('k') | webrtc/audio/audio_send_stream_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 12 matching lines...) Expand all
23 #include "webrtc/modules/pacing/paced_sender.h" 23 #include "webrtc/modules/pacing/paced_sender.h"
24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
25 #include "webrtc/voice_engine/channel_proxy.h" 25 #include "webrtc/voice_engine/channel_proxy.h"
26 #include "webrtc/voice_engine/include/voe_audio_processing.h" 26 #include "webrtc/voice_engine/include/voe_audio_processing.h"
27 #include "webrtc/voice_engine/include/voe_codec.h" 27 #include "webrtc/voice_engine/include/voe_codec.h"
28 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" 28 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
29 #include "webrtc/voice_engine/include/voe_volume_control.h" 29 #include "webrtc/voice_engine/include/voe_volume_control.h"
30 #include "webrtc/voice_engine/voice_engine_impl.h" 30 #include "webrtc/voice_engine/voice_engine_impl.h"
31 31
32 namespace webrtc { 32 namespace webrtc {
33
34 namespace {
35
36 constexpr char kOpusCodecName[] = "opus";
37
38 // TODO(minyue): Remove |LOG_RTCERR2|.
39 #define LOG_RTCERR2(func, a1, a2, err) \
40 LOG(LS_WARNING) << "" << #func << "(" << a1 << ", " << a2 \
41 << ") failed, err=" << err
42
43 // TODO(minyue): Remove |LOG_RTCERR3|.
44 #define LOG_RTCERR3(func, a1, a2, a3, err) \
45 LOG(LS_WARNING) << "" << #func << "(" << a1 << ", " << a2 << ", " << a3 \
46 << ") failed, err=" << err
47
48 std::string ToString(const webrtc::CodecInst& codec) {
49 std::stringstream ss;
50 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels << " ("
51 << codec.pltype << ")";
52 return ss.str();
53 }
54
55 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
56 return (_stricmp(codec.plname, ref_name) == 0);
57 }
58
59 } // namespace
60
33 std::string AudioSendStream::Config::Rtp::ToString() const { 61 std::string AudioSendStream::Config::Rtp::ToString() const {
34 std::stringstream ss; 62 std::stringstream ss;
35 ss << "{ssrc: " << ssrc; 63 ss << "{ssrc: " << ssrc;
36 ss << ", extensions: ["; 64 ss << ", extensions: [";
37 for (size_t i = 0; i < extensions.size(); ++i) { 65 for (size_t i = 0; i < extensions.size(); ++i) {
38 ss << extensions[i].ToString(); 66 ss << extensions[i].ToString();
39 if (i != extensions.size() - 1) { 67 if (i != extensions.size() - 1) {
40 ss << ", "; 68 ss << ", ";
41 } 69 }
42 } 70 }
43 ss << ']'; 71 ss << ']';
44 ss << ", nack: " << nack.ToString(); 72 ss << ", nack: " << nack.ToString();
45 ss << ", c_name: " << c_name; 73 ss << ", c_name: " << c_name;
46 ss << '}'; 74 ss << '}';
47 return ss.str(); 75 return ss.str();
48 } 76 }
49 77
50 std::string AudioSendStream::Config::ToString() const { 78 std::string AudioSendStream::Config::ToString() const {
51 std::stringstream ss; 79 std::stringstream ss;
52 ss << "{rtp: " << rtp.ToString(); 80 ss << "{rtp: " << rtp.ToString();
53 ss << ", voe_channel_id: " << voe_channel_id; 81 ss << ", voe_channel_id: " << voe_channel_id;
54 // TODO(solenberg): Encoder config. 82 // TODO(solenberg): Encoder config.
55 ss << ", cng_payload_type: " << cng_payload_type; 83 ss << ", cng_payload_type: " << send_codec_spec.cng_payload_type;
56 ss << '}'; 84 ss << '}';
57 return ss.str(); 85 return ss.str();
58 } 86 }
59 87
60 namespace internal { 88 namespace internal {
61 AudioSendStream::AudioSendStream( 89 AudioSendStream::AudioSendStream(
62 const webrtc::AudioSendStream::Config& config, 90 const webrtc::AudioSendStream::Config& config,
63 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 91 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
64 rtc::TaskQueue* worker_queue, 92 rtc::TaskQueue* worker_queue,
65 CongestionController* congestion_controller, 93 CongestionController* congestion_controller,
(...skipping 29 matching lines...) Expand all
95 if (extension.uri == RtpExtension::kAbsSendTimeUri) { 123 if (extension.uri == RtpExtension::kAbsSendTimeUri) {
96 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); 124 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
97 } else if (extension.uri == RtpExtension::kAudioLevelUri) { 125 } else if (extension.uri == RtpExtension::kAudioLevelUri) {
98 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); 126 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
99 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { 127 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
100 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); 128 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
101 } else { 129 } else {
102 RTC_NOTREACHED() << "Registering unsupported RTP extension."; 130 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
103 } 131 }
104 } 132 }
133 if (!SetupSendCodec()) {
134 LOG(LS_ERROR) << "Failed to set up send codec state.";
135 }
105 } 136 }
106 137
107 AudioSendStream::~AudioSendStream() { 138 AudioSendStream::~AudioSendStream() {
108 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 139 RTC_DCHECK(thread_checker_.CalledOnValidThread());
109 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); 140 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
110 channel_proxy_->DeRegisterExternalTransport(); 141 channel_proxy_->DeRegisterExternalTransport();
111 channel_proxy_->ResetCongestionControlObjects(); 142 channel_proxy_->ResetCongestionControlObjects();
112 channel_proxy_->SetRtcEventLog(nullptr); 143 channel_proxy_->SetRtcEventLog(nullptr);
113 } 144 }
114 145
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 return config_; 309 return config_;
279 } 310 }
280 311
281 VoiceEngine* AudioSendStream::voice_engine() const { 312 VoiceEngine* AudioSendStream::voice_engine() const {
282 internal::AudioState* audio_state = 313 internal::AudioState* audio_state =
283 static_cast<internal::AudioState*>(audio_state_.get()); 314 static_cast<internal::AudioState*>(audio_state_.get());
284 VoiceEngine* voice_engine = audio_state->voice_engine(); 315 VoiceEngine* voice_engine = audio_state->voice_engine();
285 RTC_DCHECK(voice_engine); 316 RTC_DCHECK(voice_engine);
286 return voice_engine; 317 return voice_engine;
287 } 318 }
319
320 // Apply current codec settings to a single voe::Channel used for sending.
321 bool AudioSendStream::SetupSendCodec() {
322 ScopedVoEInterface<VoEBase> base(voice_engine());
323 ScopedVoEInterface<VoECodec> codec(voice_engine());
324
325 const int channel = config_.voe_channel_id;
326
327 // Disable VAD and FEC unless we know the other side wants them.
328 codec->SetVADStatus(channel, false);
329 codec->SetFECStatus(channel, false);
330
331 const auto& send_codec_spec = config_.send_codec_spec;
332
333 // Set the codec immediately, since SetVADStatus() depends on whether
334 // the current codec is mono or stereo.
335 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
336 << ToString(send_codec_spec.codec_inst)
337 << ", bitrate=" << send_codec_spec.codec_inst.rate;
338
339 // If codec is already configured, we do not it again.
340 // TODO(minyue): check if this check is really needed, or can we move it into
341 // |codec->SetSendCodec|.
342 webrtc::CodecInst current_codec = {0};
343 if (codec->GetSendCodec(channel, current_codec) != 0 ||
344 (send_codec_spec.codec_inst != current_codec)) {
345 if (codec->SetSendCodec(channel, send_codec_spec.codec_inst) == -1) {
346 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec_spec.codec_inst),
347 base->LastError());
348 return false;
349 }
350 }
351
352 // FEC should be enabled after SetSendCodec.
353 if (send_codec_spec.enable_codec_fec) {
354 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
355 << channel;
356 if (codec->SetFECStatus(channel, true) == -1) {
357 // Enable codec internal FEC. Treat any failure as fatal internal error.
358 LOG_RTCERR2(SetFECStatus, channel, true, base->LastError());
359 return false;
360 }
361 }
362
363 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) {
364 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
365 // send codec has to be Opus.
366
367 // Set Opus internal DTX.
368 LOG(LS_INFO) << "Attempt to "
369 << (send_codec_spec.enable_opus_dtx ? "enable" : "disable")
370 << " Opus DTX on channel " << channel;
371 if (codec->SetOpusDtx(channel, send_codec_spec.enable_opus_dtx)) {
372 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec.enable_opus_dtx,
373 base->LastError());
374 return false;
375 }
376
377 // If opus_max_playback_rate <= 0, the default maximum playback rate
378 // (48 kHz) will be used.
379 if (send_codec_spec.opus_max_playback_rate > 0) {
380 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
381 << send_codec_spec.opus_max_playback_rate
382 << " Hz on channel " << channel;
383 if (codec->SetOpusMaxPlaybackRate(
384 channel, send_codec_spec.opus_max_playback_rate) == -1) {
385 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
386 send_codec_spec.opus_max_playback_rate, base->LastError());
387 return false;
388 }
389 }
390 }
391
392 // Set the CN payloadtype and the VAD status.
393 if (send_codec_spec.cng_payload_type != -1) {
394 // The CN payload type for 8000 Hz clockrate is fixed at 13.
395 if (send_codec_spec.cng_plfreq != 8000) {
396 webrtc::PayloadFrequencies cn_freq;
397 switch (send_codec_spec.cng_plfreq) {
398 case 16000:
399 cn_freq = webrtc::kFreq16000Hz;
400 break;
401 case 32000:
402 cn_freq = webrtc::kFreq32000Hz;
403 break;
404 default:
405 RTC_NOTREACHED();
406 return false;
407 }
408 if (codec->SetSendCNPayloadType(channel, send_codec_spec.cng_payload_type,
409 cn_freq) == -1) {
410 LOG_RTCERR3(SetSendCNPayloadType, channel,
411 send_codec_spec.cng_payload_type, cn_freq,
412 base->LastError());
413
414 // TODO(ajm): This failure condition will be removed from VoE.
415 // Restore the return here when we update to a new enough webrtc.
416 //
417 // Not returning false because the SetSendCNPayloadType will fail if
418 // the channel is already sending.
419 // This can happen if the remote description is applied twice, for
420 // example in the case of ROAP on top of JSEP, where both side will
421 // send the offer.
422 }
423 }
424
425 // Only turn on VAD if we have a CN payload type that matches the
426 // clockrate for the codec we are going to use.
427 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq &&
428 send_codec_spec.codec_inst.channels == 1) {
429 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
430 // interaction between VAD and Opus FEC.
431 LOG(LS_INFO) << "Enabling VAD";
432 if (codec->SetVADStatus(channel, true) == -1) {
433 LOG_RTCERR2(SetVADStatus, channel, true, base->LastError());
434 return false;
435 }
436 }
437 }
438 return true;
439 }
440
288 } // namespace internal 441 } // namespace internal
289 } // namespace webrtc 442 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/audio/audio_send_stream.h ('k') | webrtc/audio/audio_send_stream_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698