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

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

Powered by Google App Engine
This is Rietveld 408576698