* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HISTREAMER_PLUGIN_COMMON_BUFFER_H
#define HISTREAMER_PLUGIN_COMMON_BUFFER_H
#include <memory>
#include <map>
#include <vector>
#include "plugin/common/plugin_memory.h"
#include "plugin/common/plugin_meta.h"
#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
#include "refbase.h"
#include "surface/surface.h"
#endif
namespace OHOS {
namespace Media {
namespace Plugin {
#define BUFFER_FLAG_EOS 0x00000001
#define BUFFER_FLAG_KEY_FRAME 0x00000002
template <typename T>
using MakeUnsigned = typename std::make_unsigned<T>::type;
template <typename T, typename U>
constexpr T AlignUp(T num, U alignment)
{
return (alignment > 0) ? (static_cast<uint64_t>((static_cast<MakeUnsigned<T>>(num)
+ static_cast<MakeUnsigned<T>>(alignment) - 1)) &
static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
num;
}
* @enum Buffer Meta Type
*
* @since 1.0
* @version 1.0
*/
enum struct BufferMetaType : uint32_t {
AUDIO,
VIDEO,
};
* @brief Buffer Meta.
* Base class that describes various media metadata.
*
* @since 1.0
* @version 1.0
*/
class BufferMeta {
public:
virtual ~BufferMeta() = default;
ValueType GetMeta(Tag tag);
void SetMeta(Tag tag, ValueType value);
BufferMetaType GetType() const;
bool IsExist(Tag tag);
void Update(const BufferMeta& bufferMeta);
virtual std::shared_ptr<BufferMeta> Clone() = 0;
protected:
explicit BufferMeta(BufferMetaType type);
private:
BufferMetaType type_;
std::shared_ptr<Meta> tags_ {};
};
* @brief Audio buffer metadata.
*
* Buffer metadata describing how data is laid out inside the buffer.
*
* @since 1.0
* @version 1.0
*/
class AudioBufferMeta : public BufferMeta {
public:
~AudioBufferMeta() override = default;
std::shared_ptr<BufferMeta> Clone() override;
size_t samples {0};
AudioSampleFormat sampleFormat {AudioSampleFormat::S8};
uint32_t sampleRate {0};
uint32_t channels {0};
uint32_t bytesPreFrame {0};
AudioChannelLayout channelLayout {AudioChannelLayout::MONO};
std::vector<size_t> offsets {};
private:
AudioBufferMeta() : BufferMeta(BufferMetaType::AUDIO) {}
friend class Buffer;
};
* @brief Video buffer metadata.
*
* Extra buffer metadata describing video properties.
*
* @since 1.0
* @version 1.0
*/
class VideoBufferMeta : public BufferMeta {
public:
~VideoBufferMeta() override = default;
std::shared_ptr<BufferMeta> Clone() override;
VideoPixelFormat videoPixelFormat {VideoPixelFormat::UNKNOWN};
uint32_t id {0};
uint32_t width {0};
uint32_t height {0};
uint32_t planes {0};
std::vector<uint32_t> stride {};
std::vector<uint32_t> offset {};
private:
VideoBufferMeta() : BufferMeta(BufferMetaType::VIDEO) {}
friend class Buffer;
};
* @brief Buffer base class.
* Contains the data storage and metadata information of the buffer (buffer description information).
*
* @since 1.0
* @version 1.0
*/
class Buffer {
public:
explicit Buffer(BufferMetaType type = BufferMetaType::AUDIO);
~Buffer() = default;
static std::shared_ptr<Buffer> CreateDefaultBuffer(BufferMetaType type, size_t capacity,
std::shared_ptr<Allocator> allocator = nullptr,
size_t align = 1);
std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer);
#endif
uint32_t GetMemoryCount();
std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
std::shared_ptr<BufferMeta> GetBufferMeta();
void UpdateBufferMeta(const BufferMeta& bufferMeta);
void Reset();
bool IsEmpty();
void ChangeBufferMetaType(BufferMetaType type);
uint32_t trackID;
int64_t pts;
int64_t dts;
int64_t duration;
uint64_t flag;
private:
std::vector<std::shared_ptr<Memory>> data {};
std::shared_ptr<BufferMeta> meta;
};
}
}
}
#endif