media_stream_buffer_manager.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 16namespace ppapi { 17 18union 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. 37class 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 52 int32_t number_of_buffers() const { return number_of_buffers_; } 53 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 // Puts a buffer into |buffer_queue_|. 66 void EnqueueBuffer(int32_t index); 67 68 // Gets the buffer address for the given buffer index. 69 MediaStreamBuffer* GetBufferPointer(int32_t index); 70 71 private: 72 Delegate* delegate_; 73 74 // A queue of buffer indices. 75 std::deque<int32_t> buffer_queue_; 76 77 // A vector of buffer pointers. It is used for index to pointer converting. 78 std::vector<MediaStreamBuffer*> buffers_; 79 80 // The buffer size in bytes. 81 int32_t buffer_size_; 82 83 // The number of buffers in the shared memory. 84 int32_t number_of_buffers_; 85 86 // A memory block shared between renderer process and plugin process. 87 scoped_ptr<base::SharedMemory> shm_; 88 89 DISALLOW_COPY_AND_ASSIGN(MediaStreamBufferManager); 90}; 91 92} // namespace ppapi 93 94#endif // PPAPI_SHAERD_IMPL_MEDIA_STREAM_BUFFER_MANAGER_H_ 95