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