diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-23 17:13:18 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-23 17:13:18 +0000 |
commit | 52e456b96b2b2c82a24a63a1d0cfc90904a5a72c (patch) | |
tree | 0886be2d3b349179899dd0d2cdff6a9066024f87 /content/browser | |
parent | c66e3a642f309df26af612e38320126a348a7fe8 (diff) | |
download | chromium_src-52e456b96b2b2c82a24a63a1d0cfc90904a5a72c.zip chromium_src-52e456b96b2b2c82a24a63a1d0cfc90904a5a72c.tar.gz chromium_src-52e456b96b2b2c82a24a63a1d0cfc90904a5a72c.tar.bz2 |
Remove GetAudioManager and GetMediaStreamManager from ResourceContext. The reason is the content module should create all the concrete objects that it knows about, instead of depending on every embedder to do so. ResourceContext is then just the interfaces that it implements and which it provides on the IO thread.
The AudioManager getter was a global, so I added a getter to the AudioManager class. MediaStreamManager now has its own static getter like other internal content classes on ResourceContext or BrowserContext.
BUG=98716
Review URL: https://chromiumcodereview.appspot.com/9433006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123264 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
27 files changed, 210 insertions, 172 deletions
diff --git a/content/browser/DEPS b/content/browser/DEPS index 5475da9..c120aee 100644 --- a/content/browser/DEPS +++ b/content/browser/DEPS @@ -2,6 +2,7 @@ include_rules = [ "+content/gpu", # For gpu_info_collector.h and in-process GPU "+content/port/browser", "+content/public/browser", + "+media/audio", # For audio input for speech input feature. # Other libraries. "+third_party/iaccessible2", diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc index 4096821..ca9f7ec 100644 --- a/content/browser/browser_main_loop.cc +++ b/content/browser/browser_main_loop.cc @@ -32,6 +32,7 @@ #include "content/public/common/main_function_params.h" #include "content/public/common/result_codes.h" #include "crypto/nss_util.h" +#include "media/audio/audio_manager.h" #include "net/base/network_change_notifier.h" #include "net/base/ssl_config_service.h" #include "net/socket/client_socket_factory.h" @@ -169,16 +170,15 @@ static void SetUpGLibLogHandler() { namespace content { // The currently-running BrowserMainLoop. There can be one or zero. -// This is stored to enable immediate shutdown when needed. -BrowserMainLoop* current_browser_main_loop = NULL; +BrowserMainLoop* g_current_browser_main_loop = NULL; // This is just to be able to keep ShutdownThreadsAndCleanUp out of // the public interface of BrowserMainLoop. class BrowserShutdownImpl { public: static void ImmediateShutdownAndExitProcess() { - DCHECK(current_browser_main_loop); - current_browser_main_loop->ShutdownThreadsAndCleanUp(); + DCHECK(g_current_browser_main_loop); + g_current_browser_main_loop->ShutdownThreadsAndCleanUp(); #if defined(OS_WIN) // At this point the message loop is still running yet we've shut everything @@ -196,22 +196,27 @@ void ImmediateShutdownAndExitProcess() { BrowserShutdownImpl::ImmediateShutdownAndExitProcess(); } +// static +AudioManager* BrowserMainLoop::GetAudioManager() { + return g_current_browser_main_loop->audio_manager_.get(); +} + // BrowserMainLoop construction / destructione ============================= BrowserMainLoop::BrowserMainLoop(const content::MainFunctionParams& parameters) : parameters_(parameters), parsed_command_line_(parameters.command_line), result_code_(content::RESULT_CODE_NORMAL_EXIT) { - DCHECK(!current_browser_main_loop); - current_browser_main_loop = this; + DCHECK(!g_current_browser_main_loop); + g_current_browser_main_loop = this; #if defined(OS_WIN) OleInitialize(NULL); #endif } BrowserMainLoop::~BrowserMainLoop() { - DCHECK_EQ(this, current_browser_main_loop); - current_browser_main_loop = NULL; + DCHECK_EQ(this, g_current_browser_main_loop); + g_current_browser_main_loop = NULL; #if defined(OS_WIN) OleUninitialize(); #endif @@ -316,6 +321,7 @@ void BrowserMainLoop::MainMessageLoopStart() { hi_res_timer_manager_.reset(new HighResolutionTimerManager); network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); + audio_manager_.reset(AudioManager::Create()); #if defined(OS_WIN) system_message_window_.reset(new SystemMessageWindowWin); diff --git a/content/browser/browser_main_loop.h b/content/browser/browser_main_loop.h index d6f351d..d6ba7c8 100644 --- a/content/browser/browser_main_loop.h +++ b/content/browser/browser_main_loop.h @@ -10,6 +10,7 @@ #include "base/memory/scoped_ptr.h" #include "content/browser/browser_process_sub_thread.h" +class AudioManager; class CommandLine; class HighResolutionTimerManager; class MessageLoop; @@ -34,6 +35,7 @@ class WebKitThread; // Implements the main browser loop stages called from |BrowserMain()|. // See comments in browser_main_parts.h for additional info. +// All functions are to be called only on the UI thread unless otherwise noted. class BrowserMainLoop { public: explicit BrowserMainLoop(const content::MainFunctionParams& parameters); @@ -57,6 +59,9 @@ class BrowserMainLoop { int GetResultCode() const { return result_code_; } + // Can be called on any thread. + static AudioManager* GetAudioManager(); + private: // For ShutdownThreadsAndCleanUp. friend class BrowserShutdownImpl; @@ -78,6 +83,7 @@ class BrowserMainLoop { scoped_ptr<base::SystemMonitor> system_monitor_; scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_; scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; + scoped_ptr<AudioManager> audio_manager_; #if defined(OS_WIN) scoped_ptr<SystemMessageWindowWin> system_message_window_; #endif diff --git a/content/browser/mock_resource_context.cc b/content/browser/mock_resource_context.cc index a7d85bd..df31042 100644 --- a/content/browser/mock_resource_context.cc +++ b/content/browser/mock_resource_context.cc @@ -10,16 +10,12 @@ namespace content { MockResourceContext::MockResourceContext() : test_request_context_(new TestURLRequestContext), - media_observer_(NULL), - media_stream_manager_(NULL), - audio_manager_(NULL) { + media_observer_(NULL) { } MockResourceContext::MockResourceContext(net::URLRequestContext* context) : test_request_context_(context), - media_observer_(NULL), - media_stream_manager_(NULL), - audio_manager_(NULL) { + media_observer_(NULL) { } MockResourceContext::~MockResourceContext() {} @@ -36,13 +32,4 @@ MediaObserver* MockResourceContext::GetMediaObserver() { return media_observer_; } -media_stream::MediaStreamManager* - MockResourceContext::GetMediaStreamManager() { - return media_stream_manager_; -} - -AudioManager* MockResourceContext::GetAudioManager() { - return audio_manager_; -} - } // namespace content diff --git a/content/browser/mock_resource_context.h b/content/browser/mock_resource_context.h index 1fd916e..417838c 100644 --- a/content/browser/mock_resource_context.h +++ b/content/browser/mock_resource_context.h @@ -26,23 +26,15 @@ class MockResourceContext : public ResourceContext { void set_media_observer(MediaObserver* observer) { media_observer_ = observer; } - void set_media_stream_manager(media_stream::MediaStreamManager* manager) { - media_stream_manager_ = manager; - } - void set_audio_manager(AudioManager* manager) { audio_manager_ = manager; } // ResourceContext implementation: virtual net::HostResolver* GetHostResolver() OVERRIDE; virtual net::URLRequestContext* GetRequestContext() OVERRIDE; virtual MediaObserver* GetMediaObserver() OVERRIDE; - virtual media_stream::MediaStreamManager* GetMediaStreamManager() OVERRIDE; - virtual AudioManager* GetAudioManager() OVERRIDE; private: scoped_refptr<net::URLRequestContext> test_request_context_; MediaObserver* media_observer_; - media_stream::MediaStreamManager* media_stream_manager_; - AudioManager* audio_manager_; }; } // namespace content diff --git a/content/browser/renderer_host/media/audio_input_renderer_host.cc b/content/browser/renderer_host/media/audio_input_renderer_host.cc index bc9a0fb..de3fe09 100644 --- a/content/browser/renderer_host/media/audio_input_renderer_host.cc +++ b/content/browser/renderer_host/media/audio_input_renderer_host.cc @@ -13,7 +13,6 @@ #include "content/browser/renderer_host/media/audio_input_sync_writer.h" #include "content/browser/renderer_host/media/media_stream_manager.h" #include "content/common/media/audio_messages.h" -#include "content/public/browser/resource_context.h" using content::BrowserMessageFilter; using content::BrowserThread; @@ -26,8 +25,10 @@ AudioInputRendererHost::AudioEntry::AudioEntry() AudioInputRendererHost::AudioEntry::~AudioEntry() {} AudioInputRendererHost::AudioInputRendererHost( - content::ResourceContext* resource_context) - : resource_context_(resource_context) { + content::ResourceContext* resource_context, + AudioManager* audio_manager) + : resource_context_(resource_context), + audio_manager_(audio_manager) { } AudioInputRendererHost::~AudioInputRendererHost() { @@ -187,7 +188,8 @@ void AudioInputRendererHost::OnStartDevice(int stream_id, int session_id) { // Get access to the AudioInputDeviceManager to start the device. media_stream::AudioInputDeviceManager* audio_input_man = - resource_context_->GetMediaStreamManager()->audio_input_device_manager(); + media_stream::MediaStreamManager::GetForResourceContext( + resource_context_, audio_manager_)->audio_input_device_manager(); // Add the session entry to the map. session_entries_[session_id] = stream_id; @@ -239,7 +241,7 @@ void AudioInputRendererHost::OnCreateStream(int stream_id, // latency path. See crbug.com/112472 for details. entry->writer.reset(writer.release()); entry->controller = media::AudioInputController::CreateLowLatency( - resource_context_->GetAudioManager(), + audio_manager_, this, audio_params, device_id, @@ -364,7 +366,8 @@ void AudioInputRendererHost::StopAndDeleteDevice(int session_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); media_stream::AudioInputDeviceManager* audio_input_man = - resource_context_->GetMediaStreamManager()->audio_input_device_manager(); + media_stream::MediaStreamManager::GetForResourceContext( + resource_context_, audio_manager_)->audio_input_device_manager(); audio_input_man->Stop(session_id); // Delete the session entry. diff --git a/content/browser/renderer_host/media/audio_input_renderer_host.h b/content/browser/renderer_host/media/audio_input_renderer_host.h index f6d1873..a764551 100644 --- a/content/browser/renderer_host/media/audio_input_renderer_host.h +++ b/content/browser/renderer_host/media/audio_input_renderer_host.h @@ -98,8 +98,8 @@ class CONTENT_EXPORT AudioInputRendererHost }; // Called from UI thread from the owner of this object. - explicit AudioInputRendererHost( - content::ResourceContext* resource_context); + AudioInputRendererHost(content::ResourceContext* resource_context, + AudioManager* audio_manager); // content::BrowserMessageFilter implementation. virtual void OnChannelClosing() OVERRIDE; @@ -202,6 +202,7 @@ class CONTENT_EXPORT AudioInputRendererHost // Used to get an instance of AudioInputDeviceManager. content::ResourceContext* resource_context_; + AudioManager* audio_manager_; // A map of stream IDs to audio sources. typedef std::map<int, AudioEntry*> AudioEntryMap; diff --git a/content/browser/renderer_host/media/audio_renderer_host.cc b/content/browser/renderer_host/media/audio_renderer_host.cc index d3dbb75..8595197 100644 --- a/content/browser/renderer_host/media/audio_renderer_host.cc +++ b/content/browser/renderer_host/media/audio_renderer_host.cc @@ -8,6 +8,7 @@ #include "base/metrics/histogram.h" #include "base/process.h" #include "base/shared_memory.h" +#include "content/browser/browser_main_loop.h" #include "content/browser/renderer_host/media/audio_common.h" #include "content/browser/renderer_host/media/audio_sync_reader.h" #include "content/browser/renderer_host/media/media_observer.h" @@ -28,8 +29,10 @@ AudioRendererHost::AudioEntry::~AudioEntry() {} /////////////////////////////////////////////////////////////////////////////// // AudioRendererHost implementations. AudioRendererHost::AudioRendererHost( - content::ResourceContext* resource_context) + content::ResourceContext* resource_context, + AudioManager* audio_manager) : resource_context_(resource_context), + audio_manager_(audio_manager), media_observer_(NULL) { } @@ -228,8 +231,7 @@ void AudioRendererHost::OnCreateStream( // entry and construct an AudioOutputController. entry->reader.reset(reader.release()); entry->controller = media::AudioOutputController::Create( - resource_context_->GetAudioManager(), this, audio_params, - entry->reader.get()); + audio_manager_, this, audio_params, entry->reader.get()); if (!entry->controller) { SendErrorMessage(stream_id); diff --git a/content/browser/renderer_host/media/audio_renderer_host.h b/content/browser/renderer_host/media/audio_renderer_host.h index d7248f8..5e9c88d 100644 --- a/content/browser/renderer_host/media/audio_renderer_host.h +++ b/content/browser/renderer_host/media/audio_renderer_host.h @@ -89,7 +89,8 @@ class CONTENT_EXPORT AudioRendererHost typedef std::map<int, AudioEntry*> AudioEntryMap; // Called from UI thread from the owner of this object. - AudioRendererHost(content::ResourceContext* resource_context); + AudioRendererHost(content::ResourceContext* resource_context, + AudioManager* audio_manager); // content::BrowserMessageFilter implementation. virtual void OnChannelClosing() OVERRIDE; @@ -187,6 +188,7 @@ class CONTENT_EXPORT AudioRendererHost AudioEntryMap audio_entries_; content::ResourceContext* resource_context_; + AudioManager* audio_manager_; MediaObserver* media_observer_; DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); diff --git a/content/browser/renderer_host/media/audio_renderer_host_unittest.cc b/content/browser/renderer_host/media/audio_renderer_host_unittest.cc index f3acdb9..4e09d65 100644 --- a/content/browser/renderer_host/media/audio_renderer_host_unittest.cc +++ b/content/browser/renderer_host/media/audio_renderer_host_unittest.cc @@ -43,8 +43,9 @@ static bool IsRunningHeadless() { class MockAudioRendererHost : public AudioRendererHost { public: explicit MockAudioRendererHost( - content::ResourceContext* resource_context) - : AudioRendererHost(resource_context), + content::ResourceContext* resource_context, + AudioManager* audio_manager) + : AudioRendererHost(resource_context, audio_manager), shared_memory_length_(0) { } @@ -161,8 +162,7 @@ class AudioRendererHostTest : public testing::Test { audio_manager_.reset(AudioManager::Create()); observer_.reset(new MockMediaObserver()); resource_context_.set_media_observer(observer_.get()); - resource_context_.set_audio_manager(audio_manager_.get()); - host_ = new MockAudioRendererHost(&resource_context_); + host_ = new MockAudioRendererHost(&resource_context_, audio_manager_.get()); // Simulate IPC channel connected. host_->OnChannelConnected(base::GetCurrentProcId()); diff --git a/content/browser/renderer_host/media/media_stream_dispatcher_host.cc b/content/browser/renderer_host/media/media_stream_dispatcher_host.cc index 0d3a7b2..f48f826 100644 --- a/content/browser/renderer_host/media/media_stream_dispatcher_host.cc +++ b/content/browser/renderer_host/media/media_stream_dispatcher_host.cc @@ -4,7 +4,6 @@ #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" -#include "content/public/browser/resource_context.h" #include "content/common/media/media_stream_messages.h" #include "content/common/media/media_stream_options.h" @@ -25,16 +24,20 @@ struct MediaStreamDispatcherHost::StreamRequest { }; MediaStreamDispatcherHost::MediaStreamDispatcherHost( - content::ResourceContext* resource_context, int render_process_id) + content::ResourceContext* resource_context, + int render_process_id, + AudioManager* audio_manager) : resource_context_(resource_context), - render_process_id_(render_process_id) { + render_process_id_(render_process_id), + audio_manager_(audio_manager) { } MediaStreamDispatcherHost::~MediaStreamDispatcherHost() { } MediaStreamManager* MediaStreamDispatcherHost::manager() { - return resource_context_->GetMediaStreamManager(); + return MediaStreamManager::GetForResourceContext( + resource_context_, audio_manager_); } bool MediaStreamDispatcherHost::OnMessageReceived( diff --git a/content/browser/renderer_host/media/media_stream_dispatcher_host.h b/content/browser/renderer_host/media/media_stream_dispatcher_host.h index 6fa68c6..aa44969 100644 --- a/content/browser/renderer_host/media/media_stream_dispatcher_host.h +++ b/content/browser/renderer_host/media/media_stream_dispatcher_host.h @@ -29,7 +29,8 @@ class CONTENT_EXPORT MediaStreamDispatcherHost public MediaStreamRequester { public: MediaStreamDispatcherHost(content::ResourceContext* resource_context, - int render_process_id); + int render_process_id, + AudioManager* audio_manager); virtual ~MediaStreamDispatcherHost(); // MediaStreamRequester implementation. @@ -79,6 +80,7 @@ class CONTENT_EXPORT MediaStreamDispatcherHost content::ResourceContext* resource_context_; int render_process_id_; + AudioManager* audio_manager_; struct StreamRequest; typedef std::map<std::string, StreamRequest> StreamMap; diff --git a/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc b/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc index 536578a..4cf1b2c 100644 --- a/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc +++ b/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc @@ -35,8 +35,9 @@ namespace media_stream { class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost { public: MockMediaStreamDispatcherHost(content::ResourceContext* resource_context, - MessageLoop* message_loop) - : MediaStreamDispatcherHost(resource_context, kProcessId), + MessageLoop* message_loop, + AudioManager* audio_manager) + : MediaStreamDispatcherHost(resource_context, kProcessId, audio_manager), message_loop_(message_loop) {} virtual ~MockMediaStreamDispatcherHost() {} @@ -153,15 +154,12 @@ class MediaStreamDispatcherHostTest : public testing::Test { audio_manager_.reset(AudioManager::Create()); - // Create a MediaStreamManager instance and hand over pointer to - // ResourceContext. - media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); // Make sure we use fake devices to avoid long delays. - media_stream_manager_->UseFakeDevice(); - resource_context_.set_media_stream_manager(media_stream_manager_.get()); + MediaStreamManager::GetForResourceContext( + &resource_context_, audio_manager_.get())->UseFakeDevice(); host_ = new MockMediaStreamDispatcherHost( - &resource_context_, message_loop_.get()); + &resource_context_, message_loop_.get(), audio_manager_.get()); } virtual void TearDown() { @@ -192,7 +190,9 @@ class MediaStreamDispatcherHostTest : public testing::Test { message_loop_->PostTask( FROM_HERE, base::Bind(&PostQuitOnVideoCaptureManagerThread, - message_loop_.get(), media_stream_manager_.get())); + message_loop_.get(), + MediaStreamManager::GetForResourceContext( + &resource_context_, audio_manager_.get()))); message_loop_->Run(); } @@ -200,7 +200,6 @@ class MediaStreamDispatcherHostTest : public testing::Test { scoped_ptr<MessageLoop> message_loop_; scoped_ptr<BrowserThreadImpl> ui_thread_; scoped_ptr<BrowserThreadImpl> io_thread_; - scoped_ptr<MediaStreamManager> media_stream_manager_; scoped_ptr<AudioManager> audio_manager_; content::MockResourceContext resource_context_; }; @@ -300,8 +299,9 @@ TEST_F(MediaStreamDispatcherHostTest, FailDevice) { EXPECT_CALL(*host_, OnVideoDeviceFailed(kRenderId, 0)); int session_id = host_->video_devices_[0].session_id; - resource_context_.GetMediaStreamManager()->video_capture_manager()->Error( - session_id); + MediaStreamManager::GetForResourceContext( + &resource_context_, audio_manager_.get())-> + video_capture_manager()->Error(session_id); WaitForResult(); EXPECT_EQ(host_->video_devices_.size(), 0u); EXPECT_EQ(host_->NumberOfStreams(), 1u); diff --git a/content/browser/renderer_host/media/media_stream_manager.cc b/content/browser/renderer_host/media/media_stream_manager.cc index bbad6c2..894426a 100644 --- a/content/browser/renderer_host/media/media_stream_manager.cc +++ b/content/browser/renderer_host/media/media_stream_manager.cc @@ -14,12 +14,15 @@ #include "content/browser/renderer_host/media/media_stream_device_settings.h" #include "content/browser/renderer_host/media/media_stream_requester.h" #include "content/browser/renderer_host/media/video_capture_manager.h" +#include "content/browser/resource_context_impl.h" #include "content/common/media/media_stream_options.h" #include "content/public/browser/browser_thread.h" #include "media/audio/audio_manager.h" using content::BrowserThread; +static const char* kMediaStreamManagerKeyName = "content_media_stream_manager"; + namespace media_stream { // Creates a random label used to identify requests. @@ -93,6 +96,19 @@ struct MediaStreamManager::DeviceRequest { StreamDeviceInfoArray video_devices; }; +// static +MediaStreamManager* MediaStreamManager::GetForResourceContext( + content::ResourceContext* resource_context, + AudioManager* audio_manager) { + MediaStreamManager* rv = static_cast<MediaStreamManager*>( + resource_context->GetUserData(kMediaStreamManagerKeyName)); + if (!rv) { + rv = new MediaStreamManager(audio_manager); + resource_context->SetUserData(kMediaStreamManagerKeyName, rv); + } + return rv; +} + MediaStreamManager::MediaStreamManager(AudioManager* audio_manager) : ALLOW_THIS_IN_INITIALIZER_LIST( device_settings_(new MediaStreamDeviceSettings(this))), diff --git a/content/browser/renderer_host/media/media_stream_manager.h b/content/browser/renderer_host/media/media_stream_manager.h index 032008b..48dce10 100644 --- a/content/browser/renderer_host/media/media_stream_manager.h +++ b/content/browser/renderer_host/media/media_stream_manager.h @@ -29,6 +29,7 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "base/memory/ref_counted.h" +#include "base/supports_user_data.h" #include "content/browser/renderer_host/media/media_stream_provider.h" #include "content/browser/renderer_host/media/media_stream_settings_requester.h" #include "content/common/media/media_stream_options.h" @@ -36,6 +37,10 @@ class AudioManager; +namespace content { +class ResourceContext; +} + namespace media_stream { class AudioInputDeviceManager; @@ -49,8 +54,15 @@ class VideoCaptureManager; // MediaStreamManager::Listener. class CONTENT_EXPORT MediaStreamManager : public MediaStreamProviderListener, - public SettingsRequester { + public SettingsRequester, + public base::SupportsUserData::Data { public: + // Returns the MediaStreamManager for the given ResourceContext. If it hasn't + // been created yet, it will be constructed with the given AudioManager. + static MediaStreamManager* GetForResourceContext( + content::ResourceContext* resource_context, + AudioManager* audio_manager); + explicit MediaStreamManager(AudioManager* audio_manager); virtual ~MediaStreamManager(); diff --git a/content/browser/renderer_host/media/video_capture_host.cc b/content/browser/renderer_host/media/video_capture_host.cc index 5653200..3efcd7c 100644 --- a/content/browser/renderer_host/media/video_capture_host.cc +++ b/content/browser/renderer_host/media/video_capture_host.cc @@ -10,7 +10,6 @@ #include "content/browser/renderer_host/media/media_stream_manager.h" #include "content/browser/renderer_host/media/video_capture_manager.h" #include "content/common/media/video_capture_messages.h" -#include "content/public/browser/resource_context.h" using content::BrowserMessageFilter; using content::BrowserThread; @@ -38,8 +37,10 @@ struct VideoCaptureHost::Entry { video_capture::State state; }; -VideoCaptureHost::VideoCaptureHost(content::ResourceContext* resource_context) - : resource_context_(resource_context) { +VideoCaptureHost::VideoCaptureHost(content::ResourceContext* resource_context, + AudioManager* audio_manager) + : resource_context_(resource_context), + audio_manager_(audio_manager) { } VideoCaptureHost::~VideoCaptureHost() {} @@ -285,5 +286,6 @@ void VideoCaptureHost::DoDeleteVideoCaptureController( media_stream::VideoCaptureManager* VideoCaptureHost::GetVideoCaptureManager() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - return resource_context_->GetMediaStreamManager()->video_capture_manager(); + return media_stream::MediaStreamManager::GetForResourceContext( + resource_context_, audio_manager_)->video_capture_manager(); } diff --git a/content/browser/renderer_host/media/video_capture_host.h b/content/browser/renderer_host/media/video_capture_host.h index 9ee7e1b..bea1657 100644 --- a/content/browser/renderer_host/media/video_capture_host.h +++ b/content/browser/renderer_host/media/video_capture_host.h @@ -45,6 +45,8 @@ #include "content/public/browser/browser_message_filter.h" #include "ipc/ipc_message.h" +class AudioManager; + namespace content { class ResourceContext; } // namespace content @@ -53,7 +55,8 @@ class CONTENT_EXPORT VideoCaptureHost : public content::BrowserMessageFilter, public VideoCaptureControllerEventHandler { public: - explicit VideoCaptureHost(content::ResourceContext* resource_context); + explicit VideoCaptureHost(content::ResourceContext* resource_context, + AudioManager* audio_manager); // content::BrowserMessageFilter implementation. virtual void OnChannelClosing() OVERRIDE; @@ -142,6 +145,7 @@ class CONTENT_EXPORT VideoCaptureHost // Used to get a pointer to VideoCaptureManager to start/stop capture devices. content::ResourceContext* resource_context_; + AudioManager* audio_manager_; DISALLOW_COPY_AND_ASSIGN(VideoCaptureHost); }; diff --git a/content/browser/renderer_host/media/video_capture_host_unittest.cc b/content/browser/renderer_host/media/video_capture_host_unittest.cc index 43b8799..5229ca2 100644 --- a/content/browser/renderer_host/media/video_capture_host_unittest.cc +++ b/content/browser/renderer_host/media/video_capture_host_unittest.cc @@ -31,7 +31,6 @@ using ::testing::InSequence; using ::testing::Mock; using ::testing::Return; using content::BrowserThread; - using content::BrowserThreadImpl; // Id used to identify the capture session between renderer and @@ -71,8 +70,9 @@ class DumpVideo { class MockVideoCaptureHost : public VideoCaptureHost { public: - explicit MockVideoCaptureHost(content::ResourceContext* resource_context) - : VideoCaptureHost(resource_context), + MockVideoCaptureHost(content::ResourceContext* resource_context, + AudioManager* audio_manager) + : VideoCaptureHost(resource_context, audio_manager), return_buffers_(false), dump_video_(false) {} virtual ~MockVideoCaptureHost() { @@ -206,17 +206,12 @@ class VideoCaptureHostTest : public testing::Test { audio_manager_.reset(AudioManager::Create()); - // Create a MediaStreamManager instance and hand over pointer to - // ResourceContext. - media_stream_manager_.reset(new media_stream::MediaStreamManager( - audio_manager_.get())); - #ifndef TEST_REAL_CAPTURE_DEVICE - media_stream_manager_->UseFakeDevice(); + media_stream::MediaStreamManager::GetForResourceContext( + &resource_context_, audio_manager_.get())->UseFakeDevice(); #endif - resource_context_.set_media_stream_manager(media_stream_manager_.get()); - host_ = new MockVideoCaptureHost(&resource_context_); + host_ = new MockVideoCaptureHost(&resource_context_, audio_manager_.get()); // Simulate IPC channel connected. host_->OnChannelConnected(base::GetCurrentProcId()); @@ -249,11 +244,13 @@ class VideoCaptureHostTest : public testing::Test { // Called on the main thread. static void PostQuitOnVideoCaptureManagerThread( - MessageLoop* message_loop, content::ResourceContext* resource_context) { - resource_context->GetMediaStreamManager()->video_capture_manager()-> - GetMessageLoop()->PostTask(FROM_HERE, - base::Bind(&PostQuitMessageLoop, - message_loop)); + MessageLoop* message_loop, content::ResourceContext* resource_context, + AudioManager* audio_manager) { + media_stream::MediaStreamManager* manager = + media_stream::MediaStreamManager::GetForResourceContext( + resource_context, audio_manager); + manager->video_capture_manager()->GetMessageLoop()->PostTask( + FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); } // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the @@ -264,8 +261,8 @@ class VideoCaptureHostTest : public testing::Test { void SyncWithVideoCaptureManagerThread() { message_loop_->PostTask( FROM_HERE, - base::Bind(&PostQuitOnVideoCaptureManagerThread, - message_loop_.get(), &resource_context_)); + base::Bind(&PostQuitOnVideoCaptureManagerThread, message_loop_.get(), + &resource_context_, audio_manager_.get())); message_loop_->Run(); } @@ -364,7 +361,6 @@ class VideoCaptureHostTest : public testing::Test { scoped_ptr<MessageLoop> message_loop_; scoped_ptr<BrowserThreadImpl> ui_thread_; scoped_ptr<BrowserThreadImpl> io_thread_; - scoped_ptr<media_stream::MediaStreamManager> media_stream_manager_; scoped_ptr<AudioManager> audio_manager_; content::MockResourceContext resource_context_; diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc index d78bf2e..38d7202 100644 --- a/content/browser/renderer_host/render_process_host_impl.cc +++ b/content/browser/renderer_host/render_process_host_impl.cc @@ -41,6 +41,7 @@ #include "content/browser/appcache/appcache_dispatcher_host.h" #include "content/browser/appcache/chrome_appcache_service.h" #include "content/browser/browser_main.h" +#include "content/browser/browser_main_loop.h" #include "content/browser/child_process_security_policy_impl.h" #include "content/browser/chrome_blob_storage_context.h" #include "content/browser/device_orientation/message_filter.h" @@ -456,15 +457,17 @@ void RenderProcessHostImpl::CreateMessageFilters() { content::BrowserContext* browser_context = GetBrowserContext(); content::ResourceContext* resource_context = browser_context->GetResourceContext(); + AudioManager* audio_manager = content::BrowserMainLoop::GetAudioManager(); ResourceMessageFilter* resource_message_filter = new ResourceMessageFilter( GetID(), content::PROCESS_TYPE_RENDERER, resource_context, new RendererURLRequestContextSelector(browser_context, GetID())); channel_->AddFilter(resource_message_filter); - channel_->AddFilter(new AudioInputRendererHost(resource_context)); - channel_->AddFilter(new AudioRendererHost(resource_context)); - channel_->AddFilter(new VideoCaptureHost(resource_context)); + channel_->AddFilter(new AudioInputRendererHost( + resource_context, audio_manager)); + channel_->AddFilter(new AudioRendererHost(resource_context, audio_manager)); + channel_->AddFilter(new VideoCaptureHost(resource_context, audio_manager)); channel_->AddFilter(new AppCacheDispatcherHost( static_cast<ChromeAppCacheService*>( BrowserContext::GetAppCacheService(browser_context)), @@ -478,14 +481,14 @@ void RenderProcessHostImpl::CreateMessageFilters() { GetID(), browser_context->GetGeolocationPermissionContext())); channel_->AddFilter(new GpuMessageFilter(GetID(), widget_helper_.get())); channel_->AddFilter(new media_stream::MediaStreamDispatcherHost( - resource_context, GetID())); + resource_context, GetID(), audio_manager)); channel_->AddFilter(new PepperFileMessageFilter(GetID(), browser_context)); channel_->AddFilter(new PepperMessageFilter(PepperMessageFilter::RENDERER, GetID(), resource_context)); #if defined(ENABLE_INPUT_SPEECH) channel_->AddFilter(new speech_input::SpeechInputDispatcherHost( GetID(), browser_context->GetRequestContext(), - browser_context->GetSpeechInputPreferences(), resource_context)); + browser_context->GetSpeechInputPreferences(), audio_manager)); #endif channel_->AddFilter(new FileSystemDispatcherHost( browser_context->GetRequestContext(), diff --git a/content/browser/speech/speech_input_browsertest.cc b/content/browser/speech/speech_input_browsertest.cc index 872d2cd..f87207e 100644 --- a/content/browser/speech/speech_input_browsertest.cc +++ b/content/browser/speech/speech_input_browsertest.cc @@ -72,8 +72,7 @@ class FakeSpeechInputManager : public SpeechInputManager { const std::string& grammar, const std::string& origin_url, net::URLRequestContextGetter* context_getter, - content::SpeechInputPreferences* speech_input_prefs, - AudioManager* audio_manager) OVERRIDE { + content::SpeechInputPreferences* speech_input_prefs) OVERRIDE { VLOG(1) << "StartRecognition invoked."; EXPECT_EQ(0, caller_id_); EXPECT_EQ(NULL, delegate_); @@ -115,20 +114,20 @@ class FakeSpeechInputManager : public SpeechInputManager { } protected: - virtual void GetRequestInfo(AudioManager* audio_manager, - bool* can_report_metrics, - std::string* request_info) OVERRIDE {} - virtual void ShowRecognitionRequested(int caller_id, int render_process_id, - int render_view_id, const gfx::Rect& element_rect) OVERRIDE {} + virtual void GetRequestInfo(bool* can_report_metrics, + std::string* request_info) OVERRIDE {} + virtual void ShowRecognitionRequested( + int caller_id, int render_process_id, int render_view_id, + const gfx::Rect& element_rect) OVERRIDE {} virtual void ShowWarmUp(int caller_id) OVERRIDE {} virtual void ShowRecognizing(int caller_id) OVERRIDE {} virtual void ShowRecording(int caller_id) OVERRIDE {} - virtual void ShowInputVolume(int caller_id, float volume, - float noise_volume) OVERRIDE {} - virtual void ShowMicError(int caller_id, - SpeechInputManager::MicError error) OVERRIDE {} - virtual void ShowRecognizerError(int caller_id, - content::SpeechInputError error) OVERRIDE {} + virtual void ShowInputVolume( + int caller_id, float volume, float noise_volume) OVERRIDE {} + virtual void ShowMicError( + int caller_id, SpeechInputManager::MicError error) OVERRIDE {} + virtual void ShowRecognizerError( + int caller_id, content::SpeechInputError error) OVERRIDE {} virtual void DoClose(int caller_id) OVERRIDE {} private: diff --git a/content/browser/speech/speech_input_dispatcher_host.cc b/content/browser/speech/speech_input_dispatcher_host.cc index 3511123..1660f46 100644 --- a/content/browser/speech/speech_input_dispatcher_host.cc +++ b/content/browser/speech/speech_input_dispatcher_host.cc @@ -9,7 +9,6 @@ #include "content/browser/speech/speech_recognizer.h" #include "content/common/speech_input_messages.h" #include "content/public/browser/content_browser_client.h" -#include "content/public/browser/resource_context.h" #include "content/public/browser/speech_input_preferences.h" using content::BrowserThread; @@ -118,12 +117,12 @@ SpeechInputDispatcherHost::SpeechInputDispatcherHost( int render_process_id, net::URLRequestContextGetter* context_getter, content::SpeechInputPreferences* speech_input_preferences, - content::ResourceContext* resource_context) + AudioManager* audio_manager) : render_process_id_(render_process_id), may_have_pending_requests_(false), context_getter_(context_getter), speech_input_preferences_(speech_input_preferences), - resource_context_(resource_context) { + audio_manager_(audio_manager) { // This is initialized by Browser. Do not add any non-trivial // initialization here, instead do it lazily when required (e.g. see the // method |manager()|) or add an Init() method. @@ -175,8 +174,7 @@ void SpeechInputDispatcherHost::OnStartRecognition( params.language, params.grammar, params.origin_url, context_getter_.get(), - speech_input_preferences_.get(), - resource_context_->GetAudioManager()); + speech_input_preferences_.get()); } void SpeechInputDispatcherHost::OnCancelRecognition(int render_view_id, diff --git a/content/browser/speech/speech_input_dispatcher_host.h b/content/browser/speech/speech_input_dispatcher_host.h index a955c83..cb9284d 100644 --- a/content/browser/speech/speech_input_dispatcher_host.h +++ b/content/browser/speech/speech_input_dispatcher_host.h @@ -11,10 +11,10 @@ #include "content/public/browser/browser_message_filter.h" #include "net/url_request/url_request_context_getter.h" +class AudioManager; struct SpeechInputHostMsg_StartRecognition_Params; namespace content { -class ResourceContext; class SpeechInputPreferences; } @@ -34,7 +34,7 @@ class SpeechInputDispatcherHost : public content::BrowserMessageFilter, int render_process_id, net::URLRequestContextGetter* context_getter, content::SpeechInputPreferences* speech_input_preferences, - content::ResourceContext* resource_context); + AudioManager* audio_manager); // SpeechInputManager::Delegate methods. virtual void SetRecognitionResult( @@ -67,7 +67,7 @@ class SpeechInputDispatcherHost : public content::BrowserMessageFilter, scoped_refptr<net::URLRequestContextGetter> context_getter_; scoped_refptr<content::SpeechInputPreferences> speech_input_preferences_; - content::ResourceContext* resource_context_; + AudioManager* audio_manager_; static SpeechInputManager* manager_; diff --git a/content/browser/speech/speech_input_manager.cc b/content/browser/speech/speech_input_manager.cc index b8e8f98..4b7b9fb 100644 --- a/content/browser/speech/speech_input_manager.cc +++ b/content/browser/speech/speech_input_manager.cc @@ -5,6 +5,7 @@ #include "content/browser/speech/speech_input_manager.h" #include "base/bind.h" +#include "content/browser/browser_main_loop.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/speech/speech_input_manager_delegate.h" #include "content/browser/speech/speech_recognizer.h" @@ -13,8 +14,8 @@ #include "content/public/browser/resource_context.h" #include "content/public/browser/speech_input_preferences.h" #include "content/public/common/view_type.h" -#include "media/audio/audio_manager.h" +using content::BrowserMainLoop; using content::BrowserThread; using content::SpeechInputManagerDelegate; @@ -30,8 +31,7 @@ struct SpeechInputManager::SpeechInputParams { const std::string& grammar, const std::string& origin_url, net::URLRequestContextGetter* context_getter, - content::SpeechInputPreferences* speech_input_prefs, - AudioManager* audio_manager) + content::SpeechInputPreferences* speech_input_prefs) : delegate(delegate), caller_id(caller_id), render_process_id(render_process_id), @@ -41,8 +41,7 @@ struct SpeechInputManager::SpeechInputParams { grammar(grammar), origin_url(origin_url), context_getter(context_getter), - speech_input_prefs(speech_input_prefs), - audio_manager_(audio_manager) { + speech_input_prefs(speech_input_prefs) { } SpeechInputManagerDelegate* delegate; @@ -55,7 +54,6 @@ struct SpeechInputManager::SpeechInputParams { std::string origin_url; net::URLRequestContextGetter* context_getter; content::SpeechInputPreferences* speech_input_prefs; - AudioManager* audio_manager_; }; SpeechInputManager::SpeechInputManager() @@ -68,6 +66,18 @@ SpeechInputManager::~SpeechInputManager() { CancelRecognition(requests_.begin()->first); } +bool SpeechInputManager::HasAudioInputDevices() { + return BrowserMainLoop::GetAudioManager()->HasAudioInputDevices(); +} + +bool SpeechInputManager::IsRecordingInProcess() { + return BrowserMainLoop::GetAudioManager()->IsRecordingInProcess(); +} + +string16 SpeechInputManager::GetAudioInputDeviceModel() { + return BrowserMainLoop::GetAudioManager()->GetAudioInputDeviceModel(); +} + bool SpeechInputManager::HasPendingRequest(int caller_id) const { return requests_.find(caller_id) != requests_.end(); } @@ -78,35 +88,22 @@ SpeechInputManagerDelegate* SpeechInputManager::GetDelegate( } // static -void SpeechInputManager::ShowAudioInputSettings(AudioManager* audio_manager) { +void SpeechInputManager::ShowAudioInputSettings() { // Since AudioManager::ShowAudioInputSettings can potentially launch external // processes, do that in the FILE thread to not block the calling threads. if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { BrowserThread::PostTask( BrowserThread::FILE, FROM_HERE, - base::Bind(&SpeechInputManager::ShowAudioInputSettings, - base::Unretained(audio_manager))); + base::Bind(&SpeechInputManager::ShowAudioInputSettings)); return; } + AudioManager* audio_manager = BrowserMainLoop::GetAudioManager(); DCHECK(audio_manager->CanShowAudioInputSettings()); if (audio_manager->CanShowAudioInputSettings()) audio_manager->ShowAudioInputSettings(); } -// static -void SpeechInputManager::ShowAudioInputSettingsFromUI( - content::ResourceContext* resource_context) { - if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::Bind(&SpeechInputManager::ShowAudioInputSettingsFromUI, - base::Unretained(resource_context))); - return; - } - ShowAudioInputSettings(resource_context->GetAudioManager()); -} - void SpeechInputManager::StartRecognition( SpeechInputManagerDelegate* delegate, int caller_id, @@ -117,15 +114,17 @@ void SpeechInputManager::StartRecognition( const std::string& grammar, const std::string& origin_url, net::URLRequestContextGetter* context_getter, - content::SpeechInputPreferences* speech_input_prefs, - AudioManager* audio_manager) { + content::SpeechInputPreferences* speech_input_prefs) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, - base::Bind(&SpeechInputManager::CheckRenderViewTypeAndStartRecognition, - base::Unretained(this), SpeechInputParams(delegate, caller_id, - render_process_id, render_view_id, element_rect, language, grammar, - origin_url, context_getter, speech_input_prefs, audio_manager))); + base::Bind( + &SpeechInputManager::CheckRenderViewTypeAndStartRecognition, + base::Unretained(this), + SpeechInputParams( + delegate, caller_id, render_process_id, render_view_id, + element_rect, language, grammar, origin_url, context_getter, + speech_input_prefs))); } void SpeechInputManager::CheckRenderViewTypeAndStartRecognition( @@ -160,15 +159,13 @@ void SpeechInputManager::ProceedStartingRecognition( ShowRecognitionRequested( params.caller_id, params.render_process_id, params.render_view_id, params.element_rect); - GetRequestInfo(params.audio_manager_, &can_report_metrics_, - &request_info_); + GetRequestInfo(&can_report_metrics_, &request_info_); SpeechInputRequest* request = &requests_[params.caller_id]; request->delegate = params.delegate; request->recognizer = new SpeechRecognizer( this, params.caller_id, params.language, params.grammar, - params.context_getter, params.audio_manager_, - params.speech_input_prefs->FilterProfanities(), + params.context_getter, params.speech_input_prefs->FilterProfanities(), request_info_, can_report_metrics_ ? params.origin_url : ""); request->is_active = false; @@ -186,10 +183,9 @@ void SpeechInputManager::StartRecognitionForRequest(int caller_id) { if (recording_caller_id_) CancelRecognitionAndInformDelegate(recording_caller_id_); - AudioManager* audio_man = request->second.recognizer->audio_manager(); - if (!audio_man->HasAudioInputDevices()) { + if (!HasAudioInputDevices()) { ShowMicError(caller_id, kNoDeviceAvailable); - } else if (audio_man->IsRecordingInProcess()) { + } else if (IsRecordingInProcess()) { ShowMicError(caller_id, kDeviceInUse); } else { recording_caller_id_ = caller_id; diff --git a/content/browser/speech/speech_input_manager.h b/content/browser/speech/speech_input_manager.h index b5976f3..186b969 100644 --- a/content/browser/speech/speech_input_manager.h +++ b/content/browser/speech/speech_input_manager.h @@ -49,16 +49,15 @@ class CONTENT_EXPORT SpeechInputManager // Invokes the platform provided microphone settings UI in a non-blocking way, // via the BrowserThread::FILE thread. - static void ShowAudioInputSettings(AudioManager* audio_manager); - - // Same as ShowAudioInputSettings above but can be called from the UI thread - // where the caller has a pointer to a resource context, but due to not - // running on the IO thread, cannot access its properties. - static void ShowAudioInputSettingsFromUI( - content::ResourceContext* resource_context); + static void ShowAudioInputSettings(); virtual ~SpeechInputManager(); + // These are wrappers around AudioManager. + bool HasAudioInputDevices(); + bool IsRecordingInProcess(); + string16 GetAudioInputDeviceModel(); + // Handlers for requests from render views. // |delegate| is a weak pointer and should remain valid until @@ -77,8 +76,7 @@ class CONTENT_EXPORT SpeechInputManager const std::string& grammar, const std::string& origin_url, net::URLRequestContextGetter* context_getter, - content::SpeechInputPreferences* speech_input_prefs, - AudioManager* audio_manager); + content::SpeechInputPreferences* speech_input_prefs); virtual void CancelRecognition(int caller_id); virtual void CancelAllRequestsWithDelegate( content::SpeechInputManagerDelegate* delegate); @@ -105,8 +103,7 @@ class CONTENT_EXPORT SpeechInputManager // recognition and for fetching optional request information. // Get the optional request information if available. - virtual void GetRequestInfo(AudioManager* audio_manager, - bool* can_report_metrics, + virtual void GetRequestInfo(bool* can_report_metrics, std::string* request_info) = 0; // Called when recognition has been requested from point |element_rect_| on diff --git a/content/browser/speech/speech_recognizer.cc b/content/browser/speech/speech_recognizer.cc index 550aea9..b3f1716 100644 --- a/content/browser/speech/speech_recognizer.cc +++ b/content/browser/speech/speech_recognizer.cc @@ -6,11 +6,13 @@ #include "base/bind.h" #include "base/time.h" +#include "content/browser/browser_main_loop.h" #include "content/public/browser/speech_recognizer_delegate.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/speech_input_result.h" #include "net/url_request/url_request_context_getter.h" +using content::BrowserMainLoop; using content::BrowserThread; using media::AudioInputController; using std::string; @@ -62,7 +64,6 @@ SpeechRecognizer::SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, const std::string& language, const std::string& grammar, net::URLRequestContextGetter* context_getter, - AudioManager* audio_manager, bool filter_profanities, const std::string& hardware_info, const std::string& origin_url) @@ -74,12 +75,12 @@ SpeechRecognizer::SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, hardware_info_(hardware_info), origin_url_(origin_url), context_getter_(context_getter), - audio_manager_(audio_manager), codec_(AudioEncoder::CODEC_FLAC), encoder_(NULL), endpointer_(kAudioSampleRate), num_samples_recorded_(0), - audio_level_(0.0f) { + audio_level_(0.0f), + audio_manager_(NULL) { endpointer_.set_speech_input_complete_silence_length( base::Time::kMicrosecondsPerSecond / 2); endpointer_.set_long_speech_input_complete_silence_length( @@ -114,8 +115,9 @@ bool SpeechRecognizer::StartRecording() { AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kAudioSampleRate, kNumBitsPerAudioSample, samples_per_packet); - audio_controller_ = AudioInputController::Create(audio_manager_, this, - params); + audio_controller_ = AudioInputController::Create( + audio_manager_ ? audio_manager_ : BrowserMainLoop::GetAudioManager(), + this, params); DCHECK(audio_controller_.get()); VLOG(1) << "SpeechRecognizer starting record."; num_samples_recorded_ = 0; @@ -319,4 +321,8 @@ void SpeechRecognizer::CloseAudioControllerSynchronously() { audio_controller_ = NULL; // Releases the ref ptr. } +void SpeechRecognizer::SetAudioManagerForTesting(AudioManager* audio_manager) { + audio_manager_ = audio_manager; +} + } // namespace speech_input diff --git a/content/browser/speech/speech_recognizer.h b/content/browser/speech/speech_recognizer.h index 67b1732..23c2814 100644 --- a/content/browser/speech/speech_recognizer.h +++ b/content/browser/speech/speech_recognizer.h @@ -18,6 +18,8 @@ #include "content/public/common/speech_input_result.h" #include "media/audio/audio_input_controller.h" +class AudioManager; + namespace content { class SpeechRecognizerDelegate; } @@ -40,7 +42,6 @@ class CONTENT_EXPORT SpeechRecognizer const std::string& language, const std::string& grammar, net::URLRequestContextGetter* context_getter, - AudioManager* audio_manager, bool filter_profanities, const std::string& hardware_info, const std::string& origin_url); @@ -74,8 +75,6 @@ class CONTENT_EXPORT SpeechRecognizer virtual void SetRecognitionResult( const content::SpeechInputResult& result) OVERRIDE; - AudioManager* audio_manager() const { return audio_manager_; } - static const int kAudioSampleRate; static const int kAudioPacketIntervalMs; // Duration of each audio packet. static const ChannelLayout kChannelLayout; @@ -84,6 +83,8 @@ class CONTENT_EXPORT SpeechRecognizer static const int kEndpointerEstimationTimeMs; private: + friend class SpeechRecognizerTest; + void InformErrorAndCancelRecognition(content::SpeechInputError error); void SendRecordedAudioToServer(); @@ -95,6 +96,8 @@ class CONTENT_EXPORT SpeechRecognizer // Helper method which closes the audio controller and blocks until done. void CloseAudioControllerSynchronously(); + void SetAudioManagerForTesting(AudioManager* audio_manager); + content::SpeechRecognizerDelegate* delegate_; int caller_id_; std::string language_; @@ -106,12 +109,12 @@ class CONTENT_EXPORT SpeechRecognizer scoped_ptr<SpeechRecognitionRequest> request_; scoped_refptr<media::AudioInputController> audio_controller_; scoped_refptr<net::URLRequestContextGetter> context_getter_; - AudioManager* audio_manager_; AudioEncoder::Codec codec_; scoped_ptr<AudioEncoder> encoder_; Endpointer endpointer_; int num_samples_recorded_; float audio_level_; + AudioManager* audio_manager_; DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); }; diff --git a/content/browser/speech/speech_recognizer_unittest.cc b/content/browser/speech/speech_recognizer_unittest.cc index 17a18c9..948d397 100644 --- a/content/browser/speech/speech_recognizer_unittest.cc +++ b/content/browser/speech/speech_recognizer_unittest.cc @@ -35,8 +35,9 @@ class SpeechRecognizerTest : public content::SpeechRecognizerDelegate, error_(content::SPEECH_INPUT_ERROR_NONE), volume_(-1.0f) { recognizer_ = new SpeechRecognizer(this, 1, std::string(), std::string(), - NULL, audio_manager_.get(), false, - std::string(), std::string()); + NULL, false, std::string(), + std::string()); + recognizer_->SetAudioManagerForTesting(audio_manager_.get()); int audio_packet_length_bytes = (SpeechRecognizer::kAudioSampleRate * SpeechRecognizer::kAudioPacketIntervalMs * |