OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/webrtc_audio_capturer.h" | 5 #include "content/renderer/media/webrtc_audio_capturer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "content/child/child_process.h" | 12 #include "content/child/child_process.h" |
13 #include "content/renderer/media/audio_device_factory.h" | 13 #include "content/renderer/media/audio_device_factory.h" |
14 #include "content/renderer/media/media_stream_audio_processor.h" | 14 #include "content/renderer/media/media_stream_audio_processor.h" |
15 #include "content/renderer/media/media_stream_audio_processor_options.h" | 15 #include "content/renderer/media/media_stream_audio_processor_options.h" |
16 #include "content/renderer/media/media_stream_audio_source.h" | 16 #include "content/renderer/media/media_stream_audio_source.h" |
17 #include "content/renderer/media/webrtc_audio_device_impl.h" | 17 #include "content/renderer/media/webrtc_audio_device_impl.h" |
18 #include "content/renderer/media/webrtc_local_audio_track.h" | 18 #include "content/renderer/media/webrtc_local_audio_track.h" |
19 #include "content/renderer/media/webrtc_logging.h" | 19 #include "content/renderer/media/webrtc_logging.h" |
20 #include "media/audio/sample_rates.h" | 20 #include "media/audio/sample_rates.h" |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 | 23 |
| 24 namespace { |
| 25 |
| 26 // Method to check if any of the data in |audio_source| has energy. |
| 27 bool HasDataEnergy(const media::AudioBus& audio_source) { |
| 28 for (int ch = 0; ch < audio_source.channels(); ++ch) { |
| 29 const float* channel_ptr = audio_source.channel(ch); |
| 30 for (int frame = 0; frame < audio_source.frames(); ++frame) { |
| 31 if (channel_ptr[frame] != 0) |
| 32 return true; |
| 33 } |
| 34 } |
| 35 |
| 36 // All the data is zero. |
| 37 return false; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
24 // Reference counted container of WebRtcLocalAudioTrack delegate. | 42 // Reference counted container of WebRtcLocalAudioTrack delegate. |
25 // TODO(xians): Switch to MediaStreamAudioSinkOwner. | 43 // TODO(xians): Switch to MediaStreamAudioSinkOwner. |
26 class WebRtcAudioCapturer::TrackOwner | 44 class WebRtcAudioCapturer::TrackOwner |
27 : public base::RefCountedThreadSafe<WebRtcAudioCapturer::TrackOwner> { | 45 : public base::RefCountedThreadSafe<WebRtcAudioCapturer::TrackOwner> { |
28 public: | 46 public: |
29 explicit TrackOwner(WebRtcLocalAudioTrack* track) | 47 explicit TrackOwner(WebRtcLocalAudioTrack* track) |
30 : delegate_(track) {} | 48 : delegate_(track) {} |
31 | 49 |
32 void Capture(const int16* audio_data, | 50 void Capture(const int16* audio_data, |
33 base::TimeDelta delay, | 51 base::TimeDelta delay, |
34 double volume, | 52 double volume, |
35 bool key_pressed, | 53 bool key_pressed, |
36 bool need_audio_processing) { | 54 bool need_audio_processing, |
| 55 bool force_report_nonzero_energy) { |
37 base::AutoLock lock(lock_); | 56 base::AutoLock lock(lock_); |
38 if (delegate_) { | 57 if (delegate_) { |
39 delegate_->Capture(audio_data, | 58 delegate_->Capture(audio_data, |
40 delay, | 59 delay, |
41 volume, | 60 volume, |
42 key_pressed, | 61 key_pressed, |
43 need_audio_processing); | 62 need_audio_processing, |
| 63 force_report_nonzero_energy); |
44 } | 64 } |
45 } | 65 } |
46 | 66 |
47 void OnSetFormat(const media::AudioParameters& params) { | 67 void OnSetFormat(const media::AudioParameters& params) { |
48 base::AutoLock lock(lock_); | 68 base::AutoLock lock(lock_); |
49 if (delegate_) | 69 if (delegate_) |
50 delegate_->OnSetFormat(params); | 70 delegate_->OnSetFormat(params); |
51 } | 71 } |
52 | 72 |
53 void SetAudioProcessor( | 73 void SetAudioProcessor( |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 500 |
481 // Notify the tracks on when the format changes. This will do nothing if | 501 // Notify the tracks on when the format changes. This will do nothing if |
482 // |tracks_to_notify_format| is empty. | 502 // |tracks_to_notify_format| is empty. |
483 media::AudioParameters output_params = audio_processor_->OutputFormat(); | 503 media::AudioParameters output_params = audio_processor_->OutputFormat(); |
484 for (TrackList::ItemList::const_iterator it = tracks_to_notify_format.begin(); | 504 for (TrackList::ItemList::const_iterator it = tracks_to_notify_format.begin(); |
485 it != tracks_to_notify_format.end(); ++it) { | 505 it != tracks_to_notify_format.end(); ++it) { |
486 (*it)->OnSetFormat(output_params); | 506 (*it)->OnSetFormat(output_params); |
487 (*it)->SetAudioProcessor(audio_processor_); | 507 (*it)->SetAudioProcessor(audio_processor_); |
488 } | 508 } |
489 | 509 |
| 510 // Figure out if the pre-processed data has any energy or not, the |
| 511 // information will be passed to the track to force the calculator |
| 512 // to report energy in case the post-processed data is zeroed by the audio |
| 513 // processing. |
| 514 const bool force_report_nonzero_energy = HasDataEnergy(*audio_source); |
| 515 |
490 // Push the data to the processor for processing. | 516 // Push the data to the processor for processing. |
491 audio_processor_->PushCaptureData(audio_source); | 517 audio_processor_->PushCaptureData(audio_source); |
492 | 518 |
493 // Process and consume the data in the processor until there is not enough | 519 // Process and consume the data in the processor until there is not enough |
494 // data in the processor. | 520 // data in the processor. |
495 int16* output = NULL; | 521 int16* output = NULL; |
496 int new_volume = 0; | 522 int new_volume = 0; |
497 while (audio_processor_->ProcessAndConsumeData( | 523 while (audio_processor_->ProcessAndConsumeData( |
498 audio_delay, current_volume, key_pressed, &new_volume, &output)) { | 524 audio_delay, current_volume, key_pressed, &new_volume, &output)) { |
499 // Feed the post-processed data to the tracks. | 525 // Feed the post-processed data to the tracks. |
500 for (TrackList::ItemList::const_iterator it = tracks.begin(); | 526 for (TrackList::ItemList::const_iterator it = tracks.begin(); |
501 it != tracks.end(); ++it) { | 527 it != tracks.end(); ++it) { |
502 (*it)->Capture(output, audio_delay, current_volume, key_pressed, | 528 (*it)->Capture(output, audio_delay, current_volume, key_pressed, |
503 need_audio_processing); | 529 need_audio_processing, force_report_nonzero_energy); |
504 } | 530 } |
505 | 531 |
506 if (new_volume) { | 532 if (new_volume) { |
507 SetVolume(new_volume); | 533 SetVolume(new_volume); |
508 | 534 |
509 // Update the |current_volume| to avoid passing the old volume to AGC. | 535 // Update the |current_volume| to avoid passing the old volume to AGC. |
510 current_volume = new_volume; | 536 current_volume = new_volume; |
511 } | 537 } |
512 } | 538 } |
513 } | 539 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 | 599 |
574 void WebRtcAudioCapturer::SetCapturerSourceForTesting( | 600 void WebRtcAudioCapturer::SetCapturerSourceForTesting( |
575 const scoped_refptr<media::AudioCapturerSource>& source, | 601 const scoped_refptr<media::AudioCapturerSource>& source, |
576 media::AudioParameters params) { | 602 media::AudioParameters params) { |
577 // Create a new audio stream as source which uses the new source. | 603 // Create a new audio stream as source which uses the new source. |
578 SetCapturerSource(source, params.channel_layout(), | 604 SetCapturerSource(source, params.channel_layout(), |
579 static_cast<float>(params.sample_rate())); | 605 static_cast<float>(params.sample_rate())); |
580 } | 606 } |
581 | 607 |
582 } // namespace content | 608 } // namespace content |
OLD | NEW |