root/ppapi/shared_impl/media_stream_buffer_manager.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. OnNewBufferEnqueued
  2. number_of_buffers_
  3. SetBuffers
  4. DequeueBuffer
  5. EnqueueBuffer
  6. GetBufferPointer

// 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

/* [<][>][^][v][top][bottom][index][help] */