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

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

Issue 2405183002: Moving WebRtcVoiceMediaChannel::SendSetCodec to AudioSendStream. (Closed)
Patch Set: on fredrik's comments 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
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 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
49 return (_stricmp(codec.plname, ref_name) == 0);
50 }
51
52 } // namespace
53
33 std::string AudioSendStream::Config::Rtp::ToString() const { 54 std::string AudioSendStream::Config::Rtp::ToString() const {
34 std::stringstream ss; 55 std::stringstream ss;
35 ss << "{ssrc: " << ssrc; 56 ss << "{ssrc: " << ssrc;
36 ss << ", extensions: ["; 57 ss << ", extensions: [";
37 for (size_t i = 0; i < extensions.size(); ++i) { 58 for (size_t i = 0; i < extensions.size(); ++i) {
38 ss << extensions[i].ToString(); 59 ss << extensions[i].ToString();
39 if (i != extensions.size() - 1) { 60 if (i != extensions.size() - 1) {
40 ss << ", "; 61 ss << ", ";
41 } 62 }
42 } 63 }
43 ss << ']'; 64 ss << ']';
44 ss << ", nack: " << nack.ToString(); 65 ss << ", nack: " << nack.ToString();
45 ss << ", c_name: " << c_name; 66 ss << ", c_name: " << c_name;
46 ss << '}'; 67 ss << '}';
47 return ss.str(); 68 return ss.str();
48 } 69 }
49 70
50 std::string AudioSendStream::Config::ToString() const { 71 std::string AudioSendStream::Config::ToString() const {
51 std::stringstream ss; 72 std::stringstream ss;
52 ss << "{rtp: " << rtp.ToString(); 73 ss << "{rtp: " << rtp.ToString();
53 ss << ", voe_channel_id: " << voe_channel_id; 74 ss << ", voe_channel_id: " << voe_channel_id;
54 // TODO(solenberg): Encoder config. 75 // TODO(solenberg): Encoder config.
55 ss << ", cng_payload_type: " << cng_payload_type; 76 ss << ", cng_payload_type: " << send_codec_spec.cng_payload_type;
56 ss << '}'; 77 ss << '}';
57 return ss.str(); 78 return ss.str();
58 } 79 }
59 80
60 namespace internal { 81 namespace internal {
61 AudioSendStream::AudioSendStream( 82 AudioSendStream::AudioSendStream(
62 const webrtc::AudioSendStream::Config& config, 83 const webrtc::AudioSendStream::Config& config,
63 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 84 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
64 rtc::TaskQueue* worker_queue, 85 rtc::TaskQueue* worker_queue,
65 CongestionController* congestion_controller, 86 CongestionController* congestion_controller,
(...skipping 29 matching lines...) Expand all
95 if (extension.uri == RtpExtension::kAbsSendTimeUri) { 116 if (extension.uri == RtpExtension::kAbsSendTimeUri) {
96 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); 117 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
97 } else if (extension.uri == RtpExtension::kAudioLevelUri) { 118 } else if (extension.uri == RtpExtension::kAudioLevelUri) {
98 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); 119 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
99 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { 120 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
100 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); 121 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
101 } else { 122 } else {
102 RTC_NOTREACHED() << "Registering unsupported RTP extension."; 123 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
103 } 124 }
104 } 125 }
126 if (!SetupSendCodec()) {
127 LOG(LS_ERROR) << "Failed to set up send codec state.";
128 }
105 } 129 }
106 130
107 AudioSendStream::~AudioSendStream() { 131 AudioSendStream::~AudioSendStream() {
108 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 132 RTC_DCHECK(thread_checker_.CalledOnValidThread());
109 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); 133 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
110 channel_proxy_->DeRegisterExternalTransport(); 134 channel_proxy_->DeRegisterExternalTransport();
111 channel_proxy_->ResetCongestionControlObjects(); 135 channel_proxy_->ResetCongestionControlObjects();
112 channel_proxy_->SetRtcEventLog(nullptr); 136 channel_proxy_->SetRtcEventLog(nullptr);
113 } 137 }
114 138
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 return config_; 302 return config_;
279 } 303 }
280 304
281 VoiceEngine* AudioSendStream::voice_engine() const { 305 VoiceEngine* AudioSendStream::voice_engine() const {
282 internal::AudioState* audio_state = 306 internal::AudioState* audio_state =
283 static_cast<internal::AudioState*>(audio_state_.get()); 307 static_cast<internal::AudioState*>(audio_state_.get());
284 VoiceEngine* voice_engine = audio_state->voice_engine(); 308 VoiceEngine* voice_engine = audio_state->voice_engine();
285 RTC_DCHECK(voice_engine); 309 RTC_DCHECK(voice_engine);
286 return voice_engine; 310 return voice_engine;
287 } 311 }
312
313 // Apply current codec settings to a single voe::Channel used for sending.
314 bool AudioSendStream::SetupSendCodec() {
315 ScopedVoEInterface<VoEBase> base(voice_engine());
316 ScopedVoEInterface<VoECodec> codec(voice_engine());
317
318 const int channel = config_.voe_channel_id;
319
320 // Disable VAD and FEC unless we know the other side wants them.
321 codec->SetVADStatus(channel, false);
322 codec->SetFECStatus(channel, false);
323
324 auto send_codec_spec = config_.send_codec_spec;
325
326 // Set the codec immediately, since SetVADStatus() depends on whether
327 // the current codec is mono or stereo.
328 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
329 << send_codec_spec.codec_inst.plname << "/"
330 << send_codec_spec.codec_inst.plfreq << "/"
331 << send_codec_spec.codec_inst.channels << " ("
332 << send_codec_spec.codec_inst.pltype
333 << "), bitrate=" << send_codec_spec.codec_inst.rate;
334
335 // If codec is already configured, we do not it again.
336 // TODO(minyue): check if this check is really needed, or can we move it into
337 // |codec->SetSendCodec|.
338 webrtc::CodecInst current_codec = {0};
339 if (codec->GetSendCodec(channel, current_codec) != 0 ||
340 (send_codec_spec.codec_inst != current_codec)) {
341 if (codec->SetSendCodec(channel, send_codec_spec.codec_inst) == -1) {
342 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec_spec.codec_inst),
343 base->LastError());
344 return false;
345 }
346 }
347
348 // FEC should be enabled after SetSendCodec.
349 if (send_codec_spec.enable_codec_fec) {
350 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
351 << channel;
352 if (codec->SetFECStatus(channel, true) == -1) {
353 // Enable codec internal FEC. Treat any failure as fatal internal error.
354 LOG_RTCERR2(SetFECStatus, channel, true, base->LastError());
355 return false;
356 }
357 }
358
359 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) {
360 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
361 // send codec has to be Opus.
362
363 // Set Opus internal DTX.
364 LOG(LS_INFO) << "Attempt to "
365 << (send_codec_spec.enable_opus_dtx ? "enable" : "disable")
366 << " Opus DTX on channel " << channel;
367 if (codec->SetOpusDtx(channel, send_codec_spec.enable_opus_dtx)) {
368 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec.enable_opus_dtx,
369 base->LastError());
370 return false;
371 }
372
373 // If opus_max_playback_rate <= 0, the default maximum playback rate
374 // (48 kHz) will be used.
375 if (send_codec_spec.opus_max_playback_rate > 0) {
376 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
377 << send_codec_spec.opus_max_playback_rate
378 << " Hz on channel " << channel;
379 if (codec->SetOpusMaxPlaybackRate(
380 channel, send_codec_spec.opus_max_playback_rate) == -1) {
381 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
382 send_codec_spec.opus_max_playback_rate, base->LastError());
383 return false;
384 }
385 }
386 }
387
388 // Set the CN payloadtype and the VAD status.
389 if (send_codec_spec.cng_payload_type != -1) {
390 // The CN payload type for 8000 Hz clockrate is fixed at 13.
391 if (send_codec_spec.cng_plfreq != 8000) {
392 webrtc::PayloadFrequencies cn_freq;
393 switch (send_codec_spec.cng_plfreq) {
394 case 16000:
395 cn_freq = webrtc::kFreq16000Hz;
396 break;
397 case 32000:
398 cn_freq = webrtc::kFreq32000Hz;
399 break;
400 default:
401 RTC_NOTREACHED();
402 return false;
403 }
404 if (codec->SetSendCNPayloadType(channel, send_codec_spec.cng_payload_type,
405 cn_freq) == -1) {
406 LOG_RTCERR3(SetSendCNPayloadType, channel,
407 send_codec_spec.cng_payload_type, cn_freq,
408 base->LastError());
409
410 // TODO(ajm): This failure condition will be removed from VoE.
411 // Restore the return here when we update to a new enough webrtc.
412 //
413 // Not returning false because the SetSendCNPayloadType will fail if
414 // the channel is already sending.
415 // This can happen if the remote description is applied twice, for
416 // example in the case of ROAP on top of JSEP, where both side will
417 // send the offer.
418 }
419 }
420
421 // Only turn on VAD if we have a CN payload type that matches the
422 // clockrate for the codec we are going to use.
423 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq &&
424 send_codec_spec.codec_inst.channels == 1) {
425 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
426 // interaction between VAD and Opus FEC.
427 LOG(LS_INFO) << "Enabling VAD";
428 if (codec->SetVADStatus(channel, true) == -1) {
429 LOG_RTCERR2(SetVADStatus, channel, true, base->LastError());
430 return false;
431 }
432 }
433 }
434 return true;
435 }
436
288 } // namespace internal 437 } // namespace internal
289 } // namespace webrtc 438 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698