1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <mutex>
19 #include <unordered_map>
20 
21 #include "Encoder.h"
22 #include "FrameProvider.h"
23 
24 namespace android {
25 namespace webcam {
26 
27 // Class which controls camera operation using sdk.
28 class SdkFrameProvider : public FrameProvider,
29                          public EncoderCallback,
30                          public std::enable_shared_from_this<SdkFrameProvider> {
31   public:
32     SdkFrameProvider(std::shared_ptr<BufferProducer> producer, CameraConfig config);
33     ~SdkFrameProvider() override;
34 
35     void setStreamConfig() override;
36     Status startStreaming() override;
37     Status stopStreaming() final ;
38 
39     Status encodeImage(AHardwareBuffer* hardwareBuffer, long timestamp, int rotation) override;
40 
41     // EncoderCallback overrides
42     void onEncoded(Buffer* producerBuffer, HardwareBufferDesc& hardwareBufferDesc,
43                            bool success) override;
44 
45   private:
46     Status getHardwareBufferDescFromHardwareBuffer(AHardwareBuffer* hardwareBuffer,
47                                                    HardwareBufferDesc& ret);
48     Status encodeImage(HardwareBufferDesc desc, jlong timestamp, jint rotation);
49     void releaseHardwareBuffer(const HardwareBufferDesc& desc);
50 
51     std::mutex mMapLock;
52     std::unordered_map<uint32_t, AHardwareBuffer*>
53             mBufferIdToAHardwareBuffer;  // guarded by mMapLock
54     uint32_t mNextBufferId = 0;          // guarded by mMapLock
55     std::shared_ptr<Encoder> mEncoder;
56 };
57 
58 }  // namespace webcam
59 }  // namespace android
60