blob: ccf4146dd9b2dfe26ab03af19340fe98a123f49e [file] [log] [blame]
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <vector>
#include "encoder/basictypes.h"
namespace webmlive {
struct DataSinkBuffer {
std::string id;
std::vector<uint8> data;
typedef std::shared_ptr<DataSinkBuffer> SharedDataSinkBuffer;
class SharedBufferQueue {
SharedBufferQueue() {}
~SharedBufferQueue() {}
// Enqueues |buffer| and returns true. Returns false upon failure. Blocks on
// |mutex_|.
bool EnqueueBuffer(const SharedDataSinkBuffer& buffer);
// Returns a buffer if one is available. Does not block waiting on |mutex_|;
// gives up and returns empty |std::shared_ptr| when unable to obtain lock.
SharedDataSinkBuffer DequeueBuffer();
// Returns number of buffers queued. Blocks on |mutex_| acquisition.
size_t GetNumBuffers();
std::mutex mutex_;
std::queue<const SharedDataSinkBuffer> buffer_q_;
class DataSinkInterface {
virtual ~DataSinkInterface() {}
virtual bool WriteData(const SharedDataSinkBuffer& buffer) = 0;
virtual std::string Name() const = 0;
class DataSink {
DataSink() {}
~DataSink() {}
// Adds |data_sink| to |data_sinks_|.
void AddDataSink(DataSinkInterface* data_sink);
// Writes |id| and |ptr_data| to all data sinks in |data_sinks_|. Returns
// true when the data has been sent to all sinks.
bool WriteData(const std::string& id, const uint8* ptr_data, int data_length);
std::mutex mutex_;
std::vector<DataSinkInterface*> data_sinks_;
} // namespace webmlive