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 ONE_BILLION = 1000000000LL;
48 constexpr uint32_t MAX_FRAME_COUNT = 90;
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 PrepareAudioBuffer(vector<sptr<FrameRecord>>& choosedBuffer, vector<sptr<AudioRecord>>& audioRecords,
56         vector<sptr<AudioRecord>>& processedAudioRecords);
57     void CollectAudioBuffer(vector<sptr<AudioRecord>> audioRecordVec, sptr<AudioVideoMuxer> muxer);
58     void DoMuxerVideo(vector<sptr<FrameRecord>> frameRecords, uint64_t taskName, int32_t captureRotation,
59         int32_t captureId);
60     sptr<AudioVideoMuxer> CreateAVMuxer(vector<sptr<FrameRecord>> frameRecords, int32_t captureRotation,
61         vector<sptr<FrameRecord>> &choosedBuffer, int32_t captureId);
62     void SubmitTask(function<void()> task);
63     void SetVideoFd(int64_t timestamp, PhotoAssetIntf* photoAssetProxy, int32_t captureId);
64     void Stop();
65     void ClearTaskResource();
66     void SetVideoBufferDuration(uint32_t preBufferCount, uint32_t postBufferCount);
67     shared_ptr<TaskManager>& GetTaskManager();
68     shared_ptr<TaskManager>& GetEncoderManager();
69     mutex startTimeMutex_;
70     mutex endTimeMutex_;
71     std::map<int32_t, int64_t> mPStartTimeMap_ = {};
72     std::map<int32_t, int64_t> mPEndTimeMap_ = {};
73 private:
74     void FinishMuxer(sptr<AudioVideoMuxer> muxer);
75     void ChooseVideoBuffer(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer,
76         int64_t shutterTime, int32_t captureId);
77     size_t FindIdrFrameIndex(vector<sptr<FrameRecord>> frameRecords, int64_t shutterTime, int32_t captureId);
78     void IgnoreDeblur(vector<sptr<FrameRecord>> frameRecords, vector<sptr<FrameRecord>> &choosedBuffer,
79         int64_t shutterTime);
80     void Release();
81     unique_ptr<VideoEncoder> videoEncoder_ = nullptr;
82     unique_ptr<AudioEncoder> audioEncoder_ = nullptr;
83     shared_ptr<TaskManager> taskManager_ = nullptr;
84     shared_ptr<TaskManager> videoEncoderManager_ = nullptr;
85     sptr<AudioCapturerSession> audioCapturerSession_ = nullptr;
86     condition_variable cvEmpty_;
87     mutex videoFdMutex_;
88     mutex taskManagerMutex_;
89     mutex encoderManagerMutex_;
90     std::atomic<bool> isActive_ { true };
91     map<int32_t, std::pair<int64_t, PhotoAssetIntf*>> videoFdMap_;
92     VideoCodecType videoCodecType_ = VideoCodecType::VIDEO_ENCODE_TYPE_AVC;
93     int64_t preBufferDuration_ = NANOSEC_RANGE;
94     int64_t postBufferDuration_ = NANOSEC_RANGE;
95 };
96 } // CameraStandard
97 } // OHOS
98 #endif // CAMERA_FRAMEWORK_LIVEPHOTO_GENERATOR_H