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 // | |
5 // VideoCaptureHost serves video capture related messages from | |
6 // VideoCaptureMessageFilter which lives inside the render process. | |
7 // | |
8 // This class is owned by RenderProcessHostImpl, and instantiated on UI | |
9 // thread, but all other operations and method calls happen on IO thread. | |
10 // | |
11 // Here's an example of a typical IPC dialog for video capture: | |
12 // | |
13 // Renderer VideoCaptureHost | |
14 // | | | |
15 // | --------- StartCapture --------> | | |
16 // | <------ VideoCaptureObserver ------ | | |
17 // | ::StateChanged(STARTED) | | |
18 // | < VideoCaptureMsg_NewBuffer(1) | | |
19 // | < VideoCaptureMsg_NewBuffer(2) | | |
20 // | < VideoCaptureMsg_NewBuffer(3) | | |
21 // | | | |
22 // | <-------- OnBufferReady(1) --------- | | |
23 // | <-------- OnBufferReady(2) --------- | | |
24 // | -------- ReleaseBuffer(1) ---------> | | |
25 // | <-------- OnBufferReady(3) --------- | | |
26 // | -------- ReleaseBuffer(2) ---------> | | |
27 // | <-------- OnBufferReady(1) --------- | | |
28 // | -------- ReleaseBuffer(3) ---------> | | |
29 // | <-------- OnBufferReady(2) --------- | | |
30 // | -------- ReleaseBuffer(1) ---------> | | |
31 // | ... | | |
32 // | <-------- OnBufferReady(3) --------- | | |
33 // = = | |
34 // | ... (resolution change) | | |
35 // | <------ OnBufferDestroyed(3) ------- | Buffers are re-allocated | |
36 // | < VideoCaptureMsg_NewBuffer(4) | with a larger size, as | |
37 // | <-------- OnBufferReady(4) --------- | needed. | |
38 // | -------- ReleaseBuffer(2) ---------> | | |
39 // | <------ OnBufferDestroyed(2) ------- | | |
40 // | < VideoCaptureMsg_NewBuffer(5) | | |
41 // | <-------- OnBufferReady(5) --------- | | |
42 // = ... = | |
43 // | | | |
44 // | < VideoCaptureMsg_BufferReady | | |
45 // | --------- StopCapture ---------> | | |
46 // | -------- ReleaseBuffer(n) ---------> | | |
47 // | <------ VideoCaptureObserver ------ | | |
48 // | ::StateChanged(STOPPED) | | |
49 // v v | |
50 | 4 |
51 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ |
52 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ |
53 | 7 |
54 #include <map> | 8 #include <map> |
55 | 9 |
56 #include "base/macros.h" | 10 #include "base/macros.h" |
57 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
58 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
59 #include "base/sequenced_task_runner_helpers.h" | 13 #include "base/sequenced_task_runner_helpers.h" |
60 #include "content/browser/renderer_host/media/video_capture_controller.h" | 14 #include "content/browser/renderer_host/media/video_capture_controller.h" |
61 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 15 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
62 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
63 #include "content/common/video_capture.mojom.h" | 17 #include "content/common/video_capture.mojom.h" |
64 #include "content/public/browser/browser_associated_interface.h" | 18 #include "content/public/browser/browser_associated_interface.h" |
65 #include "content/public/browser/browser_message_filter.h" | 19 #include "content/public/browser/browser_message_filter.h" |
66 #include "ipc/ipc_message.h" | 20 #include "ipc/ipc_message.h" |
67 | 21 |
68 namespace content { | 22 namespace content { |
69 class MediaStreamManager; | 23 class MediaStreamManager; |
70 | 24 |
25 // This class is essentially a super adapter between several different areas. It | |
ncarter (slow)
2016/10/19 18:58:21
This comment might be better if the first sentence
mcasas
2016/10/19 19:29:51
Done.
| |
26 // is: | |
27 // - a mojom::VideoCaptureHost to receive commands from the Renderer, indicating | |
28 // to Start(), Stop() etc, the capture. Every remote client is identified via a | |
29 // unique |device_id|, and is paired with a single VideoCaptureController. The | |
30 // received commands are acted upon via requests to the VideoCaptureManager | |
31 // inside |media_stream_manager_|. | |
32 // - a VideoCaptureControllerEventHandler, to receive updates from a given | |
33 // VideoCaptureController and forward them to a mojom::VideoCaptureObserver | |
34 // remote implementation in Renderer; this Observer is passed during Start(). | |
35 // - a BrowserMessageFilter, to listen to OnChannelClosing() and OnDestruct(). | |
36 // | |
37 // This class is owned by RenderProcessHostImpl, and instantiated on UI thread, | |
38 // but all other operations and method calls happen on IO thread. | |
71 class CONTENT_EXPORT VideoCaptureHost | 39 class CONTENT_EXPORT VideoCaptureHost |
72 : public BrowserMessageFilter, | 40 : public BrowserMessageFilter, |
73 public VideoCaptureControllerEventHandler, | 41 public VideoCaptureControllerEventHandler, |
74 public BrowserAssociatedInterface<mojom::VideoCaptureHost>, | 42 public BrowserAssociatedInterface<mojom::VideoCaptureHost>, |
75 public mojom::VideoCaptureHost { | 43 public mojom::VideoCaptureHost { |
76 public: | 44 public: |
77 explicit VideoCaptureHost(MediaStreamManager* media_stream_manager); | 45 explicit VideoCaptureHost(MediaStreamManager* media_stream_manager); |
78 | 46 |
47 private: | |
48 friend class BrowserThread; | |
49 friend class base::DeleteHelper<VideoCaptureHost>; | |
50 friend class VideoCaptureHostTest; | |
51 | |
52 ~VideoCaptureHost() override; | |
53 | |
79 // BrowserMessageFilter implementation. | 54 // BrowserMessageFilter implementation. |
80 void OnChannelClosing() override; | 55 void OnChannelClosing() override; |
81 void OnDestruct() const override; | 56 void OnDestruct() const override; |
82 bool OnMessageReceived(const IPC::Message& message) override; | 57 bool OnMessageReceived(const IPC::Message& message) override; |
83 | 58 |
84 // VideoCaptureControllerEventHandler implementation. | 59 // VideoCaptureControllerEventHandler implementation. |
85 void OnError(VideoCaptureControllerID id) override; | 60 void OnError(VideoCaptureControllerID id) override; |
86 void OnBufferCreated(VideoCaptureControllerID id, | 61 void OnBufferCreated(VideoCaptureControllerID id, |
87 base::SharedMemoryHandle handle, | 62 mojo::ScopedSharedBufferHandle handle, |
88 int length, | 63 int length, |
89 int buffer_id) override; | 64 int buffer_id) override; |
90 void OnBufferDestroyed(VideoCaptureControllerID id, | 65 void OnBufferDestroyed(VideoCaptureControllerID id, |
91 int buffer_id) override; | 66 int buffer_id) override; |
92 void OnBufferReady(VideoCaptureControllerID id, | 67 void OnBufferReady(VideoCaptureControllerID id, |
93 int buffer_id, | 68 int buffer_id, |
94 const scoped_refptr<media::VideoFrame>& frame) override; | 69 const scoped_refptr<media::VideoFrame>& frame) override; |
95 void OnEnded(VideoCaptureControllerID id) override; | 70 void OnEnded(VideoCaptureControllerID id) override; |
96 | 71 |
97 private: | |
98 friend class BrowserThread; | |
99 friend class base::DeleteHelper<VideoCaptureHost>; | |
100 friend class MockVideoCaptureHost; | |
101 friend class VideoCaptureHostTest; | |
102 | |
103 void DoError(VideoCaptureControllerID id); | |
104 void DoEnded(VideoCaptureControllerID id); | |
105 | |
106 ~VideoCaptureHost() override; | |
107 | |
108 // mojom::VideoCaptureHost implementation | 72 // mojom::VideoCaptureHost implementation |
109 void Start(int32_t device_id, | 73 void Start(int32_t device_id, |
110 int32_t session_id, | 74 int32_t session_id, |
111 const media::VideoCaptureParams& params, | 75 const media::VideoCaptureParams& params, |
112 mojom::VideoCaptureObserverPtr observer) override; | 76 mojom::VideoCaptureObserverPtr observer) override; |
113 void Stop(int32_t device_id) override; | 77 void Stop(int32_t device_id) override; |
114 void Pause(int32_t device_id) override; | 78 void Pause(int32_t device_id) override; |
115 void Resume(int32_t device_id, | 79 void Resume(int32_t device_id, |
116 int32_t session_id, | 80 int32_t session_id, |
117 const media::VideoCaptureParams& params) override; | 81 const media::VideoCaptureParams& params) override; |
118 void RequestRefreshFrame(int32_t device_id) override; | 82 void RequestRefreshFrame(int32_t device_id) override; |
119 void ReleaseBuffer(int32_t device_id, | 83 void ReleaseBuffer(int32_t device_id, |
120 int32_t buffer_id, | 84 int32_t buffer_id, |
121 const gpu::SyncToken& sync_token, | 85 const gpu::SyncToken& sync_token, |
122 double consumer_resource_utilization) override; | 86 double consumer_resource_utilization) override; |
123 void GetDeviceSupportedFormats( | 87 void GetDeviceSupportedFormats( |
124 int32_t device_id, | 88 int32_t device_id, |
125 int32_t session_id, | 89 int32_t session_id, |
126 const GetDeviceSupportedFormatsCallback& callback) override; | 90 const GetDeviceSupportedFormatsCallback& callback) override; |
127 void GetDeviceFormatsInUse( | 91 void GetDeviceFormatsInUse( |
128 int32_t device_id, | 92 int32_t device_id, |
129 int32_t session_id, | 93 int32_t session_id, |
130 const GetDeviceFormatsInUseCallback& callback) override; | 94 const GetDeviceFormatsInUseCallback& callback) override; |
131 | 95 |
96 void DoError(VideoCaptureControllerID id); | |
97 void DoEnded(VideoCaptureControllerID id); | |
98 | |
99 // Bound as callback for VideoCaptureManager::StartCaptureForClient(). | |
132 void OnControllerAdded( | 100 void OnControllerAdded( |
133 int device_id, | 101 int device_id, |
134 const base::WeakPtr<VideoCaptureController>& controller); | 102 const base::WeakPtr<VideoCaptureController>& controller); |
135 | 103 |
136 // Deletes the controller and notifies the VideoCaptureManager. |on_error| is | 104 // Helper function that deletes the controller and tells VideoCaptureManager |
137 // true if this is triggered by VideoCaptureControllerEventHandler::OnError. | 105 // to StopCaptureForClient(). |on_error| is true if this is triggered by |
106 // VideoCaptureControllerEventHandler::OnError. | |
138 void DeleteVideoCaptureController(VideoCaptureControllerID controller_id, | 107 void DeleteVideoCaptureController(VideoCaptureControllerID controller_id, |
139 bool on_error); | 108 bool on_error); |
140 | 109 |
141 MediaStreamManager* const media_stream_manager_; | 110 MediaStreamManager* const media_stream_manager_; |
142 | 111 |
143 // A map of VideoCaptureControllerID to the VideoCaptureController to which it | 112 // A map of VideoCaptureControllerID to the VideoCaptureController to which it |
144 // is connected. An entry in this map holds a null controller while it is in | 113 // is connected. An entry in this map holds a null controller while it is in |
145 // the process of starting. | 114 // the process of starting. |
146 std::map<VideoCaptureControllerID, base::WeakPtr<VideoCaptureController>> | 115 std::map<VideoCaptureControllerID, base::WeakPtr<VideoCaptureController>> |
147 controllers_; | 116 controllers_; |
148 | 117 |
149 // VideoCaptureObservers map, each one is used and should be valid between | 118 // VideoCaptureObservers map, each one is used and should be valid between |
150 // Start() and the corresponding Stop(). | 119 // Start() and the corresponding Stop(). |
151 std::map<int32_t, mojom::VideoCaptureObserverPtr> device_id_to_observer_map_; | 120 std::map<int32_t, mojom::VideoCaptureObserverPtr> device_id_to_observer_map_; |
152 | 121 |
153 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHost); | 122 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHost); |
154 }; | 123 }; |
155 | 124 |
156 } // namespace content | 125 } // namespace content |
157 | 126 |
158 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ | 127 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_ |
OLD | NEW |