• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong DID 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_MANAGER_H
17 #define BUFFER_MANAGER_H
18 
19 #include <queue>
20 #include <signal.h>
21 #include <pthread.h>
22 #include "codec_type.h"
23 #include "osal_mutex.h"
24 
25 #define CODEC_STATUS_RUN        0
26 #define CODEC_STATUS_STOPPED    1
27 
28 #define HDF_NANO_UNITS 1000000000
29 
30 template <class T>
31 class BufferManager {
32 public:
33     BufferManager();
34     ~BufferManager();
35     void Stop();
36     T* GetBuffer(uint32_t timeoutMs, bool isChecking);
37     T* GetUsedBuffer(uint32_t timeoutMs, bool isChecking);
38     void PutBuffer(T *info);
39     void PutUsedBuffer(T *info);
40 
41 private:
42     void ConstructTimespec(struct timespec *time,  uint32_t timeoutMs);
43     T* PollBufferQueue(bool isChecking);
44     T* PollUsedBufferQueue(bool isChecking);
45 
46     int32_t status;
47     OsalMutex bufferQueueLock;
48     OsalMutex usedBufferQueueLock;
49     pthread_cond_t inputCond = PTHREAD_COND_INITIALIZER;
50     pthread_cond_t outputCond = PTHREAD_COND_INITIALIZER;
51     std::queue<T*> bufferQueue;
52     std::queue<T*> usedBufferQueue;
53 };
54 
55 #endif  // BUFFER_MANAGER_H
56