summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/media_stream_buffer_manager.cc
blob: d6c4b0d597e22db3ff670eabc49552ceb5feec4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/shared_impl/media_stream_buffer_manager.h"

#include "base/logging.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/media_stream_buffer.h"

namespace ppapi {

MediaStreamBufferManager::Delegate::~Delegate() {}

void MediaStreamBufferManager::Delegate::OnNewBufferEnqueued() {}

MediaStreamBufferManager::MediaStreamBufferManager(Delegate* delegate)
    : delegate_(delegate), buffer_size_(0), number_of_buffers_(0) {
  DCHECK(delegate_);
}

MediaStreamBufferManager::~MediaStreamBufferManager() {}

bool MediaStreamBufferManager::SetBuffers(int32_t number_of_buffers,
                                          int32_t buffer_size,
                                          scoped_ptr<base::SharedMemory> shm,
                                          bool enqueue_all_buffers) {
  DCHECK(shm);
  DCHECK_GT(number_of_buffers, 0);
  DCHECK_GT(buffer_size,
            static_cast<int32_t>(sizeof(MediaStreamBuffer::Header)));
  DCHECK_EQ(buffer_size & 0x3, 0);

  number_of_buffers_ = number_of_buffers;
  buffer_size_ = buffer_size;

  size_t size = number_of_buffers_ * buffer_size;
  shm_ = shm.Pass();
  if (!shm_->Map(size))
    return false;

  buffer_queue_.clear();
  buffers_.clear();
  uint8_t* p = reinterpret_cast<uint8_t*>(shm_->memory());
  for (int32_t i = 0; i < number_of_buffers; ++i) {
    if (enqueue_all_buffers)
      buffer_queue_.push_back(i);
    buffers_.push_back(reinterpret_cast<MediaStreamBuffer*>(p));
    p += buffer_size_;
  }
  return true;
}

int32_t MediaStreamBufferManager::DequeueBuffer() {
  if (buffer_queue_.empty())
    return PP_ERROR_FAILED;
  int32_t buffer = buffer_queue_.front();
  buffer_queue_.pop_front();
  return buffer;
}

void MediaStreamBufferManager::EnqueueBuffer(int32_t index) {
  CHECK_GE(index, 0) << "Invalid buffer index";
  CHECK_LT(index, number_of_buffers_) << "Invalid buffer index";
  buffer_queue_.push_back(index);
  delegate_->OnNewBufferEnqueued();
}

MediaStreamBuffer* MediaStreamBufferManager::GetBufferPointer(
    int32_t index) {
  CHECK_GE(index, 0) << "Invalid buffer index";
  CHECK_LT(index, number_of_buffers_) << "Invalid buffer index";
  return buffers_[index];
}

}  // namespace ppapi