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 #ifndef RECORDER_MOCK_H 16 #define RECORDER_MOCK_H 17 18 #include <atomic> 19 #include <thread> 20 #include <string> 21 #include "gtest/gtest.h" 22 #include "media_errors.h" 23 #include "recorder.h" 24 #include "unittest_log.h" 25 #include "recorder.h" 26 #include "surface.h" 27 #include "securec.h" 28 #include "nocopyable.h" 29 30 namespace OHOS { 31 namespace Media { 32 namespace RecorderTestParam { 33 constexpr uint32_t STUB_STREAM_SIZE = 602; 34 constexpr uint32_t FRAME_RATE = 30000; 35 constexpr uint32_t CODEC_BUFFER_WIDTH = 1024; 36 constexpr uint32_t CODEC_BUFFER_HEIGHT = 25; 37 constexpr uint32_t YUV_BUFFER_WIDTH = 1280; 38 constexpr uint32_t YUV_BUFFER_HEIGHT = 720; 39 constexpr uint32_t STRIDE_ALIGN = 8; 40 constexpr uint32_t FRAME_DURATION = 40000000; 41 constexpr uint32_t RECORDER_TIME = 5; 42 constexpr uint32_t YUV_BUFFER_SIZE = YUV_BUFFER_WIDTH * YUV_BUFFER_HEIGHT * 3 / 2; 43 constexpr uint32_t SEC_TO_NS = 1000000000; 44 const std::string RECORDER_ROOT = "/data/test/media/"; 45 const std::string PURE_VIDEO = "video"; 46 const std::string PURE_AUDIO = "audio"; 47 const std::string AUDIO_VIDEO = "av"; 48 const std::string PURE_ERROR = "error"; 49 // this array contains each buffer size of the stub stream 50 const uint32_t HIGH_VIDEO_FRAME_SIZE[STUB_STREAM_SIZE] = { 51 13571, 321, 72, 472, 68, 76, 79, 509, 90, 677, 88, 956, 99, 347, 77, 452, 681, 81, 1263, 94, 106, 97, 998, 52 97, 797, 93, 1343, 150, 116, 117, 926, 1198, 128, 110, 78, 1582, 158, 135, 112, 1588, 165, 132, 128, 1697, 53 168, 149, 117, 1938, 170, 141, 142, 1830, 106, 161, 122, 1623, 160, 154, 156, 1998, 230, 177, 139, 1650, 54 186, 128, 134, 1214, 122, 1411, 120, 1184, 128, 1591, 195, 145, 105, 1587, 169, 140, 118, 1952, 177, 150, 55 161, 1437, 159, 123, 1758, 180, 165, 144, 1936, 214, 191, 175, 2122, 180, 179, 160, 1927, 161, 184, 119, 56 1973, 218, 210, 129, 1962, 196, 127, 154, 2308, 173, 127, 1572, 142, 122, 2065, 262, 159, 206, 2251, 269, 57 179, 170, 2056, 308, 168, 191, 2090, 303, 191, 110, 1932, 272, 162, 122, 1877, 245, 167, 141, 1908, 294, 58 162, 118, 1493, 132, 1782, 273, 184, 133, 1958, 274, 180, 149, 2070, 216, 169, 143, 1882, 224, 149, 139, 59 1749, 277, 184, 139, 2141, 197, 170, 140, 2002, 269, 162, 140, 1862, 202, 179, 131, 1868, 214, 164, 140, 60 1546, 226, 150, 130, 1707, 162, 146, 1824, 181, 147, 130, 1898, 209, 143, 131, 1805, 180, 148, 106, 1776, 61 147, 141, 1572, 177, 130, 105, 1776, 178, 144, 122, 1557, 142, 124, 114, 1436, 143, 126, 1326, 127, 1755, 62 169, 127, 105, 1807, 177, 131, 134, 1613, 187, 137, 136, 1314, 134, 118, 2005, 194, 129, 147, 1566, 185, 63 132, 131, 1236, 174, 137, 106, 11049, 574, 126, 1242, 188, 130, 119, 1450, 187, 137, 141, 1116, 124, 1848, 64 138, 122, 1605, 186, 127, 140, 1798, 170, 124, 121, 1666, 157, 128, 130, 1678, 135, 118, 1804, 169, 135, 65 125, 1837, 168, 124, 124, 2049, 180, 122, 128, 1334, 143, 128, 1379, 116, 1884, 149, 122, 150, 1962, 176, 66 122, 122, 1197, 139, 1853, 184, 151, 148, 1692, 209, 129, 126, 1736, 149, 135, 104, 1775, 165, 160, 121, 67 1653, 163, 123, 112, 1907, 181, 129, 107, 1808, 177, 125, 111, 2405, 166, 144, 114, 1833, 198, 136, 113, 68 1960, 206, 139, 116, 1791, 175, 130, 129, 1909, 194, 138, 119, 1807, 160, 156, 124, 1998, 184, 173, 114, 69 2069, 181, 127, 139, 2212, 182, 138, 146, 1993, 214, 135, 139, 2286, 194, 137, 120, 2090, 196, 159, 132, 70 2294, 194, 148, 137, 2312, 183, 163, 106, 2118, 201, 158, 127, 2291, 187, 144, 116, 2413, 139, 115, 2148, 71 178, 122, 103, 2370, 207, 161, 117, 2291, 213, 159, 129, 2244, 243, 157, 133, 2418, 255, 171, 127, 2316, 72 185, 160, 132, 2405, 220, 165, 155, 2539, 219, 172, 128, 2433, 199, 154, 119, 1681, 140, 1960, 143, 2682, 73 202, 153, 127, 2794, 239, 158, 155, 2643, 229, 172, 125, 2526, 201, 181, 159, 2554, 233, 167, 125, 2809, 74 205, 164, 117, 2707, 221, 156, 138, 2922, 240, 160, 146, 2952, 267, 177, 149, 3339, 271, 175, 136, 3006, 75 242, 168, 141, 3068, 232, 194, 149, 2760, 214, 208, 143, 2811, 218, 184, 149, 137, 15486, 2116, 235, 167, 76 157, 2894, 305, 184, 139, 3090, 345, 179, 155, 3226, 347, 160, 164, 3275, 321, 184, 174, 3240, 269, 166, 77 170, 3773, 265, 169, 155, 3023, 301, 188, 161, 3343, 275, 174, 155, 3526, 309, 177, 173, 3546, 307, 183, 78 149, 3648, 295, 213, 170, 3568, 305, 198, 166, 3641, 297, 172, 148, 3608, 301, 200, 159, 3693, 322, 209, 79 166, 3453, 318, 206, 162, 3696, 341, 200, 176, 3386, 320, 192, 176, 3903, 373, 207, 187, 3305, 361, 200, 80 202, 3110, 367, 220, 197, 2357, 332, 196, 201, 1827, 377, 187, 199, 860, 472, 173, 223, 238}; 81 82 struct VideoRecorderConfig { 83 int32_t audioSourceId = 0; 84 int32_t videoSourceId = 0; 85 int32_t metaSourceId = 0; 86 int32_t audioEncodingBitRate = 48000; 87 int32_t channelCount = 2; 88 int32_t duration = 60; 89 int32_t width = YUV_BUFFER_WIDTH; 90 int32_t height = YUV_BUFFER_HEIGHT; 91 int32_t frameRate = 30; 92 int32_t videoEncodingBitRate = 2000000; 93 int32_t sampleRate = 48000; 94 double captureFps = 30; 95 int32_t outputFd = 0; 96 AudioCodecFormat audioFormat = AAC_LC; 97 AudioSourceType aSource = AUDIO_SOURCE_DEFAULT; 98 OutputFormatType outPutFormat = FORMAT_MPEG_4; 99 VideoSourceType vSource = VIDEO_SOURCE_SURFACE_ES; 100 VideoCodecFormat videoFormat = H264; 101 MetaSourceType metaSourceType = MetaSourceType::VIDEO_META_SOURCE_INVALID; 102 bool enableTemporalScale = false; 103 float latitude = 30.0; 104 float longitude = 60.0; 105 std::string genre = "hi"; 106 }; 107 struct AudioRecorderConfig { 108 int32_t outputFd = 0; 109 int32_t audioSourceId = 0; 110 int32_t audioEncodingBitRate = 48000; 111 int32_t channelCount = 2; 112 int32_t duration = 60; 113 int32_t sampleRate = 48000; 114 AudioCodecFormat audioFormat = AAC_LC; 115 AudioSourceType inputSource = AUDIO_MIC; 116 OutputFormatType outPutFormat = FORMAT_M4A; 117 }; 118 } // namespace RecorderTestParam 119 class RecorderMock { 120 public: 121 RecorderMock() = default; 122 ~RecorderMock() = default; 123 bool CreateRecorder(); 124 int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId); 125 int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId); 126 int32_t SetDataSource(DataSourceType dataType, int32_t &sourceId); 127 int32_t SetOutputFormat(OutputFormatType format); 128 int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder); 129 int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height); 130 int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate); 131 int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate); 132 int32_t SetCaptureRate(int32_t sourceId, double fps); 133 OHOS::sptr<OHOS::Surface> GetSurface(int32_t sourceId); 134 OHOS::sptr<OHOS::Surface> GetMetaSurface(int32_t sourceId); 135 int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder); 136 int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate); 137 int32_t SetAudioChannels(int32_t sourceId, int32_t num); 138 int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate); 139 int32_t SetMaxDuration(int32_t duration); 140 int32_t SetMaxFileSize(int64_t size); 141 int32_t SetOutputFile(int32_t fd); 142 int32_t SetNextOutputFile(int32_t fd); 143 void SetLocation(float latitude, float longitude); 144 void SetOrientationHint(int32_t rotation); 145 int32_t SetGenre(std::string &genre); 146 int32_t SetUserCustomInfo(Meta &userCustomInfo); 147 int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback); 148 int32_t Prepare(); 149 int32_t Start(); 150 int32_t Pause(); 151 int32_t Resume(); 152 int32_t Stop(bool block); 153 int32_t Reset(); 154 int32_t Release(); 155 int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration); 156 int32_t SetParameter(int32_t sourceId, const Format &format); 157 int32_t RequesetBuffer(const std::string &recorderType, RecorderTestParam::VideoRecorderConfig &recorderConfig); 158 void StopBuffer(const std::string &recorderType); 159 void HDICreateESBuffer(); 160 void HDICreateYUVBuffer(); 161 void HDICreateYUVBufferError(); 162 int32_t CameraServicesForVideo(RecorderTestParam::VideoRecorderConfig &recorderConfig) const; 163 int32_t CameraServicesForAudio(RecorderTestParam::VideoRecorderConfig &recorderConfig) const; 164 int32_t SetFormat(const std::string &type, RecorderTestParam::VideoRecorderConfig &recorderConfig) const; 165 int32_t GetStubFile(); 166 void GetFileFd(); 167 uint64_t GetPts(); 168 int32_t GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo); 169 170 private: 171 int32_t SetAudVidFormat( 172 const std::string &recorderType, RecorderTestParam::VideoRecorderConfig &recorderConfig) const; 173 std::shared_ptr<Recorder> recorder_ = nullptr; 174 OHOS::sptr<OHOS::Surface> producerSurface_ = nullptr; 175 std::shared_ptr<std::ifstream> file_ = nullptr; 176 std::unique_ptr<std::thread> camereHDIThread_; 177 std::atomic<bool> isExit_ { false }; 178 std::atomic<bool> isStart_ { true }; 179 int64_t pts_ = 0; 180 int32_t isKeyFrame_ = 1; 181 uint32_t count_ = 0; 182 unsigned char color_ = 0xFF; 183 }; 184 185 class RecorderCallbackTest : public RecorderCallback, public NoCopyable { 186 public: 187 RecorderCallbackTest() = default; 188 virtual ~RecorderCallbackTest() = default; 189 190 void OnError(RecorderErrorType errorType, int32_t errorCode) override; 191 void OnInfo(int32_t type, int32_t extra) override; 192 void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) override; 193 int32_t GetErrorCode(); 194 private: 195 int32_t errorCode_ = 0; 196 }; 197 } 198 } 199 #endif 200