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

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

Powered by Google App Engine
This is Rietveld 408576698