1 /* 2 * Copyright (c) 2024-2024 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 CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H 17 #define CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H 18 19 #include <mutex> 20 #include <memory> 21 #include <atomic> 22 #include <queue> 23 #include <thread> 24 #include <fstream> 25 #include "frame_record.h" 26 #include "audio_record.h" 27 #include "audio_capturer_session.h" 28 #include "native_avmuxer.h" 29 #include "refbase.h" 30 #include "surface_buffer.h" 31 #include "video_encoder.h" 32 #include "audio_encoder.h" 33 #include "audio_video_muxer.h" 34 #include "iconsumer_surface.h" 35 #include "blocking_queue.h" 36 #include "task_manager.h" 37 #include "camera_util.h" 38 #include "camera_timer.h" 39 namespace OHOS { 40 namespace CameraStandard { 41 using namespace std; 42 using namespace DeferredProcessing; 43 using namespace Media; 44 using CacheCbFunc = function<void(sptr<FrameRecord>, bool)>; 45 constexpr uint32_t DEFAULT_THREAD_NUMBER = 6; 46 constexpr uint32_t DEFAULT_ENCODER_THREAD_NUMBER = 1; 47 constexpr uint32_t GET_FD_EXPIREATION_TIME = 2000; 48 constexpr int64_t ONE_BILLION = 1000000000LL; 49 constexpr uint32_t MAX_FRAME_COUNT = 90; 50 constexpr uint32_t RELEASE_WAIT_TIME = 10000; 51 constexpr size_t MIN_FRAME_RECORD_BUFFER_SIZE = 9; 52 53 class AvcodecTaskManager : public RefBase, public std::enable_shared_from_this<AvcodecTaskManager> { 54 public: 55 explicit AvcodecTaskManager(sptr<AudioCapturerSession> audioCapturerSession, VideoCodecType type, 56 ColorSpace colorSpace); 57 ~AvcodecTaskManager(); 58 void EncodeVideoBuffer(sptr<FrameRecord> frameRecord, CacheCbFunc cacheCallback); 59 void PrepareAudioBuffer(vector<sptr<FrameRecord>>& choosedBuffer, vector<sptr<AudioRecord>>& audioRecords, 60 vector<sptr<AudioRecord>>& processedRecords); 61 void CollectAudioBuffer(vector<sptr<AudioRecord>> audioRecordVec, sptr<AudioVideoMuxer> muxer); 62 void DoMuxerVideo(vector<sptr<FrameRecord>> frameRecords, uint64_t taskName, int32_t captureRotation, 63 int32_t captureId); 64 sptr<AudioVideoMuxer> CreateAVMuxer(vector<sptr<FrameRecord>> frameRecords, int32_t captureRotation, 65 vector<sptr<FrameRecord>> &choosedBuffer, int32_t captureId); 66 void SubmitTask(function<void()> task); 67 void SetVideoFd(int64_t timestamp, std::shared_ptr<PhotoAssetIntf> photoAssetProxy, int32_t captureId); 68 void Stop(); 69 void ClearTaskResource(); 70 void SetVideoBufferDuration(uint32_t preBufferCount, uint32_t postBufferCount); 71 shared_ptr<TaskManager>& GetTaskManager(); 72 shared_ptr<TaskManager>& GetEncoderManager(); 73 mutex startTimeMutex_; 74 mutex endTimeMutex_; 75 std::map<int32_t, int64_t> mPStartTimeMap_ = {}; 76 std::map<int32_t, int64_t> mPEndTimeMap_ = {}; 77 78 private: 79 void FinishMuxer(sptr<AudioVideoMuxer> muxer); 80 void ChooseVideoBuffer(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer, 81 int64_t shutterTime, int32_t captureId); 82 size_t FindIdrFrameIndex(vector<sptr<FrameRecord>> frameRecords, int64_t clearVideoEndTime, 83 int64_t shutterTime, int32_t captureId); 84 void IgnoreDeblur(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer, 85 int64_t shutterTime); 86 void Release(); 87 shared_ptr<VideoEncoder> videoEncoder_ = nullptr; 88 unique_ptr<AudioEncoder> audioEncoder_ = nullptr; 89 shared_ptr<TaskManager> taskManager_ = nullptr; 90 shared_ptr<TaskManager> videoEncoderManager_ = nullptr; 91 sptr<AudioCapturerSession> audioCapturerSession_ = nullptr; 92 condition_variable cvEmpty_; 93 mutex videoFdMutex_; 94 mutex taskManagerMutex_; 95 mutex encoderManagerMutex_; 96 mutex deferredProcessMutex_; 97 std::atomic<bool> isActive_ { true }; 98 map<int32_t, std::pair<int64_t, std::shared_ptr<PhotoAssetIntf>>> videoFdMap_; 99 VideoCodecType videoCodecType_ = VideoCodecType::VIDEO_ENCODE_TYPE_AVC; 100 int64_t preBufferDuration_ = NANOSEC_RANGE; 101 int64_t postBufferDuration_ = NANOSEC_RANGE; 102 uint32_t timerId_ = 0; 103 shared_ptr<AudioDeferredProcess> audioDeferredProcess_ = nullptr; 104 ColorSpace colorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN; 105 }; 106 } // CameraStandard 107 } // OHOS 108 #endif // CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H