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 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ |
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/message_loop.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
16 #include "content/common/media/media_stream_options.h" | 17 #include "content/common/media/media_stream_options.h" |
17 #include "content/public/renderer/render_view_observer.h" | 18 #include "content/public/renderer/render_view_observer.h" |
18 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" | 19 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
19 | 20 |
| 21 namespace base { |
| 22 class MessageLoopProxy; |
| 23 } |
| 24 |
20 class RenderViewImpl; | 25 class RenderViewImpl; |
21 | 26 |
22 // MediaStreamDispatcher is a delegate for the Media Stream API messages. | 27 // MediaStreamDispatcher is a delegate for the Media Stream API messages. |
23 // MediaStreams are used by WebKit to open media devices such as Video Capture | 28 // MediaStreams are used by WebKit to open media devices such as Video Capture |
24 // and Audio input devices. | 29 // and Audio input devices. |
25 // It's the complement of MediaStreamDispatcherHost (owned by | 30 // It's the complement of MediaStreamDispatcherHost (owned by |
26 // BrowserRenderProcessHost). | 31 // BrowserRenderProcessHost). |
27 class CONTENT_EXPORT MediaStreamDispatcher | 32 class CONTENT_EXPORT MediaStreamDispatcher |
28 : public content::RenderViewObserver { | 33 : public content::RenderViewObserver, |
| 34 public base::SupportsWeakPtr<MediaStreamDispatcher> { |
29 public: | 35 public: |
30 explicit MediaStreamDispatcher(RenderViewImpl* render_view); | 36 explicit MediaStreamDispatcher(RenderViewImpl* render_view); |
31 virtual ~MediaStreamDispatcher(); | 37 virtual ~MediaStreamDispatcher(); |
32 | 38 |
33 // Request a new media stream to be created. | 39 // Request a new media stream to be created. |
34 // This can be used either of WebKit or a plugin. | 40 // This can be used either of WebKit or a plugin. |
35 // Note: The event_handler must be valid for as long as the stream exists. | 41 // Note: The event_handler must be valid for as long as the stream exists. |
36 virtual void GenerateStream( | 42 virtual void GenerateStream( |
37 int request_id, | 43 int request_id, |
38 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 44 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
39 media_stream::StreamOptions components, | 45 media_stream::StreamOptions components, |
40 const GURL& security_origin); | 46 const GURL& security_origin); |
41 | 47 |
42 // Cancel the request for a new media stream to be created. | 48 // Cancel the request for a new media stream to be created. |
43 virtual void CancelGenerateStream(int request_id); | 49 virtual void CancelGenerateStream(int request_id); |
44 | 50 |
45 // Stop a started stream. Label is the label provided in OnStreamGenerated. | 51 // Stop a started stream. Label is the label provided in OnStreamGenerated. |
46 virtual void StopStream(const std::string& label); | 52 virtual void StopStream(const std::string& label); |
47 | 53 |
48 // Request to enumerate devices. | 54 // Request to enumerate devices. |
49 void EnumerateDevices( | 55 void EnumerateDevices( |
50 int request_id, | 56 int request_id, |
51 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 57 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
52 media_stream::MediaStreamType type, | 58 media_stream::MediaStreamType type, |
53 const GURL& security_origin); | 59 const GURL& security_origin); |
54 | 60 |
| 61 // Request to stop enumerating devices. |
| 62 void StopEnumerateDevices( |
| 63 int request_id, |
| 64 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler); |
| 65 |
55 // Request to open a device. | 66 // Request to open a device. |
56 void OpenDevice( | 67 void OpenDevice( |
57 int request_id, | 68 int request_id, |
58 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 69 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
59 const std::string& device_id, | 70 const std::string& device_id, |
60 media_stream::MediaStreamType type, | 71 media_stream::MediaStreamType type, |
61 const GURL& security_origin); | 72 const GURL& security_origin); |
62 | 73 |
63 // Close a started device. |label| is provided in OnDeviceOpened. | 74 // Close a started device. |label| is provided in OnDeviceOpened. |
64 void CloseDevice(const std::string& label); | 75 void CloseDevice(const std::string& label); |
(...skipping 11 matching lines...) Expand all Loading... |
76 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicVideoDevice); | 87 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicVideoDevice); |
77 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, TestFailure); | 88 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, TestFailure); |
78 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CancelGenerateStream); | 89 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CancelGenerateStream); |
79 | 90 |
80 struct Request; | 91 struct Request; |
81 | 92 |
82 // Private class for keeping track of opened devices and who have | 93 // Private class for keeping track of opened devices and who have |
83 // opened it. | 94 // opened it. |
84 struct Stream; | 95 struct Stream; |
85 | 96 |
| 97 struct EnumerationRequest { |
| 98 EnumerationRequest( |
| 99 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler, |
| 100 int request_id); |
| 101 ~EnumerationRequest(); |
| 102 |
| 103 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; |
| 104 int request_id; |
| 105 }; |
| 106 |
| 107 // List of requests made to EnumerateDevices. |
| 108 typedef std::list<EnumerationRequest> EnumerationRequestList; |
| 109 |
| 110 struct EnumerationState { |
| 111 EnumerationState(); |
| 112 ~EnumerationState(); |
| 113 |
| 114 struct CachedDevices; |
| 115 |
| 116 // If |ipc_id| >= 0, then we've started. |
| 117 int ipc_id; |
| 118 scoped_ptr<CachedDevices> cached_devices; |
| 119 EnumerationRequestList requests; |
| 120 }; |
| 121 |
86 // Messages from the browser. | 122 // Messages from the browser. |
87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 123 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
88 void OnStreamGenerated( | 124 void OnStreamGenerated( |
89 int request_id, | 125 int request_id, |
90 const std::string& label, | 126 const std::string& label, |
91 const media_stream::StreamDeviceInfoArray& audio_array, | 127 const media_stream::StreamDeviceInfoArray& audio_array, |
92 const media_stream::StreamDeviceInfoArray& video_array); | 128 const media_stream::StreamDeviceInfoArray& video_array); |
93 void OnStreamGenerationFailed(int request_id); | 129 void OnStreamGenerationFailed(int request_id); |
94 void OnVideoDeviceFailed(const std::string& label, int index); | 130 void OnVideoDeviceFailed(const std::string& label, int index); |
95 void OnAudioDeviceFailed(const std::string& label, int index); | 131 void OnAudioDeviceFailed(const std::string& label, int index); |
96 void OnDevicesEnumerated( | 132 void OnDevicesEnumerated( |
97 int request_id, | 133 int request_id, |
| 134 const std::string& label, |
98 const media_stream::StreamDeviceInfoArray& device_array); | 135 const media_stream::StreamDeviceInfoArray& device_array); |
99 void OnDevicesEnumerationFailed(int request_id); | 136 void OnDevicesEnumerationFailed(int request_id); |
100 void OnDeviceOpened( | 137 void OnDeviceOpened( |
101 int request_id, | 138 int request_id, |
102 const std::string& label, | 139 const std::string& label, |
103 const media_stream::StreamDeviceInfo& device_info); | 140 const media_stream::StreamDeviceInfo& device_info); |
104 void OnDeviceOpenFailed(int request_id); | 141 void OnDeviceOpenFailed(int request_id); |
105 | 142 |
| 143 void RemoveEnumerationRequest( |
| 144 int request_id, |
| 145 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
| 146 EnumerationState* state); |
| 147 |
| 148 // Used for DCHECKs so methods calls won't execute in the wrong thread. |
| 149 scoped_refptr<base::MessageLoopProxy> main_loop_; |
| 150 |
106 int next_ipc_id_; | 151 int next_ipc_id_; |
107 typedef std::map<std::string, Stream> LabelStreamMap; | 152 typedef std::map<std::string, Stream> LabelStreamMap; |
108 LabelStreamMap label_stream_map_; | 153 LabelStreamMap label_stream_map_; |
109 | 154 |
| 155 EnumerationState audio_enumeration_state_; |
| 156 EnumerationState video_enumeration_state_; |
| 157 |
110 // List of calls made to GenerateStream that has not yet completed. | 158 // List of calls made to GenerateStream that has not yet completed. |
111 typedef std::list<Request> RequestList; | 159 typedef std::list<Request> RequestList; |
112 RequestList requests_; | 160 RequestList requests_; |
113 | 161 |
114 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher); | 162 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher); |
115 }; | 163 }; |
116 | 164 |
117 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ | 165 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_ |
OLD | NEW |