diff --git a/interfaces/kits/data_stream.h b/interfaces/kits/data_stream.h new file mode 100644 index 0000000000000000000000000000000000000000..b943ae175864892e95e3cca915c0638570321743 --- /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 c7673bc370a8ebb8276773d0a08b6946e94f7c4c..4fdd00e03c9a9d08fcff05769fec4c61c348724d 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 ffcf8a7ac2cb22ecfd7ad62ecdee9cd6bedd0998..79f9b9c1cb5564b38477bb8ebf317dfea7e31792 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),