1 /* 2 * Copyright (C) 2021 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 #ifndef RECORDER_DEMO_H 17 #define RECORDER_DEMO_H 18 19 #include <atomic> 20 #include <thread> 21 #include <string> 22 #include "recorder.h" 23 #include "surface.h" 24 #include "nocopyable.h" 25 26 namespace OHOS { 27 namespace Media { 28 const std::string SAVE_PATH = "/data/recorder"; 29 struct VideoRecorderConfig { 30 int32_t audioSourceId = 0; 31 int32_t videoSourceId = 0; 32 int32_t audioEncodingBitRate = 48000; 33 int32_t channelCount = 2; 34 int32_t duration = 60; 35 int32_t width = 1280; 36 int32_t height = 720; 37 int32_t frameRate = 30; 38 int32_t videoEncodingBitRate = 5000000; 39 int32_t sampleRate = 48000; 40 double captureFps = 30; 41 int32_t outputFd = 0; 42 AudioCodecFormat audioFormat = AAC_LC; 43 AudioSourceType aSource = AUDIO_MIC; 44 OutputFormatType outPutFormat = FORMAT_MPEG_4; 45 VideoSourceType vSource = VIDEO_SOURCE_SURFACE_ES; 46 VideoCodecFormat videoFormat = MPEG4; 47 }; 48 49 struct AudioRecorderConfig { 50 int32_t audioSourceId = 0; 51 int32_t audioEncodingBitRate = 48000; 52 int32_t channelCount = 2; 53 int32_t duration = 60; 54 int32_t sampleRate = 48000; 55 AudioCodecFormat audioFormat = AAC_LC; 56 AudioSourceType inputSource = AUDIO_MIC; 57 OutputFormatType outPutFormat = FORMAT_M4A; 58 }; 59 60 class RecorderDemo : public NoCopyable { 61 public: 62 RecorderDemo() = default; 63 virtual ~RecorderDemo() = default; 64 65 void RunCase(); 66 void HDICreateESBuffer(); 67 void HDICreateYUVBuffer(); 68 void HDICreateRGBABuffer(); 69 int32_t CameraServicesForVideo() const; 70 int32_t CameraServicesForAudio() const; 71 int32_t SetFormat(const std::string &type) const; 72 int32_t GetStubFile(); 73 void GetFileFd(); 74 uint64_t GetPts(); 75 76 private: 77 void SetVideoSource(); 78 void SetVideoEncodeMode(); 79 int64_t pts_ = 0; 80 int32_t isKeyFrame_ = 1; 81 OHOS::sptr<OHOS::Surface> producerSurface_ = nullptr; 82 std::shared_ptr<std::ifstream> file_ = nullptr; 83 std::atomic<bool> isExit_{ false }; 84 std::atomic<bool> isStart_{ true }; 85 std::shared_ptr<Recorder> recorder_ = nullptr; 86 std::unique_ptr<std::thread> camereHDIThread_; 87 uint32_t count_ = 0; 88 unsigned char color_ = 0xFF; 89 }; 90 91 class RecorderCallbackDemo : public RecorderCallback, public NoCopyable { 92 public: 93 RecorderCallbackDemo() = default; 94 virtual ~RecorderCallbackDemo() = default; 95 96 void OnError(RecorderErrorType errorType, int32_t errorCode) override; 97 void OnInfo(int32_t type, int32_t extra) override; 98 }; 99 } // namespace Media 100 } // namespace OHOS 101 #endif // RECORDER_DEMO_H 102