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 "serverdec_sample.h"
19 #include <iostream>
20 #include <chrono>
21 using namespace OHOS;
22 using namespace OHOS::Media;
23 using namespace OHOS::MediaAVCodec;
24 using namespace OHOS::MediaAVCodec::Codec;
25 using namespace std;
26 namespace {
27
28 } // namespace
29
OnError(AVCodecErrorType errorType,int32_t errorCode)30 void VDecServerSample::CallBack::OnError(AVCodecErrorType errorType, int32_t errorCode)
31 {
32 cout << "--OnError--" << endl;
33 tester->isRunning_.store(false);
34 tester->signal_->inCond_.notify_all();
35 }
36
OnOutputFormatChanged(const Format & format)37 void VDecServerSample::CallBack::OnOutputFormatChanged(const Format &format)
38 {
39 tester->GetOutputFormat();
40 }
41
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)42 void VDecServerSample::CallBack::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
43 {
44 unique_lock<mutex> lock(tester->signal_->inMutex_);
45 tester->signal_->inIdxQueue_.push(index);
46 tester->signal_->inBufferQueue_.push(buffer);
47 tester->signal_->inCond_.notify_all();
48 }
49
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)50 void VDecServerSample::CallBack::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
51 {
52 tester->codec_->ReleaseOutputBuffer(index);
53 }
54
~VDecServerSample()55 VDecServerSample::~VDecServerSample()
56 {
57 if (codec_ != nullptr) {
58 codec_->Stop();
59 codec_->Release();
60 }
61 if (signal_ != nullptr) {
62 delete signal_;
63 signal_ = nullptr;
64 }
65 }
66
ConfigServerDecoder()67 int32_t VDecServerSample::ConfigServerDecoder()
68 {
69 Format fmt;
70 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, width);
71 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height);
72 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, 1);
73 fmt.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, frameRate);
74 fmt.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, 0);
75 return codec_->Configure(fmt);
76 }
77
SetCallback()78 int32_t VDecServerSample::SetCallback()
79 {
80 shared_ptr<CallBack> cb = make_shared<CallBack>(this);
81 return codec_->SetCallback(cb);
82 }
83
RunVideoServerDecoder()84 void VDecServerSample::RunVideoServerDecoder()
85 {
86 codec_ = make_shared<FCodec>("OH.Media.Codec.Decoder.Video.AVC");
87 if (codec_ == nullptr) {
88 cout << "Create failed" << endl;
89 return;
90 }
91 int32_t err = ConfigServerDecoder();
92 if (err != AVCS_ERR_OK) {
93 cout << "ConfigServerDecoder failed" << endl;
94 return;
95 }
96 signal_ = new VDecSignal();
97 if (signal_ == nullptr) {
98 cout << "Failed to new VDecSignal" << endl;
99 return;
100 }
101 err = SetCallback();
102 if (err != AVCS_ERR_OK) {
103 cout << "SetCallback failed" << endl;
104 return;
105 }
106 err = codec_->Start();
107 if (err != AVCS_ERR_OK) {
108 cout << "Start failed" << endl;
109 return;
110 }
111 isRunning_.store(true);
112 inputLoop_ = make_unique<thread>(&VDecServerSample::InputFunc, this);
113 if (inputLoop_ == nullptr) {
114 cout << "Failed to create input loop" << endl;
115 isRunning_.store(false);
116 }
117 }
118
InputFunc()119 void VDecServerSample::InputFunc()
120 {
121 int32_t time = 1000;
122 while (sendFrameIndex < frameIndex) {
123 if (!isRunning_.load()) {
124 break;
125 }
126 unique_lock<mutex> lock(signal_->inMutex_);
127 signal_->inCond_.wait_for(lock, std::chrono::milliseconds(time), [this]() {
128 if (!isRunning_.load()) {
129 cout << "quit signal" << endl;
130 return true;
131 }
132 return signal_->inIdxQueue_.size() > 0;
133 });
134 if (!isRunning_.load() || signal_->inIdxQueue_.size() == 0) {
135 break;
136 }
137 uint32_t index = signal_->inIdxQueue_.front();
138 auto buffer = signal_->inBufferQueue_.front();
139 signal_->inIdxQueue_.pop();
140 signal_->inBufferQueue_.pop();
141 lock.unlock();
142 if (buffer->memory_ == nullptr) {
143 isRunning_.store(false);
144 break;
145 }
146 uint8_t *bufferAddr = buffer->memory_->GetAddr();
147 if (memcpy_s(bufferAddr, buffer->memory_->GetCapacity(), fuzzData, fuzzSize) != EOK) {
148 break;
149 }
150 int32_t err = codec_->QueueInputBuffer(index);
151 if (err != AVCS_ERR_OK) {
152 cout << "QueueInputBuffer fail" << endl;
153 break;
154 }
155 sendFrameIndex++;
156 }
157 }
158
WaitForEos()159 void VDecServerSample::WaitForEos()
160 {
161 if (inputLoop_ && inputLoop_->joinable()) {
162 inputLoop_->join();
163 }
164 }
165
GetOutputFormat()166 void VDecServerSample::GetOutputFormat()
167 {
168 Format fmt;
169 int32_t err = codec_->GetOutputFormat(fmt);
170 if (err != AVCS_ERR_OK) {
171 cout << "GetOutputFormat fail" << endl;
172 isRunning_.store(false);
173 signal_->inCond_.notify_all();
174 }
175 }
176
Flush()177 void VDecServerSample::Flush()
178 {
179 int32_t err = codec_->Flush();
180 if (err != AVCS_ERR_OK) {
181 cout << "Flush fail" << endl;
182 isRunning_.store(false);
183 signal_->inCond_.notify_all();
184 }
185 }
186
Reset()187 void VDecServerSample::Reset()
188 {
189 int32_t err = codec_->Reset();
190 if (err != AVCS_ERR_OK) {
191 cout << "Reset fail" << endl;
192 isRunning_.store(false);
193 signal_->inCond_.notify_all();
194 }
195 }