1 /* 2 * Copyright (c) 2020-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 BUFFER_SOURCE_H 17 #define BUFFER_SOURCE_H 18 19 #include <stdlib.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include <cmath> 23 #include <pthread.h> 24 #include <unistd.h> 25 #include <sys/time.h> 26 #include <errno.h> 27 #include <vector> 28 #include <stddef.h> 29 #include <stdint.h> 30 #include <mutex> 31 32 #ifdef __cplusplus 33 #if __cplusplus 34 extern "C" { 35 #endif 36 #endif 37 38 typedef struct tagBufferInfo { 39 unsigned long long phyAddr; 40 void *virAddr; 41 int fd; 42 int bufLen; 43 int offset; 44 int size; 45 int idx; 46 } BufferInfo; 47 48 typedef struct tagQueBuffer { 49 int idx; 50 int flag; 51 int offset; 52 int size; 53 long long timestamp; 54 } QueBuffer; 55 56 typedef enum TagBufferFlag { 57 BUFFER_FLAG_SYNCFRAME = 1, 58 BUFFER_FLAG_CODECCONFIG = 2, 59 BUFFER_FLAG_EOS = 4, 60 BUFFER_FLAG_PARTIAL_FRAME = 8, 61 BUFFER_FLAG_ENDOFFRAME = 16, 62 BUFFER_FLAG_MUXER_DATA = 32, 63 }BufferFlag; 64 65 class BufferSource { 66 #define QUEUE_SIZE 20 67 #define BUFFER_SIZE (2* 1024) /* 2KB */ 68 69 public: 70 BufferSource(void); 71 virtual ~BufferSource(void); 72 int Init(void); 73 int GetQueSize(void); 74 int GetBufferInfo(int idx, BufferInfo* info); 75 int QueIdleBuffer(const QueBuffer *buffer); 76 int DequeIdleBuffer(QueBuffer* buffer, int timeOut); 77 size_t GetIdleQueSize(void); 78 int QueFilledBuffer(const QueBuffer *buffer); 79 int DequeFilledBuffer(QueBuffer* buffer, int timeOut); 80 size_t GetFilledQueSize(void); 81 int32_t GetFilledQueDataSize(void); 82 int GetFilledBuffer(size_t idx, QueBuffer* buffer); 83 84 private: 85 bool inited_; 86 BufferInfo buffer_[QUEUE_SIZE]; 87 std::vector<QueBuffer> idleBuffer_; 88 std::vector<QueBuffer> filledBuffer_; 89 std::mutex idleQueMutex_; 90 std::mutex filledQueMutex_; 91 }; 92 93 94 #ifdef __cplusplus 95 #if __cplusplus 96 } 97 #endif 98 #endif /* End of #ifdef __cplusplus */ 99 100 #endif /* End of #ifndef BUFFER_SOURCE_H */ 101