| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_BUFFER_POOL_H_ | |
| 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" | |
| 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" | |
| 21 #include "media/base/video_frame.h" | |
| 22 #include "ui/gfx/geometry/size.h" | |
| 23 #include "ui/gfx/gpu_memory_buffer.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 class VideoCaptureBufferHandle; | |
| 28 class VideoCaptureBufferTracker; | |
| 29 | |
| 30 class CONTENT_EXPORT VideoCaptureBufferPool | |
| 31 : public base::RefCountedThreadSafe<VideoCaptureBufferPool> { | |
| 32 public: | |
| 33 static constexpr int kInvalidId = -1; | |
| 34 | |
| 35 // One-time (per client/per-buffer) initialization to share a particular | |
| 36 // buffer to a process. The shared handle is returned as |new_handle|. | |
| 37 virtual bool ShareToProcess(int buffer_id, | |
| 38 base::ProcessHandle process_handle, | |
| 39 base::SharedMemoryHandle* new_handle) = 0; | |
| 40 virtual bool ShareToProcess2(int buffer_id, | |
| 41 int plane, | |
| 42 base::ProcessHandle process_handle, | |
| 43 gfx::GpuMemoryBufferHandle* new_handle) = 0; | |
| 44 | |
| 45 // Try and obtain a BufferHandle for |buffer_id|. | |
| 46 virtual std::unique_ptr<VideoCaptureBufferHandle> GetBufferHandle( | |
| 47 int buffer_id) = 0; | |
| 48 | |
| 49 // Reserve or allocate a buffer to support a packed frame of |dimensions| of | |
| 50 // pixel |format| and return its id. This will fail (returning kInvalidId) if | |
| 51 // the pool already is at its |count| limit of the number of allocations, and | |
| 52 // all allocated buffers are in use by the producer and/or consumers. | |
| 53 // | |
| 54 // If successful, the reserved buffer remains reserved (and writable by the | |
| 55 // producer) until ownership is transferred either to the consumer via | |
| 56 // HoldForConsumers(), or back to the pool with | |
| 57 // RelinquishProducerReservation(). | |
| 58 // | |
| 59 // On occasion, this call will decide to free an old buffer to make room for a | |
| 60 // new allocation at a larger size. If so, the ID of the destroyed buffer is | |
| 61 // returned via |buffer_id_to_drop|. | |
| 62 virtual int ReserveForProducer(const gfx::Size& dimensions, | |
| 63 media::VideoPixelFormat format, | |
| 64 media::VideoPixelStorage storage, | |
| 65 int* buffer_id_to_drop) = 0; | |
| 66 | |
| 67 // Indicate that a buffer held for the producer should be returned back to the | |
| 68 // pool without passing on to the consumer. This effectively is the opposite | |
| 69 // of ReserveForProducer(). | |
| 70 virtual void RelinquishProducerReservation(int buffer_id) = 0; | |
| 71 | |
| 72 // Attempt to reserve the same buffer that was relinquished in the last call | |
| 73 // to RelinquishProducerReservation(). If the buffer is not still being | |
| 74 // consumed, and has not yet been re-used since being consumed, and the | |
| 75 // specified |dimensions|, |format|, and |storage| agree with its last | |
| 76 // reservation, this will succeed. Otherwise, |kInvalidId| will be returned. | |
| 77 // | |
| 78 // A producer may assume the content of the buffer has been preserved and may | |
| 79 // also make modifications. | |
| 80 virtual int ResurrectLastForProducer(const gfx::Size& dimensions, | |
| 81 media::VideoPixelFormat format, | |
| 82 media::VideoPixelStorage storage) = 0; | |
| 83 | |
| 84 // Returns a snapshot of the current number of buffers in-use divided by the | |
| 85 // maximum |count_|. | |
| 86 virtual double GetBufferPoolUtilization() const = 0; | |
| 87 | |
| 88 // Transfer a buffer from producer to consumer ownership. | |
| 89 // |buffer_id| must be a buffer index previously returned by | |
| 90 // ReserveForProducer(), and not already passed to HoldForConsumers(). | |
| 91 virtual void HoldForConsumers(int buffer_id, int num_clients) = 0; | |
| 92 | |
| 93 // Indicate that one or more consumers are done with a particular buffer. This | |
| 94 // effectively is the opposite of HoldForConsumers(). Once the consumers are | |
| 95 // done, a buffer is returned to the pool for reuse. | |
| 96 virtual void RelinquishConsumerHold(int buffer_id, int num_clients) = 0; | |
| 97 | |
| 98 protected: | |
| 99 virtual ~VideoCaptureBufferPool() {} | |
| 100 | |
| 101 private: | |
| 102 friend class base::RefCountedThreadSafe<VideoCaptureBufferPool>; | |
| 103 }; | |
| 104 | |
| 105 // A thread-safe class that does the bookkeeping and lifetime management for a | |
| 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 | |
| 130 // Implementation of VideoCaptureBufferPool interface: | |
| 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 |