Index: content/renderer/media/media_stream_dispatcher.cc |
=================================================================== |
--- content/renderer/media/media_stream_dispatcher.cc (revision 148913) |
+++ content/renderer/media/media_stream_dispatcher.cc (working copy) |
@@ -31,6 +31,19 @@ |
media_stream::StreamDeviceInfoArray video_array; |
}; |
+MediaStreamDispatcher::EnumerationRequest::EnumerationRequest( |
+ const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, |
+ int request_id) |
+ : handler(handler), |
+ request_id(request_id) { |
+} |
+ |
+MediaStreamDispatcher::EnumerationState::EnumerationState() |
+ : started(false), |
+ ipc_id(0), |
+ cache_valid(false) { |
+} |
+ |
MediaStreamDispatcher::MediaStreamDispatcher(RenderViewImpl* render_view) |
: content::RenderViewObserver(render_view), |
next_ipc_id_(0) { |
@@ -88,13 +101,63 @@ |
DVLOG(1) << "MediaStreamDispatcher::EnumerateDevices(" |
tommi (sloooow) - chröme
2012/07/30 12:10:21
Is this method (and the other methods that touch t
wjia(left Chromium)
2012/08/01 01:28:59
They are all called on main thread. DCHECK has bee
|
<< request_id << ")"; |
- requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); |
- Send(new MediaStreamHostMsg_EnumerateDevices(routing_id(), |
- next_ipc_id_++, |
- type, |
- security_origin)); |
+ DCHECK(type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE || |
+ type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE); |
+ |
+ EnumerationState* state = |
+ (type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE ? |
+ &audio_enumeration_state_ : &video_enumeration_state_); |
+ state->requests.push_back( |
+ EnumerationRequest(event_handler, request_id)); |
+ |
+ if (state->cache_valid) { |
+ event_handler->OnDevicesEnumerated(request_id, state->cached_device); |
+ } else if (!state->started) { |
+ state->started = true; |
+ Send(new MediaStreamHostMsg_EnumerateDevices(routing_id(), |
+ next_ipc_id_, |
+ type, |
+ security_origin)); |
+ state->ipc_id = next_ipc_id_++; |
+ } |
} |
+void MediaStreamDispatcher::StopEnumerateDevices( |
+ int request_id, |
+ const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) { |
+ DVLOG(1) << "MediaStreamDispatcher::StopEnumerateDevices(" |
+ << request_id << ")"; |
+ |
+ // Just need remove the request from one type. |
+ RemoveEnumerationRequest(request_id, event_handler, |
+ &audio_enumeration_state_) || |
+ RemoveEnumerationRequest(request_id, event_handler, |
+ &video_enumeration_state_); |
+} |
+ |
+bool MediaStreamDispatcher::RemoveEnumerationRequest( |
+ int request_id, |
+ const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
+ EnumerationState* state) { |
+ EnumerationRequestList* requests = &state->requests; |
+ for (EnumerationRequestList::iterator it = requests->begin(); |
+ it != requests->end(); ++it) { |
+ if (it->request_id == request_id && it->handler == event_handler) { |
+ requests->erase(it); |
+ if (requests->empty() && !state->label.empty()) { |
+ // No more request and has a label, try to stop the label |
+ // and invalidate the state. |
+ Send(new MediaStreamHostMsg_StopGeneratedStream( |
+ routing_id(), state->label)); |
+ state->started = false; |
+ state->cache_valid = false; |
+ } |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
void MediaStreamDispatcher::OpenDevice( |
int request_id, |
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
@@ -217,19 +280,29 @@ |
void MediaStreamDispatcher::OnDevicesEnumerated( |
int request_id, |
+ const std::string& label, |
const media_stream::StreamDeviceInfoArray& device_array) { |
+ EnumerationState* state; |
+ if (audio_enumeration_state_.started && |
+ request_id == audio_enumeration_state_.ipc_id) { |
+ state = &audio_enumeration_state_; |
+ } else if (video_enumeration_state_.started && |
+ request_id == video_enumeration_state_.ipc_id) { |
+ state = &video_enumeration_state_; |
+ } else { |
+ return; |
+ } |
- for (RequestList::iterator it = requests_.begin(); |
- it != requests_.end(); ++it) { |
- Request& request = *it; |
- if (request.ipc_request == request_id) { |
- if (request.handler) { |
- request.handler->OnDevicesEnumerated(request.request_id, device_array); |
- DVLOG(1) << "MediaStreamDispatcher::OnDevicesEnumerated(" |
- << request.request_id << ")"; |
- } |
- requests_.erase(it); |
- break; |
+ state->label = label; |
+ state->cache_valid = true; |
+ state->cached_device = device_array; |
+ |
+ for (EnumerationRequestList::iterator it = state->requests.begin(); |
+ it != state->requests.end(); ++it) { |
+ if (it->handler) { |
+ it->handler->OnDevicesEnumerated(it->request_id, device_array); |
+ DVLOG(1) << "MediaStreamDispatcher::OnDevicesEnumerated(" |
+ << it->request_id << ")"; |
} |
} |
} |