• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include "venc_sample.h"
17 #include <gtest/gtest.h>
18 
19 using namespace std;
20 using namespace OHOS::MediaAVCodec::VCodecTestParam;
21 namespace OHOS {
22 namespace MediaAVCodec {
VEncCallbackTest(std::shared_ptr<VEncSignal> signal)23 VEncCallbackTest::VEncCallbackTest(std::shared_ptr<VEncSignal> signal) : signal_(signal) {}
24 
~VEncCallbackTest()25 VEncCallbackTest::~VEncCallbackTest() {}
26 
OnError(int32_t errorCode)27 void VEncCallbackTest::OnError(int32_t errorCode)
28 {
29     cout << "ADec Error errorCode=" << errorCode;
30     if (signal_ == nullptr) {
31         return;
32     }
33     signal_->errorNum_ += 1;
34     cout << ", errorNum=" << signal_->errorNum_ << endl;
35 }
36 
OnStreamChanged(std::shared_ptr<FormatMock> format)37 void VEncCallbackTest::OnStreamChanged(std::shared_ptr<FormatMock> format)
38 {
39     (void)format;
40     cout << "VEnc Format Changed" << endl;
41 }
42 
OnNeedInputData(uint32_t index,std::shared_ptr<AVMemoryMock> data)43 void VEncCallbackTest::OnNeedInputData(uint32_t index, std::shared_ptr<AVMemoryMock> data)
44 {
45     if (signal_ == nullptr) {
46         return;
47     }
48     unique_lock<mutex> lock(signal_->inMutex_);
49     if (!signal_->isRunning_.load()) {
50         return;
51     }
52     signal_->inIndexQueue_.push(index);
53     signal_->inBufferQueue_.push(data);
54     signal_->inCond_.notify_all();
55 }
56 
OnNewOutputData(uint32_t index,std::shared_ptr<AVMemoryMock> data,OH_AVCodecBufferAttr attr)57 void VEncCallbackTest::OnNewOutputData(uint32_t index, std::shared_ptr<AVMemoryMock> data, OH_AVCodecBufferAttr attr)
58 {
59     if (signal_ == nullptr) {
60         return;
61     }
62     unique_lock<mutex> lock(signal_->outMutex_);
63     if (!signal_->isRunning_.load()) {
64         return;
65     }
66     signal_->outIndexQueue_.push(index);
67     signal_->outBufferQueue_.push(data);
68     signal_->outAttrQueue_.push(attr);
69     signal_->outCond_.notify_all();
70 }
71 
VideoEncSample(std::shared_ptr<VEncSignal> signal)72 VideoEncSample::VideoEncSample(std::shared_ptr<VEncSignal> signal) : signal_(signal) {}
73 
~VideoEncSample()74 VideoEncSample::~VideoEncSample()
75 {
76     if (videoEnc_ != nullptr) {
77         (void)videoEnc_->Release();
78     }
79 }
80 
CreateVideoEncMockByMime(const std::string & mime)81 bool VideoEncSample::CreateVideoEncMockByMime(const std::string &mime)
82 {
83     videoEnc_ = VCodecMockFactory::CreateVideoEncMockByMime(mime);
84     return videoEnc_ != nullptr;
85 }
86 
CreateVideoEncMockByName(const std::string & name)87 bool VideoEncSample::CreateVideoEncMockByName(const std::string &name)
88 {
89     videoEnc_ = VCodecMockFactory::CreateVideoEncMockByName(name);
90     return videoEnc_ != nullptr;
91 }
92 
SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)93 int32_t VideoEncSample::SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)
94 {
95     if (videoEnc_ == nullptr) {
96         return AV_ERR_INVALID_VAL;
97     }
98     return videoEnc_->SetCallback(cb);
99 }
100 
Configure(std::shared_ptr<FormatMock> format)101 int32_t VideoEncSample::Configure(std::shared_ptr<FormatMock> format)
102 {
103     if (videoEnc_ == nullptr) {
104         return AV_ERR_INVALID_VAL;
105     }
106     return videoEnc_->Configure(format);
107 }
108 
Start()109 int32_t VideoEncSample::Start()
110 {
111     if (videoEnc_ == nullptr) {
112         return AV_ERR_INVALID_VAL;
113     }
114     int32_t ret = videoEnc_->Start();
115     return ret;
116 }
117 
Stop()118 int32_t VideoEncSample::Stop()
119 {
120     if (videoEnc_ == nullptr) {
121         return AV_ERR_INVALID_VAL;
122     }
123     return videoEnc_->Stop();
124 }
125 
Flush()126 int32_t VideoEncSample::Flush()
127 {
128     if (videoEnc_ == nullptr) {
129         return AV_ERR_INVALID_VAL;
130     }
131     return videoEnc_->Flush();
132 }
133 
Reset()134 int32_t VideoEncSample::Reset()
135 {
136     if (videoEnc_ == nullptr) {
137         return AV_ERR_INVALID_VAL;
138     }
139     return videoEnc_->Reset();
140 }
141 
Release()142 int32_t VideoEncSample::Release()
143 {
144     if (videoEnc_ == nullptr) {
145         return AV_ERR_INVALID_VAL;
146     }
147     return videoEnc_->Release();
148 }
149 
GetOutputDescription()150 std::shared_ptr<FormatMock> VideoEncSample::GetOutputDescription()
151 {
152     if (videoEnc_ == nullptr) {
153         return nullptr;
154     }
155     return videoEnc_->GetOutputDescription();
156 }
157 
SetParameter(std::shared_ptr<FormatMock> format)158 int32_t VideoEncSample::SetParameter(std::shared_ptr<FormatMock> format)
159 {
160     if (videoEnc_ == nullptr) {
161         return AV_ERR_INVALID_VAL;
162     }
163     return videoEnc_->SetParameter(format);
164 }
165 
PushInputData(uint32_t index,OH_AVCodecBufferAttr & attr)166 int32_t VideoEncSample::PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr)
167 {
168     if (videoEnc_ == nullptr) {
169         return AV_ERR_INVALID_VAL;
170     }
171     return videoEnc_->PushInputData(index, attr);
172 }
173 
NotifyEos()174 int32_t VideoEncSample::NotifyEos()
175 {
176     if (videoEnc_ == nullptr) {
177         return AV_ERR_INVALID_VAL;
178     }
179     return videoEnc_->NotifyEos();
180 }
181 
FreeOutputData(uint32_t index)182 int32_t VideoEncSample::FreeOutputData(uint32_t index)
183 {
184     if (videoEnc_ == nullptr) {
185         return AV_ERR_INVALID_VAL;
186     }
187     return videoEnc_->FreeOutputData(index);
188 }
189 
IsValid()190 bool VideoEncSample::IsValid()
191 {
192     if (videoEnc_ == nullptr) {
193         return false;
194     }
195     return videoEnc_->IsValid();
196 }
197 } // namespace MediaAVCodec
198 } // namespace OHOS
199