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

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' & emircan's 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
40 : public base::RefCountedThreadSafe<ProducerVideoCaptureBufferPool> {
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;
86
87 protected:
88 virtual ~ProducerVideoCaptureBufferPool() {}
89
90 private:
91 friend class base::RefCountedThreadSafe<ProducerVideoCaptureBufferPool>;
92 };
mcasas 2016/09/09 22:28:01 nit : add here a DISALLOW_IMPLICIT_CONSTRUCTORS(Cl
chfremer 2016/09/12 18:31:22 Done, but I have a question about it. Why is this
chfremer 2016/09/13 00:30:56 Hmm. I tried both, but neither of the macros seems
93
27 // A thread-safe class that does the bookkeeping and lifetime management for a 94 // 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 95 // 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 96 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is
30 // intended to be orchestrated by a VideoCaptureDevice::Client, but is designed 97 // 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 98 // to outlive the controller if necessary. The pixel buffers may be backed by a
32 // SharedMemory, but this is not compulsory. 99 // SharedMemory, but this is not compulsory.
33 // 100 //
34 // Producers get a buffer by calling ReserveForProducer(), and may pass on their 101 // Producers get a buffer by calling ReserveForProducer(), and may pass on their
35 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer 102 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer
36 // (without further processing) by calling RelinquishProducerReservation(). 103 // (without further processing) by calling RelinquishProducerReservation().
37 // Consumers signal that they are done with the buffer by calling 104 // Consumers signal that they are done with the buffer by calling
38 // RelinquishConsumerHold(). 105 // RelinquishConsumerHold().
39 // 106 //
40 // Buffers are allocated on demand, but there will never be more than |count| 107 // 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 108 // 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 109 // 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 110 // 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 111 // 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 112 // reallocated at larger size. When reallocation occurs, new buffer IDs will
46 // circulate. 113 // circulate.
47 class CONTENT_EXPORT VideoCaptureBufferPool 114 class CONTENT_EXPORT VideoCaptureBufferPool
48 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { 115 : public ProducerVideoCaptureBufferPool {
49 public: 116 public:
50 static const int kInvalidId; 117 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 118
66 explicit VideoCaptureBufferPool(int count); 119 explicit VideoCaptureBufferPool(int count);
67 120
68 // One-time (per client/per-buffer) initialization to share a particular 121 // One-time (per client/per-buffer) initialization to share a particular
69 // buffer to a process. The shared handle is returned as |new_handle|. 122 // buffer to a process. The shared handle is returned as |new_handle|.
70 bool ShareToProcess(int buffer_id, 123 bool ShareToProcess(int buffer_id,
71 base::ProcessHandle process_handle, 124 base::ProcessHandle process_handle,
72 base::SharedMemoryHandle* new_handle); 125 base::SharedMemoryHandle* new_handle);
73 bool ShareToProcess2(int buffer_id, 126 bool ShareToProcess2(int buffer_id,
74 int plane, 127 int plane,
75 base::ProcessHandle process_handle, 128 base::ProcessHandle process_handle,
76 gfx::GpuMemoryBufferHandle* new_handle); 129 gfx::GpuMemoryBufferHandle* new_handle);
77 130
78 // Try and obtain a BufferHandle for |buffer_id|. 131 // Implementation of ProducerVideoCaptureBufferPool interface:
79 std::unique_ptr<BufferHandle> GetBufferHandle(int buffer_id); 132 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, 133 int ReserveForProducer(const gfx::Size& dimensions,
95 media::VideoPixelFormat format, 134 media::VideoPixelFormat format,
96 media::VideoPixelStorage storage, 135 media::VideoPixelStorage storage,
97 int* buffer_id_to_drop); 136 int* buffer_id_to_drop) override;
98 137 void RelinquishProducerReservation(int buffer_id) override;
99 // Indicate that a buffer held for the producer should be returned back to the 138 int ResurrectLastForProducer(const gfx::Size& dimensions,
100 // pool without passing on to the consumer. This effectively is the opposite 139 media::VideoPixelFormat format,
101 // of ReserveForProducer(). 140 media::VideoPixelStorage storage) override;
102 void RelinquishProducerReservation(int buffer_id); 141 double GetBufferPoolUtilization() override;
103 142
104 // Transfer a buffer from producer to consumer ownership. 143 // Transfer a buffer from producer to consumer ownership.
105 // |buffer_id| must be a buffer index previously returned by 144 // |buffer_id| must be a buffer index previously returned by
106 // ReserveForProducer(), and not already passed to HoldForConsumers(). 145 // ReserveForProducer(), and not already passed to HoldForConsumers().
107 void HoldForConsumers(int buffer_id, int num_clients); 146 void HoldForConsumers(int buffer_id, int num_clients);
108 147
109 // Indicate that one or more consumers are done with a particular buffer. This 148 // Indicate that one or more consumers are done with a particular buffer. This
110 // effectively is the opposite of HoldForConsumers(). Once the consumers are 149 // effectively is the opposite of HoldForConsumers(). Once the consumers are
111 // done, a buffer is returned to the pool for reuse. 150 // done, a buffer is returned to the pool for reuse.
112 void RelinquishConsumerHold(int buffer_id, int num_clients); 151 void RelinquishConsumerHold(int buffer_id, int num_clients);
113 152
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: 153 private:
131 class GpuMemoryBufferTracker; 154 class GpuMemoryBufferTracker;
132 class SharedMemTracker; 155 class SharedMemTracker;
133 // Generic class to keep track of the state of a given mappable resource. Each 156 // Generic class to keep track of the state of a given mappable resource. Each
134 // Tracker carries indication of pixel format and storage type. 157 // Tracker carries indication of pixel format and storage type.
135 class Tracker { 158 class Tracker {
136 public: 159 public:
137 static std::unique_ptr<Tracker> CreateTracker( 160 static std::unique_ptr<Tracker> CreateTracker(
138 media::VideoPixelStorage storage); 161 media::VideoPixelStorage storage);
139 162
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 media::VideoPixelStorage storage_type_; 209 media::VideoPixelStorage storage_type_;
187 210
188 // Indicates whether this Tracker is currently referenced by the producer. 211 // Indicates whether this Tracker is currently referenced by the producer.
189 bool held_by_producer_; 212 bool held_by_producer_;
190 213
191 // Number of consumer processes which hold this Tracker. 214 // Number of consumer processes which hold this Tracker.
192 int consumer_hold_count_; 215 int consumer_hold_count_;
193 }; 216 };
194 217
195 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; 218 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>;
196 virtual ~VideoCaptureBufferPool(); 219 ~VideoCaptureBufferPool() override;
197 220
198 int ReserveForProducerInternal(const gfx::Size& dimensions, 221 int ReserveForProducerInternal(const gfx::Size& dimensions,
199 media::VideoPixelFormat format, 222 media::VideoPixelFormat format,
200 media::VideoPixelStorage storage, 223 media::VideoPixelStorage storage,
201 int* tracker_id_to_drop); 224 int* tracker_id_to_drop);
202 225
203 Tracker* GetTracker(int buffer_id); 226 Tracker* GetTracker(int buffer_id);
204 227
205 // The max number of buffers that the pool is allowed to have at any moment. 228 // The max number of buffers that the pool is allowed to have at any moment.
206 const int count_; 229 const int count_;
(...skipping 11 matching lines...) Expand all
218 // The buffers, indexed by the first parameter, a buffer id. 241 // The buffers, indexed by the first parameter, a buffer id.
219 using TrackerMap = std::map<int, Tracker*>; 242 using TrackerMap = std::map<int, Tracker*>;
220 TrackerMap trackers_; 243 TrackerMap trackers_;
221 244
222 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); 245 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool);
223 }; 246 };
224 247
225 } // namespace content 248 } // namespace content
226 249
227 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ 250 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698