1 /*
2 * Copyright (c) 2025 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 #include <arpa/inet.h>
16 #include <sys/time.h>
17 #include <utility>
18 #include "hevcserverdec_sample.h"
19 #include <iostream>
20 #include "hevc_decoder_api.h"
21 #include "window.h"
22 #include "window_manager.h"
23 #include "window_option.h"
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 using namespace OHOS::MediaAVCodec;
27 using namespace OHOS::MediaAVCodec::Codec;
28 using namespace std;
29 namespace {
30 constexpr int32_t WIDTH = 1920;
31 constexpr int32_t HIGHT = 1080;
32 constexpr int32_t FORMAT = 2;
33 constexpr int32_t ANGLE = 0;
34 constexpr int32_t FRAME_RATE = 30;
35 constexpr int32_t TIME = 12345;
36 constexpr int32_t MAX_SEND_FRAMES = 10;
37 } // namespace
38
OnError(AVCodecErrorType errorType,int32_t errorCode)39 void VDecServerSample::CallBack::OnError(AVCodecErrorType errorType, int32_t errorCode)
40 {
41 tester->Flush();
42 tester->Reset();
43 }
44
OnOutputFormatChanged(const Format & format)45 void VDecServerSample::CallBack::OnOutputFormatChanged(const Format &format)
46 {
47 tester->GetOutputFormat();
48 }
49
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)50 void VDecServerSample::CallBack::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
51 {
52 unique_lock<mutex> lock(tester->signal_->inMutex_);
53 tester->signal_->inIdxQueue_.push(index);
54 tester->signal_->inBufferQueue_.push(buffer);
55 tester->signal_->inCond_.notify_all();
56 }
57
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)58 void VDecServerSample::CallBack::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
59 {
60 tester->codec_->ReleaseOutputBuffer(index);
61 }
62
~VDecServerSample()63 VDecServerSample::~VDecServerSample()
64 {
65 if (codec_ != nullptr) {
66 codec_->Stop();
67 codec_->Release();
68 HevcDecoder *codec = reinterpret_cast<HevcDecoder*>(codec_.get());
69 codec->DecStrongRef(codec);
70 }
71 }
72
ConfigServerDecoder()73 int32_t VDecServerSample::ConfigServerDecoder()
74 {
75 Format fmt;
76 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, WIDTH);
77 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, HIGHT);
78 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, FORMAT);
79 fmt.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, FRAME_RATE);
80 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, ANGLE);
81 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
82 return codec_->Configure(fmt);
83 }
84
SetParameter()85 int32_t VDecServerSample::SetParameter()
86 {
87 Format fmt;
88 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, WIDTH);
89 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, HIGHT);
90 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, FORMAT);
91 fmt.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, FRAME_RATE);
92 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, ANGLE);
93 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
94 return codec_->SetParameter(fmt);
95 }
96
SetCallback()97 int32_t VDecServerSample::SetCallback()
98 {
99 shared_ptr<CallBack> cb = make_shared<CallBack>(this);
100 return codec_->SetCallback(cb);
101 }
102
SetOutputSurface()103 int32_t VDecServerSample::SetOutputSurface()
104 {
105 auto cs = Surface::CreateSurfaceAsConsumer();
106 cs_vector.push_back(cs);
107 sptr<IBufferConsumerListener> listener = new ConsumerListener(cs);
108 cs->RegisterConsumerListener(listener);
109 auto p = cs->GetProducer();
110 auto ps = Surface::CreateSurfaceAsProducer(p);
111 ps_vector.push_back(ps);
112 return codec_->SetOutputSurface(ps);
113 }
114
InitDecoder()115 int32_t VDecServerSample::InitDecoder()
116 {
117 int32_t err;
118 Media::Meta codecInfo;
119 int32_t instanceid = 0;
120 codecInfo.SetData("av_codec_event_info_instance_id", instanceid);
121 err = codec_->Init(codecInfo);
122 if (err != AVCS_ERR_OK) {
123 cout << "decoder Init failed!" << endl;
124 return err;
125 }
126 std::vector<CapabilityData> caps;
127 err = GetHevcDecoderCapabilityList(caps);
128 if (err != AVCS_ERR_OK) {
129 cout << "GetHevcDecoderCapabilityList failed" << endl;
130 return err;
131 }
132 err = ConfigServerDecoder();
133 if (err != AVCS_ERR_OK) {
134 cout << "ConfigServerDecoder failed" << endl;
135 return err;
136 }
137 err = SetCallback();
138 if (err != AVCS_ERR_OK) {
139 cout << "SetCallback failed" << endl;
140 return err;
141 }
142 signal_ = std::make_shared<VDecSignal>();
143 if (signal_ == nullptr) {
144 cout << "Failed to new VDecSignal" << endl;
145 err = AVCS_ERR_NO_MEMORY;
146 return err;
147 }
148 err = SetOutputSurface();
149 if (err != AVCS_ERR_OK) {
150 cout << "SetOutputSurface failed" << endl;
151 return err;
152 }
153 return err;
154 }
155
RunVideoServerSurfaceDecoder()156 void VDecServerSample::RunVideoServerSurfaceDecoder()
157 {
158 CreateHevcDecoderByName("OH.Media.Codec.Decoder.Video.HEVC", codec_);
159 if (codec_ == nullptr) {
160 cout << "Create failed" << endl;
161 return;
162 }
163 int32_t err = InitDecoder();
164 if (err != AVCS_ERR_OK) {
165 cout << "Init decoder failed" << endl;
166 return;
167 }
168 err = codec_->Start();
169 if (err != AVCS_ERR_OK) {
170 cout << "Start failed" << endl;
171 return;
172 }
173 isRunning_.store(true);
174 inputLoop_ = make_unique<thread>(&VDecServerSample::InputFunc, this);
175 if (inputLoop_ == nullptr) {
176 cout << "Failed to create input loop" << endl;
177 isRunning_.store(false);
178 }
179 err = SetOutputSurface();
180 if (err != AVCS_ERR_OK) {
181 cout << "SetOutputSurface 2 failed" << endl;
182 return;
183 }
184 err = SetParameter();
185 if (err != AVCS_ERR_OK) {
186 cout << "SetParameter failed" << endl;
187 return;
188 }
189 GetOutputFormat();
190 }
191
InputFunc()192 void VDecServerSample::InputFunc()
193 {
194 while (sendFrameIndex < MAX_SEND_FRAMES) {
195 if (!isRunning_.load()) {
196 break;
197 }
198 unique_lock<mutex> lock(signal_->inMutex_);
199 signal_->inCond_.wait(lock, [this]() {
200 if (!isRunning_.load()) {
201 cout << "quit signal" << endl;
202 return true;
203 }
204 return signal_->inIdxQueue_.size() > 0;
205 });
206 if (!isRunning_.load()) {
207 break;
208 }
209 uint32_t index = signal_->inIdxQueue_.front();
210 auto buffer = signal_->inBufferQueue_.front();
211 signal_->inIdxQueue_.pop();
212 signal_->inBufferQueue_.pop();
213 lock.unlock();
214 if (buffer->memory_ == nullptr) {
215 isRunning_.store(false);
216 break;
217 }
218 uint8_t *bufferAddr = buffer->memory_->GetAddr();
219 if (memcpy_s(bufferAddr, buffer->memory_->GetCapacity(), fuzzData, fuzzSize) != EOK) {
220 break;
221 }
222 buffer->pts_ = TIME;
223 buffer->flag_ = 0;
224 buffer->memory_->SetOffset(0);
225 buffer->memory_->SetSize(fuzzSize);
226 int32_t err = codec_->QueueInputBuffer(index);
227 if (err != AVCS_ERR_OK) {
228 cout << "QueueInputBuffer fail" << endl;
229 break;
230 }
231 sendFrameIndex++;
232 }
233 }
234
WaitForEos()235 void VDecServerSample::WaitForEos()
236 {
237 if (inputLoop_ && inputLoop_->joinable()) {
238 inputLoop_->join();
239 }
240 }
241
GetOutputFormat()242 void VDecServerSample::GetOutputFormat()
243 {
244 Format fmt;
245 int32_t err = codec_->GetOutputFormat(fmt);
246 if (err != AVCS_ERR_OK) {
247 cout << "GetOutputFormat fail" << endl;
248 isRunning_.store(false);
249 signal_->inCond_.notify_all();
250 }
251 }
252
Flush()253 void VDecServerSample::Flush()
254 {
255 int32_t err = codec_->Flush();
256 if (err != AVCS_ERR_OK) {
257 cout << "Flush fail" << endl;
258 isRunning_.store(false);
259 signal_->inCond_.notify_all();
260 }
261 }
262
Reset()263 void VDecServerSample::Reset()
264 {
265 int32_t err = codec_->Reset();
266 if (err != AVCS_ERR_OK) {
267 cout << "Reset fail" << endl;
268 isRunning_.store(false);
269 signal_->inCond_.notify_all();
270 }
271 }
272
Stop()273 void VDecServerSample::Stop()
274 {
275 int32_t err = codec_->Stop();
276 if (err != AVCS_ERR_OK) {
277 cout << "Stop fail" << endl;
278 isRunning_.store(false);
279 signal_->inCond_.notify_all();
280 }
281 }