blob: 061175ceb76cc2604b574f572e4747a184822684 [file] [log] [blame]
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001/*
2 * Copyright (C) 2015 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#ifndef _ACAMERA_DEVICE_H
17#define _ACAMERA_DEVICE_H
18
19#include <memory>
20#include <atomic>
21#include <utils/StrongPointer.h>
22#include <utils/Mutex.h>
23#include <utils/String8.h>
24
25#include <media/stagefright/foundation/ALooper.h>
26#include <media/stagefright/foundation/AHandler.h>
27#include <media/stagefright/foundation/AMessage.h>
28#include <camera/camera2/ICameraDeviceCallbacks.h>
29#include <camera/camera2/ICameraDeviceUser.h>
30
31#include <NdkCameraDevice.h>
32#include "ACameraMetadata.h"
33
34
35using namespace android;
36
37namespace android {
38
39struct CameraDevice final : public RefBase {
40 public:
41 CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
42 std::unique_ptr<ACameraMetadata> chars,
43 ACameraDevice* wrapper);
44 ~CameraDevice();
45
46 inline const char* getId() const { return mCameraId.string(); }
47
48 camera_status_t createCaptureRequest(
49 ACameraDevice_request_template templateId,
50 ACaptureRequest** request) const;
51
52 // Callbacks from camera service
53 class ServiceCallback : public BnCameraDeviceCallbacks {
54 public:
55 ServiceCallback(CameraDevice* device) : mDevice(device) {}
56 void onDeviceError(CameraErrorCode errorCode,
57 const CaptureResultExtras& resultExtras) override;
58 void onDeviceIdle() override;
59 void onCaptureStarted(const CaptureResultExtras& resultExtras,
60 int64_t timestamp) override;
61 void onResultReceived(const CameraMetadata& metadata,
62 const CaptureResultExtras& resultExtras) override;
63 void onPrepared(int streamId) override;
64 private:
65 const wp<CameraDevice> mDevice;
66 };
67 inline sp<ICameraDeviceCallbacks> getServiceCallback() { return mServiceCallback; };
68
69 // Camera device is only functional after remote being set
70 void setRemoteDevice(sp<ICameraDeviceUser> remote);
71
72 private:
73 void disconnectLocked(); // disconnect from camera service
74 camera_status_t checkCameraClosedOrErrorLocked() const;
75
76
77 mutable Mutex mDeviceLock;
78 const String8 mCameraId; // Camera ID
79 const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app
80 const std::unique_ptr<ACameraMetadata> mChars; // Camera characteristics
81 const sp<ServiceCallback> mServiceCallback;
82 ACameraDevice* mWrapper;
83
84 // TODO: maybe a bool will suffice for synchronous implementation?
85 std::atomic_bool mClosing;
86 inline bool isClosed() { return mClosing; }
87
88 bool mInError;
89 camera_status_t mError;
90 void onCaptureErrorLocked(
91 ICameraDeviceCallbacks::CameraErrorCode errorCode,
92 const CaptureResultExtras& resultExtras);
93
94 bool mIdle;
95
96 sp<ICameraDeviceUser> mRemote;
97
98 // Looper thread to handle callback to app
99 sp<ALooper> mCbLooper;
100 // definition of handler and message
101 enum {
102 kWhatOnDisconnected,
103 kWhatOnError
104 };
105 static const char* kContextKey;
106 static const char* kDeviceKey;
107 static const char* kErrorCodeKey;
108 static const char* kCallbackKey;
109 class CallbackHandler : public AHandler {
110 public:
111 CallbackHandler() {}
112 void onMessageReceived(const sp<AMessage> &msg) override;
113 };
114 sp<CallbackHandler> mHandler;
115
116 inline ACameraDevice* getWrapper() { return mWrapper; };
117
118 // TODO: might need another looper/handler to handle callbacks from service
119
120
121};
122
123} // namespace android;
124
125/**
126 * ACameraDevice opaque struct definition
127 * Leave outside of android namespace because it's NDK struct
128 */
129struct ACameraDevice {
130 ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
131 std::unique_ptr<ACameraMetadata> chars) :
132 mDevice(new CameraDevice(id, cb, std::move(chars), this)) {}
133
134 ~ACameraDevice() {};
135
136 inline const char* getId() const { return mDevice->getId(); }
137
138 camera_status_t createCaptureRequest(
139 ACameraDevice_request_template templateId,
140 ACaptureRequest** request) const {
141 return mDevice->createCaptureRequest(templateId, request);
142 }
143
144 inline sp<ICameraDeviceCallbacks> getServiceCallback() {
145 return mDevice->getServiceCallback();
146 };
147
148 // Camera device is only functional after remote being set
149 inline void setRemoteDevice(sp<ICameraDeviceUser> remote) {
150 mDevice->setRemoteDevice(remote);
151 }
152
153 private:
154 // TODO: might need an API to give wp of mDevice to capture session
155 sp<CameraDevice> mDevice;
156};
157
158#endif // _ACAMERA_DEVICE_H