• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 PPAPI_SHARED_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_
6 #define PPAPI_SHARED_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_
7 
8 #include <deque>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/shared_memory.h"
14 #include "ppapi/shared_impl/ppapi_shared_export.h"
15 
16 namespace ppapi {
17 
18 union MediaStreamBuffer;
19 
20 // This class is used by both read side and write side of a MediaStreamTrack to
21 // maintain a queue of buffers for reading or writing.
22 //
23 // An example:
24 //  1. The writer calls the writer's |buffer_manager_.Dequeue()| to get a free
25 //     buffer.
26 //  2. The writer fills data into the buffer.
27 //  3. The writer sends the buffer index to the reader via an IPC message.
28 //  4. The reader receives the buffer index and calls the reader's
29 //     |buffer_buffer.Enqueue()| to put the buffer into the read's queue.
30 //  5. The reader calls reader's |buffer_buffer_.Dequeue()| to get a received
31 //     buffer.
32 //  6. When the buffer from the step 5 is consumed, the reader sends the buffer
33 //     index back to writer via an IPC message.
34 //  7. The writer receives the buffer index and puts it back to the writer's
35 //     free buffer queue by calling the writer's |buffer_manager_.Enqueue()|.
36 //  8. Go back to step 1.
37 class PPAPI_SHARED_EXPORT MediaStreamBufferManager {
38  public:
39   class PPAPI_SHARED_EXPORT Delegate {
40    public:
41     virtual ~Delegate();
42     // It is called when a new buffer is enqueued.
43     virtual void OnNewBufferEnqueued();
44   };
45 
46   // MediaStreamBufferManager doesn't own |delegate|, the caller should keep
47   // it alive during the MediaStreamBufferManager's lifecycle.
48   explicit MediaStreamBufferManager(Delegate* delegate);
49 
50   ~MediaStreamBufferManager();
51 
number_of_buffers()52   int32_t number_of_buffers() const { return number_of_buffers_; }
53 
buffer_size()54   int32_t buffer_size() const { return buffer_size_; }
55 
56   // Initializes shared memory for buffers transmission.
57   bool SetBuffers(int32_t number_of_buffers,
58                   int32_t buffer_size,
59                   scoped_ptr<base::SharedMemory> shm,
60                   bool enqueue_all_buffers);
61 
62   // Dequeues a buffer from |buffer_queue_|.
63   int32_t DequeueBuffer();
64 
65   // Dequeues all the buffers from |buffer_queue_|.
66   std::vector<int32_t> DequeueBuffers();
67 
68   // Puts a buffer into |buffer_queue_|.
69   void EnqueueBuffer(int32_t index);
70 
71   // Gets the buffer address for the given buffer index.
72   MediaStreamBuffer* GetBufferPointer(int32_t index);
73 
74  private:
75   Delegate* delegate_;
76 
77   // A queue of buffer indices.
78   std::deque<int32_t> buffer_queue_;
79 
80   // A vector of buffer pointers. It is used for index to pointer converting.
81   std::vector<MediaStreamBuffer*> buffers_;
82 
83   // The buffer size in bytes.
84   int32_t buffer_size_;
85 
86   // The number of buffers in the shared memory.
87   int32_t number_of_buffers_;
88 
89   // A memory block shared between renderer process and plugin process.
90   scoped_ptr<base::SharedMemory> shm_;
91 
92   DISALLOW_COPY_AND_ASSIGN(MediaStreamBufferManager);
93 };
94 
95 }  // namespace ppapi
96 
97 #endif  // PPAPI_SHAERD_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_
98