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

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 + rebase. 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"
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,
111 RenderProcessHost* render_process_host,
75 media::AudioManager* audio_manager, 112 media::AudioManager* audio_manager,
76 MediaStreamManager* media_stream_manager, 113 MediaStreamManager* media_stream_manager,
77 AudioMirroringManager* audio_mirroring_manager, 114 AudioMirroringManager* audio_mirroring_manager,
78 media::UserInputMonitor* user_input_monitor) 115 media::UserInputMonitor* user_input_monitor)
79 : BrowserMessageFilter(AudioMsgStart), 116 : BrowserMessageFilter(AudioMsgStart),
80 render_process_id_(render_process_id), 117 render_process_id_(render_process_id),
118 render_process_host_(render_process_host),
nasko 2015/08/19 22:04:15 Why pass the full RenderProcessHost object? It is
Henrik Grunell 2015/08/20 07:26:18 Actually, I think it's better to get the pid in RP
nasko 2015/08/20 19:08:56 What happens if the process crashes though? The RP
Henrik Grunell 2015/08/20 20:03:49 Each time the recording is enabled it constructs t
nasko 2015/08/20 20:56:13 Do we stop recording if the renderer process dies?
Henrik Grunell 2015/08/21 13:51:03 Yes, then the channel closes and in AudioInputRend
81 audio_manager_(audio_manager), 119 audio_manager_(audio_manager),
82 media_stream_manager_(media_stream_manager), 120 media_stream_manager_(media_stream_manager),
83 audio_mirroring_manager_(audio_mirroring_manager), 121 audio_mirroring_manager_(audio_mirroring_manager),
84 user_input_monitor_(user_input_monitor), 122 user_input_monitor_(user_input_monitor),
85 audio_log_(MediaInternals::GetInstance()->CreateAudioLog( 123 audio_log_(MediaInternals::GetInstance()->CreateAudioLog(
86 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)) {} 124 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER)),
125 weak_factory_(this) {}
87 126
88 AudioInputRendererHost::~AudioInputRendererHost() { 127 AudioInputRendererHost::~AudioInputRendererHost() {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO); 128 DCHECK_CURRENTLY_ON(BrowserThread::IO);
90 DCHECK(audio_entries_.empty()); 129 DCHECK(audio_entries_.empty());
91 } 130 }
92 131
132 void AudioInputRendererHost::EnableDebugRecording(const base::FilePath& file) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134 base::FilePath file_with_extensions =
135 GetDebugRecordingFilePathWithExtensions(file);
136 for (const auto& entry : audio_entries_)
137 EnabledDebugRecordingForId(file_with_extensions, entry.first);
138 }
139
140 void AudioInputRendererHost::DisableDebugRecording() {
141 for (const auto& entry : audio_entries_) {
142 entry.second->controller->DisableDebugRecording(
143 base::Bind(&AudioInputRendererHost::DeleteDebugWriter,
144 this,
145 entry.first));
146 }
147 }
148
93 void AudioInputRendererHost::OnChannelClosing() { 149 void AudioInputRendererHost::OnChannelClosing() {
94 // Since the IPC sender is gone, close all requested audio streams. 150 // Since the IPC sender is gone, close all requested audio streams.
95 DeleteEntries(); 151 DeleteEntries();
96 } 152 }
97 153
98 void AudioInputRendererHost::OnDestruct() const { 154 void AudioInputRendererHost::OnDestruct() const {
99 BrowserThread::DeleteOnIOThread::Destruct(this); 155 BrowserThread::DeleteOnIOThread::Destruct(this);
100 } 156 }
101 157
102 void AudioInputRendererHost::OnCreated( 158 void AudioInputRendererHost::OnCreated(
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 458
403 if (!entry->controller.get()) { 459 if (!entry->controller.get()) {
404 SendErrorMessage(stream_id, STREAM_CREATE_ERROR); 460 SendErrorMessage(stream_id, STREAM_CREATE_ERROR);
405 MaybeUnregisterKeyboardMicStream(config); 461 MaybeUnregisterKeyboardMicStream(config);
406 return; 462 return;
407 } 463 }
408 464
409 #if defined(OS_CHROMEOS) 465 #if defined(OS_CHROMEOS)
410 if (config.params.channel_layout() == 466 if (config.params.channel_layout() ==
411 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) { 467 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) {
412 entry->has_keyboard_mic_ = true; 468 entry->has_keyboard_mic = true;
413 } 469 }
414 #endif 470 #endif
415 471
416 MediaStreamManager::SendMessageToNativeLog(oss.str()); 472 MediaStreamManager::SendMessageToNativeLog(oss.str());
417 DVLOG(1) << oss.str(); 473 DVLOG(1) << oss.str();
418 474
419 // Since the controller was created successfully, create an entry and add it 475 // Since the controller was created successfully, create an entry and add it
420 // to the map. 476 // to the map.
421 entry->stream_id = stream_id; 477 entry->stream_id = stream_id;
422 audio_entries_.insert(std::make_pair(stream_id, entry.release())); 478 audio_entries_.insert(std::make_pair(stream_id, entry.release()));
423 audio_log_->OnCreated(stream_id, audio_params, device_id); 479 audio_log_->OnCreated(stream_id, audio_params, device_id);
424 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( 480 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry(
425 stream_id, render_process_id_, render_frame_id, audio_log_.get()); 481 stream_id, render_process_id_, render_frame_id, audio_log_.get());
482
483 BrowserThread::PostTask(
484 BrowserThread::UI,
485 FROM_HERE,
486 base::Bind(
487 &AudioInputRendererHost::MaybeEnableDebugRecordingForId,
488 this,
489 stream_id));
426 } 490 }
427 491
428 void AudioInputRendererHost::OnRecordStream(int stream_id) { 492 void AudioInputRendererHost::OnRecordStream(int stream_id) {
429 DCHECK_CURRENTLY_ON(BrowserThread::IO); 493 DCHECK_CURRENTLY_ON(BrowserThread::IO);
430 LogMessage(stream_id, "OnRecordStream", true); 494 LogMessage(stream_id, "OnRecordStream", true);
431 495
432 AudioEntry* entry = LookupById(stream_id); 496 AudioEntry* entry = LookupById(stream_id);
433 if (!entry) { 497 if (!entry) {
434 SendErrorMessage(stream_id, INVALID_AUDIO_ENTRY); 498 SendErrorMessage(stream_id, INVALID_AUDIO_ENTRY);
435 return; 499 return;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 entry->pending_close = true; 555 entry->pending_close = true;
492 audio_log_->OnClosed(entry->stream_id); 556 audio_log_->OnClosed(entry->stream_id);
493 } 557 }
494 } 558 }
495 559
496 void AudioInputRendererHost::DeleteEntry(AudioEntry* entry) { 560 void AudioInputRendererHost::DeleteEntry(AudioEntry* entry) {
497 DCHECK_CURRENTLY_ON(BrowserThread::IO); 561 DCHECK_CURRENTLY_ON(BrowserThread::IO);
498 LogMessage(entry->stream_id, "DeleteEntry: stream is now closed", true); 562 LogMessage(entry->stream_id, "DeleteEntry: stream is now closed", true);
499 563
500 #if defined(OS_CHROMEOS) 564 #if defined(OS_CHROMEOS)
501 if (entry->has_keyboard_mic_) { 565 if (entry->has_keyboard_mic) {
502 media_stream_manager_->audio_input_device_manager() 566 media_stream_manager_->audio_input_device_manager()
503 ->UnregisterKeyboardMicStream(); 567 ->UnregisterKeyboardMicStream();
504 } 568 }
505 #endif 569 #endif
506 570
571 if (entry->input_debug_writer.get()) {
572 BrowserThread::PostTask(
573 BrowserThread::FILE,
574 FROM_HERE,
575 base::Bind(&DeleteInputDebugWriterOnFileThread,
576 base::Passed(entry->input_debug_writer.Pass())));
577 entry->input_debug_writer.reset();
tommi (sloooow) - chröme 2015/08/19 20:06:34 should be no need for this since you've already ca
Henrik Grunell 2015/08/20 07:26:18 Done.
578 }
579
507 // Delete the entry when this method goes out of scope. 580 // Delete the entry when this method goes out of scope.
508 scoped_ptr<AudioEntry> entry_deleter(entry); 581 scoped_ptr<AudioEntry> entry_deleter(entry);
509 582
510 // Erase the entry from the map. 583 // Erase the entry from the map.
511 audio_entries_.erase(entry->stream_id); 584 audio_entries_.erase(entry->stream_id);
512 } 585 }
513 586
514 void AudioInputRendererHost::DeleteEntryOnError(AudioEntry* entry, 587 void AudioInputRendererHost::DeleteEntryOnError(AudioEntry* entry,
515 ErrorCode error_code) { 588 ErrorCode error_code) {
516 DCHECK_CURRENTLY_ON(BrowserThread::IO); 589 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 const AudioInputHostMsg_CreateStream_Config& config) { 622 const AudioInputHostMsg_CreateStream_Config& config) {
550 #if defined(OS_CHROMEOS) 623 #if defined(OS_CHROMEOS)
551 if (config.params.channel_layout() == 624 if (config.params.channel_layout() ==
552 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) { 625 media::CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC) {
553 media_stream_manager_->audio_input_device_manager() 626 media_stream_manager_->audio_input_device_manager()
554 ->UnregisterKeyboardMicStream(); 627 ->UnregisterKeyboardMicStream();
555 } 628 }
556 #endif 629 #endif
557 } 630 }
558 631
632 void AudioInputRendererHost::MaybeEnableDebugRecordingForId(int stream_id) {
633 DCHECK_CURRENTLY_ON(BrowserThread::UI);
634 if (WebRTCInternals::GetInstance()->IsAudioDebugRecordingsEnabled()) {
635 BrowserThread::PostTask(
636 BrowserThread::IO,
637 FROM_HERE,
638 base::Bind(
639 &AudioInputRendererHost::EnabledDebugRecordingForId,
640 this,
641 GetDebugRecordingFilePathWithExtensions(
642 WebRTCInternals::GetInstance()->
643 GetAudioDebugRecordingsFilePath()),
644 stream_id));
645 }
646 }
647
648 #if defined(OS_WIN)
649 #define IntToStringType base::IntToString16
650 #else
651 #define IntToStringType base::IntToString
652 #endif
653
654 base::FilePath AudioInputRendererHost::GetDebugRecordingFilePathWithExtensions(
655 const base::FilePath& file) {
656 return file.AddExtension(kDebugRecordingFileNameAddition).AddExtension(
657 IntToStringType(base::GetProcId(render_process_host_->GetHandle())));
nasko 2015/08/19 22:04:15 This is not allowed. From what I can see in this f
Henrik Grunell 2015/08/20 07:26:18 As above comment, this is removed now.
658 }
659
660 void AudioInputRendererHost::EnabledDebugRecordingForId(
661 const base::FilePath& file,
662 int stream_id) {
663 DCHECK_CURRENTLY_ON(BrowserThread::IO);
664 BrowserThread::PostTaskAndReplyWithResult(
665 BrowserThread::FILE,
666 FROM_HERE,
667 base::Bind(
668 &CreateDebugRecordingFile,
669 file.AddExtension(IntToStringType(stream_id))
670 .AddExtension(
671 FILE_PATH_LITERAL(kDebugRecordingFileNameExtension))),
672 base::Bind(
673 &AudioInputRendererHost::DoEnableDebugRecording,
674 weak_factory_.GetWeakPtr(),
675 stream_id));
676 }
677
678 #undef IntToStringType
679
680 void AudioInputRendererHost::DoEnableDebugRecording(
681 int stream_id,
682 base::File file) {
683 DCHECK_CURRENTLY_ON(BrowserThread::IO);
684 if (!file.IsValid())
685 return;
686 AudioEntry* entry = LookupById(stream_id);
687 if (!entry) {
688 BrowserThread::PostTask(
689 BrowserThread::FILE,
690 FROM_HERE,
691 base::Bind(
692 &CloseFile,
693 Passed(file.Pass())));
694 return;
695 }
696 entry->input_debug_writer.reset(new AudioInputDebugWriter(file.Pass()));
697 entry->controller->EnableDebugRecording(entry->input_debug_writer.get());
698 }
699
700 void AudioInputRendererHost::DeleteDebugWriter(int stream_id) {
701 DCHECK_CURRENTLY_ON(BrowserThread::IO);
702 AudioEntry* entry = LookupById(stream_id);
703 if (!entry) {
704 // This happens if DisableDebugRecording is called right after
705 // DeleteEntries.
706 return;
707 }
708
709 if (entry->input_debug_writer.get()) {
710 BrowserThread::PostTask(
711 BrowserThread::FILE,
712 FROM_HERE,
713 base::Bind(&DeleteInputDebugWriterOnFileThread,
714 base::Passed(entry->input_debug_writer.Pass())));
715 entry->input_debug_writer.reset();
tommi (sloooow) - chröme 2015/08/19 20:06:34 ditto
Henrik Grunell 2015/08/20 07:26:18 Done.
716 }
717 }
718
559 } // namespace content 719 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698