1 #ifndef ANDROID_DVR_BUFFERHUBD_CONSUMER_QUEUE_CHANNEL_H_
2 #define ANDROID_DVR_BUFFERHUBD_CONSUMER_QUEUE_CHANNEL_H_
3 
4 #include <queue>
5 
6 #include <private/dvr/buffer_hub.h>
7 #include <private/dvr/bufferhub_rpc.h>
8 #include <private/dvr/consumer_channel.h>
9 #include <private/dvr/producer_queue_channel.h>
10 
11 namespace android {
12 namespace dvr {
13 
14 class ConsumerQueueChannel : public BufferHubChannel {
15  public:
16   using Message = pdx::Message;
17   using RemoteChannelHandle = pdx::RemoteChannelHandle;
18 
19   ConsumerQueueChannel(BufferHubService* service, int buffer_id, int channel_id,
20                        const std::shared_ptr<Channel>& producer, bool silent);
21   ~ConsumerQueueChannel() override;
22 
23   bool HandleMessage(Message& message) override;
24   void HandleImpulse(Message& message) override;
25 
26   BufferInfo GetBufferInfo() const override;
27 
28   // Called by ProdcuerQueueChannel to notify consumer queue that a new
29   // buffer has been allocated.
30   void RegisterNewBuffer(
31       const std::shared_ptr<ProducerChannel>& producer_channel,
32       size_t producer_slot);
33 
34   // Called after clients been signaled by service that new buffer has been
35   // allocated. Clients uses kOpConsumerQueueImportBuffers to import new
36   // consumer buffers and this handler returns a vector of fd representing
37   // BufferConsumers that clients can import.
38   pdx::Status<std::vector<std::pair<RemoteChannelHandle, size_t>>>
39   OnConsumerQueueImportBuffers(Message& message);
40 
41   void OnProducerClosed();
42 
43  private:
44   // Data structure to store relavant info of a newly allocated producer buffer
45   // so that consumer channel and buffer can be created later.
46   struct PendingBuffer {
PendingBufferPendingBuffer47     PendingBuffer(std::shared_ptr<ProducerChannel> channel, size_t slot,
48                   uint64_t mask) {
49       producer_channel = channel;
50       producer_slot = slot;
51       consumer_state_mask = mask;
52     }
53     PendingBuffer() = delete;
54 
55     std::weak_ptr<ProducerChannel> producer_channel;
56     size_t producer_slot;
57     uint64_t consumer_state_mask;
58   };
59 
60   std::shared_ptr<ProducerQueueChannel> GetProducer() const;
61 
62   // Pointer to the producer channel.
63   std::weak_ptr<Channel> producer_;
64 
65   // Tracks newly allocated buffer producers along with it's slot number.
66   std::queue<PendingBuffer> pending_buffer_slots_;
67 
68   // Tracks how many buffers have this queue imported.
69   size_t capacity_;
70 
71   // A silent queue does not signal or export buffers. It is only used to spawn
72   // another consumer queue.
73   bool silent_;
74 
75   ConsumerQueueChannel(const ConsumerQueueChannel&) = delete;
76   void operator=(const ConsumerQueueChannel&) = delete;
77 };
78 
79 }  // namespace dvr
80 }  // namespace android
81 
82 #endif  // ANDROID_DVR_BUFFERHUBD_CONSUMER_QUEUE_CHANNEL_H_
83