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 #include "ppapi/shared_impl/media_stream_buffer_manager.h"
7 #include "base/logging.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/shared_impl/media_stream_buffer.h"
13 MediaStreamBufferManager::Delegate::~Delegate() {}
15 void MediaStreamBufferManager::Delegate::OnNewBufferEnqueued() {}
17 MediaStreamBufferManager::MediaStreamBufferManager(Delegate
* delegate
)
18 : delegate_(delegate
), buffer_size_(0), number_of_buffers_(0) {
22 MediaStreamBufferManager::~MediaStreamBufferManager() {}
24 bool MediaStreamBufferManager::SetBuffers(int32_t number_of_buffers
,
26 scoped_ptr
<base::SharedMemory
> shm
,
27 bool enqueue_all_buffers
) {
29 DCHECK_GT(number_of_buffers
, 0);
30 DCHECK_GT(buffer_size
,
31 static_cast<int32_t>(sizeof(MediaStreamBuffer::Header
)));
32 DCHECK_EQ(buffer_size
& 0x3, 0);
34 number_of_buffers_
= number_of_buffers
;
35 buffer_size_
= buffer_size
;
37 size_t size
= number_of_buffers_
* buffer_size
;
42 buffer_queue_
.clear();
44 uint8_t* p
= reinterpret_cast<uint8_t*>(shm_
->memory());
45 for (int32_t i
= 0; i
< number_of_buffers
; ++i
) {
46 if (enqueue_all_buffers
)
47 buffer_queue_
.push_back(i
);
48 buffers_
.push_back(reinterpret_cast<MediaStreamBuffer
*>(p
));
54 int32_t MediaStreamBufferManager::DequeueBuffer() {
55 if (buffer_queue_
.empty())
56 return PP_ERROR_FAILED
;
57 int32_t buffer
= buffer_queue_
.front();
58 buffer_queue_
.pop_front();
62 std::vector
<int32_t> MediaStreamBufferManager::DequeueBuffers() {
63 std::vector
<int32_t> buffers(buffer_queue_
.begin(), buffer_queue_
.end());
64 buffer_queue_
.clear();
68 void MediaStreamBufferManager::EnqueueBuffer(int32_t index
) {
69 CHECK_GE(index
, 0) << "Invalid buffer index";
70 CHECK_LT(index
, number_of_buffers_
) << "Invalid buffer index";
71 buffer_queue_
.push_back(index
);
72 delegate_
->OnNewBufferEnqueued();
75 MediaStreamBuffer
* MediaStreamBufferManager::GetBufferPointer(int32_t index
) {
76 if (index
< 0 || index
>= number_of_buffers_
)
78 return buffers_
[index
];