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

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

Issue 1064703002: VideoCaptureBufferPool: Refactor to allow support of non-ShMem backed buffers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: miu@s comments Created 5 years, 8 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 <map> 8 #include <map>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/shared_memory.h" 13 #include "base/memory/shared_memory.h"
14 #include "base/process/process.h" 14 #include "base/process/process.h"
15 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
16 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
17 #include "media/base/video_capture_types.h"
18 #include "media/base/video_frame.h"
17 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
18 20
19 namespace media {
20
21 class VideoFrame;
22
23 } // namespace media
24
25 namespace content { 21 namespace content {
26 22
27 // A thread-safe class that does the bookkeeping and lifetime management for a 23 // A thread-safe class that does the bookkeeping and lifetime management for a
28 // pool of shared-memory pixel buffers cycled between an in-process producer 24 // pool of pixel buffers cycled between an in-process producer (e.g. a
29 // (e.g. a VideoCaptureDevice) and a set of out-of-process consumers. The pool 25 // VideoCaptureDevice) and a set of out-of-process consumers. The pool is
30 // is intended to be orchestrated by a VideoCaptureController, but is designed 26 // intended to be orchestrated by a VideoCaptureController, but is designed
31 // to outlive the controller if necessary. 27 // to outlive the controller if necessary. The pixel buffers may be backed by a
28 // SharedMemory, but this is not compulsory.
32 // 29 //
33 // Producers get a buffer by calling ReserveForProducer(), and may pass on their 30 // Producers get a buffer by calling ReserveForProducer(), and may pass on their
34 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer 31 // ownership to the consumer by calling HoldForConsumers(), or drop the buffer
35 // (without further processing) by calling RelinquishProducerReservation(). 32 // (without further processing) by calling RelinquishProducerReservation().
36 // Consumers signal that they are done with the buffer by calling 33 // Consumers signal that they are done with the buffer by calling
37 // RelinquishConsumerHold(). 34 // RelinquishConsumerHold().
38 // 35 //
39 // Buffers are allocated on demand, but there will never be more than |count| 36 // Buffers are allocated on demand, but there will never be more than |count|
40 // buffers in existence at any time. Buffers are identified by an int value 37 // buffers in existence at any time. Buffers are identified by an int value
41 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by 38 // called |buffer_id|. -1 (kInvalidId) is never a valid ID, and is returned by
42 // some methods to indicate failure. The active set of buffer ids may change 39 // some methods to indicate failure. The active set of buffer ids may change
43 // over the lifetime of the buffer pool, as existing buffers are freed and 40 // over the lifetime of the buffer pool, as existing buffers are freed and
44 // reallocated at larger size. When reallocation occurs, new buffer IDs will 41 // reallocated at larger size. When reallocation occurs, new buffer IDs will
45 // circulate. 42 // circulate.
46 class CONTENT_EXPORT VideoCaptureBufferPool 43 class CONTENT_EXPORT VideoCaptureBufferPool
47 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { 44 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> {
48 public: 45 public:
49 static const int kInvalidId; 46 static const int kInvalidId;
50 explicit VideoCaptureBufferPool(int count); 47 explicit VideoCaptureBufferPool(int count);
51 48
52 // One-time (per client/per-buffer) initialization to share a particular 49 // One-time (per client/per-buffer) initialization to share a particular
53 // buffer to a process. The size of the allocation is returned as 50 // buffer to a process. The size of the allocation is returned as
54 // |memory_size|. 51 // |memory_size|.
55 base::SharedMemoryHandle ShareToProcess(int buffer_id, 52 base::SharedMemoryHandle ShareToProcess(int buffer_id,
56 base::ProcessHandle process_handle, 53 base::ProcessHandle process_handle,
57 size_t* memory_size); 54 size_t* memory_size);
58 55
59 // Query the memory parameters of |buffer_id|. Fills in parameters in the 56 // Query the memory parameters of |buffer_id|. Fills in parameters in the
60 // pointer arguments, and returns true iff the buffer exists. 57 // pointer arguments, and returns true iff the buffer exists.
61 bool GetBufferInfo(int buffer_id, void** memory, size_t* size); 58 bool GetBufferInfo(int buffer_id, void** storage, size_t* size);
62 59
63 // Reserve or allocate a buffer of at least |size| bytes and return its id. 60 // Reserve or allocate a buffer to support a frame of |dimensions| of pixel
miu 2015/04/08 22:26:14 nit: s/to support a frame/to support a packed fram
mcasas 2015/04/09 02:03:22 Done.
64 // This will fail (returning kInvalidId) if the pool already is at its |count| 61 // |format| and return its id. This will fail (returning kInvalidId) if the
65 // limit of the number of allocations, and all allocated buffers are in use by 62 // pool already is at its |count| limit of the number of allocations, and all
66 // the producer and/or consumers. 63 // allocated buffers are in use by the producer and/or consumers.
67 // 64 //
68 // If successful, the reserved buffer remains reserved (and writable by the 65 // If successful, the reserved buffer remains reserved (and writable by the
69 // producer) until ownership is transferred either to the consumer via 66 // producer) until ownership is transferred either to the consumer via
70 // HoldForConsumers(), or back to the pool with 67 // HoldForConsumers(), or back to the pool with
71 // RelinquishProducerReservation(). 68 // RelinquishProducerReservation().
72 // 69 //
73 // On occasion, this call will decide to free an old buffer to make room for a 70 // On occasions, this call will decide to free an old buffer to make room for
miu 2015/04/08 22:26:14 nit: English is weird, but this should be "occasio
mcasas 2015/04/09 02:03:22 Argh! Spanish got in the way ("En ocasiones"). Rev
74 // new allocation at a larger size. If so, the ID of the destroyed buffer is 71 // a new allocation at a larger size. If so, the ID of the destroyed buffer is
75 // returned via |buffer_id_to_drop|. 72 // returned via |buffer_id_to_drop|.
76 int ReserveForProducer(size_t size, int* buffer_id_to_drop); 73 int ReserveForProducer(media::VideoPixelFormat format,
74 const gfx::Size& dimensions,
75 int* buffer_id_to_drop);
77 76
78 // Indicate that a buffer held for the producer should be returned back to the 77 // Indicate that a buffer held for the producer should be returned back to the
79 // pool without passing on to the consumer. This effectively is the opposite 78 // pool without passing on to the consumer. This effectively is the opposite
80 // of ReserveForProducer(). 79 // of ReserveForProducer().
81 void RelinquishProducerReservation(int buffer_id); 80 void RelinquishProducerReservation(int buffer_id);
82 81
83 // Transfer a buffer from producer to consumer ownership. 82 // Transfer a buffer from producer to consumer ownership.
84 // |buffer_id| must be a buffer index previously returned by 83 // |buffer_id| must be a buffer index previously returned by
85 // ReserveForProducer(), and not already passed to HoldForConsumers(). 84 // ReserveForProducer(), and not already passed to HoldForConsumers().
86 void HoldForConsumers(int buffer_id, int num_clients); 85 void HoldForConsumers(int buffer_id, int num_clients);
87 86
88 // Indicate that one or more consumers are done with a particular buffer. This 87 // Indicate that one or more consumers are done with a particular buffer. This
89 // effectively is the opposite of HoldForConsumers(). Once the consumers are 88 // effectively is the opposite of HoldForConsumers(). Once the consumers are
90 // done, a buffer is returned to the pool for reuse. 89 // done, a buffer is returned to the pool for reuse.
91 void RelinquishConsumerHold(int buffer_id, int num_clients); 90 void RelinquishConsumerHold(int buffer_id, int num_clients);
92 91
93 int count() const { return count_; } 92 private:
93 class SharedMemTracker;
94 // Generic class to keep track of the state of a given mappable resource.
95 class Tracker {
96 public:
97 static scoped_ptr<Tracker> CreateTracker();
94 98
95 private: 99 Tracker() : held_by_producer_(false), consumer_hold_count_(0) {}
96 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; 100 virtual bool Init(media::VideoFrame::Format format,
101 const gfx::Size& dimensions) = 0;
102 virtual ~Tracker();
97 103
98 // Per-buffer state. 104 bool held_by_producer() const { return held_by_producer_; }
99 struct Buffer { 105 void set_held_by_producer(bool value) { held_by_producer_ = value; }
100 Buffer(); 106 int consumer_hold_count() const { return consumer_hold_count_; }
107 void set_consumer_hold_count(int value) { consumer_hold_count_ = value; }
101 108
102 // The memory created to be shared with renderer processes. 109 // Returns a void* to the underlying storage, be that a memory block for
103 base::SharedMemory shared_memory; 110 // Shared Memory, or a GpuMemoryBuffer.
111 virtual void* storage() = 0;
112 // Amount of bytes requested when first created. Can be zero if it does not
113 // need RAM, e.g. is allocated in GPU memory.
114 virtual size_t requested_size() = 0;
115 // The actual size of the underlying backing resource.
116 virtual size_t mapped_size() = 0;
104 117
105 // Tracks whether this buffer is currently referenced by the producer. 118 virtual bool ShareToProcess(base::ProcessHandle process_handle,
106 bool held_by_producer; 119 base::SharedMemoryHandle* new_handle) = 0;
107 120
108 // Number of consumer processes which hold this shared memory. 121 private:
109 int consumer_hold_count; 122 // Indicates whether this Tracker is currently referenced by the producer.
123 bool held_by_producer_;
124 // Number of consumer processes which hold this Tracker.
125 int consumer_hold_count_;
110 }; 126 };
111 127
112 typedef std::map<int, Buffer*> BufferMap; 128 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>;
113
114 virtual ~VideoCaptureBufferPool(); 129 virtual ~VideoCaptureBufferPool();
115 130
116 int ReserveForProducerInternal(size_t size, int* buffer_id_to_drop); 131 int ReserveForProducerInternal(media::VideoPixelFormat format,
132 const gfx::Size& dimensions,
133 int* tracker_id_to_drop);
117 134
118 Buffer* GetBuffer(int buffer_id); 135 Tracker* GetTracker(int buffer_id);
119 136
120 // The max number of buffers that the pool is allowed to have at any moment. 137 // The max number of buffers that the pool is allowed to have at any moment.
121 const int count_; 138 const int count_;
122 139
123 // Protects everything below it. 140 // Protects everything below it.
124 base::Lock lock_; 141 base::Lock lock_;
125 142
126 // The ID of the next buffer. 143 // The ID of the next buffer.
127 int next_buffer_id_; 144 int next_buffer_id_;
128 145
129 // The buffers, indexed by |buffer_id|. 146 // The buffers, indexed by the first parameter, a buffer id.
130 BufferMap buffers_; 147 using TrackerMap = std::map<int, Tracker*>;
148 TrackerMap trackers_;
131 149
132 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool); 150 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureBufferPool);
133 }; 151 };
134 152
135 } // namespace content 153 } // namespace content
136 154
137 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ 155 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698