OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // VideoCaptureController is the glue between VideoCaptureHost, | 5 // VideoCaptureController is the glue between VideoCaptureHost, |
6 // VideoCaptureManager and VideoCaptureDevice. | 6 // VideoCaptureManager and VideoCaptureDevice. |
7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and | 7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and |
8 // is responsible for keeping track of TransportDIBs and filling them with I420 | 8 // is responsible for keeping track of shared DIBs and filling them with I420 |
9 // video frames for IPC communication between VideoCaptureHost and | 9 // video frames for IPC communication between VideoCaptureHost and |
10 // VideoCaptureMessageFilter. | 10 // VideoCaptureMessageFilter. |
11 // It implements media::VideoCaptureDevice::EventHandler to get video frames | 11 // It implements media::VideoCaptureDevice::EventHandler to get video frames |
12 // from a VideoCaptureDevice object and do color conversion straight into the | 12 // from a VideoCaptureDevice object and do color conversion straight into the |
13 // TransportDIBs to avoid a memory copy. | 13 // shared DIBs to avoid a memory copy. |
14 // It serves multiple VideoCaptureControllerEventHandlers. | |
14 | 15 |
15 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 16 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
16 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 17 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
17 | 18 |
18 #include <list> | 19 #include <list> |
19 #include <map> | 20 #include <map> |
20 | 21 |
21 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
22 #include "base/process.h" | 23 #include "base/process.h" |
23 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
24 #include "base/task.h" | 25 #include "base/task.h" |
25 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 26 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
27 #include "media/video/capture/video_capture.h" | |
26 #include "media/video/capture/video_capture_device.h" | 28 #include "media/video/capture/video_capture_device.h" |
27 #include "media/video/capture/video_capture_types.h" | 29 #include "media/video/capture/video_capture_types.h" |
28 #include "ui/gfx/surface/transport_dib.h" | |
29 | 30 |
30 namespace media_stream { | 31 namespace media_stream { |
31 class VideoCaptureManager; | 32 class VideoCaptureManager; |
32 } // namespace media_stream | 33 } // namespace media_stream |
33 | 34 |
34 class VideoCaptureController | 35 class VideoCaptureController |
35 : public base::RefCountedThreadSafe<VideoCaptureController>, | 36 : public base::RefCountedThreadSafe<VideoCaptureController>, |
36 public media::VideoCaptureDevice::EventHandler { | 37 public media::VideoCaptureDevice::EventHandler { |
37 public: | 38 public: |
38 VideoCaptureController( | 39 VideoCaptureController( |
39 const VideoCaptureControllerID& id, | |
40 base::ProcessHandle render_process, | |
41 VideoCaptureControllerEventHandler* event_handler, | |
42 media_stream::VideoCaptureManager* video_capture_manager); | 40 media_stream::VideoCaptureManager* video_capture_manager); |
43 virtual ~VideoCaptureController(); | 41 virtual ~VideoCaptureController(); |
44 | 42 |
45 // Starts video capturing and tries to use the resolution specified in | 43 // Start video capturing and try to use the resolution specified in |
46 // params. | 44 // |params|. |
47 // When capturing has started VideoCaptureControllerEventHandler::OnFrameInfo | 45 // When capturing has started, the |event_handler| receives a call OnFrameInfo |
48 // is called with resolution that best matches the requested that the video | 46 // with resolution that best matches the requested that the video |
49 // capture device support. | 47 // capture device support. |
50 void StartCapture(const media::VideoCaptureParams& params); | 48 void StartCapture(const VideoCaptureControllerID& id, |
49 VideoCaptureControllerEventHandler* event_handler, | |
50 base::ProcessHandle render_process, | |
51 const media::VideoCaptureParams& params); | |
51 | 52 |
52 // Stop video capture. | 53 // Stop video capture. |
53 // When the capture is stopped and all TransportDIBS have been returned | 54 // When the capture is stopped and all DIBs have been returned, |
54 // VideoCaptureControllerEventHandler::OnReadyToDelete will be called. | 55 // VideoCaptureControllerEventHandler::OnReadyToDelete will be called. |
55 // |stopped_cb| may be NULL. But a non-NULL Closure can be used to get | 56 // |force_buffer_return| allows controller to take back all buffers used |
56 // a notification when the device is stopped, regardless of | 57 // by |event_handler|. |
57 // VideoCaptureController's state. | 58 void StopCapture(const VideoCaptureControllerID& id, |
58 void StopCapture(base::Closure stopped_cb); | 59 VideoCaptureControllerEventHandler* event_handler, |
60 bool force_buffer_return); | |
59 | 61 |
60 // Return a buffer previously given in | 62 // Return a buffer previously given in |
61 // VideoCaptureControllerEventHandler::OnBufferReady. | 63 // VideoCaptureControllerEventHandler::OnBufferReady. |
62 void ReturnBuffer(int buffer_id); | 64 void ReturnBuffer(const VideoCaptureControllerID& id, |
65 VideoCaptureControllerEventHandler* event_handler, | |
66 int buffer_id); | |
63 | 67 |
64 // Implement media::VideoCaptureDevice::EventHandler. | 68 // Implement media::VideoCaptureDevice::EventHandler. |
65 virtual void OnIncomingCapturedFrame(const uint8* data, | 69 virtual void OnIncomingCapturedFrame(const uint8* data, |
66 int length, | 70 int length, |
67 base::Time timestamp); | 71 base::Time timestamp); |
68 virtual void OnError(); | 72 virtual void OnError(); |
69 virtual void OnFrameInfo(const media::VideoCaptureDevice::Capability& info); | 73 virtual void OnFrameInfo(const media::VideoCaptureDevice::Capability& info); |
74 virtual void OnDeviceState(bool in_use); | |
70 | 75 |
71 private: | 76 private: |
72 // Called by VideoCaptureManager when a device have been stopped. | 77 struct ControllerClient { |
scherkus (not reviewing)
2011/10/19 18:02:21
nit: you can forward declare this and move the imp
wjia(left Chromium)
2011/10/21 00:56:13
Done.
| |
73 void OnDeviceStopped(base::Closure stopped_cb); | 78 ControllerClient(const VideoCaptureControllerID& id, |
79 VideoCaptureControllerEventHandler* handler, | |
80 base::ProcessHandle render_process, | |
81 const media::VideoCaptureParams& params); | |
82 ~ControllerClient(); | |
83 | |
84 // ID used for identifying this object. | |
85 VideoCaptureControllerID controller_id; | |
86 VideoCaptureControllerEventHandler* event_handler; | |
87 // Handle to the render process that will receive the DIBs. | |
88 base::ProcessHandle render_process_handle; | |
89 const media::VideoCaptureParams parameters; | |
90 // Buffers used by this client. | |
91 std::list<int> buffers; | |
92 bool report_ready_to_delete; | |
perkj_chrome
2011/10/17 08:39:43
can you explain how report_ready_to_delete is used
wjia(left Chromium)
2011/10/21 00:56:13
When controller client calls StopCapture, but it h
| |
93 }; | |
94 typedef std::list<ControllerClient*> ControllerClients; | |
95 | |
96 // Worker functions on IO thread. | |
97 void DoIncomingCapturedFrameOnIOThread(int buffer_id, base::Time timestamp); | |
98 void DoFrameInfoOnIOThread(const media::VideoCaptureDevice::Capability info); | |
99 void DoErrorOnIOThread(); | |
100 void DoDeviceStateOnIOThread(bool in_use); | |
101 | |
102 // Send frame info and init buffers to |client|. | |
103 void SendFrameInfoAndBuffers(ControllerClient* client); | |
104 | |
105 // Helpers. | |
106 ControllerClients::iterator FindClient( | |
107 const VideoCaptureControllerID& id, | |
108 VideoCaptureControllerEventHandler* handler); | |
74 | 109 |
75 // Lock to protect free_dibs_ and owned_dibs_. | 110 // Lock to protect free_dibs_ and owned_dibs_. |
76 base::Lock lock_; | 111 base::Lock lock_; |
77 // Handle to the render process that will receive the DIBs. | |
78 base::ProcessHandle render_handle_; | |
79 bool report_ready_to_delete_; | |
80 typedef std::list<int> DIBHandleList; | |
81 typedef std::map<int, base::SharedMemory*> DIBMap; | |
82 | 112 |
83 // Free DIBS that can be filled with video frames. | 113 typedef std::list<int /*buffer_id*/> DIBHandleList; |
114 // Free DIBs that can be filled with video frames. | |
115 // Modified on both IO and device threads. | |
84 DIBHandleList free_dibs_; | 116 DIBHandleList free_dibs_; |
85 | 117 |
86 // All DIBS created by this object. | 118 typedef std::map<int /*buffer_id*/, base::SharedMemory*> DIBMap; |
119 // All DIBs created by this object. | |
120 // Modified on only IO thread. | |
perkj_chrome
2011/10/17 08:39:43
nit: sentence
wjia(left Chromium)
2011/10/21 00:56:13
Done.
| |
87 DIBMap owned_dibs_; | 121 DIBMap owned_dibs_; |
88 VideoCaptureControllerEventHandler* event_handler_; | 122 |
123 typedef std::map<int /*buffer_id*/, int /*count*/> UsedDIBsCount; | |
124 // Keep count of clients to which a DIB has been sent. | |
125 // Modified on only IO thread. | |
126 UsedDIBsCount used_dibs_count_; | |
127 | |
128 // All clients served by this controller. | |
129 ControllerClients controller_clients_; | |
89 | 130 |
90 // The parameter that was requested when starting the capture device. | 131 // The parameter that was requested when starting the capture device. |
91 media::VideoCaptureParams params_; | 132 media::VideoCaptureParams params_; |
92 | 133 |
93 // ID used for identifying this object. | |
94 VideoCaptureControllerID id_; | |
95 media::VideoCaptureDevice::Capability frame_info_; | 134 media::VideoCaptureDevice::Capability frame_info_; |
135 bool frame_info_available_; | |
96 | 136 |
97 media_stream::VideoCaptureManager* video_capture_manager_; | 137 media_stream::VideoCaptureManager* video_capture_manager_; |
98 | 138 |
139 media::VideoCapture::State state_; | |
140 | |
99 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); | 141 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController); |
100 }; | 142 }; |
101 | 143 |
102 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 144 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
OLD | NEW |