1 /* 2 * Copyright (c) 2023-2023 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 DATA_PACKER_H 17 #define DATA_PACKER_H 18 19 #include <atomic> 20 #include <deque> 21 #include <vector> 22 #include "plugin/plugin_buffer.h" 23 #include "osal/task/mutex.h" 24 #include "osal/task/condition_variable.h" 25 26 namespace OHOS { 27 namespace Media { 28 class DataPacker { 29 public: 30 DataPacker(); 31 32 ~DataPacker(); 33 34 DataPacker(const DataPacker& other) = delete; 35 36 DataPacker& operator=(const DataPacker& other) = delete; 37 38 void PushData(std::shared_ptr<Plugins::Buffer>& bufferPtr, uint64_t offset); 39 40 bool IsDataAvailable(uint64_t offset, uint32_t size); 41 42 bool PeekRange(uint64_t offset, uint32_t size, std::shared_ptr<Plugins::Buffer>& bufferPtr); 43 44 bool GetRange(uint64_t offset, uint32_t size, std::shared_ptr<Plugins::Buffer>& bufferPtr); 45 46 bool GetRange(uint32_t size, std::shared_ptr<Plugins::Buffer>& bufferPtr, 47 uint64_t preRemoveOffset, bool isPreRemove); // For live play 48 49 void PreRemove(uint64_t preRemoveOffset, bool isPreRemove); 50 51 bool GetOrWaitDataAvailable(uint64_t offset, uint32_t size); 52 53 void Flush(); 54 55 void SetEos(); 56 57 void IsSupportPreDownload(bool isSupport); 58 59 bool IsEmpty(); 60 61 void Start(); 62 63 void Stop(); 64 65 // Record the position that GerRange copy start or end. 66 struct Position { 67 int32_t index; // Buffer index, -1 means this Position is invalid 68 uint32_t bufferOffset; // Offset in the buffer 69 uint64_t mediaOffset; // Offset in the media file 70 PositionPosition71 Position(int32_t index, uint32_t bufferOffset, uint64_t mediaOffset) noexcept 72 { 73 this->index = index; 74 this->bufferOffset = bufferOffset; 75 this->mediaOffset = mediaOffset; 76 } 77 78 bool operator < (const Position& other) const 79 { 80 if (index < 0 || other.index < 0) { // Position invalid 81 return false; 82 } 83 if (index != other.index) { 84 return index < other.index; 85 } 86 return bufferOffset < other.bufferOffset; // use bufferOffset, maybe live play mediaOffset is invalid 87 } 88 ToStringPosition89 std::string ToString() const 90 { 91 return "Position (index " + std::to_string(index) + ", bufferOffset " + std::to_string(bufferOffset) + 92 ", mediaOffset " + std::to_string(mediaOffset); 93 } 94 }; 95 96 private: 97 void RemoveBufferContent(std::shared_ptr<Plugins::Buffer> &buffer, size_t removeSize); 98 99 bool PeekRangeInternal(uint64_t offset, uint32_t size, std::shared_ptr<Plugins::Buffer>& bufferPtr, bool isGet); 100 101 bool IsDataAvailableInternal(uint64_t offset, uint32_t size); 102 103 void FlushInternal(); 104 105 bool FindFirstBufferToCopy(uint64_t offset, int32_t &startIndex, uint64_t &prevOffset); 106 107 size_t CopyFirstBuffer(size_t size, int32_t index, uint8_t *dstPtr, std::shared_ptr<Plugins::Buffer>& dstBufferPtr, 108 int32_t bufferOffset); 109 110 int32_t CopyFromSuccessiveBuffer(uint64_t prevOffset, uint64_t offsetEnd, int32_t startIndex, uint8_t *dstPtr, 111 uint32_t &needCopySize); 112 113 void RemoveOldData(const Position& position); 114 115 bool RemoveTo(const Position& position); 116 117 bool UpdateWhenFrontDataRemoved(size_t removeSize); 118 119 std::string ToString() const; 120 121 Mutex mutex_; 122 std::deque<std::shared_ptr<Plugins::Buffer>> que_; 123 std::atomic<uint32_t> size_; 124 uint64_t mediaOffset_; // The media file offset of the first byte in data packer 125 uint64_t pts_; 126 uint64_t dts_; 127 bool isEos_ {false}; 128 bool isSupportPreDownload_ {false}; 129 130 // The position in prev GetRange, current GetRange 131 Position prevGet_ ; 132 Position currentGet_ ; 133 134 ConditionVariable cvFull_; 135 ConditionVariable cvEmpty_; 136 ConditionVariable cvAllowRead_; 137 size_t capacity_; 138 std::atomic<bool> stopped_ {false}; 139 }; 140 } // namespace Media 141 } // namespace OHOS 142 #endif // DATA_PACKER_H 143