summaryrefslogtreecommitdiffstats
path: root/content/browser/speech/speech_recognition_manager_impl.cc
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-16 17:21:12 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-16 17:21:12 +0000
commit82342d9ca8a73ad35270a4581658c7dcbf5c678f (patch)
tree53e1d48a96f28cee3bce347ae2f3de796661f775 /content/browser/speech/speech_recognition_manager_impl.cc
parenta25437d93f3c3524bb02540a955223d815b35ca4 (diff)
downloadchromium_src-82342d9ca8a73ad35270a4581658c7dcbf5c678f.zip
chromium_src-82342d9ca8a73ad35270a4581658c7dcbf5c678f.tar.gz
chromium_src-82342d9ca8a73ad35270a4581658c7dcbf5c678f.tar.bz2
Replace MediaStreamUIController with MediaStreamUIProxy.
Previously a single object MediaStreamUIController was used to control UI for all streams. Replaced it with a per-stream MediaStreamUIProxy that simplifies code in many places. Also moved media request queueing logic from content layer to chrome. Now different types of requests may be queued differently (e.g. there is no reason to block screen capture requests on webcam infobar). This change was previously landed in 197222 and 197755 and reverted in 197242 and 197768. Each time it caused test failures on some Mac bots. The failure seems to be caused by bug 236846 which has been fixed. TBR=tommi@chromium.org Review URL: https://codereview.chromium.org/14750011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200559 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/speech/speech_recognition_manager_impl.cc')
-rw-r--r--content/browser/speech/speech_recognition_manager_impl.cc165
1 files changed, 78 insertions, 87 deletions
diff --git a/content/browser/speech/speech_recognition_manager_impl.cc b/content/browser/speech/speech_recognition_manager_impl.cc
index ea49579..402dd00 100644
--- a/content/browser/speech/speech_recognition_manager_impl.cc
+++ b/content/browser/speech/speech_recognition_manager_impl.cc
@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "content/browser/browser_main_loop.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
+#include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
#include "content/browser/speech/google_one_shot_remote_engine.h"
#include "content/browser/speech/google_streaming_remote_engine.h"
#include "content/browser/speech/speech_recognition_engine.h"
@@ -68,7 +69,14 @@ SpeechRecognitionManagerImpl::SpeechRecognitionManagerImpl()
SpeechRecognitionManagerImpl::~SpeechRecognitionManagerImpl() {
DCHECK(g_speech_recognition_manager_impl);
g_speech_recognition_manager_impl = NULL;
- // Recognition sessions will be aborted by the corresponding destructors.
+
+ for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
+ ++it) {
+ // MediaStreamUIProxy must be deleted on the IO thread.
+ BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
+ it->second->ui.release());
+ delete it->second;
+ }
sessions_.clear();
}
@@ -79,10 +87,11 @@ int SpeechRecognitionManagerImpl::CreateSession(
const int session_id = GetNextSessionID();
DCHECK(!SessionExists(session_id));
// Set-up the new session.
- Session& session = sessions_[session_id];
- session.id = session_id;
- session.config = config;
- session.context = config.initial_context;
+ Session* session = new Session();
+ sessions_[session_id] = session;
+ session->id = session_id;
+ session->config = config;
+ session->context = config.initial_context;
std::string hardware_info;
bool can_report_metrics = false;
@@ -117,7 +126,7 @@ int SpeechRecognitionManagerImpl::CreateSession(
// The legacy api cannot use continuous mode.
DCHECK(!config.is_legacy_api || !config.continuous);
- session.recognizer = new SpeechRecognizer(
+ session->recognizer = new SpeechRecognizer(
this,
session_id,
!config.continuous,
@@ -162,7 +171,7 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
if (ask_user) {
SessionsTable::iterator iter = sessions_.find(session_id);
DCHECK(iter != sessions_.end());
- SpeechRecognitionSessionContext& context = iter->second.context;
+ SpeechRecognitionSessionContext& context = iter->second->context;
context.label =
BrowserMainLoop::GetMediaStreamManager()->MakeMediaAccessRequest(
context.render_process_id,
@@ -171,8 +180,7 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
GURL(context.context_name),
base::Bind(
&SpeechRecognitionManagerImpl::MediaRequestPermissionCallback,
- weak_factory_.GetWeakPtr()));
-
+ weak_factory_.GetWeakPtr(), session_id));
return;
}
#endif // defined(OS_IOS)
@@ -197,26 +205,29 @@ void SpeechRecognitionManagerImpl::RecognitionAllowedCallback(int session_id,
}
void SpeechRecognitionManagerImpl::MediaRequestPermissionCallback(
- const std::string& label, const MediaStreamDevices& devices) {
+ int session_id,
+ const MediaStreamDevices& devices,
+ scoped_ptr<MediaStreamUIProxy> stream_ui) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- for (SessionsTable::iterator iter = sessions_.begin();
- iter != sessions_.end(); ++iter) {
- if (iter->second.context.label == label) {
- bool is_allowed = false;
- if (!devices.empty()) {
- // Copy the approved devices array to the context for UI indication.
- iter->second.context.devices = devices;
- is_allowed = true;
- }
- // Clear the label to indicate the request has been done.
- iter->second.context.label.clear();
+ SessionsTable::iterator iter = sessions_.find(session_id);
+ if (iter == sessions_.end())
+ return;
- // Notify the recognition about the request result.
- RecognitionAllowedCallback(iter->first, false, is_allowed);
- break;
- }
+ bool is_allowed = !devices.empty();
+ if (is_allowed) {
+ // Copy the approved devices array to the context for UI indication.
+ iter->second->context.devices = devices;
+
+ // Save the UI object.
+ iter->second->ui = stream_ui.Pass();
}
+
+ // Clear the label to indicate the request has been done.
+ iter->second->context.label.clear();
+
+ // Notify the recognition about the request result.
+ RecognitionAllowedCallback(iter->first, false, is_allowed);
}
void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
@@ -224,12 +235,8 @@ void SpeechRecognitionManagerImpl::AbortSession(int session_id) {
if (!SessionExists(session_id))
return;
-#if !defined(OS_IOS)
- const SpeechRecognitionSessionContext& context =
- GetSessionContext(session_id);
- if (!context.label.empty())
- BrowserMainLoop::GetMediaStreamManager()->CancelRequest(context.label);
-#endif // !defined(OS_IOS)
+ SessionsTable::iterator iter = sessions_.find(session_id);
+ iter->second->ui.reset();
base::MessageLoop::current()->PostTask(
FROM_HERE,
@@ -244,12 +251,8 @@ void SpeechRecognitionManagerImpl::StopAudioCaptureForSession(int session_id) {
if (!SessionExists(session_id))
return;
-#if !defined(OS_IOS)
- const SpeechRecognitionSessionContext& context =
- GetSessionContext(session_id);
- if (!context.label.empty())
- BrowserMainLoop::GetMediaStreamManager()->CancelRequest(context.label);
-#endif // !defined(OS_IOS)
+ SessionsTable::iterator iter = sessions_.find(session_id);
+ iter->second->ui.reset();
base::MessageLoop::current()->PostTask(
FROM_HERE,
@@ -269,15 +272,11 @@ void SpeechRecognitionManagerImpl::OnRecognitionStart(int session_id) {
if (!SessionExists(session_id))
return;
-#if !defined(OS_IOS)
- const SpeechRecognitionSessionContext& context =
- GetSessionContext(session_id);
- if (!context.devices.empty()) {
- // Notify the UI the devices are being used.
- BrowserMainLoop::GetMediaStreamManager()->NotifyUIDevicesOpened(
- context.label);
+ SessionsTable::iterator iter = sessions_.find(session_id);
+ if (iter->second->ui) {
+ // Notify the UI that the devices are being used.
+ iter->second->ui->OnStarted(base::Closure());
}
-#endif // !defined(OS_IOS)
DCHECK_EQ(primary_session_id_, session_id);
if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
@@ -391,15 +390,6 @@ void SpeechRecognitionManagerImpl::OnRecognitionEnd(int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!SessionExists(session_id))
return;
-#if !defined(OS_IOS)
- const SpeechRecognitionSessionContext& context =
- GetSessionContext(session_id);
- if (!context.devices.empty()) {
- // Notify the UI the devices has been closed.
- BrowserMainLoop::GetMediaStreamManager()->NotifyUIDevicesClosed(
- context.label);
- }
-#endif // !defined(OS_IOS)
if (SpeechRecognitionEventListener* delegate_listener = GetDelegateListener())
delegate_listener->OnRecognitionEnd(session_id);
@@ -419,7 +409,7 @@ int SpeechRecognitionManagerImpl::GetSession(
SessionsTable::const_iterator iter;
for(iter = sessions_.begin(); iter != sessions_.end(); ++iter) {
const int session_id = iter->first;
- const SpeechRecognitionSessionContext& context = iter->second.context;
+ const SpeechRecognitionSessionContext& context = iter->second->context;
if (context.render_process_id == render_process_id &&
context.render_view_id == render_view_id &&
context.request_id == request_id) {
@@ -431,7 +421,7 @@ int SpeechRecognitionManagerImpl::GetSession(
SpeechRecognitionSessionContext
SpeechRecognitionManagerImpl::GetSessionContext(int session_id) const {
- return GetSession(session_id).context;
+ return GetSession(session_id)->context;
}
void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
@@ -442,10 +432,10 @@ void SpeechRecognitionManagerImpl::AbortAllSessionsForListener(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
++it) {
- Session& session = it->second;
- if (session.config.event_listener == listener) {
- AbortSession(session.id);
- session.listener_is_active = false;
+ Session* session = it->second;
+ if (session->config.event_listener == listener) {
+ AbortSession(session->id);
+ session->listener_is_active = false;
}
}
}
@@ -456,10 +446,10 @@ void SpeechRecognitionManagerImpl::AbortAllSessionsForRenderView(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
for (SessionsTable::iterator it = sessions_.begin(); it != sessions_.end();
++it) {
- Session& session = it->second;
- if (session.context.render_process_id == render_process_id &&
- session.context.render_view_id == render_view_id) {
- AbortSession(session.id);
+ Session* session = it->second;
+ if (session->context.render_process_id == render_process_id &&
+ session->context.render_view_id == render_view_id) {
+ AbortSession(session->id);
}
}
}
@@ -474,7 +464,7 @@ void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
if (!SessionExists(session_id))
return;
- const Session& session = GetSession(session_id);
+ Session* session = GetSession(session_id);
FSMState session_state = GetSessionState(session_id);
DCHECK_LE(session_state, SESSION_STATE_MAX_VALUE);
DCHECK_LE(event, EVENT_MAX_VALUE);
@@ -494,7 +484,7 @@ void SpeechRecognitionManagerImpl::DispatchEvent(int session_id,
// session) are always routed to the SpeechRecognitionEventListener(s)
// regardless the choices taken in this FSM.
void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
- const Session& session, FSMState session_state, FSMEvent event) {
+ Session* session, FSMState session_state, FSMEvent event) {
// Note: since we're not tracking the state of the recognizer object, rather
// we're directly retrieving it (through GetSessionState), we see its events
// (that are AUDIO_ENDED and RECOGNITION_ENDED) after its state evolution
@@ -507,13 +497,13 @@ void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
case SESSION_STATE_IDLE:
switch (event) {
case EVENT_START:
- return SessionStart(session);
+ return SessionStart(*session);
case EVENT_ABORT:
- return SessionAbort(session);
+ return SessionAbort(*session);
case EVENT_RECOGNITION_ENDED:
return SessionDelete(session);
case EVENT_STOP_CAPTURE:
- return SessionStopAudioCapture(session);
+ return SessionStopAudioCapture(*session);
case EVENT_AUDIO_ENDED:
return;
}
@@ -521,39 +511,39 @@ void SpeechRecognitionManagerImpl::ExecuteTransitionAndGetNextState(
case SESSION_STATE_CAPTURING_AUDIO:
switch (event) {
case EVENT_STOP_CAPTURE:
- return SessionStopAudioCapture(session);
+ return SessionStopAudioCapture(*session);
case EVENT_ABORT:
- return SessionAbort(session);
+ return SessionAbort(*session);
case EVENT_START:
return;
case EVENT_AUDIO_ENDED:
case EVENT_RECOGNITION_ENDED:
- return NotFeasible(session, event);
+ return NotFeasible(*session, event);
}
break;
case SESSION_STATE_WAITING_FOR_RESULT:
switch (event) {
case EVENT_ABORT:
- return SessionAbort(session);
+ return SessionAbort(*session);
case EVENT_AUDIO_ENDED:
- return ResetCapturingSessionId(session);
+ return ResetCapturingSessionId(*session);
case EVENT_START:
case EVENT_STOP_CAPTURE:
return;
case EVENT_RECOGNITION_ENDED:
- return NotFeasible(session, event);
+ return NotFeasible(*session, event);
}
break;
}
- return NotFeasible(session, event);
+ return NotFeasible(*session, event);
}
SpeechRecognitionManagerImpl::FSMState
SpeechRecognitionManagerImpl::GetSessionState(int session_id) const {
- const Session& session = GetSession(session_id);
- if (!session.recognizer.get() || !session.recognizer->IsActive())
+ Session* session = GetSession(session_id);
+ if (!session->recognizer.get() || !session->recognizer->IsActive())
return SESSION_STATE_IDLE;
- if (session.recognizer->IsCapturingAudio())
+ if (session->recognizer->IsCapturingAudio())
return SESSION_STATE_CAPTURING_AUDIO;
return SESSION_STATE_WAITING_FOR_RESULT;
}
@@ -586,11 +576,12 @@ void SpeechRecognitionManagerImpl::ResetCapturingSessionId(
primary_session_id_ = kSessionIDInvalid;
}
-void SpeechRecognitionManagerImpl::SessionDelete(const Session& session) {
- DCHECK(session.recognizer == NULL || !session.recognizer->IsActive());
- if (primary_session_id_ == session.id)
+void SpeechRecognitionManagerImpl::SessionDelete(Session* session) {
+ DCHECK(session->recognizer == NULL || !session->recognizer->IsActive());
+ if (primary_session_id_ == session->id)
primary_session_id_ = kSessionIDInvalid;
- sessions_.erase(session.id);
+ sessions_.erase(session->id);
+ delete session;
}
void SpeechRecognitionManagerImpl::NotFeasible(const Session& session,
@@ -612,7 +603,7 @@ bool SpeechRecognitionManagerImpl::SessionExists(int session_id) const {
return sessions_.find(session_id) != sessions_.end();
}
-const SpeechRecognitionManagerImpl::Session&
+SpeechRecognitionManagerImpl::Session*
SpeechRecognitionManagerImpl::GetSession(int session_id) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
SessionsTable::const_iterator iter = sessions_.find(session_id);
@@ -622,8 +613,8 @@ SpeechRecognitionManagerImpl::GetSession(int session_id) const {
SpeechRecognitionEventListener* SpeechRecognitionManagerImpl::GetListener(
int session_id) const {
- const Session& session = GetSession(session_id);
- return session.listener_is_active ? session.config.event_listener : NULL;
+ Session* session = GetSession(session_id);
+ return session->listener_is_active ? session->config.event_listener : NULL;
}
SpeechRecognitionEventListener*
@@ -633,7 +624,7 @@ SpeechRecognitionManagerImpl::GetDelegateListener() const {
const SpeechRecognitionSessionConfig&
SpeechRecognitionManagerImpl::GetSessionConfig(int session_id) const {
- return GetSession(session_id).config;
+ return GetSession(session_id)->config;
}
bool SpeechRecognitionManagerImpl::HasAudioInputDevices() {