Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 5 #ifndef MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | 6 #define MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <map> | |
| 11 | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/shared_memory.h" | |
| 16 #include "base/process/process.h" | |
| 17 #include "base/synchronization/lock.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "content/common/content_export.h" | |
| 20 #include "media/base/video_capture_types.h" | 9 #include "media/base/video_capture_types.h" |
| 21 #include "media/base/video_frame.h" | 10 #include "media/capture/capture_export.h" |
| 11 #include "media/capture/video/video_capture_buffer_handle.h" | |
|
mcasas
2016/09/27 00:12:57
Nit: VideoCaptureBufferHandle can be fwd declared?
chfremer
2016/09/27 22:32:27
Done.
| |
| 22 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 23 #include "ui/gfx/gpu_memory_buffer.h" | 13 #include "ui/gfx/gpu_memory_buffer.h" |
| 24 | 14 |
| 25 namespace content { | 15 namespace media { |
| 26 | 16 |
| 27 class VideoCaptureBufferHandle; | 17 // A thread-safe class that does the bookkeeping and lifetime management for a |
| 28 class VideoCaptureBufferTracker; | 18 // pool of pixel buffers cycled between an in-process producer (e.g. a |
| 29 | 19 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is |
| 30 class CONTENT_EXPORT VideoCaptureBufferPool | 20 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed |
| 21 // to outlive the controller if necessary. The pixel buffers may be backed by a | |
| 22 // SharedMemory, but this is not compulsory. | |
| 23 // | |
| 24 // Producers get a buffer by calling ReserveForProducer(), and may pass on their | |
| 25 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer | |
| 26 // (without further processing) by calling RelinquishProducerReservation(). | |
| 27 // Consumers signal that they are done with the buffer by calling | |
| 28 // RelinquishConsumerHold(). | |
| 29 // | |
| 30 // Buffers are allocated on demand, but there will never be more than |count| | |
| 31 // buffers in existence at any time. Buffers are identified by an int value | |
| 32 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | |
| 33 // some methods to indicate failure. The active set of buffer ids may change | |
| 34 // over the lifetime of the buffer pool, as existing buffers are freed and | |
| 35 // reallocated at larger size. When reallocation occurs, new buffer IDs will | |
| 36 // circulate. | |
| 37 class CAPTURE_EXPORT VideoCaptureBufferPool | |
| 31 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | 38 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { |
| 32 public: | 39 public: |
| 33 static constexpr int kInvalidId = -1; | 40 static constexpr int kInvalidId = -1; |
| 34 | 41 |
| 35 // One-time (per client/per-buffer) initialization to share a particular | 42 // One-time (per client/per-buffer) initialization to share a particular |
| 36 // buffer to a process. The shared handle is returned as |new_handle|. | 43 // buffer to a process. The shared handle is returned as |new_handle|. |
| 37 virtual bool ShareToProcess(int buffer_id, | 44 virtual bool ShareToProcess(int buffer_id, |
| 38 base::ProcessHandle process_handle, | 45 base::ProcessHandle process_handle, |
| 39 base::SharedMemoryHandle* new_handle) = 0; | 46 base::SharedMemoryHandle* new_handle) = 0; |
| 40 virtual bool ShareToProcess2(int buffer_id, | 47 virtual bool ShareToProcess2(int buffer_id, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 // done, a buffer is returned to the pool for reuse. | 102 // done, a buffer is returned to the pool for reuse. |
| 96 virtual void RelinquishConsumerHold(int buffer_id, int num_clients) = 0; | 103 virtual void RelinquishConsumerHold(int buffer_id, int num_clients) = 0; |
| 97 | 104 |
| 98 protected: | 105 protected: |
| 99 virtual ~VideoCaptureBufferPool() {} | 106 virtual ~VideoCaptureBufferPool() {} |
| 100 | 107 |
| 101 private: | 108 private: |
| 102 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | 109 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; |
| 103 }; | 110 }; |
| 104 | 111 |
| 105 // A thread-safe class that does the bookkeeping and lifetime management for a | 112 } // namespace media |
| 106 // pool of pixel buffers cycled between an in-process producer (e.g. a | |
| 107 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is | |
| 108 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed | |
| 109 // to outlive the controller if necessary. The pixel buffers may be backed by a | |
| 110 // SharedMemory, but this is not compulsory. | |
| 111 // | |
| 112 // Producers get a buffer by calling ReserveForProducer(), and may pass on their | |
| 113 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer | |
| 114 // (without further processing) by calling RelinquishProducerReservation(). | |
| 115 // Consumers signal that they are done with the buffer by calling | |
| 116 // RelinquishConsumerHold(). | |
| 117 // | |
| 118 // Buffers are allocated on demand, but there will never be more than |count| | |
| 119 // buffers in existence at any time. Buffers are identified by an int value | |
| 120 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by | |
| 121 // some methods to indicate failure. The active set of buffer ids may change | |
| 122 // over the lifetime of the buffer pool, as existing buffers are freed and | |
| 123 // reallocated at larger size. When reallocation occurs, new buffer IDs will | |
| 124 // circulate. | |
| 125 class CONTENT_EXPORT VideoCaptureBufferPoolImpl | |
| 126 : public VideoCaptureBufferPool { | |
| 127 public: | |
| 128 explicit VideoCaptureBufferPoolImpl(int count); | |
| 129 | 113 |
| 130 // Implementation of VideoCaptureBufferPool interface: | 114 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_BUFFER_POOL_H_ |
| 131 bool ShareToProcess(int buffer_id, | |
| 132 base::ProcessHandle process_handle, | |
| 133 base::SharedMemoryHandle* new_handle) override; | |
| 134 bool ShareToProcess2(int buffer_id, | |
| 135 int plane, | |
| 136 base::ProcessHandle process_handle, | |
| 137 gfx::GpuMemoryBufferHandle* new_handle) override; | |
| 138 std::unique_ptr<VideoCaptureBufferHandle> GetBufferHandle( | |
| 139 int buffer_id) override; | |
| 140 int ReserveForProducer(const gfx::Size& dimensions, | |
| 141 media::VideoPixelFormat format, | |
| 142 media::VideoPixelStorage storage, | |
| 143 int* buffer_id_to_drop) override; | |
| 144 void RelinquishProducerReservation(int buffer_id) override; | |
| 145 int ResurrectLastForProducer(const gfx::Size& dimensions, | |
| 146 media::VideoPixelFormat format, | |
| 147 media::VideoPixelStorage storage) override; | |
| 148 double GetBufferPoolUtilization() const override; | |
| 149 void HoldForConsumers(int buffer_id, int num_clients) override; | |
| 150 void RelinquishConsumerHold(int buffer_id, int num_clients) override; | |
| 151 | |
| 152 private: | |
| 153 friend class base::RefCountedThreadSafe<VideoCaptureBufferPoolImpl>; | |
| 154 ~VideoCaptureBufferPoolImpl() override; | |
| 155 | |
| 156 int ReserveForProducerInternal(const gfx::Size& dimensions, | |
| 157 media::VideoPixelFormat format, | |
| 158 media::VideoPixelStorage storage, | |
| 159 int* tracker_id_to_drop); | |
| 160 | |
| 161 VideoCaptureBufferTracker* GetTracker(int buffer_id); | |
| 162 | |
| 163 // The max number of buffers that the pool is allowed to have at any moment. | |
| 164 const int count_; | |
| 165 | |
| 166 // Protects everything below it. | |
| 167 mutable base::Lock lock_; | |
| 168 | |
| 169 // The ID of the next buffer. | |
| 170 int next_buffer_id_; | |
| 171 | |
| 172 // The ID of the buffer last relinquished by the producer (a candidate for | |
| 173 // resurrection). | |
| 174 int last_relinquished_buffer_id_; | |
| 175 | |
| 176 // The buffers, indexed by the first parameter, a buffer id. | |
| 177 using TrackerMap = std::map<int, VideoCaptureBufferTracker*>; | |
| 178 TrackerMap trackers_; | |
| 179 | |
| 180 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPoolImpl); | |
| 181 }; | |
| 182 | |
| 183 } // namespace content | |
| 184 | |
| 185 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | |
| OLD | NEW |