From e003dd0c6f4a53ed85524220cdb04ac1d5e3a3aa Mon Sep 17 00:00:00 2001 From: Chen Guodong Date: Sun, 6 Mar 2022 09:57:42 +0800 Subject: [PATCH] Add DataStream to simplify data sream operation. Signed-off-by: Chen Guodong --- interfaces/kits/data_stream.h | 189 ++++++++++++++++++++++++++++++++++ interfaces/kits/source.h | 22 ++++ src/source.cpp | 11 ++ 3 files changed, 222 insertions(+) create mode 100644 interfaces/kits/data_stream.h diff --git a/interfaces/kits/data_stream.h b/interfaces/kits/data_stream.h new file mode 100644 index 0000000..b943ae1 --- /dev/null +++ b/interfaces/kits/data_stream.h @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2022-2022 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 MEDIA_DATA_STREAM +#define MEDIA_DATA_STREAM + +#include +#include + +namespace OHOS { +namespace Media { + +/** + * @enum MemoryType + * + * @since 1.0 + * @version 1.0 + */ +enum class MemoryType { + VIRTUAL_ADDR = 0, ///< Virtual address + SURFACE_BUFFER, ///< Surface + SHARE_MEMORY, ///< Share Memory fd +}; + +/** + * @brief Data buffer, contains the data. + * + * @since 1.0 + * @version 1.0 + */ +class DataBuffer { +public: + virtual ~DataBuffer() = default; + + /** + * @brief Get the EOS status. + * + * @return Returns the EOS status, true if this is the end of steam. + * @since 1.0 + * @version 1.0 + */ + virtual bool IsEos() = 0; + + /** + * @brief Set the EOS status. + * + * @since 1.0 + * @version 1.0 + */ + virtual void SetEos(bool isEos) = 0; + + /** + * @brief Get the buffer address. + * + * @since 1.0 + * @version 1.0 + */ + virtual uint8_t* GetAddress() = 0; + + /** + * @brief Get the buffer capacity. + * + * @since 1.0 + * @version 1.0 + */ + virtual size_t GetCapacity() = 0; + + /** + * @brief Get the size of the valid data in this data buffer. + * + * @since 1.0 + * @version 1.0 + */ + virtual size_t GetSize() = 0; + + /** + * @brief Set the size of the valid data in this data buffer. + * + * @param size Indicates the size of the valid data. + * @since 1.0 + * @version 1.0 + */ + virtual void SetSize(size_t size) = 0; +}; + +/** + * @brief Data producer uses this interface to produce data. + * + * @since 1.0 + * @version 1.0 + */ +class DataProducer { +public: + virtual ~DataProducer() = default; + + /** + * @brief Get empty buffer. + * + * @param buffer Out parameter to obtain the buffer. + * @param timeout Indicates how much time (millisecond) to wait, default -1 means wait until buffer obtained. + * @since 1.0 + * @version 1.0 + */ + virtual bool GetEmptyBuffer(std::shared_ptr& buffer, int timeout = -1) = 0; + + /** + * @brief Queue data buffer. + * + * @param buffer the buffer contains data. + * @since 1.0 + * @version 1.0 + */ + virtual bool QueueDataBuffer(const std::shared_ptr& buffer) = 0; +}; + +/** + * @brief Data consumer uses this interface to consume data. + * + * @since 1.0 + * @version 1.0 + */ +class DataConsumer { +public: + virtual ~DataConsumer() = default; + + /** + * @brief Get data buffer. + * + * @param buffer Out parameter to obtain the buffer contains data. + * @param timeout Indicates how much time (millisecond) to wait, default -1 means wait until data buffer obtained. + * @since 1.0 + * @version 1.0 + */ + virtual bool GetDataBuffer(std::shared_ptr& buffer, int timeout = -1) = 0; + + /** + * @brief Use the shared_ptr of buffer to queue empty buffer to data stream. + * + * @param buffer Indicates the shared_ptr of the empty buffer. + * @since 1.0 + * @version 1.0 + */ + virtual bool QueueEmptyBuffer(const std::shared_ptr& buffer) = 0; + + /** + * @brief Use the buffer address to queue empty buffer to data stream. + * + * @param address Indicates the address of the empty buffer. + * @since 1.0 + * @version 1.0 + */ + virtual bool QueueEmptyBuffer(uint8_t* address) = 0; +}; + +/** + * @brief Data stream, extends DataConsumer and DataProducer. + * + * @since 1.0 + * @version 1.0 + */ +class DataStream : public DataConsumer, public DataProducer{ +}; + +/** + * @brief The factory function to create {@link DataStream}. + * + * @param size Indicates the size of each buffer. + * @param count Indicates the count of buffers. + * @param type Indicates the memory type, default is virtual address. + * @since 1.0 + * @version 1.0 + */ +std::shared_ptr CreateDataStream(size_t size, size_t count, MemoryType type = MemoryType::VIRTUAL_ADDR); + +} // Media +} // OHOS +#endif // MEDIA_DATA_STREAM diff --git a/interfaces/kits/source.h b/interfaces/kits/source.h index c7673bc..4fdd00e 100644 --- a/interfaces/kits/source.h +++ b/interfaces/kits/source.h @@ -39,6 +39,7 @@ #include #include #include +#include "data_stream.h" #include "format.h" #ifndef SURFACE_DISABLED #include "surface.h" @@ -227,6 +228,15 @@ public: */ Source(const std::shared_ptr &stream, const Format &formats); + /** + * @brief A constructor used to create a {@link Source} instance based on the data stream consumer. + * + * @param dataConsumer Indicates the data stream consumer. For details, see {@link DataConsumer}. + * @since 1.0 + * @version 1.0 + */ + explicit Source(std::shared_ptr dataConsumer); + ~Source() = default; /** @@ -280,12 +290,24 @@ public: */ const Format &GetSourceStreamFormat() const; + /** + * @brief Obtains the data stream consumer interface. + * + * This function is called only when the {@link SourceType} is {@link SOURCE_TYPE_STREAM}. + * + * @return Returns the data stream consumer interface. For details, see {@link DataConsumer}. + * @since 1.0 + * @version 1.0 + */ + std::shared_ptr GetDataConsumer() const; + private: std::string uri_; SourceType sourceType_; std::map header_; std::shared_ptr stream_; Format format_; + std::shared_ptr dataConsumer_; }; } // namespace Media } // namespace OHOS diff --git a/src/source.cpp b/src/source.cpp index ffcf8a7..79f9b9c 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -36,6 +36,12 @@ Source::Source(const std::shared_ptr &stream, const Format &format format_.CopyFrom(formats); } +Source::Source(std::shared_ptr dataConsumer) + : sourceType_(SourceType::SOURCE_TYPE_STREAM), + dataConsumer_(std::move(dataConsumer)) +{ +} + SourceType Source::GetSourceType() const { return sourceType_; @@ -60,6 +66,11 @@ const Format &Source::GetSourceStreamFormat() const return format_; } +std::shared_ptr Source::GetDataConsumer() const +{ + return dataConsumer_; +} + StreamSource::StreamSource() #ifndef SURFACE_DISABLED : surface_(nullptr), -- GitLab