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_MOVING_PHOTO_VIDEO_CACHE_H
17 #define CAMERA_FRAMEWORK_MOVING_PHOTO_VIDEO_CACHE_H
18 
19 #include <functional>
20 #include <mutex>
21 #include <memory>
22 #include <atomic>
23 #include <thread>
24 #include <fstream>
25 #include "native_avmuxer.h"
26 #include "refbase.h"
27 #include "video_encoder.h"
28 #include "audio_encoder.h"
29 #include "audio_video_muxer.h"
30 #include "iconsumer_surface.h"
31 #include <unordered_set>
32 #include <vector>
33 #include "frame_record.h"
34 #include <refbase.h>
35 #include "avcodec_task_manager.h"
36 
37 namespace OHOS {
38 namespace CameraStandard {
39 using namespace std;
40 using CachedFrameSet = unordered_set<sptr<FrameRecord>, FrameRecord::HashFunction, FrameRecord::EqualFunction>;
41 using EncodedEndCbFunc = function<void(vector<sptr<FrameRecord>>, uint64_t, int32_t, int32_t)>;
42 class CachedFrameCallbackHandle;
43 class MovingPhotoVideoCache : public RefBase {
44 public:
45     explicit MovingPhotoVideoCache(sptr<AvcodecTaskManager> taskManager);
46     ~MovingPhotoVideoCache();
47     void CacheFrame(sptr<FrameRecord> frameRecord);
48     void OnImageEncoded(sptr<FrameRecord> frameRecord, bool encodeResult);
49     void GetFrameCachedResult(vector<sptr<FrameRecord>> frameRecords,
50         EncodedEndCbFunc encodedEndCbFunc, uint64_t taskName, int32_t rotation, int32_t captureId);
51     void DoMuxerVideo(vector<sptr<FrameRecord>> frameRecords, uint64_t taskName, int32_t rotation, int32_t captureId_);
52     void ClearCallbackHandler();
53     void ClearCache();
54 private:
55     mutex callbackVecLock_; // Guard cachedFrameCallbackHandles
56     vector<sptr<CachedFrameCallbackHandle>> cachedFrameCallbackHandles_;
57     mutex taskManagerLock_; // Guard cachedFrameCallbackHandles
58     sptr<AvcodecTaskManager> taskManager_;
59 };
60 
61 class CachedFrameCallbackHandle : public RefBase {
62 public:
63     CachedFrameCallbackHandle(vector<sptr<FrameRecord>> frameRecords,
64         EncodedEndCbFunc encodedEndCbFunc, uint64_t taskName, int32_t rotation, int32_t captureId);
65     ~CachedFrameCallbackHandle();
66     void OnCacheFrameFinish(sptr<FrameRecord> frameRecord, bool cachedSuccess);
67     void AbortCapture();
68     CachedFrameSet GetCacheRecord();
69 private:
70     CachedFrameSet cacheRecords_; //set
71     vector<sptr<FrameRecord>> errorCacheRecords_;
72     vector<sptr<FrameRecord>> successCacheRecords_;
73     EncodedEndCbFunc encodedEndCbFunc_;
74     atomic<bool> isAbort_ { false };
75     mutex cacheFrameMutex_;
76     uint64_t taskName_;
77     int32_t rotation_;
78     int32_t captureId_;
79 };
80 } // CameraStandard
81 } // OHOS
82 #endif // CAMERA_FRAMEWORK_MOVING_PHOTO_VIDEO_CACHE_H