* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <arpa/inet.h>
#include <sys/time.h>
#include <utility>
#include "vp9serverdec_sample.h"
#include <iostream>
#include "vpx_decoder_api.h"
#include "window.h"
#include "window_manager.h"
#include "window_option.h"
#include "video_decoder.h"
using namespace OHOS;
using namespace OHOS::Media;
using namespace OHOS::MediaAVCodec;
using namespace OHOS::MediaAVCodec::Codec;
using namespace std;
namespace {
constexpr int32_t WIDTH = 1920;
constexpr int32_t HEIGHT = 1080;
constexpr int32_t FORMAT = 2;
constexpr int32_t ANGLE = 0;
constexpr int32_t FRAME_RATE = 30;
constexpr int32_t TIME = 12345;
constexpr int32_t MAX_SEND_FRAMES = 10;
}
void Vp9VDecServerSample::CallBack::OnError(AVCodecErrorType errorType, int32_t errorCode)
{
tester->Flush();
tester->Reset();
}
void Vp9VDecServerSample::CallBack::OnOutputFormatChanged(const Format &format)
{
tester->GetOutputFormat();
}
void Vp9VDecServerSample::CallBack::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
{
unique_lock<mutex> lock(tester->signal_->inMutex_);
tester->signal_->inIdxQueue_.push(index);
tester->signal_->inBufferQueue_.push(buffer);
tester->signal_->inCond_.notify_all();
}
void Vp9VDecServerSample::CallBack::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
{
tester->codec_->ReleaseOutputBuffer(index);
}
Vp9VDecServerSample::~Vp9VDecServerSample()
{
if (codec_ != nullptr) {
codec_->Stop();
codec_->Release();
VideoDecoder *codec = static_cast<VideoDecoder*>(codec_.get());
codec->DecStrongRef(codec);
}
}
int32_t Vp9VDecServerSample::ConfigServerDecoder()
{
Format fmt;
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, WIDTH);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, HEIGHT);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, FORMAT);
fmt.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, FRAME_RATE);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, ANGLE);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
return codec_->Configure(fmt);
}
int32_t Vp9VDecServerSample::SetParameter()
{
Format fmt;
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, WIDTH);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, HEIGHT);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, FORMAT);
fmt.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, FRAME_RATE);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, ANGLE);
fmt.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, ScalingMode::SCALING_MODE_SCALE_TO_WINDOW);
return codec_->SetParameter(fmt);
}
int32_t Vp9VDecServerSample::SetCallback()
{
shared_ptr<CallBack> cb = make_shared<CallBack>(this);
return codec_->SetCallback(cb);
}
int32_t Vp9VDecServerSample::SetOutputSurface()
{
auto cs = Surface::CreateSurfaceAsConsumer();
cs_vector.push_back(cs);
sptr<IBufferConsumerListener> listener = new ConsumerListener(cs);
cs->RegisterConsumerListener(listener);
auto p = cs->GetProducer();
auto ps = Surface::CreateSurfaceAsProducer(p);
ps_vector.push_back(ps);
return codec_->SetOutputSurface(ps);
}
int32_t Vp9VDecServerSample::InitDecoder()
{
int32_t err;
Media::Meta codecInfo;
int32_t instanceid = 0;
codecInfo.SetData("av_codec_event_info_instance_id", instanceid);
err = codec_->Init(codecInfo);
if (err != AVCS_ERR_OK) {
cout << "VP9 decoder Init failed!" << endl;
return err;
}
std::vector<CapabilityData> caps;
err = GetVpxDecoderCapabilityList(caps);
if (err != AVCS_ERR_OK) {
cout << "VP9 GetVp9DecoderCapabilityList failed" << endl;
return err;
}
err = ConfigServerDecoder();
if (err != AVCS_ERR_OK) {
cout << "VP9 ConfigServerDecoder failed" << endl;
return err;
}
err = SetCallback();
if (err != AVCS_ERR_OK) {
cout << "VP9 SetCallback failed" << endl;
return err;
}
signal_ = std::make_shared<VDecSignal>();
if (signal_ == nullptr) {
cout << "VP9 Failed to new VDecSignal" << endl;
err = AVCS_ERR_NO_MEMORY;
return err;
}
err = SetOutputSurface();
if (err != AVCS_ERR_OK) {
cout << "VP9 SetOutputSurface failed" << endl;
return err;
}
return err;
}
void Vp9VDecServerSample::RunVideoServerSurfaceDecoder()
{
CreateVpxDecoderByName("OH.Media.Codec.Decoder.Video.VP9", codec_);
if (codec_ == nullptr) {
cout << "VP9 Create failed" << endl;
return;
}
int32_t err = InitDecoder();
if (err != AVCS_ERR_OK) {
cout << "VP9 Init decoder failed" << endl;
return;
}
err = codec_->Start();
if (err != AVCS_ERR_OK) {
cout << "VP9 Start failed" << endl;
return;
}
isRunning_.store(true);
inputLoop_ = make_unique<thread>(&Vp9VDecServerSample::InputFunc, this);
if (inputLoop_ == nullptr) {
cout << "VP9 Failed to create input loop" << endl;
isRunning_.store(false);
}
err = SetOutputSurface();
if (err != AVCS_ERR_OK) {
cout << "VP9 SetOutputSurface 2 failed" << endl;
return;
}
err = SetParameter();
if (err != AVCS_ERR_OK) {
cout << "VP9 SetParameter failed" << endl;
return;
}
GetOutputFormat();
}
void Vp9VDecServerSample::InputFunc()
{
while (sendFrameIndex < MAX_SEND_FRAMES) {
if (!isRunning_.load()) {
break;
}
unique_lock<mutex> lock(signal_->inMutex_);
signal_->inCond_.wait(lock, [this]() {
if (!isRunning_.load()) {
cout << "VP9 quit signal" << endl;
return true;
}
return signal_->inIdxQueue_.size() > 0;
});
if (!isRunning_.load()) {
break;
}
uint32_t index = signal_->inIdxQueue_.front();
auto buffer = signal_->inBufferQueue_.front();
signal_->inIdxQueue_.pop();
signal_->inBufferQueue_.pop();
lock.unlock();
if (buffer->memory_ == nullptr) {
isRunning_.store(false);
break;
}
uint8_t *bufferAddr = buffer->memory_->GetAddr();
if (memcpy_s(bufferAddr, buffer->memory_->GetCapacity(), fuzzData, fuzzSize) != EOK) {
break;
}
buffer->pts_ = TIME;
buffer->flag_ = 0;
buffer->memory_->SetOffset(0);
buffer->memory_->SetSize(fuzzSize);
int32_t err = codec_->QueueInputBuffer(index);
if (err != AVCS_ERR_OK) {
cout << "VP9 QueueInputBuffer fail" << endl;
break;
}
sendFrameIndex++;
}
}
void Vp9VDecServerSample::WaitForEos()
{
if (inputLoop_ && inputLoop_->joinable()) {
inputLoop_->join();
}
}
void Vp9VDecServerSample::GetOutputFormat()
{
Format fmt;
int32_t err = codec_->GetOutputFormat(fmt);
if (err != AVCS_ERR_OK) {
cout << "VP9 GetOutputFormat fail" << endl;
isRunning_.store(false);
signal_->inCond_.notify_all();
}
}
void Vp9VDecServerSample::Flush()
{
int32_t err = codec_->Flush();
if (err != AVCS_ERR_OK) {
cout << "VP9 Flush fail" << endl;
isRunning_.store(false);
signal_->inCond_.notify_all();
}
}
void Vp9VDecServerSample::Reset()
{
int32_t err = codec_->Reset();
if (err != AVCS_ERR_OK) {
cout << "VP9 Reset fail" << endl;
isRunning_.store(false);
signal_->inCond_.notify_all();
}
}
void Vp9VDecServerSample::Stop()
{
int32_t err = codec_->Stop();
if (err != AVCS_ERR_OK) {
cout << "VP9 Stop fail" << endl;
isRunning_.store(false);
signal_->inCond_.notify_all();
}
}