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 namespace OHOS { 39 namespace CameraStandard { 40 using namespace std; 41 using namespace DeferredProcessing; 42 using namespace Media; 43 using CacheCbFunc = function<void(sptr<FrameRecord>, bool)>; 44 constexpr uint32_t DEFAULT_THREAD_NUMBER = 6; 45 constexpr uint32_t DEFAULT_ENCODER_THREAD_NUMBER = 1; 46 constexpr uint32_t GET_FD_EXPIREATION_TIME = 2000; 47 constexpr int64_t NANOSEC_RANGE = 1500000000LL; 48 constexpr int64_t ONE_BILLION = 1000000000LL; 49 50 class AvcodecTaskManager : public RefBase, public std::enable_shared_from_this<AvcodecTaskManager> { 51 public: 52 explicit AvcodecTaskManager(sptr<AudioCapturerSession> audioCapturerSession, VideoCodecType type); 53 ~AvcodecTaskManager(); 54 void EncodeVideoBuffer(sptr<FrameRecord> frameRecord, CacheCbFunc cacheCallback); 55 void CollectAudioBuffer(vector<sptr<AudioRecord>> audioRecordVec, sptr<AudioVideoMuxer> muxer); 56 void DoMuxerVideo(vector<sptr<FrameRecord>> frameRecords, uint64_t taskName, int32_t captureRotation, 57 int32_t captureId); 58 sptr<AudioVideoMuxer> CreateAVMuxer(vector<sptr<FrameRecord>> frameRecords, int32_t captureRotation, 59 vector<sptr<FrameRecord>> &choosedBuffer, int32_t captureId); 60 void SubmitTask(function<void()> task); 61 void SetVideoFd(int64_t timestamp, PhotoAssetIntf* photoAssetProxy); 62 void Stop(); 63 void ClearTaskResource(); 64 void SetVideoBufferDuration(uint32_t preBufferCount, uint32_t postBufferCount); 65 shared_ptr<TaskManager>& GetTaskManager(); 66 shared_ptr<TaskManager>& GetEncoderManager(); 67 mutex startTimeMutex_; 68 mutex endTimeMutex_; 69 std::map<int32_t, int64_t> mPStartTimeMap_ = {}; 70 std::map<int32_t, int64_t> mPEndTimeMap_ = {}; 71 private: 72 void FinishMuxer(sptr<AudioVideoMuxer> muxer); 73 void ChooseVideoBuffer(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer, 74 int64_t shutterTime, int32_t captureId); 75 void Release(); 76 unique_ptr<VideoEncoder> videoEncoder_ = nullptr; 77 unique_ptr<AudioEncoder> audioEncoder_ = nullptr; 78 shared_ptr<TaskManager> taskManager_ = nullptr; 79 shared_ptr<TaskManager> videoEncoderManager_ = nullptr; 80 sptr<AudioCapturerSession> audioCapturerSession_ = nullptr; 81 condition_variable cvEmpty_; 82 mutex videoFdMutex_; 83 mutex taskManagerMutex_; 84 mutex encoderManagerMutex_; 85 std::atomic<bool> isActive_ { true }; 86 queue<std::pair<int64_t, PhotoAssetIntf*>> videoFdQueue_; 87 VideoCodecType videoCodecType_ = VideoCodecType::VIDEO_ENCODE_TYPE_AVC; 88 int64_t preBufferDuration_ = NANOSEC_RANGE; 89 int64_t postBufferDuration_ = NANOSEC_RANGE; 90 }; 91 } // CameraStandard 92 } // OHOS 93 #endif // CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H