1 /*
2 * Copyright (C) 2024-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
16 #include "transcoder_mock.h"
17 #include <sync_fence.h>
18 #include <cstdio>
19 #include <fstream>
20 #include <iostream>
21 #include <sstream>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::Media;
31 using namespace testing::ext;
32 using namespace OHOS::Media::TranscoderTestParam;
33
OnError(int32_t errorCode,const std::string & errorMsg)34 void TransCoderCallbackTest::OnError(int32_t errorCode, const std::string &errorMsg)
35 {
36 cout << "Error received, errorType:" << errorCode << " errorCode:" << errorMsg << endl;
37 }
38
OnInfo(int32_t type,int32_t extra)39 void TransCoderCallbackTest::OnInfo(int32_t type, int32_t extra)
40 {
41 cout << "Info received, Infotype:" << type << " Infocode:" << extra << endl;
42 }
43
CreateTranscoder()44 bool TranscoderMock::CreateTranscoder()
45 {
46 transcoder_ = TransCoderFactory::CreateTransCoder();
47 return transcoder_ != nullptr;
48 }
49
SetOutputFormat(OutputFormatType format)50 int32_t TranscoderMock::SetOutputFormat(OutputFormatType format)
51 {
52 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
53 return transcoder_->SetOutputFormat(format);
54 }
55
SetVideoEncoder(VideoCodecFormat encoder)56 int32_t TranscoderMock::SetVideoEncoder(VideoCodecFormat encoder)
57 {
58 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
59 return transcoder_->SetVideoEncoder(encoder);
60 }
61
SetVideoEncodingBitRate(int32_t rate)62 int32_t TranscoderMock::SetVideoEncodingBitRate(int32_t rate)
63 {
64 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
65 return transcoder_->SetVideoEncodingBitRate(rate);
66 }
67
SetVideoSize(int32_t videoFrameWidth,int32_t videoFrameHeight)68 int32_t TranscoderMock::SetVideoSize(int32_t videoFrameWidth, int32_t videoFrameHeight)
69 {
70 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
71 return transcoder_->SetVideoSize(videoFrameWidth, videoFrameHeight);
72 }
73
SetColorSpace(TranscoderColorSpace colorSpaceFormat)74 int32_t TranscoderMock::SetColorSpace(TranscoderColorSpace colorSpaceFormat)
75 {
76 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
77 return transcoder_->SetColorSpace(colorSpaceFormat);
78 }
79
SetEnableBFrame(bool enableBFrame)80 int32_t TranscoderMock::SetEnableBFrame(bool enableBFrame)
81 {
82 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
83 return transcoder_->SetEnableBFrame(enableBFrame);
84 }
85
SetAudioEncoder(AudioCodecFormat encoder)86 int32_t TranscoderMock::SetAudioEncoder(AudioCodecFormat encoder)
87 {
88 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
89 return transcoder_->SetAudioEncoder(encoder);
90 }
91
SetAudioEncodingBitRate(int32_t bitRate)92 int32_t TranscoderMock::SetAudioEncodingBitRate(int32_t bitRate)
93 {
94 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
95 return transcoder_->SetAudioEncodingBitRate(bitRate);
96 }
97
SetInputFile(int32_t fd,int64_t offset,int64_t size)98 int32_t TranscoderMock::SetInputFile(int32_t fd, int64_t offset, int64_t size)
99 {
100 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
101 return transcoder_->SetInputFile(fd, offset, size);
102 }
103
SetOutputFile(int32_t fd)104 int32_t TranscoderMock::SetOutputFile(int32_t fd)
105 {
106 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
107 return transcoder_->SetOutputFile(fd);
108 }
109
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)110 int32_t TranscoderMock::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
111 {
112 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
113 return transcoder_->SetTransCoderCallback(callback);
114 }
115
Prepare()116 int32_t TranscoderMock::Prepare()
117 {
118 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
119 return transcoder_->Prepare();
120 }
121
Start()122 int32_t TranscoderMock::Start()
123 {
124 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
125 return transcoder_->Start();
126 }
127
Pause()128 int32_t TranscoderMock::Pause()
129 {
130 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
131 if (!isExit_.load()) {
132 isExit_.store(true);
133 }
134 return transcoder_->Pause();
135 }
136
Resume()137 int32_t TranscoderMock::Resume()
138 {
139 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
140 if (!isExit_.load()) {
141 isExit_.store(true);
142 }
143 return transcoder_->Resume();
144 }
145
Cancel()146 int32_t TranscoderMock::Cancel()
147 {
148 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
149 if (!isExit_.load()) {
150 isExit_.store(true);
151 }
152 return transcoder_->Cancel();
153 }
154
Release()155 int32_t TranscoderMock::Release()
156 {
157 UNITTEST_CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, MSERR_INVALID_OPERATION, "transcoder_ == nullptr");
158 if (!isExit_.load()) {
159 isExit_.store(true);
160 }
161 return transcoder_->Release();
162 }