• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 CAMERA_FRAMEWORK_AUDIO_RECORD_H
17 #define CAMERA_FRAMEWORK_AUDIO_RECORD_H
18 #include <string>
19 #include <condition_variable>
20 #include "native_avcodec_base.h"
21 #include "native_avbuffer.h"
22 #include <refbase.h>
23 #include "camera_log.h"
24 #include "sample_info.h"
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 using namespace std;
29 using ByteArrayPtr = std::unique_ptr<uint8_t[]>;
30 
31 class AudioRecord : public RefBase {
32 public:
AudioRecord(int64_t timestamp)33     explicit AudioRecord(int64_t timestamp) : timestamp_(timestamp)
34     {
35         frameId_ = std::to_string(timestamp);
36         bufferSize = 0;
37     }
~AudioRecord()38     ~AudioRecord()
39     {
40         MEDIA_DEBUG_LOG("AudioRecord release start");
41         if (audioBuffer_ != nullptr) {
42             delete audioBuffer_;
43             audioBuffer_ = nullptr;
44         }
45         if (encodedBuffer) {
46             OH_AVBuffer_Destroy(encodedBuffer);
47             encodedBuffer = nullptr;
48         }
49     }
50     OH_AVBuffer *encodedBuffer = nullptr;
51 
52     std::string frameId_;
53 
SetStatusReadyConvertStatus()54     void SetStatusReadyConvertStatus()
55     {
56         status = STATUS_READY_CONVERT;
57     }
58 
SetStatusFinishEncodeStatus()59     void SetStatusFinishEncodeStatus()
60     {
61         status = STATUS_FINISH_ENCODE;
62         canReleased_.notify_one();
63     }
64 
IsIdle()65     bool IsIdle()
66     {
67         return status == STATUS_NONE;
68     }
69 
IsReadyConvert()70     bool IsReadyConvert()
71     {
72         return status == STATUS_READY_CONVERT;
73     }
74 
IsFinishCache()75     bool IsFinishCache()
76     {
77         return status == STATUS_FINISH_ENCODE;
78     }
79 
CacheEncodedBuffer(OH_AVBuffer * buffer)80     void CacheEncodedBuffer(OH_AVBuffer *buffer)
81     {
82         MEDIA_DEBUG_LOG("cacheEncodedBuffer start");
83         encodedBuffer = buffer;
84         SetStatusFinishEncodeStatus();
85     }
86 
SetEncodedResult(bool encodedResult)87     void SetEncodedResult(bool encodedResult)
88     {
89         isEncoded_ = encodedResult;
90     }
91 
IsEncoded()92     bool IsEncoded()
93     {
94         return isEncoded_;
95     }
96 
GetFrameId()97     const std::string& GetFrameId() const
98     {
99         return frameId_;
100     }
101 
GetBufferSize()102     uint32_t GetBufferSize()
103     {
104         return bufferSize;
105     }
106 
GetTimeStamp()107     int64_t GetTimeStamp()
108     {
109         return timestamp_;
110     }
111 
112     struct HashFunction {
operatorHashFunction113         std::size_t operator()(const sptr<AudioRecord>& obj) const
114         {
115             return std::hash<std::string>()(obj->GetFrameId());
116         }
117     };
118 
119     struct EqualFunction {
operatorEqualFunction120         bool operator()(const sptr<AudioRecord>& obj1, const sptr<AudioRecord>& obj2) const
121         {
122             return obj1->GetFrameId() == obj2->GetFrameId();
123         }
124     };
125 
ReleaseAudioBuffer()126     void ReleaseAudioBuffer()
127     {
128         std::unique_lock<std::mutex> lock(mutex_);
129         if (IsReadyConvert()) {
130             MEDIA_DEBUG_LOG("ReleaseAudioBuffer when isReadyConvert");
131             canReleased_.wait_for(lock, std::chrono::milliseconds(BUFFER_RELEASE_EXPIREATION_TIME),
132                 [this] { return IsFinishCache(); });
133             MEDIA_DEBUG_LOG("releaseSurfaceBuffer go %{public}s", frameId_.c_str());
134         }
135         if (audioBuffer_ != nullptr) {
136             delete audioBuffer_;
137             audioBuffer_ = nullptr;
138         }
139     }
140 
GetAudioBuffer()141     uint8_t* GetAudioBuffer()
142     {
143         std::unique_lock<std::mutex> lock(mutex_);
144         return audioBuffer_;
145     }
146 
SetAudioBuffer(uint8_t * audioBuffer,uint32_t inputSize)147     void SetAudioBuffer(uint8_t* audioBuffer, uint32_t inputSize)
148     {
149         std::unique_lock<std::mutex> lock(mutex_);
150         MEDIA_DEBUG_LOG("SetAudioBuffer size:%{public}u", inputSize);
151         audioBuffer_ = audioBuffer;
152     }
153 
154 private:
155     static const int32_t STATUS_NONE = 0;
156     static const int32_t STATUS_READY_CONVERT = 1;
157     static const int32_t STATUS_FINISH_ENCODE = 2;
158     int status = STATUS_NONE;
159     std::atomic<bool> isEncoded_ { false };
160     uint32_t bufferSize;
161     int64_t timestamp_;
162     std::mutex mutex_;
163     std::condition_variable canReleased_;
164     uint8_t* audioBuffer_ = nullptr;
165 };
166 } // CameraStandard
167 } // OHOS
168 #endif //CAMERA_FRAMEWORK_AUDIO_RECORD_H
169