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.
#ifndef WEBMLIVE_ENCODER_DATA_SINK_H_
#define WEBMLIVE_ENCODER_DATA_SINK_H_
#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 {
public:
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();
private:
std::mutex mutex_;
std::queue<const SharedDataSinkBuffer> buffer_q_;
};
class DataSinkInterface {
public:
virtual ~DataSinkInterface() {}
virtual bool WriteData(const SharedDataSinkBuffer& buffer) = 0;
virtual std::string Name() const = 0;
};
class DataSink {
public:
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);
private:
std::mutex mutex_;
std::vector<DataSinkInterface*> data_sinks_;
};
} // namespace webmlive
#endif // WEBMLIVE_ENCODER_DATA_SINK_H_