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.
5 #ifndef PPAPI_SHARED_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_
6 #define PPAPI_SHARED_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_
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"
18 union MediaStreamBuffer
;
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.
24 // 1. The writer calls the writer's |buffer_manager_.Dequeue()| to get a free
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
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
{
39 class PPAPI_SHARED_EXPORT Delegate
{
42 // It is called when a new buffer is enqueued.
43 virtual void OnNewBufferEnqueued();
46 // MediaStreamBufferManager doesn't own |delegate|, the caller should keep
47 // it alive during the MediaStreamBufferManager's lifecycle.
48 explicit MediaStreamBufferManager(Delegate
* delegate
);
50 ~MediaStreamBufferManager();
52 int32_t number_of_buffers() const { return number_of_buffers_
; }
54 int32_t buffer_size() const { return buffer_size_
; }
56 // Initializes shared memory for buffers transmission.
57 bool SetBuffers(int32_t number_of_buffers
,
59 scoped_ptr
<base::SharedMemory
> shm
,
60 bool enqueue_all_buffers
);
62 // Dequeues a buffer from |buffer_queue_|.
63 int32_t DequeueBuffer();
65 // Dequeues all the buffers from |buffer_queue_|.
66 std::vector
<int32_t> DequeueBuffers();
68 // Puts a buffer into |buffer_queue_|.
69 void EnqueueBuffer(int32_t index
);
71 // Gets the buffer address for the given buffer index.
72 MediaStreamBuffer
* GetBufferPointer(int32_t index
);
77 // A queue of buffer indices.
78 std::deque
<int32_t> buffer_queue_
;
80 // A vector of buffer pointers. It is used for index to pointer converting.
81 std::vector
<MediaStreamBuffer
*> buffers_
;
83 // The buffer size in bytes.
86 // The number of buffers in the shared memory.
87 int32_t number_of_buffers_
;
89 // A memory block shared between renderer process and plugin process.
90 scoped_ptr
<base::SharedMemory
> shm_
;
92 DISALLOW_COPY_AND_ASSIGN(MediaStreamBufferManager
);
97 #endif // PPAPI_SHAERD_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_