Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / ppapi / shared_impl / media_stream_buffer_manager.cc
blob29bc1537e7d82760807adb3c894a18d0e951c0a6
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"
11 namespace ppapi {
13 MediaStreamBufferManager::Delegate::~Delegate() {}
15 void MediaStreamBufferManager::Delegate::OnNewBufferEnqueued() {}
17 MediaStreamBufferManager::MediaStreamBufferManager(Delegate* delegate)
18 : delegate_(delegate), buffer_size_(0), number_of_buffers_(0) {
19 DCHECK(delegate_);
22 MediaStreamBufferManager::~MediaStreamBufferManager() {}
24 bool MediaStreamBufferManager::SetBuffers(int32_t number_of_buffers,
25 int32_t buffer_size,
26 scoped_ptr<base::SharedMemory> shm,
27 bool enqueue_all_buffers) {
28 DCHECK(shm);
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;
38 shm_ = shm.Pass();
39 if (!shm_->Map(size))
40 return false;
42 buffer_queue_.clear();
43 buffers_.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));
49 p += buffer_size_;
51 return true;
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();
59 return buffer;
62 std::vector<int32_t> MediaStreamBufferManager::DequeueBuffers() {
63 std::vector<int32_t> buffers(buffer_queue_.begin(), buffer_queue_.end());
64 buffer_queue_.clear();
65 return buffers;
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_)
77 return NULL;
78 return buffers_[index];
81 } // namespace ppapi