1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MEDIA_DATA_STREAM_IMPL
17 #define MEDIA_DATA_STREAM_IMPL
18 
19 #include <queue>
20 #include <vector>
21 #include "data_stream.h"
22 #include "foundation/osal/thread/condition_variable.h"
23 #include "foundation/osal/thread/mutex.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class DataStreamImpl : public DataStream {
28 public:
29     DataStreamImpl(size_t size, size_t count, MemoryType type);
30     bool GetDataBuffer(std::shared_ptr<DataBuffer>& buffer, int timeout) override;
31     bool QueueEmptyBuffer(const std::shared_ptr<DataBuffer>& buffer) override;
32     bool QueueEmptyBuffer(uint8_t* address) override;
33 
34     bool GetEmptyBuffer(std::shared_ptr<DataBuffer>& buffer, int timeout) override;
35     bool QueueDataBuffer(const std::shared_ptr<DataBuffer>& buffer) override;
36 
37 private:
38     OSAL::Mutex emptyMutex_ {};
39     OSAL::Mutex dataMutex_ {};
40     OSAL::ConditionVariable emptyCondition_ {};
41     OSAL::ConditionVariable dataCondition_ {};
42     std::queue<std::shared_ptr<DataBuffer>> emptyBuffers_ {};
43     std::queue<std::shared_ptr<DataBuffer>> dataBuffers_ {};
44     std::vector<std::shared_ptr<DataBuffer>> allBuffers_ {}; // keep all buffers
45 };
46 
47 class VirtualDataBuffer : public DataBuffer {
48 public:
49     explicit VirtualDataBuffer(size_t capacity);
50     ~VirtualDataBuffer() override;
51     bool IsEos() override;
52     void SetEos(bool isEos) override;
53     uint8_t* GetAddress() override;
54     size_t GetCapacity() override;
55     size_t GetSize() override;
56     void SetSize(size_t size) override;
57 
58 private:
59     bool isEos_ {false};
60     uint8_t* address_ {nullptr};
61     size_t capacity_ {0};
62     size_t size_ {0};
63 };
64 } // Media
65 } // OHOS
66 #endif // MEDIA_DATA_STREAM_IMPL
67