Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_host.h
blobf2709123e1bd0e0cff79e0ff089d77629d81f513
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
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.
11 // Here's an example of a typical IPC dialog for video capture:
13 // Renderer VideoCaptureHost
14 // | |
15 // | VideoCaptureHostMsg_Start > |
16 // | < VideoCaptureMsg_StateChanged |
17 // | (VIDEO_CAPTURE_STATE_STARTED) |
18 // | < VideoCaptureMsg_NewBuffer(1) |
19 // | < VideoCaptureMsg_NewBuffer(2) |
20 // | < VideoCaptureMsg_NewBuffer(3) |
21 // | |
22 // | < VideoCaptureMsg_BufferReady(1) |
23 // | < VideoCaptureMsg_BufferReady(2) |
24 // | VideoCaptureHostMsg_BufferReady(1) > |
25 // | < VideoCaptureMsg_BufferReady(3) |
26 // | VideoCaptureHostMsg_BufferReady(2) > |
27 // | < VideoCaptureMsg_BufferReady(1) |
28 // | VideoCaptureHostMsg_BufferReady(3) > |
29 // | < VideoCaptureMsg_BufferReady(2) |
30 // | VideoCaptureHostMsg_BufferReady(1) > |
31 // | ... |
32 // | < VideoCaptureMsg_BufferReady(3) |
33 // | |
34 // | ... (resolution change) |
35 // | < VideoCaptureMsg_FreeBuffer(1) | Buffers are re-allocated
36 // | < VideoCaptureMsg_NewBuffer(4) | with a larger size, as
37 // | < VideoCaptureMsg_BufferReady(4) | needed.
38 // | VideoCaptureHostMsg_BufferReady(2) > |
39 // | < VideoCaptureMsg_FreeBuffer(2) |
40 // | < VideoCaptureMsg_NewBuffer(5) |
41 // | < VideoCaptureMsg_BufferReady(5) |
42 // | ... |
43 // | |
44 // | < VideoCaptureMsg_BufferReady |
45 // | VideoCaptureHostMsg_Stop > |
46 // | VideoCaptureHostMsg_BufferReady > |
47 // | < VideoCaptureMsg_StateChanged |
48 // | (VIDEO_CAPTURE_STATE_STOPPED) |
49 // v v
51 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_
52 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_
54 #include <map>
56 #include "base/memory/ref_counted.h"
57 #include "base/memory/weak_ptr.h"
58 #include "base/sequenced_task_runner_helpers.h"
59 #include "content/browser/renderer_host/media/video_capture_controller.h"
60 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
61 #include "content/common/content_export.h"
62 #include "content/public/browser/browser_message_filter.h"
63 #include "ipc/ipc_message.h"
65 namespace content {
66 class MediaStreamManager;
68 class CONTENT_EXPORT VideoCaptureHost
69 : public BrowserMessageFilter,
70 public VideoCaptureControllerEventHandler {
71 public:
72 explicit VideoCaptureHost(MediaStreamManager* media_stream_manager);
74 // BrowserMessageFilter implementation.
75 void OnChannelClosing() override;
76 void OnDestruct() const override;
77 bool OnMessageReceived(const IPC::Message& message) override;
79 // VideoCaptureControllerEventHandler implementation.
80 void OnError(VideoCaptureControllerID id) override;
81 void OnBufferCreated(VideoCaptureControllerID id,
82 base::SharedMemoryHandle handle,
83 int length,
84 int buffer_id) override;
85 void OnBufferCreated2(VideoCaptureControllerID id,
86 const std::vector<gfx::GpuMemoryBufferHandle>& handles,
87 const gfx::Size& size,
88 int buffer_id) override;
89 void OnBufferDestroyed(VideoCaptureControllerID id,
90 int buffer_id) override;
91 void OnBufferReady(VideoCaptureControllerID id,
92 int buffer_id,
93 const scoped_refptr<media::VideoFrame>& frame,
94 const base::TimeTicks& timestamp) override;
95 void OnEnded(VideoCaptureControllerID id) override;
97 private:
98 friend class BrowserThread;
99 friend class base::DeleteHelper<VideoCaptureHost>;
100 friend class MockVideoCaptureHost;
101 friend class VideoCaptureHostTest;
103 void DoError(VideoCaptureControllerID id);
104 void DoEnded(VideoCaptureControllerID id);
106 ~VideoCaptureHost() override;
108 // IPC message: Start capture on the VideoCaptureDevice referenced by
109 // |device_id|. |session_id| is an id created by VideoCaptureMessageFilter
110 // to identify a session between a VideoCaptureMessageFilter and a
111 // VideoCaptureHost.
112 void OnStartCapture(int device_id,
113 media::VideoCaptureSessionId session_id,
114 const media::VideoCaptureParams& params);
115 void OnControllerAdded(
116 int device_id,
117 const base::WeakPtr<VideoCaptureController>& controller);
119 // IPC message: Stop capture on device referenced by |device_id|.
120 void OnStopCapture(int device_id);
122 // IPC message: Pause capture on device referenced by |device_id|.
123 void OnPauseCapture(int device_id);
125 void OnResumeCapture(int device_id,
126 media::VideoCaptureSessionId session_id,
127 const media::VideoCaptureParams& params);
129 // IPC message: Called when a renderer is finished using a buffer. Notifies
130 // the controller.
131 void OnRendererFinishedWithBuffer(int device_id,
132 int buffer_id,
133 uint32 sync_point,
134 double consumer_resource_utilization);
136 // IPC message: Get supported formats referenced by |capture_session_id|.
137 // |device_id| is needed for message back-routing purposes.
138 void OnGetDeviceSupportedFormats(
139 int device_id,
140 media::VideoCaptureSessionId capture_session_id);
142 // IPC message: Get a device's currently in use format(s), referenced by
143 // |capture_session_id|. |device_id| is needed for message back-routing
144 // purposes.
145 void OnGetDeviceFormatsInUse(int device_id,
146 media::VideoCaptureSessionId capture_session_id);
148 // Deletes the controller and notifies the VideoCaptureManager. |on_error| is
149 // true if this is triggered by VideoCaptureControllerEventHandler::OnError.
150 void DeleteVideoCaptureController(VideoCaptureControllerID controller_id,
151 bool on_error);
153 MediaStreamManager* const media_stream_manager_;
155 typedef std::map<VideoCaptureControllerID,
156 base::WeakPtr<VideoCaptureController>> EntryMap;
158 // A map of VideoCaptureControllerID to the VideoCaptureController to which it
159 // is connected. An entry in this map holds a null controller while it is in
160 // the process of starting.
161 EntryMap entries_;
163 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHost);
166 } // namespace content
168 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_