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(wptr<Surface> movingSurface, shared_ptr<Size> size, 58 sptr<AudioCapturerSession> audioCapturerSession, VideoCodecType type, ColorSpace colorSpace); 59 ~AvcodecTaskManager(); 60 void EncodeVideoBuffer(sptr<FrameRecord> frameRecord, CacheCbFunc cacheCallback); 61 void PrepareAudioBuffer(vector<sptr<FrameRecord>>& choosedBuffer, vector<sptr<AudioRecord>>& audioRecords, 62 vector<sptr<AudioRecord>>& processedRecords); 63 void CollectAudioBuffer(vector<sptr<AudioRecord>> audioRecordVec, sptr<AudioVideoMuxer> muxer); 64 void DoMuxerVideo(vector<sptr<FrameRecord>> frameRecords, uint64_t taskName, int32_t captureRotation, 65 int32_t captureId); 66 sptr<AudioVideoMuxer> CreateAVMuxer(vector<sptr<FrameRecord>> frameRecords, int32_t captureRotation, 67 vector<sptr<FrameRecord>> &choosedBuffer, int32_t captureId); 68 void SubmitTask(function<void()> task); 69 void SetVideoFd(int64_t timestamp, std::shared_ptr<PhotoAssetIntf> photoAssetProxy, int32_t captureId); 70 void Stop(); 71 void ClearTaskResource(); 72 void SetVideoBufferDuration(uint32_t preBufferCount, uint32_t postBufferCount); 73 bool isEmptyVideoFdMap(); 74 shared_ptr<TaskManager>& GetTaskManager(); 75 shared_ptr<TaskManager>& GetEncoderManager(); 76 void AsyncInitVideoCodec(); 77 mutex startTimeMutex_; 78 mutex endTimeMutex_; 79 std::map<int32_t, int64_t> mPStartTimeMap_ = {}; 80 std::map<int32_t, int64_t> mPEndTimeMap_ = {}; 81 82 private: 83 void FinishMuxer(sptr<AudioVideoMuxer> muxer, int32_t captureId); 84 void ChooseVideoBuffer(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer, 85 int64_t shutterTime, int32_t captureId); 86 size_t FindIdrFrameIndex(vector<sptr<FrameRecord>> frameRecords, int64_t clearVideoEndTime, 87 int64_t shutterTime, int32_t captureId); 88 void IgnoreDeblur(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer, 89 int64_t shutterTime); 90 void Release(); 91 shared_ptr<VideoEncoder> videoEncoder_ = nullptr; 92 unique_ptr<AudioEncoder> audioEncoder_ = nullptr; 93 shared_ptr<TaskManager> taskManager_ = nullptr; 94 shared_ptr<TaskManager> videoEncoderManager_ = nullptr; 95 sptr<AudioCapturerSession> audioCapturerSession_ = nullptr; 96 condition_variable cvEmpty_; 97 mutex videoFdMutex_; 98 mutex taskManagerMutex_; 99 mutex encoderManagerMutex_; 100 mutex deferredProcessMutex_; 101 std::atomic<bool> isActive_ { true }; 102 map<int32_t, std::pair<int64_t, std::shared_ptr<PhotoAssetIntf>>> videoFdMap_; 103 VideoCodecType videoCodecType_ = VideoCodecType::VIDEO_ENCODE_TYPE_AVC; 104 int64_t preBufferDuration_ = NANOSEC_RANGE; 105 int64_t postBufferDuration_ = NANOSEC_RANGE; 106 uint32_t timerId_ = 0; 107 shared_ptr<AudioDeferredProcess> audioDeferredProcess_ = nullptr; 108 ColorSpace colorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN; 109 wptr<Surface> movingSurface_; 110 shared_ptr<Size> size_ = std::make_shared<Size>(); 111 }; 112 } // CameraStandard 113 } // OHOS 114 #endif // CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H