| OLD | NEW |
| 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/renderer/media/media_stream_dispatcher.h" | 5 #include "content/renderer/media/media_stream_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "content/common/media/media_stream_messages.h" | 9 #include "content/common/media/media_stream_messages.h" |
| 10 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" | 10 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
| 11 #include "content/renderer/render_thread_impl.h" | 11 #include "content/renderer/render_thread_impl.h" |
| 12 #include "content/renderer/render_view_impl.h" | 12 #include "content/renderer/render_view_impl.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 // A request is identified by pair (request_id, handler), or ipc_request. |
| 18 // There could be multiple clients making requests and each has its own |
| 19 // request_id sequence. |
| 20 // The ipc_request is garanteed to be unique when it's created in |
| 21 // MediaStreamDispatcher. |
| 17 struct MediaStreamDispatcher::Request { | 22 struct MediaStreamDispatcher::Request { |
| 18 Request(const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, | 23 Request(const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, |
| 19 int request_id, | 24 int request_id, |
| 20 int ipc_request) | 25 int ipc_request) |
| 21 : handler(handler), | 26 : handler(handler), |
| 22 request_id(request_id), | 27 request_id(request_id), |
| 23 ipc_request(ipc_request) { | 28 ipc_request(ipc_request) { |
| 24 } | 29 } |
| 30 bool IsThisRequest( |
| 31 int request_id1, |
| 32 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler1) { |
| 33 return (request_id1 == request_id && handler1.get() == handler.get()); |
| 34 } |
| 25 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; | 35 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; |
| 26 int request_id; | 36 int request_id; |
| 27 int ipc_request; | 37 int ipc_request; |
| 28 }; | 38 }; |
| 29 | 39 |
| 30 struct MediaStreamDispatcher::Stream { | 40 struct MediaStreamDispatcher::Stream { |
| 31 Stream() {} | 41 Stream() {} |
| 32 ~Stream() {} | 42 ~Stream() {} |
| 33 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; | 43 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; |
| 34 StreamDeviceInfoArray audio_array; | 44 StreamDeviceInfoArray audio_array; |
| 35 StreamDeviceInfoArray video_array; | 45 StreamDeviceInfoArray video_array; |
| 36 }; | 46 }; |
| 37 | 47 |
| 38 MediaStreamDispatcher::EnumerationRequest::EnumerationRequest( | 48 MediaStreamDispatcher::EnumerationRequest::EnumerationRequest( |
| 39 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, | 49 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, |
| 40 int request_id) | 50 int request_id) |
| 41 : handler(handler), | 51 : handler(handler), |
| 42 request_id(request_id) { | 52 request_id(request_id) { |
| 43 } | 53 } |
| 44 | 54 |
| 45 MediaStreamDispatcher::EnumerationRequest::~EnumerationRequest() {} | 55 MediaStreamDispatcher::EnumerationRequest::~EnumerationRequest() {} |
| 46 | 56 |
| 57 bool MediaStreamDispatcher::EnumerationRequest::IsThisRequest( |
| 58 int request_id1, |
| 59 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler1) { |
| 60 return (request_id1 == request_id && handler1.get() == handler.get()); |
| 61 } |
| 62 |
| 47 MediaStreamDispatcher::EnumerationState::EnumerationState() | 63 MediaStreamDispatcher::EnumerationState::EnumerationState() |
| 48 : ipc_id(-1) { | 64 : ipc_id(-1) { |
| 49 } | 65 } |
| 50 | 66 |
| 51 MediaStreamDispatcher::EnumerationState::~EnumerationState() {} | 67 MediaStreamDispatcher::EnumerationState::~EnumerationState() {} |
| 52 | 68 |
| 53 struct MediaStreamDispatcher::EnumerationState::CachedDevices { | 69 struct MediaStreamDispatcher::EnumerationState::CachedDevices { |
| 54 CachedDevices(const std::string& label, | 70 CachedDevices(const std::string& label, |
| 55 const StreamDeviceInfoArray& device_array) | 71 const StreamDeviceInfoArray& device_array) |
| 56 : label(label), | 72 : label(label), |
| (...skipping 21 matching lines...) Expand all Loading... |
| 78 DCHECK(main_loop_->BelongsToCurrentThread()); | 94 DCHECK(main_loop_->BelongsToCurrentThread()); |
| 79 DVLOG(1) << "MediaStreamDispatcher::GenerateStream(" << request_id << ")"; | 95 DVLOG(1) << "MediaStreamDispatcher::GenerateStream(" << request_id << ")"; |
| 80 | 96 |
| 81 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); | 97 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); |
| 82 Send(new MediaStreamHostMsg_GenerateStream(routing_id(), | 98 Send(new MediaStreamHostMsg_GenerateStream(routing_id(), |
| 83 next_ipc_id_++, | 99 next_ipc_id_++, |
| 84 components, | 100 components, |
| 85 security_origin)); | 101 security_origin)); |
| 86 } | 102 } |
| 87 | 103 |
| 88 void MediaStreamDispatcher::CancelGenerateStream(int request_id) { | 104 void MediaStreamDispatcher::CancelGenerateStream( |
| 105 int request_id, |
| 106 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) { |
| 89 DCHECK(main_loop_->BelongsToCurrentThread()); | 107 DCHECK(main_loop_->BelongsToCurrentThread()); |
| 90 DVLOG(1) << "MediaStreamDispatcher::CancelGenerateStream" | 108 DVLOG(1) << "MediaStreamDispatcher::CancelGenerateStream" |
| 91 << ", {request_id = " << request_id << "}"; | 109 << ", {request_id = " << request_id << "}"; |
| 92 | 110 |
| 93 RequestList::iterator it = requests_.begin(); | 111 RequestList::iterator it = requests_.begin(); |
| 94 for (; it != requests_.end(); ++it) { | 112 for (; it != requests_.end(); ++it) { |
| 95 Request& request = *it; | 113 if (it->IsThisRequest(request_id, event_handler)) { |
| 96 if (request.request_id == request_id) { | 114 int ipc_request = it->ipc_request; |
| 97 requests_.erase(it); | 115 requests_.erase(it); |
| 98 Send(new MediaStreamHostMsg_CancelGenerateStream(routing_id(), | 116 Send(new MediaStreamHostMsg_CancelGenerateStream(routing_id(), |
| 99 request_id)); | 117 ipc_request)); |
| 100 break; | 118 break; |
| 101 } | 119 } |
| 102 } | 120 } |
| 103 } | 121 } |
| 104 | 122 |
| 105 void MediaStreamDispatcher::StopStream(const std::string& label) { | 123 void MediaStreamDispatcher::StopStream(const std::string& label) { |
| 106 DCHECK(main_loop_->BelongsToCurrentThread()); | 124 DCHECK(main_loop_->BelongsToCurrentThread()); |
| 107 DVLOG(1) << "MediaStreamDispatcher::StopStream" | 125 DVLOG(1) << "MediaStreamDispatcher::StopStream" |
| 108 << ", {label = " << label << "}"; | 126 << ", {label = " << label << "}"; |
| 109 | 127 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 request_id, event_handler, &video_enumeration_state_); | 175 request_id, event_handler, &video_enumeration_state_); |
| 158 } | 176 } |
| 159 | 177 |
| 160 void MediaStreamDispatcher::RemoveEnumerationRequest( | 178 void MediaStreamDispatcher::RemoveEnumerationRequest( |
| 161 int request_id, | 179 int request_id, |
| 162 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 180 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
| 163 EnumerationState* state) { | 181 EnumerationState* state) { |
| 164 EnumerationRequestList* requests = &state->requests; | 182 EnumerationRequestList* requests = &state->requests; |
| 165 for (EnumerationRequestList::iterator it = requests->begin(); | 183 for (EnumerationRequestList::iterator it = requests->begin(); |
| 166 it != requests->end(); ++it) { | 184 it != requests->end(); ++it) { |
| 167 if (it->request_id == request_id && | 185 if (it->IsThisRequest(request_id, event_handler)) { |
| 168 it->handler.get() == event_handler.get()) { | |
| 169 requests->erase(it); | 186 requests->erase(it); |
| 170 if (requests->empty() && state->cached_devices) { | 187 if (requests->empty() && state->cached_devices) { |
| 171 // No more request and has a label, try to stop the label | 188 // No more request and has a label, try to stop the label |
| 172 // and invalidate the state. | 189 // and invalidate the state. |
| 173 Send(new MediaStreamHostMsg_StopGeneratedStream( | 190 Send(new MediaStreamHostMsg_StopGeneratedStream( |
| 174 routing_id(), state->cached_devices->label)); | 191 routing_id(), state->cached_devices->label)); |
| 175 state->ipc_id = -1; | 192 state->ipc_id = -1; |
| 176 state->cached_devices.reset(); | 193 state->cached_devices.reset(); |
| 177 } | 194 } |
| 178 return; | 195 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 190 DVLOG(1) << "MediaStreamDispatcher::OpenDevice(" << request_id << ")"; | 207 DVLOG(1) << "MediaStreamDispatcher::OpenDevice(" << request_id << ")"; |
| 191 | 208 |
| 192 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); | 209 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); |
| 193 Send(new MediaStreamHostMsg_OpenDevice(routing_id(), | 210 Send(new MediaStreamHostMsg_OpenDevice(routing_id(), |
| 194 next_ipc_id_++, | 211 next_ipc_id_++, |
| 195 device_id, | 212 device_id, |
| 196 type, | 213 type, |
| 197 security_origin)); | 214 security_origin)); |
| 198 } | 215 } |
| 199 | 216 |
| 217 void MediaStreamDispatcher::CancelOpenDevice( |
| 218 int request_id, |
| 219 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) { |
| 220 CancelGenerateStream(request_id, event_handler); |
| 221 } |
| 222 |
| 200 void MediaStreamDispatcher::CloseDevice(const std::string& label) { | 223 void MediaStreamDispatcher::CloseDevice(const std::string& label) { |
| 201 DCHECK(main_loop_->BelongsToCurrentThread()); | 224 DCHECK(main_loop_->BelongsToCurrentThread()); |
| 202 DVLOG(1) << "MediaStreamDispatcher::CloseDevice" | 225 DVLOG(1) << "MediaStreamDispatcher::CloseDevice" |
| 203 << ", {label = " << label << "}"; | 226 << ", {label = " << label << "}"; |
| 204 | 227 |
| 205 StopStream(label); | 228 StopStream(label); |
| 206 } | 229 } |
| 207 | 230 |
| 208 bool MediaStreamDispatcher::Send(IPC::Message* message) { | 231 bool MediaStreamDispatcher::Send(IPC::Message* message) { |
| 209 if (!RenderThread::Get()) { | 232 if (!RenderThread::Get()) { |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 int index) { | 419 int index) { |
| 397 LabelStreamMap::iterator it = label_stream_map_.find(label); | 420 LabelStreamMap::iterator it = label_stream_map_.find(label); |
| 398 if (it == label_stream_map_.end()) | 421 if (it == label_stream_map_.end()) |
| 399 return StreamDeviceInfo::kNoId; | 422 return StreamDeviceInfo::kNoId; |
| 400 | 423 |
| 401 DCHECK_GT(it->second.video_array.size(), static_cast<size_t>(index)); | 424 DCHECK_GT(it->second.video_array.size(), static_cast<size_t>(index)); |
| 402 return it->second.video_array[index].session_id; | 425 return it->second.video_array[index].session_id; |
| 403 } | 426 } |
| 404 | 427 |
| 405 } // namespace content | 428 } // namespace content |
| OLD | NEW |