• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_DATA_PACKER_H
17 #define HISTREAMER_DATA_PACKER_H
18 
19 #include <atomic>
20 #include <deque>
21 #include <vector>
22 
23 #include "osal/thread/mutex.h"
24 #include "osal/thread/scoped_lock.h"
25 #include "utils/type_define.h"
26 
27 namespace OHOS {
28 namespace Media {
29 class DataPacker {
30 public:
31     DataPacker();
32 
33     ~DataPacker();
34 
35     DataPacker(const DataPacker& other) = delete;
36 
37     DataPacker& operator=(const DataPacker& other) = delete;
38 
39     void PushData(AVBufferPtr bufferPtr, uint64_t offset);
40 
41     bool IsDataAvailable(uint64_t offset, uint32_t size, uint64_t &curOffset);
42 
43     bool PeekRange(uint64_t offset, uint32_t size, AVBufferPtr &bufferPtr);
44 
45     bool GetRange(uint64_t offset, uint32_t size, AVBufferPtr &bufferPtr);
46 
47     void Flush();
48 
49 private:
50     AVBufferPtr WrapAssemblerBuffer(uint64_t offset);
51 
52     bool RepackBuffers(uint64_t offset, uint32_t size, AVBufferPtr &bufferPtr);
53 
54     void RemoveBufferContent(std::shared_ptr<AVBuffer> &buffer, size_t removeSize);
55 
56     bool PeekRangeInternal(uint64_t offset, uint32_t size, AVBufferPtr& bufferPtr);
57 
58     void FlushInternal();
59 
60     OSAL::Mutex mutex_;
61     std::deque<AVBufferPtr> que_;  // buffer队列
62     std::vector<uint8_t> assembler_;
63     std::atomic<uint32_t> size_;
64     uint32_t bufferOffset_; // 当前 DataPacker缓存数据的第一个字节 对应 到 媒体文件中的 offset
65     uint64_t pts_;
66     uint64_t dts_;
67 };
68 } // namespace Media
69 } // namespace OHOS
70 #endif // HISTREAMER_DATA_PACKER_H
71