1 /*
2 * Copyright (c) 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 #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
SetCallback()85 int32_t VDecServerSample::SetCallback()
86 {
87 shared_ptr<CallBack> cb = make_shared<CallBack>(this);
88 return codec_->SetCallback(cb);
89 }
90
RunVideoServerDecoder()91 void VDecServerSample::RunVideoServerDecoder()
92 {
93 signal_ = std::make_shared<VDecSignal>();
94 if (signal_ == nullptr) {
95 return;
96 }
97 CreateHevcDecoderByName("OH.Media.Codec.Decoder.Video.HEVC", codec_);
98 if (codec_ == nullptr) {
99 cout << "Create failed" << endl;
100 return;
101 }
102 int32_t err = ConfigServerDecoder();
103 if (err != AVCS_ERR_OK) {
104 cout << "ConfigServerDecoder failed" << endl;
105 return;
106 }
107 err = SetCallback();
108 if (err != AVCS_ERR_OK) {
109 cout << "SetCallback failed" << endl;
110 return;
111 }
112 err = codec_->Start();
113 if (err != AVCS_ERR_OK) {
114 cout << "Start failed" << endl;
115 return;
116 }
117 isRunning_.store(true);
118 inputLoop_ = make_unique<thread>(&VDecServerSample::InputFunc, this);
119 if (inputLoop_ == nullptr) {
120 cout << "Failed to create input loop" << endl;
121 isRunning_.store(false);
122 }
123 }
124
InputFunc()125 void VDecServerSample::InputFunc()
126 {
127 while (sendFrameIndex < MAX_SEND_FRAMES) {
128 if (!isRunning_.load()) {
129 break;
130 }
131 unique_lock<mutex> lock(signal_->inMutex_);
132 signal_->inCond_.wait(lock, [this]() {
133 if (!isRunning_.load()) {
134 cout << "quit signal" << endl;
135 return true;
136 }
137 return signal_->inIdxQueue_.size() > 0;
138 });
139 if (!isRunning_.load()) {
140 break;
141 }
142 uint32_t index = signal_->inIdxQueue_.front();
143 auto buffer = signal_->inBufferQueue_.front();
144 signal_->inIdxQueue_.pop();
145 signal_->inBufferQueue_.pop();
146 lock.unlock();
147 if (buffer->memory_ == nullptr) {
148 isRunning_.store(false);
149 break;
150 }
151 uint8_t *bufferAddr = buffer->memory_->GetAddr();
152 if (memcpy_s(bufferAddr, buffer->memory_->GetCapacity(), fuzzData, fuzzSize) != EOK) {
153 break;
154 }
155 buffer->pts_ = TIME;
156 buffer->flag_ = 0;
157 buffer->memory_->SetOffset(0);
158 buffer->memory_->SetSize(fuzzSize);
159 int32_t err = codec_->QueueInputBuffer(index);
160 if (err != AVCS_ERR_OK) {
161 cout << "QueueInputBuffer fail" << endl;
162 break;
163 }
164 sendFrameIndex++;
165 }
166 }
167
WaitForEos()168 void VDecServerSample::WaitForEos()
169 {
170 if (inputLoop_ && inputLoop_->joinable()) {
171 inputLoop_->join();
172 }
173 }
174
GetOutputFormat()175 void VDecServerSample::GetOutputFormat()
176 {
177 Format fmt;
178 int32_t err = codec_->GetOutputFormat(fmt);
179 if (err != AVCS_ERR_OK) {
180 cout << "GetOutputFormat fail" << endl;
181 isRunning_.store(false);
182 signal_->inCond_.notify_all();
183 }
184 }
185
Flush()186 void VDecServerSample::Flush()
187 {
188 int32_t err = codec_->Flush();
189 if (err != AVCS_ERR_OK) {
190 cout << "Flush fail" << endl;
191 isRunning_.store(false);
192 signal_->inCond_.notify_all();
193 }
194 }
195
Reset()196 void VDecServerSample::Reset()
197 {
198 int32_t err = codec_->Reset();
199 if (err != AVCS_ERR_OK) {
200 cout << "Reset fail" << endl;
201 isRunning_.store(false);
202 signal_->inCond_.notify_all();
203 }
204 }
205
Stop()206 void VDecServerSample::Stop()
207 {
208 int32_t err = codec_->Stop();
209 if (err != AVCS_ERR_OK) {
210 cout << "Stop fail" << endl;
211 isRunning_.store(false);
212 signal_->inCond_.notify_all();
213 }
214 }