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 #include "ppapi/shared_impl/media_stream_buffer_manager.h"
6
7 #include "base/logging.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/shared_impl/media_stream_buffer.h"
10
11 namespace ppapi {
12
~Delegate()13 MediaStreamBufferManager::Delegate::~Delegate() {}
14
OnNewBufferEnqueued()15 void MediaStreamBufferManager::Delegate::OnNewBufferEnqueued() {}
16
MediaStreamBufferManager(Delegate * delegate)17 MediaStreamBufferManager::MediaStreamBufferManager(Delegate* delegate)
18 : delegate_(delegate), buffer_size_(0), number_of_buffers_(0) {
19 DCHECK(delegate_);
20 }
21
~MediaStreamBufferManager()22 MediaStreamBufferManager::~MediaStreamBufferManager() {}
23
SetBuffers(int32_t number_of_buffers,int32_t buffer_size,scoped_ptr<base::SharedMemory> shm,bool enqueue_all_buffers)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);
33
34 number_of_buffers_ = number_of_buffers;
35 buffer_size_ = buffer_size;
36
37 size_t size = number_of_buffers_ * buffer_size;
38 shm_ = shm.Pass();
39 if (!shm_->Map(size))
40 return false;
41
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_;
50 }
51 return true;
52 }
53
DequeueBuffer()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;
60 }
61
DequeueBuffers()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;
66 }
67
EnqueueBuffer(int32_t index)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();
73 }
74
GetBufferPointer(int32_t index)75 MediaStreamBuffer* MediaStreamBufferManager::GetBufferPointer(int32_t index) {
76 if (index < 0 || index >= number_of_buffers_)
77 return NULL;
78 return buffers_[index];
79 }
80
81 } // namespace ppapi
82