1 /*
2 * Copyright (C) 2022 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 "recorder_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 #include "display_type.h"
28
29 using namespace std;
30 using namespace OHOS;
31 using namespace OHOS::Media;
32 using namespace testing::ext;
33 using namespace OHOS::Media::RecorderTestParam;
34
35 // config for surface buffer flush to the queue
36 static OHOS::BufferFlushConfig g_esFlushConfig = {
37 .damage = {
38 .x = 0,
39 .y = 0,
40 .w = CODEC_BUFFER_WIDTH,
41 .h = CODEC_BUFFER_HEIGHT
42 },
43 .timestamp = 0
44 };
45
46 // config for surface buffer request from the queue
47 static OHOS::BufferRequestConfig g_esRequestConfig = {
48 .width = CODEC_BUFFER_WIDTH,
49 .height = CODEC_BUFFER_HEIGHT,
50 .strideAlignment = STRIDE_ALIGN,
51 .format = PIXEL_FMT_RGBA_8888,
52 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
53 .timeout = INT_MAX
54 };
55
56 // config for surface buffer flush to the queue
57 static OHOS::BufferFlushConfig g_yuvFlushConfig = {
58 .damage = {
59 .x = 0,
60 .y = 0,
61 .w = YUV_BUFFER_WIDTH,
62 .h = YUV_BUFFER_HEIGHT
63 },
64 .timestamp = 0
65 };
66
67 // config for surface buffer request from the queue
68 static OHOS::BufferRequestConfig g_yuvRequestConfig = {
69 .width = YUV_BUFFER_WIDTH,
70 .height = YUV_BUFFER_HEIGHT,
71 .strideAlignment = STRIDE_ALIGN,
72 .format = PIXEL_FMT_YCRCB_420_SP,
73 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
74 .timeout = INT_MAX
75 };
76
OnError(RecorderErrorType errorType,int32_t errorCode)77 void RecorderCallbackTest::OnError(RecorderErrorType errorType, int32_t errorCode)
78 {
79 cout << "Error received, errorType:" << errorType << " errorCode:" << errorCode << endl;
80 }
81
OnInfo(int32_t type,int32_t extra)82 void RecorderCallbackTest::OnInfo(int32_t type, int32_t extra)
83 {
84 cout << "Info received, Infotype:" << type << " Infocode:" << extra << endl;
85 }
86
OnAudioCaptureChange(const AudioRecorderChangeInfo & audioRecorderChangeInfo)87 void OHOS::Media::RecorderCallbackTest::OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo)
88 {
89 cout<< "AudioCaptureChange" << audioRecorderChangeInfo.capturerState << endl;
90 }
91
GetErrorCode()92 int32_t RecorderCallbackTest::GetErrorCode()
93 {
94 return errorCode_;
95 };
96
CreateRecorder()97 bool RecorderMock::CreateRecorder()
98 {
99 recorder_ = RecorderFactory::CreateRecorder();
100 return recorder_ != nullptr;
101 }
102
SetVideoSource(VideoSourceType source,int32_t & sourceId)103 int32_t RecorderMock::SetVideoSource(VideoSourceType source, int32_t &sourceId)
104 {
105 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
106 return recorder_->SetVideoSource(source, sourceId);
107 }
108
SetAudioSource(AudioSourceType source,int32_t & sourceId)109 int32_t RecorderMock::SetAudioSource(AudioSourceType source, int32_t &sourceId)
110 {
111 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
112 return recorder_->SetAudioSource(source, sourceId);
113 }
114
SetDataSource(DataSourceType dataType,int32_t & sourceId)115 int32_t RecorderMock::SetDataSource(DataSourceType dataType, int32_t &sourceId)
116 {
117 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
118 return recorder_->SetDataSource(dataType, sourceId);
119 }
120
SetOutputFormat(OutputFormatType format)121 int32_t RecorderMock::SetOutputFormat(OutputFormatType format)
122 {
123 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
124 return recorder_->SetOutputFormat(format);
125 }
126
SetVideoEncoder(int32_t sourceId,VideoCodecFormat encoder)127 int32_t RecorderMock::SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder)
128 {
129 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
130 return recorder_->SetVideoEncoder(sourceId, encoder);
131 }
132
SetVideoSize(int32_t sourceId,int32_t width,int32_t height)133 int32_t RecorderMock::SetVideoSize(int32_t sourceId, int32_t width, int32_t height)
134 {
135 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
136 return recorder_->SetVideoSize(sourceId, width, height);
137 }
138
SetVideoFrameRate(int32_t sourceId,int32_t frameRate)139 int32_t RecorderMock::SetVideoFrameRate(int32_t sourceId, int32_t frameRate)
140 {
141 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
142 return recorder_->SetVideoFrameRate(sourceId, frameRate);
143 }
144
SetVideoEncodingBitRate(int32_t sourceId,int32_t rate)145 int32_t RecorderMock::SetVideoEncodingBitRate(int32_t sourceId, int32_t rate)
146 {
147 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
148 return recorder_->SetVideoEncodingBitRate(sourceId, rate);
149 }
150
SetCaptureRate(int32_t sourceId,double fps)151 int32_t RecorderMock::SetCaptureRate(int32_t sourceId, double fps)
152 {
153 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
154 return recorder_->SetCaptureRate(sourceId, fps);
155 }
156
GetSurface(int32_t sourceId)157 OHOS::sptr<OHOS::Surface> RecorderMock::GetSurface(int32_t sourceId)
158 {
159 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, nullptr, "recorder_ == nullptr");
160 return recorder_->GetSurface(sourceId);
161 }
162
GetMetaSurface(int32_t sourceId)163 OHOS::sptr<OHOS::Surface> RecorderMock::GetMetaSurface(int32_t sourceId)
164 {
165 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, nullptr, "recorder_ == nullptr");
166 return recorder_->GetMetaSurface(sourceId);
167 }
168
SetAudioEncoder(int32_t sourceId,AudioCodecFormat encoder)169 int32_t RecorderMock::SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder)
170 {
171 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
172 return recorder_->SetAudioEncoder(sourceId, encoder);
173 }
174
SetAudioSampleRate(int32_t sourceId,int32_t rate)175 int32_t RecorderMock::SetAudioSampleRate(int32_t sourceId, int32_t rate)
176 {
177 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
178 return recorder_->SetAudioSampleRate(sourceId, rate);
179 }
180
SetAudioChannels(int32_t sourceId,int32_t num)181 int32_t RecorderMock::SetAudioChannels(int32_t sourceId, int32_t num)
182 {
183 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
184 return recorder_->SetAudioChannels(sourceId, num);
185 }
186
SetAudioEncodingBitRate(int32_t sourceId,int32_t bitRate)187 int32_t RecorderMock::SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate)
188 {
189 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
190 return recorder_->SetAudioEncodingBitRate(sourceId, bitRate);
191 }
192
SetMaxDuration(int32_t duration)193 int32_t RecorderMock::SetMaxDuration(int32_t duration)
194 {
195 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
196 return recorder_->SetMaxDuration(duration);
197 }
198
SetMaxFileSize(int64_t size)199 int32_t RecorderMock::SetMaxFileSize(int64_t size)
200 {
201 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
202 return recorder_->SetMaxFileSize(size);
203 }
204
SetOutputFile(int32_t fd)205 int32_t RecorderMock::SetOutputFile(int32_t fd)
206 {
207 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
208 return recorder_->SetOutputFile(fd);
209 }
210
SetNextOutputFile(int32_t fd)211 int32_t RecorderMock::SetNextOutputFile(int32_t fd)
212 {
213 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
214 return recorder_->SetNextOutputFile(fd);
215 }
216
SetLocation(float latitude,float longitude)217 void RecorderMock::SetLocation(float latitude, float longitude)
218 {
219 UNITTEST_CHECK_AND_RETURN_LOG(recorder_ != nullptr, "recorder_ == nullptr");
220 return recorder_->SetLocation(latitude, longitude);
221 }
222
SetOrientationHint(int32_t rotation)223 void RecorderMock::SetOrientationHint(int32_t rotation)
224 {
225 UNITTEST_CHECK_AND_RETURN_LOG(recorder_ != nullptr, "recorder_ == nullptr");
226 return recorder_->SetOrientationHint(rotation);
227 }
228
SetGenre(std::string & genre)229 int32_t OHOS::Media::RecorderMock::SetGenre(std::string &genre)
230 {
231 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
232 return recorder_->SetGenre(genre);
233 }
234
SetUserCustomInfo(Meta & userCustomInfo)235 int32_t OHOS::Media::RecorderMock::SetUserCustomInfo(Meta &userCustomInfo)
236 {
237 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
238 return recorder_->SetUserCustomInfo(userCustomInfo);
239 }
240
SetRecorderCallback(const std::shared_ptr<RecorderCallback> & callback)241 int32_t RecorderMock::SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback)
242 {
243 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
244 return recorder_->SetRecorderCallback(callback);
245 }
246
Prepare()247 int32_t RecorderMock::Prepare()
248 {
249 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
250 return recorder_->Prepare();
251 }
252
Start()253 int32_t RecorderMock::Start()
254 {
255 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
256 return recorder_->Start();
257 }
258
Pause()259 int32_t RecorderMock::Pause()
260 {
261 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
262 if (isStart_.load()) {
263 isStart_.store(false);
264 }
265 return recorder_->Pause();
266 }
267
Resume()268 int32_t RecorderMock::Resume()
269 {
270 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
271 if (!isStart_.load()) {
272 isStart_.store(true);
273 }
274 return recorder_->Resume();
275 }
276
Stop(bool block)277 int32_t RecorderMock::Stop(bool block)
278 {
279 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
280 if (!isExit_.load()) {
281 isExit_.store(true);
282 }
283 return recorder_->Stop(block);
284 }
285
Reset()286 int32_t RecorderMock::Reset()
287 {
288 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
289 if (!isExit_.load()) {
290 isExit_.store(true);
291 }
292 return recorder_->Reset();
293 }
294
Release()295 int32_t RecorderMock::Release()
296 {
297 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
298 if (!isExit_.load()) {
299 isExit_.store(true);
300 }
301 return recorder_->Release();
302 }
303
SetFileSplitDuration(FileSplitType type,int64_t timestamp,uint32_t duration)304 int32_t RecorderMock::SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration)
305 {
306 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
307 return recorder_->SetFileSplitDuration(type, timestamp, duration);
308 }
309
SetParameter(int32_t sourceId,const Format & format)310 int32_t RecorderMock::SetParameter(int32_t sourceId, const Format &format)
311 {
312 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
313 return recorder_->SetParameter(sourceId, format);
314 }
315
RequesetBuffer(const std::string & recorderType,VideoRecorderConfig & recorderConfig)316 int32_t RecorderMock::RequesetBuffer(const std::string &recorderType, VideoRecorderConfig &recorderConfig)
317 {
318 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
319 if (recorderType != PURE_AUDIO) {
320 producerSurface_ = recorder_->GetSurface(recorderConfig.videoSourceId);
321 UNITTEST_CHECK_AND_RETURN_RET_LOG(producerSurface_ != nullptr, MSERR_INVALID_OPERATION, "GetSurface failed ");
322
323 if (recorderConfig.vSource == VIDEO_SOURCE_SURFACE_ES) {
324 cout << "es source stream, get from file" << endl;
325 int32_t ret = GetStubFile();
326 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "GetStubFile failed ");
327 camereHDIThread_.reset(new(std::nothrow) std::thread(&RecorderMock::HDICreateESBuffer, this));
328 } else {
329 if (recorderConfig.videoFormat == H264) {
330 g_yuvRequestConfig.format = PIXEL_FMT_YCBCR_420_SP;
331 } else {
332 g_yuvRequestConfig.format = PIXEL_FMT_YCRCB_420_SP;
333 }
334 if (recorderType == PURE_ERROR) {
335 camereHDIThread_.reset(new(std::nothrow) std::thread(&RecorderMock::HDICreateYUVBufferError, this));
336 } else {
337 camereHDIThread_.reset(new(std::nothrow) std::thread(&RecorderMock::HDICreateYUVBuffer, this));
338 }
339 }
340 }
341 return MSERR_OK;
342 }
343
StopBuffer(const std::string & recorderType)344 void RecorderMock::StopBuffer(const std::string &recorderType)
345 {
346 if (recorderType != PURE_AUDIO && camereHDIThread_ != nullptr) {
347 camereHDIThread_->join();
348 }
349 }
350
GetStubFile()351 int32_t RecorderMock::GetStubFile()
352 {
353 file_ = std::make_shared<std::ifstream>();
354 if (file_ == nullptr) {
355 return -1;
356 }
357 UNITTEST_CHECK_AND_RETURN_RET_LOG(file_ != nullptr, MSERR_INVALID_OPERATION, "create file failed");
358 const std::string filePath = RECORDER_ROOT + "out_320_240_10s.h264";
359 file_->open(filePath, std::ios::in | std::ios::binary);
360 UNITTEST_CHECK_AND_RETURN_RET_LOG(file_->is_open(), MSERR_INVALID_OPERATION, "open file failed");
361 if (!(file_->is_open())) {
362 return -1;
363 }
364 return MSERR_OK;
365 }
366
GetPts()367 uint64_t RecorderMock::GetPts()
368 {
369 struct timespec timestamp = {0, 0};
370 clock_gettime(CLOCK_MONOTONIC, ×tamp);
371 uint64_t time = (uint64_t)timestamp.tv_sec * SEC_TO_NS + (uint64_t)timestamp.tv_nsec;
372 return time;
373 }
374
GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo & changeInfo)375 int32_t OHOS::Media::RecorderMock::GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo)
376 {
377 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
378 return recorder_->GetCurrentCapturerChangeInfo(changeInfo);
379 }
380
HDICreateESBuffer()381 void RecorderMock::HDICreateESBuffer()
382 {
383 // camera hdi loop to requeset buffer
384 const uint32_t *frameLenArray = HIGH_VIDEO_FRAME_SIZE;
385 while (count_ < STUB_STREAM_SIZE) {
386 UNITTEST_CHECK_AND_BREAK_LOG(!isExit_.load(), "close camera hdi thread");
387 usleep(FRAME_RATE);
388 OHOS::sptr<OHOS::SurfaceBuffer> buffer;
389 int32_t releaseFence;
390 OHOS::SurfaceError ret = producerSurface_->RequestBuffer(buffer, releaseFence, g_esRequestConfig);
391 UNITTEST_CHECK_AND_BREAK_LOG(ret != OHOS::SURFACE_ERROR_NO_BUFFER, "surface loop full, no buffer now");
392 UNITTEST_CHECK_AND_BREAK_LOG(ret == SURFACE_ERROR_OK && buffer != nullptr, "RequestBuffer failed");
393
394 sptr<SyncFence> tempFence = new SyncFence(releaseFence);
395 tempFence->Wait(100); // 100ms
396
397 auto addr = static_cast<uint8_t *>(buffer->GetVirAddr());
398 if (addr == nullptr) {
399 cout << "GetVirAddr failed" << endl;
400 (void)producerSurface_->CancelBuffer(buffer);
401 break;
402 }
403 char *tempBuffer = static_cast<char *>(malloc(sizeof(char) * (*frameLenArray) + 1));
404 if (tempBuffer == nullptr) {
405 (void)producerSurface_->CancelBuffer(buffer);
406 break;
407 }
408 (void)file_->read(tempBuffer, *frameLenArray);
409 if (*frameLenArray > buffer->GetSize()) {
410 free(tempBuffer);
411 (void)producerSurface_->CancelBuffer(buffer);
412 break;
413 }
414 (void)memcpy_s(addr, *frameLenArray, tempBuffer, *frameLenArray);
415
416 if (isStart_.load()) {
417 pts_= (int64_t)(GetPts());
418 isStart_.store(false);
419 }
420
421 (void)buffer->GetExtraData()->ExtraSet("dataSize", static_cast<int32_t>(*frameLenArray));
422 (void)buffer->GetExtraData()->ExtraSet("timeStamp", pts_);
423 (void)buffer->GetExtraData()->ExtraSet("isKeyFrame", isKeyFrame_);
424 count_++;
425 (count_ % 30) == 0 ? (isKeyFrame_ = 1) : (isKeyFrame_ = 0); // keyframe every 30fps
426 pts_ += FRAME_DURATION;
427 (void)producerSurface_->FlushBuffer(buffer, -1, g_esFlushConfig);
428 frameLenArray++;
429 free(tempBuffer);
430 }
431 cout << "exit camera hdi loop" << endl;
432 if ((file_ != nullptr) && (file_->is_open())) {
433 file_->close();
434 }
435 }
436
HDICreateYUVBufferError()437 void RecorderMock::HDICreateYUVBufferError()
438 {
439 // camera hdi loop to requeset buffer
440 while (count_ < STUB_STREAM_SIZE) {
441 UNITTEST_CHECK_AND_BREAK_LOG(!isExit_.load(), "close camera hdi thread");
442 usleep(FRAME_RATE);
443 OHOS::sptr<OHOS::SurfaceBuffer> buffer;
444 int32_t releaseFence;
445 OHOS::BufferRequestConfig yuvRequestConfig = {
446 .width = YUV_BUFFER_WIDTH,
447 .height = 100,
448 .strideAlignment = STRIDE_ALIGN,
449 .format = PIXEL_FMT_YCRCB_420_SP,
450 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
451 .timeout = INT_MAX
452 };
453 OHOS::SurfaceError ret = producerSurface_->RequestBuffer(buffer, releaseFence, yuvRequestConfig);
454 UNITTEST_CHECK_AND_BREAK_LOG(ret != OHOS::SURFACE_ERROR_NO_BUFFER, "surface loop full, no buffer now");
455 UNITTEST_CHECK_AND_BREAK_LOG(ret == SURFACE_ERROR_OK && buffer != nullptr, "RequestBuffer failed");
456
457 sptr<SyncFence> tempFence = new SyncFence(releaseFence);
458 tempFence->Wait(100); // 100ms
459
460 (void)srand((int)time(0));
461 color_ = color_ - 3; // 3 is the step of the pic change
462
463 if (color_ <= 0) {
464 color_ = 0xFF;
465 }
466
467 // get time
468 pts_= (int64_t)(GetPts());
469 (void)buffer->GetExtraData()->ExtraSet("dataSize", static_cast<int32_t>(YUV_BUFFER_SIZE));
470 (void)buffer->GetExtraData()->ExtraSet("timeStamp", pts_);
471 (void)buffer->GetExtraData()->ExtraSet("isKeyFrame", isKeyFrame_);
472 count_++;
473 (count_ % 30) == 0 ? (isKeyFrame_ = 1) : (isKeyFrame_ = 0); // keyframe every 30fps
474 (void)producerSurface_->FlushBuffer(buffer, -1, g_yuvFlushConfig);
475 }
476 cout << "exit camera hdi loop" << endl;
477 }
478
HDICreateYUVBuffer()479 void RecorderMock::HDICreateYUVBuffer()
480 {
481 // camera hdi loop to requeset buffer
482 while (count_ < STUB_STREAM_SIZE) {
483 UNITTEST_CHECK_AND_BREAK_LOG(!isExit_.load(), "close camera hdi thread");
484 usleep(FRAME_RATE);
485 OHOS::sptr<OHOS::SurfaceBuffer> buffer;
486 int32_t releaseFence;
487 OHOS::SurfaceError ret = producerSurface_->RequestBuffer(buffer, releaseFence, g_yuvRequestConfig);
488 UNITTEST_CHECK_AND_BREAK_LOG(ret != OHOS::SURFACE_ERROR_NO_BUFFER, "surface loop full, no buffer now");
489 UNITTEST_CHECK_AND_BREAK_LOG(ret == SURFACE_ERROR_OK && buffer != nullptr, "RequestBuffer failed");
490
491 sptr<SyncFence> tempFence = new SyncFence(releaseFence);
492 tempFence->Wait(100); // 100ms
493
494 char *tempBuffer = static_cast<char *>(buffer->GetVirAddr());
495 (void)memset_s(tempBuffer, YUV_BUFFER_SIZE, color_, YUV_BUFFER_SIZE);
496
497 (void)srand((int)time(0));
498 for (uint32_t i = 0; i < YUV_BUFFER_SIZE - 1; i += (YUV_BUFFER_SIZE - 1)) { // 100 is the steps between noise
499 tempBuffer[i] = (unsigned char)(rand() % 255); // 255 is the size of yuv, add noise
500 }
501
502 color_ = color_ - 3; // 3 is the step of the pic change
503
504 if (color_ <= 0) {
505 color_ = 0xFF;
506 }
507
508 // get time
509 pts_= (int64_t)(GetPts());
510 (void)buffer->GetExtraData()->ExtraSet("dataSize", static_cast<int32_t>(YUV_BUFFER_SIZE));
511 (void)buffer->GetExtraData()->ExtraSet("timeStamp", pts_);
512 (void)buffer->GetExtraData()->ExtraSet("isKeyFrame", isKeyFrame_);
513 count_++;
514 (count_ % 30) == 0 ? (isKeyFrame_ = 1) : (isKeyFrame_ = 0); // keyframe every 30fps
515 (void)producerSurface_->FlushBuffer(buffer, -1, g_yuvFlushConfig);
516 }
517 cout << "exit camera hdi loop" << endl;
518 }
519
CameraServicesForVideo(VideoRecorderConfig & recorderConfig) const520 int32_t RecorderMock::CameraServicesForVideo(VideoRecorderConfig &recorderConfig) const
521 {
522 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
523 int32_t ret = recorder_->SetVideoEncoder(recorderConfig.videoSourceId,
524 recorderConfig.videoFormat);
525 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoEncoder failed ");
526
527 ret = recorder_->SetVideoSize(recorderConfig.videoSourceId,
528 recorderConfig.width, recorderConfig.height);
529 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoSize failed ");
530
531 ret = recorder_->SetVideoFrameRate(recorderConfig.videoSourceId, recorderConfig.frameRate);
532 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoFrameRate failed ");
533
534 ret = recorder_->SetVideoEncodingBitRate(recorderConfig.videoSourceId,
535 recorderConfig.videoEncodingBitRate);
536 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoEncodingBitRate failed ");
537 return MSERR_OK;
538 }
539
CameraServicesForAudio(VideoRecorderConfig & recorderConfig) const540 int32_t RecorderMock::CameraServicesForAudio(VideoRecorderConfig &recorderConfig) const
541 {
542 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
543 int32_t ret = recorder_->SetAudioEncoder(recorderConfig.audioSourceId, recorderConfig.audioFormat);
544 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioEncoder failed ");
545
546 ret = recorder_->SetAudioSampleRate(recorderConfig.audioSourceId, recorderConfig.sampleRate);
547 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioSampleRate failed ");
548
549 ret = recorder_->SetAudioChannels(recorderConfig.audioSourceId, recorderConfig.channelCount);
550 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioChannels failed ");
551
552 ret = recorder_->SetAudioEncodingBitRate(recorderConfig.audioSourceId,
553 recorderConfig.audioEncodingBitRate);
554 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioEncodingBitRate failed ");
555
556 return MSERR_OK;
557 }
558
SetAudVidFormat(const std::string & recorderType,VideoRecorderConfig & recorderConfig) const559 int32_t RecorderMock::SetAudVidFormat(const std::string &recorderType, VideoRecorderConfig &recorderConfig) const
560 {
561 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
562 int32_t ret = 0;
563 ret = recorder_->SetVideoSource(recorderConfig.vSource, recorderConfig.videoSourceId);
564 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoSource failed ");
565 ret = recorder_->SetAudioSource(recorderConfig.aSource, recorderConfig.audioSourceId);
566 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioSource failed ");
567 if (recorderConfig.metaSourceType == MetaSourceType::VIDEO_META_MAKER_INFO) {
568 ret = recorder_->SetMetaSource(recorderConfig.metaSourceType, recorderConfig.metaSourceId);
569 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetMetaSource failed ");
570 }
571 ret = recorder_->SetOutputFormat(recorderConfig.outPutFormat);
572 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetOutputFormat failed ");
573 ret = recorder_->SetVideoEnableTemporalScale(recorderConfig.videoSourceId, recorderConfig.enableTemporalScale);
574 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION,
575 "SetVideoEnableTemporalScale failed ");
576 ret = CameraServicesForVideo(recorderConfig);
577 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "CameraServicesForVideo failed ");
578 ret = CameraServicesForAudio(recorderConfig);
579 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "CameraServicesForAudio failed ");
580 if (recorderConfig.metaSourceType == MetaSourceType::VIDEO_META_MAKER_INFO) {
581 ret = recorder_->SetMetaConfigs(recorderConfig.metaSourceId);
582 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetMetaConfigs failed ");
583 }
584 return ret;
585 }
586
SetFormat(const std::string & recorderType,VideoRecorderConfig & recorderConfig) const587 int32_t RecorderMock::SetFormat(const std::string &recorderType, VideoRecorderConfig &recorderConfig) const
588 {
589 UNITTEST_CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_INVALID_OPERATION, "recorder_ == nullptr");
590 int32_t ret = 0;
591 if (recorderType == PURE_VIDEO) {
592 ret = recorder_->SetVideoSource(recorderConfig.vSource, recorderConfig.videoSourceId);
593 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetVideoSource failed ");
594 ret = recorder_->SetOutputFormat(recorderConfig.outPutFormat);
595 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetOutputFormat failed ");
596 ret = recorder_->SetVideoEnableTemporalScale(recorderConfig.videoSourceId, recorderConfig.enableTemporalScale);
597 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION,
598 "SetVideoEnableTemporalScale failed ");
599 ret = CameraServicesForVideo(recorderConfig);
600 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "CameraServices failed ");
601 } else if (recorderType == PURE_AUDIO) {
602 ret = recorder_->SetAudioSource(recorderConfig.aSource, recorderConfig.audioSourceId);
603 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudioSource failed ");
604 ret = recorder_->SetOutputFormat(recorderConfig.outPutFormat);
605 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetOutputFormat failed ");
606 ret = CameraServicesForAudio(recorderConfig);
607 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "CameraServicesForAudio failed ");
608 } else if (recorderType == AUDIO_VIDEO) {
609 ret = SetAudVidFormat(recorderType, recorderConfig);
610 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetAudVidFormat failed ");
611 }
612
613 ret = recorder_->SetMaxDuration(recorderConfig.duration);
614 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetMaxDuration failed ");
615
616 ret = recorder_->SetOutputFile(recorderConfig.outputFd);
617 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetOutputFile failed ");
618
619 std::shared_ptr<RecorderCallbackTest> cb = std::make_shared<RecorderCallbackTest>();
620 ret = recorder_->SetRecorderCallback(cb);
621 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "SetRecorderCallback failed ");
622 cout << "set format finished" << endl;
623 return ret;
624 }