Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: content/browser/renderer_host/media/video_capture_buffer_pool.h

Issue 2308533003: Break tight coupling of VideoCaptureDeviceClient to renderer_host (Closed)
Patch Set: mcasas' comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
11 11
12 #include "base/files/file.h" 12 #include "base/files/file.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory.h" 15 #include "base/memory/shared_memory.h"
16 #include "base/process/process.h" 16 #include "base/process/process.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/common/content_export.h" 19 #include "content/common/content_export.h"
20 #include "media/base/video_capture_types.h" 20 #include "media/base/video_capture_types.h"
21 #include "media/base/video_frame.h" 21 #include "media/base/video_frame.h"
22 #include "ui/gfx/geometry/size.h" 22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/gpu_memory_buffer.h" 23 #include "ui/gfx/gpu_memory_buffer.h"
24 24
25 namespace content { 25 namespace content {
26 26
27 class VideoCaptureBufferPoolBufferHandle {
28 public:
29 virtual ~VideoCaptureBufferPoolBufferHandle() {}
30 virtual gfx::Size dimensions() const = 0;
31 virtual size_t mapped_size() const = 0;
32 virtual void* data(int plane) = 0;
33 virtual ClientBuffer AsClientBuffer(int plane) = 0;
34 #if defined(OS_POSIX) && !defined(OS_MACOSX)
35 virtual base::FileDescriptor AsPlatformFile() = 0;
36 #endif
37 };
38
39 class CONTENT_EXPORT ProducerVideoCaptureBufferPool
miu 2016/09/12 20:16:46 The naming feels reversed to me: Shouldn't the int
chfremer 2016/09/12 22:23:33 I understand why it feels reversed. The reason I
chfremer 2016/09/12 23:57:10 Had an offline discussion with mcasas@ and emircan
chfremer 2016/09/13 00:30:57 One more thing maybe as a link to the "literature"
miu 2016/09/13 00:33:29 OIC now. Hmm...any other names I can think of soun
40 : public base::RefCountedThreadSafe<ProducerVideoCaptureBufferPool> {
miu 2016/09/12 20:16:46 I looked in the design doc, but I could not find a
chfremer 2016/09/12 22:23:33 My understanding of how things currently work is d
miu 2016/09/13 00:33:29 My mistake. I must've been remembering the design
41 public:
42 static constexpr int kInvalidId = -1;
43
44 // Try and obtain a BufferHandle for |buffer_id|.
45 virtual std::unique_ptr<VideoCaptureBufferPoolBufferHandle> GetBufferHandle(
46 int buffer_id) = 0;
47
48 // Reserve or allocate a buffer to support a packed frame of |dimensions| of
49 // pixel |format| and return its id. This will fail (returning kInvalidId) if
50 // the pool already is at its |count| limit of the number of allocations, and
51 // all allocated buffers are in use by the producer and/or consumers.
52 //
53 // If successful, the reserved buffer remains reserved (and writable by the
54 // producer) until ownership is transferred either to the consumer via
55 // HoldForConsumers(), or back to the pool with
56 // RelinquishProducerReservation().
57 //
58 // On occasion, this call will decide to free an old buffer to make room for a
59 // new allocation at a larger size. If so, the ID of the destroyed buffer is
60 // returned via |buffer_id_to_drop|.
61 virtual int ReserveForProducer(const gfx::Size& dimensions,
62 media::VideoPixelFormat format,
63 media::VideoPixelStorage storage,
64 int* buffer_id_to_drop) = 0;
65
66 // Indicate that a buffer held for the producer should be returned back to the
67 // pool without passing on to the consumer. This effectively is the opposite
68 // of ReserveForProducer().
69 virtual void RelinquishProducerReservation(int buffer_id) = 0;
70
71 // Attempt to reserve the same buffer that was relinquished in the last call
72 // to RelinquishProducerReservation(). If the buffer is not still being
73 // consumed, and has not yet been re-used since being consumed, and the
74 // specified |dimensions|, |format|, and |storage| agree with its last
75 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned.
76 //
77 // A producer may assume the content of the buffer has been preserved and may
78 // also make modifications.
79 virtual int ResurrectLastForProducer(const gfx::Size& dimensions,
80 media::VideoPixelFormat format,
81 media::VideoPixelStorage storage) = 0;
82
83 // Returns a snapshot of the current number of buffers in-use divided by the
84 // maximum |count_|.
85 virtual double GetBufferPoolUtilization() = 0;
miu 2016/09/12 20:16:46 Why was 'const' removed from the method signature?
chfremer 2016/09/12 22:23:33 This may be a topic worth discussing with a larger
miu 2016/09/13 00:33:29 I do have counter-arguments on this topic, but whi
chfremer 2016/09/14 16:26:01 Hehe, the answer to the "why?" was supposed to be
miu 2016/09/16 21:10:25 First of all, let me say: THANK YOU so much for ca
chfremer 2016/09/17 00:09:21 Thanks so much for these kind words and for spendi
86
87 protected:
88 virtual ~ProducerVideoCaptureBufferPool() {}
89
90 private:
91 friend class base::RefCountedThreadSafe<ProducerVideoCaptureBufferPool>;
92 DISALLOW_IMPLICIT_CONSTRUCTORS(ProducerVideoCaptureBufferPool);
93 };
94
27 // A thread-safe class that does the bookkeeping and lifetime management for a 95 // A thread-safe class that does the bookkeeping and lifetime management for a
28 // pool of pixel buffers cycled between an in-process producer (e.g. a 96 // pool of pixel buffers cycled between an in-process producer (e.g. a
29 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is 97 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is
30 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed 98 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed
31 // to outlive the controller if necessary. The pixel buffers may be backed by a 99 // to outlive the controller if necessary. The pixel buffers may be backed by a
32 // SharedMemory, but this is not compulsory. 100 // SharedMemory, but this is not compulsory.
33 // 101 //
34 // Producers get a buffer by calling ReserveForProducer(), and may pass on their 102 // Producers get a buffer by calling ReserveForProducer(), and may pass on their
35 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer 103 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer
36 // (without further processing) by calling RelinquishProducerReservation(). 104 // (without further processing) by calling RelinquishProducerReservation().
37 // Consumers signal that they are done with the buffer by calling 105 // Consumers signal that they are done with the buffer by calling
38 // RelinquishConsumerHold(). 106 // RelinquishConsumerHold().
39 // 107 //
40 // Buffers are allocated on demand, but there will never be more than |count| 108 // Buffers are allocated on demand, but there will never be more than |count|
41 // buffers in existence at any time. Buffers are identified by an int value 109 // buffers in existence at any time. Buffers are identified by an int value
42 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by 110 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by
43 // some methods to indicate failure. The active set of buffer ids may change 111 // some methods to indicate failure. The active set of buffer ids may change
44 // over the lifetime of the buffer pool, as existing buffers are freed and 112 // over the lifetime of the buffer pool, as existing buffers are freed and
45 // reallocated at larger size. When reallocation occurs, new buffer IDs will 113 // reallocated at larger size. When reallocation occurs, new buffer IDs will
46 // circulate. 114 // circulate.
47 class CONTENT_EXPORT VideoCaptureBufferPool 115 class CONTENT_EXPORT VideoCaptureBufferPool
48 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { 116 : public ProducerVideoCaptureBufferPool {
49 public: 117 public:
50 static const int kInvalidId; 118 using BufferHandle = VideoCaptureBufferPoolBufferHandle;
51
52 // Abstraction of a pool's buffer data buffer and size for clients.
53 // TODO(emircan): See https://crbug.com/521059, refactor this class.
54 class BufferHandle {
55 public:
56 virtual ~BufferHandle() {}
57 virtual gfx::Size dimensions() const = 0;
58 virtual size_t mapped_size() const = 0;
59 virtual void* data(int plane) = 0;
60 virtual ClientBuffer AsClientBuffer(int plane) = 0;
61 #if defined(OS_POSIX) && !defined(OS_MACOSX)
62 virtual base::FileDescriptor AsPlatformFile() = 0;
63 #endif
64 };
65 119
66 explicit VideoCaptureBufferPool(int count); 120 explicit VideoCaptureBufferPool(int count);
67 121
68 // One-time (per client/per-buffer) initialization to share a particular 122 // One-time (per client/per-buffer) initialization to share a particular
69 // buffer to a process. The shared handle is returned as |new_handle|. 123 // buffer to a process. The shared handle is returned as |new_handle|.
70 bool ShareToProcess(int buffer_id, 124 bool ShareToProcess(int buffer_id,
71 base::ProcessHandle process_handle, 125 base::ProcessHandle process_handle,
72 base::SharedMemoryHandle* new_handle); 126 base::SharedMemoryHandle* new_handle);
73 bool ShareToProcess2(int buffer_id, 127 bool ShareToProcess2(int buffer_id,
74 int plane, 128 int plane,
75 base::ProcessHandle process_handle, 129 base::ProcessHandle process_handle,
76 gfx::GpuMemoryBufferHandle* new_handle); 130 gfx::GpuMemoryBufferHandle* new_handle);
77 131
78 // Try and obtain a BufferHandle for |buffer_id|. 132 // Implementation of ProducerVideoCaptureBufferPool interface:
79 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id); 133 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id) override;
80
81 // Reserve or allocate a buffer to support a packed frame of |dimensions| of
82 // pixel |format| and return its id. This will fail (returning kInvalidId) if
83 // the pool already is at its |count| limit of the number of allocations, and
84 // all allocated buffers are in use by the producer and/or consumers.
85 //
86 // If successful, the reserved buffer remains reserved (and writable by the
87 // producer) until ownership is transferred either to the consumer via
88 // HoldForConsumers(), or back to the pool with
89 // RelinquishProducerReservation().
90 //
91 // On occasion, this call will decide to free an old buffer to make room for a
92 // new allocation at a larger size. If so, the ID of the destroyed buffer is
93 // returned via |buffer_id_to_drop|.
94 int ReserveForProducer(const gfx::Size& dimensions, 134 int ReserveForProducer(const gfx::Size& dimensions,
95 media::VideoPixelFormat format, 135 media::VideoPixelFormat format,
96 media::VideoPixelStorage storage, 136 media::VideoPixelStorage storage,
97 int* buffer_id_to_drop); 137 int* buffer_id_to_drop) override;
98 138 void RelinquishProducerReservation(int buffer_id) override;
99 // Indicate that a buffer held for the producer should be returned back to the 139 int ResurrectLastForProducer(const gfx::Size& dimensions,
100 // pool without passing on to the consumer. This effectively is the opposite 140 media::VideoPixelFormat format,
101 // of ReserveForProducer(). 141 media::VideoPixelStorage storage) override;
102 void RelinquishProducerReservation(int buffer_id); 142 double GetBufferPoolUtilization() override;
103 143
104 // Transfer a buffer from producer to consumer ownership. 144 // Transfer a buffer from producer to consumer ownership.
105 // |buffer_id| must be a buffer index previously returned by 145 // |buffer_id| must be a buffer index previously returned by
106 // ReserveForProducer(), and not already passed to HoldForConsumers(). 146 // ReserveForProducer(), and not already passed to HoldForConsumers().
107 void HoldForConsumers(int buffer_id, int num_clients); 147 void HoldForConsumers(int buffer_id, int num_clients);
108 148
109 // Indicate that one or more consumers are done with a particular buffer. This 149 // Indicate that one or more consumers are done with a particular buffer. This
110 // effectively is the opposite of HoldForConsumers(). Once the consumers are 150 // effectively is the opposite of HoldForConsumers(). Once the consumers are
111 // done, a buffer is returned to the pool for reuse. 151 // done, a buffer is returned to the pool for reuse.
112 void RelinquishConsumerHold(int buffer_id, int num_clients); 152 void RelinquishConsumerHold(int buffer_id, int num_clients);
113 153
114 // Attempt to reserve the same buffer that was relinquished in the last call
115 // to RelinquishProducerReservation(). If the buffer is not still being
116 // consumed, and has not yet been re-used since being consumed, and the
117 // specified |dimensions|, |format|, and |storage| agree with its last
118 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned.
119 //
120 // A producer may assume the content of the buffer has been preserved and may
121 // also make modifications.
122 int ResurrectLastForProducer(const gfx::Size& dimensions,
123 media::VideoPixelFormat format,
124 media::VideoPixelStorage storage);
125
126 // Returns a snapshot of the current number of buffers in-use divided by the
127 // maximum |count_|.
128 double GetBufferPoolUtilization() const;
129
130 private: 154 private:
131 class GpuMemoryBufferTracker; 155 class GpuMemoryBufferTracker;
132 class SharedMemTracker; 156 class SharedMemTracker;
133 // Generic class to keep track of the state of a given mappable resource. Each 157 // Generic class to keep track of the state of a given mappable resource. Each
134 // Tracker carries indication of pixel format and storage type. 158 // Tracker carries indication of pixel format and storage type.
135 class Tracker { 159 class Tracker {
136 public: 160 public:
137 static std::unique_ptr<Tracker> CreateTracker( 161 static std::unique_ptr<Tracker> CreateTracker(
138 media::VideoPixelStorage storage); 162 media::VideoPixelStorage storage);
139 163
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 media::VideoPixelStorage storage_type_; 210 media::VideoPixelStorage storage_type_;
187 211
188 // Indicates whether this Tracker is currently referenced by the producer. 212 // Indicates whether this Tracker is currently referenced by the producer.
189 bool held_by_producer_; 213 bool held_by_producer_;
190 214
191 // Number of consumer processes which hold this Tracker. 215 // Number of consumer processes which hold this Tracker.
192 int consumer_hold_count_; 216 int consumer_hold_count_;
193 }; 217 };
194 218
195 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; 219 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>;
196 virtual ~VideoCaptureBufferPool(); 220 ~VideoCaptureBufferPool() override;
197 221
198 int ReserveForProducerInternal(const gfx::Size& dimensions, 222 int ReserveForProducerInternal(const gfx::Size& dimensions,
199 media::VideoPixelFormat format, 223 media::VideoPixelFormat format,
200 media::VideoPixelStorage storage, 224 media::VideoPixelStorage storage,
201 int* tracker_id_to_drop); 225 int* tracker_id_to_drop);
202 226
203 Tracker* GetTracker(int buffer_id); 227 Tracker* GetTracker(int buffer_id);
204 228
205 // The max number of buffers that the pool is allowed to have at any moment. 229 // The max number of buffers that the pool is allowed to have at any moment.
206 const int count_; 230 const int count_;
(...skipping 11 matching lines...) Expand all
218 // The buffers, indexed by the first parameter, a buffer id. 242 // The buffers, indexed by the first parameter, a buffer id.
219 using TrackerMap = std::map<int, Tracker*>; 243 using TrackerMap = std::map<int, Tracker*>;
220 TrackerMap trackers_; 244 TrackerMap trackers_;
221 245
222 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); 246 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool);
223 }; 247 };
224 248
225 } // namespace content 249 } // namespace content
226 250
227 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ 251 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698