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

Side by Side Diff: content/browser/renderer_host/media/audio_input_renderer_host.cc

Issue 1272223003: Implement writing mic audio input data to file for debugging purposes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review Created 5 years, 4 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 // 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/browser/renderer_host/media/audio_input_renderer_host.h" 5 #include "content/browser/renderer_host/media/audio_input_renderer_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file.h"
9 #include "base/memory/ref_counted.h"
8 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
9 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
10 #include "base/numerics/safe_math.h" 12 #include "base/numerics/safe_math.h"
11 #include "base/process/process.h" 13 #include "base/process/process.h"
14 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
13 #include "content/browser/media/capture/web_contents_audio_input_stream.h" 16 #include "content/browser/media/capture/web_contents_audio_input_stream.h"
14 #include "content/browser/media/capture/web_contents_capture_util.h" 17 #include "content/browser/media/capture/web_contents_capture_util.h"
15 #include "content/browser/media/media_internals.h" 18 #include "content/browser/media/media_internals.h"
19 #include "content/browser/media/webrtc_internals.h"
20 #include "content/browser/renderer_host/media/audio_input_debug_writer.h"
16 #include "content/browser/renderer_host/media/audio_input_device_manager.h" 21 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
17 #include "content/browser/renderer_host/media/audio_input_sync_writer.h" 22 #include "content/browser/renderer_host/media/audio_input_sync_writer.h"
18 #include "content/browser/renderer_host/media/media_stream_manager.h" 23 #include "content/browser/renderer_host/media/media_stream_manager.h"
19 #include "media/audio/audio_manager_base.h" 24 #include "media/audio/audio_manager_base.h"
20 #include "media/base/audio_bus.h" 25 #include "media/base/audio_bus.h"
21 26
27 namespace content {
28
22 namespace { 29 namespace {
23 30
31 const char kDebugRecordingFileNameAddition[] = "source_input";
32 const char kDebugRecordingFileNameExtension[] = "pcm";
33
24 void LogMessage(int stream_id, const std::string& msg, bool add_prefix) { 34 void LogMessage(int stream_id, const std::string& msg, bool add_prefix) {
25 std::ostringstream oss; 35 std::ostringstream oss;
26 oss << "[stream_id=" << stream_id << "] "; 36 oss << "[stream_id=" << stream_id << "] ";
27 if (add_prefix) 37 if (add_prefix)
28 oss << "AIRH::"; 38 oss << "AIRH::";
29 oss << msg; 39 oss << msg;
30 content::MediaStreamManager::SendMessageToNativeLog(oss.str()); 40 content::MediaStreamManager::SendMessageToNativeLog(oss.str());
31 DVLOG(1) << oss.str(); 41 DVLOG(1) << oss.str();
32 } 42 }
33 43
44 base::File CreateDebugRecordingFile(base::FilePath file_path) {
45 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
46 base::File recording_file(
47 file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND);
48 PLOG_IF(ERROR, !recording_file.IsValid())
49 << "Could not open debug recording file, error="
50 << recording_file.error_details();
51 return recording_file.Pass();
52 }
53
54 void CloseFile(base::File file) {
55 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
56 // |file| must be closed and destroyed on FILE thread.
57 }
58
59 void DeleteInputDebugWriterOnFileThread(
60 scoped_ptr<AudioInputDebugWriter> writer) {
61 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
62 // |writer| must be closed and destroyed on FILE thread.
63 }
64
34 } // namespace 65 } // namespace
35 66
36 namespace content {
37
38 struct AudioInputRendererHost::AudioEntry { 67 struct AudioInputRendererHost::AudioEntry {
39 AudioEntry(); 68 AudioEntry();
40 ~AudioEntry(); 69 ~AudioEntry();
41 70
42 // The AudioInputController that manages the audio input stream. 71 // The AudioInputController that manages the audio input stream.
43 scoped_refptr<media::AudioInputController> controller; 72 scoped_refptr<media::AudioInputController> controller;
44 73
45 // The audio input stream ID in the render view. 74 // The audio input stream ID in the render view.
46 int stream_id; 75 int stream_id;
47 76
48 // Shared memory for transmission of the audio data. It has 77 // Shared memory for transmission of the audio data. It has
49 // |shared_memory_segment_count| equal lengthed segments. 78 // |shared_memory_segment_count| equal lengthed segments.
50 base::SharedMemory shared_memory; 79 base::SharedMemory shared_memory;
51 int shared_memory_segment_count; 80 int shared_memory_segment_count;
52 81
53 // The synchronous writer to be used by the controller. We have the 82 // The synchronous writer to be used by the controller. We have the
54 // ownership of the writer. 83 // ownership of the writer.
55 scoped_ptr<media::AudioInputController::SyncWriter> writer; 84 scoped_ptr<media::AudioInputController::SyncWriter> writer;
56 85
86 // Must be deleted on the file thread. Must be posted for deletion and nulled
87 // before the AudioEntry is deleted.
88 scoped_ptr<AudioInputDebugWriter> input_debug_writer;
89
57 // Set to true after we called Close() for the controller. 90 // Set to true after we called Close() for the controller.
58 bool pending_close; 91 bool pending_close;
59 92
60 // If this entry's layout has a keyboard mic channel. 93 // If this entry's layout has a keyboard mic channel.
61 bool has_keyboard_mic_; 94 bool has_keyboard_mic;
62 }; 95 };
63 96
64 AudioInputRendererHost::AudioEntry::AudioEntry() 97 AudioInputRendererHost::AudioEntry::AudioEntry()
65 : stream_id(0), 98 : stream_id(0),
66 shared_memory_segment_count(0), 99 shared_memory_segment_count(0),
67 pending_close(false), 100 pending_close(false),
68 has_keyboard_mic_(false) { 101 has_keyboard_mic(false) {
69 } 102 }
70 103
71 AudioInputRendererHost::AudioEntry::~AudioEntry() {} 104 AudioInputRendererHost::AudioEntry::~AudioEntry() {
105 DCHECK(!input_debug_writer.get());
106 }
72 107
73 AudioInputRendererHost::AudioInputRendererHost( 108 AudioInputRendererHost::AudioInputRendererHost(
74 int render_process_id, 109 int render_process_id,
75 media::AudioManager* audio_manager, 110 media::AudioManager* audio_manager,
76 MediaStreamManager* media_stream_manager, 111 MediaStreamManager* media_stream_manager,
77 AudioMirroringManager* audio_mirroring_manager, 112 AudioMirroringManager* audio_mirroring_manager,
78 media::UserInputMonitor* user_input_monitor) 113 media::UserInputMonitor* user_input_monitor)
79 : BrowserMessageFilter(AudioMsgStart), 114 : BrowserMessageFilter(AudioMsgStart),
80 render_process_id_(render_process_id), 115 render_process_id_(render_process_id),
81 audio_manager_(audio_manager), 116 audio_manager_(audio_manager),
82 media_stream_manager_(media_stream_manager), 117 media_stream_manager_(media_stream_manager),
83 audio_mirroring_manager_(audio_mirroring_manager), 118 audio_mirroring_manager_(audio_mirroring_manager),
84 user_input_monitor_(user_input_monitor), 119 user_input_monitor_(user_input_monitor),
85 audio_log_(MediaInternals::GetInstance()->CreateAudioLog( 120 audio_log_(MediaInternals::GetInstance()->CreateAudioLog(
86 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)) {} 121 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)),
122 weak_factory_(this) {}
87 123
88 AudioInputRendererHost::~AudioInputRendererHost() { 124 AudioInputRendererHost::~AudioInputRendererHost() {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO); 125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
90 DCHECK(audio_entries_.empty()); 126 DCHECK(audio_entries_.empty());
91 } 127 }
92 128
129 void AudioInputRendererHost::EnableDebugRecording(const base::FilePath& file) {
130 DCHECK_CURRENTLY_ON(BrowserThread::IO);
131 base::FilePath file_with_extensions =
132 GetDebugRecordingFilePathWithExtensions(file);
133 for (const auto& entry : audio_entries_)
134 EnabledDebugRecordingForId(file_with_extensions, entry.first);
135 }
136
137 void AudioInputRendererHost::DisableDebugRecording() {
138 for (const auto& entry : audio_entries_) {
139 entry.second->controller->DisableDebugRecording(
140 base::Bind(&AudioInputRendererHost::DeleteDebugWriter,
141 this,
142 entry.first));
143 }
144 }
145
93 void AudioInputRendererHost::OnChannelClosing() { 146 void AudioInputRendererHost::OnChannelClosing() {
94 // Since the IPC sender is gone, close all requested audio streams. 147 // Since the IPC sender is gone, close all requested audio streams.
95 DeleteEntries(); 148 DeleteEntries();
96 } 149 }
97 150
98 void AudioInputRendererHost::OnDestruct() const { 151 void AudioInputRendererHost::OnDestruct() const {
99 BrowserThread::DeleteOnIOThread::Destruct(this); 152 BrowserThread::DeleteOnIOThread::Destruct(this);
100 } 153 }
101 154
102 void AudioInputRendererHost::OnCreated( 155 void AudioInputRendererHost::OnCreated(
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 455
403 if (!entry->controller.get()) { 456 if (!entry->controller.get()) {
404 SendErrorMessage(stream_id, STREAM_CREATE_ERROR); 457 SendErrorMessage(stream_id, STREAM_CREATE_ERROR);
405 MaybeUnregisterKeyboardMicStream(config); 458 MaybeUnregisterKeyboardMicStream(config);
406 return; 459 return;
407 } 460 }
408 461
409 #if defined(OS_CHROMEOS) 462 #if defined(OS_CHROMEOS)
410 if (config.params.channel_layout() == 463 if (config.params.channel_layout() ==
411 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) { 464 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) {
412 entry->has_keyboard_mic_ = true; 465 entry->has_keyboard_mic = true;
413 } 466 }
414 #endif 467 #endif
415 468
416 MediaStreamManager::SendMessageToNativeLog(oss.str()); 469 MediaStreamManager::SendMessageToNativeLog(oss.str());
417 DVLOG(1) << oss.str(); 470 DVLOG(1) << oss.str();
418 471
419 // Since the controller was created successfully, create an entry and add it 472 // Since the controller was created successfully, create an entry and add it
420 // to the map. 473 // to the map.
421 entry->stream_id = stream_id; 474 entry->stream_id = stream_id;
422 audio_entries_.insert(std::make_pair(stream_id, entry.release())); 475 audio_entries_.insert(std::make_pair(stream_id, entry.release()));
423 audio_log_->OnCreated(stream_id, audio_params, device_id); 476 audio_log_->OnCreated(stream_id, audio_params, device_id);
424 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( 477 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry(
425 stream_id, render_process_id_, render_frame_id, audio_log_.get()); 478 stream_id, render_process_id_, render_frame_id, audio_log_.get());
479
480 BrowserThread::PostTask(
481 BrowserThread::UI,
482 FROM_HERE,
483 base::Bind(
484 &AudioInputRendererHost::MaybeEnableDebugRecordingForId,
485 this,
486 stream_id));
426 } 487 }
427 488
428 void AudioInputRendererHost::OnRecordStream(int stream_id) { 489 void AudioInputRendererHost::OnRecordStream(int stream_id) {
429 DCHECK_CURRENTLY_ON(BrowserThread::IO); 490 DCHECK_CURRENTLY_ON(BrowserThread::IO);
430 LogMessage(stream_id, "OnRecordStream", true); 491 LogMessage(stream_id, "OnRecordStream", true);
431 492
432 AudioEntry* entry = LookupById(stream_id); 493 AudioEntry* entry = LookupById(stream_id);
433 if (!entry) { 494 if (!entry) {
434 SendErrorMessage(stream_id, INVALID_AUDIO_ENTRY); 495 SendErrorMessage(stream_id, INVALID_AUDIO_ENTRY);
435 return; 496 return;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 entry->pending_close = true; 552 entry->pending_close = true;
492 audio_log_->OnClosed(entry->stream_id); 553 audio_log_->OnClosed(entry->stream_id);
493 } 554 }
494 } 555 }
495 556
496 void AudioInputRendererHost::DeleteEntry(AudioEntry* entry) { 557 void AudioInputRendererHost::DeleteEntry(AudioEntry* entry) {
497 DCHECK_CURRENTLY_ON(BrowserThread::IO); 558 DCHECK_CURRENTLY_ON(BrowserThread::IO);
498 LogMessage(entry->stream_id, "DeleteEntry: stream is now closed", true); 559 LogMessage(entry->stream_id, "DeleteEntry: stream is now closed", true);
499 560
500 #if defined(OS_CHROMEOS) 561 #if defined(OS_CHROMEOS)
501 if (entry->has_keyboard_mic_) { 562 if (entry->has_keyboard_mic) {
502 media_stream_manager_->audio_input_device_manager() 563 media_stream_manager_->audio_input_device_manager()
503 ->UnregisterKeyboardMicStream(); 564 ->UnregisterKeyboardMicStream();
504 } 565 }
505 #endif 566 #endif
506 567
568 if (entry->input_debug_writer.get()) {
569 BrowserThread::PostTask(
570 BrowserThread::FILE,
571 FROM_HERE,
572 base::Bind(&DeleteInputDebugWriterOnFileThread,
573 base::Passed(entry->input_debug_writer.Pass())));
574 }
575
507 // Delete the entry when this method goes out of scope. 576 // Delete the entry when this method goes out of scope.
508 scoped_ptr<AudioEntry> entry_deleter(entry); 577 scoped_ptr<AudioEntry> entry_deleter(entry);
509 578
510 // Erase the entry from the map. 579 // Erase the entry from the map.
511 audio_entries_.erase(entry->stream_id); 580 audio_entries_.erase(entry->stream_id);
512 } 581 }
513 582
514 void AudioInputRendererHost::DeleteEntryOnError(AudioEntry* entry, 583 void AudioInputRendererHost::DeleteEntryOnError(AudioEntry* entry,
515 ErrorCode error_code) { 584 ErrorCode error_code) {
516 DCHECK_CURRENTLY_ON(BrowserThread::IO); 585 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 const AudioInputHostMsg_CreateStream_Config& config) { 618 const AudioInputHostMsg_CreateStream_Config& config) {
550 #if defined(OS_CHROMEOS) 619 #if defined(OS_CHROMEOS)
551 if (config.params.channel_layout() == 620 if (config.params.channel_layout() ==
552 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) { 621 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) {
553 media_stream_manager_->audio_input_device_manager() 622 media_stream_manager_->audio_input_device_manager()
554 ->UnregisterKeyboardMicStream(); 623 ->UnregisterKeyboardMicStream();
555 } 624 }
556 #endif 625 #endif
557 } 626 }
558 627
628 void AudioInputRendererHost::MaybeEnableDebugRecordingForId(int stream_id) {
629 DCHECK_CURRENTLY_ON(BrowserThread::UI);
630 if (WebRTCInternals::GetInstance()->IsAudioDebugRecordingsEnabled()) {
631 BrowserThread::PostTask(
632 BrowserThread::IO,
633 FROM_HERE,
634 base::Bind(
635 &AudioInputRendererHost::EnabledDebugRecordingForId,
636 this,
637 GetDebugRecordingFilePathWithExtensions(
638 WebRTCInternals::GetInstance()->
639 GetAudioDebugRecordingsFilePath()),
640 stream_id));
641 }
642 }
643
644 #if defined(OS_WIN)
645 #define IntToStringType base::IntToString16
646 #else
647 #define IntToStringType base::IntToString
648 #endif
649
650 base::FilePath AudioInputRendererHost::GetDebugRecordingFilePathWithExtensions(
651 const base::FilePath& file) {
652 return file.AddExtension(kDebugRecordingFileNameAddition);
653 }
654
655 void AudioInputRendererHost::EnabledDebugRecordingForId(
656 const base::FilePath& file,
657 int stream_id) {
658 DCHECK_CURRENTLY_ON(BrowserThread::IO);
659 BrowserThread::PostTaskAndReplyWithResult(
660 BrowserThread::FILE,
661 FROM_HERE,
662 base::Bind(
663 &CreateDebugRecordingFile,
664 file.AddExtension(IntToStringType(stream_id))
665 .AddExtension(
666 FILE_PATH_LITERAL(kDebugRecordingFileNameExtension))),
667 base::Bind(
668 &AudioInputRendererHost::DoEnableDebugRecording,
669 weak_factory_.GetWeakPtr(),
670 stream_id));
671 }
672
673 #undef IntToStringType
674
675 void AudioInputRendererHost::DoEnableDebugRecording(
676 int stream_id,
677 base::File file) {
678 DCHECK_CURRENTLY_ON(BrowserThread::IO);
679 if (!file.IsValid())
680 return;
681 AudioEntry* entry = LookupById(stream_id);
682 if (!entry) {
683 BrowserThread::PostTask(
684 BrowserThread::FILE,
685 FROM_HERE,
686 base::Bind(
687 &CloseFile,
688 Passed(file.Pass())));
689 return;
690 }
691 entry->input_debug_writer.reset(new AudioInputDebugWriter(file.Pass()));
692 entry->controller->EnableDebugRecording(entry->input_debug_writer.get());
693 }
694
695 void AudioInputRendererHost::DeleteDebugWriter(int stream_id) {
696 DCHECK_CURRENTLY_ON(BrowserThread::IO);
697 AudioEntry* entry = LookupById(stream_id);
698 if (!entry) {
699 // This happens if DisableDebugRecording is called right after
700 // DeleteEntries.
701 return;
702 }
703
704 if (entry->input_debug_writer.get()) {
705 BrowserThread::PostTask(
706 BrowserThread::FILE,
707 FROM_HERE,
708 base::Bind(&DeleteInputDebugWriterOnFileThread,
709 base::Passed(entry->input_debug_writer.Pass())));
710 }
711 }
712
559 } // namespace content 713 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698