1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.camera.integration.antelope.cameracontrollers
import android.hardware.camera2.CameraDevice
import androidx.annotation.NonNull
import androidx.camera.integration.antelope.CameraParams
import androidx.camera.integration.antelope.MainActivity
import androidx.camera.integration.antelope.TestConfig
import androidx.camera.integration.antelope.TestType
import androidx.camera.integration.antelope.testEnded
/**
* Callbacks that track the state of the camera device using the Camera X API.
*/
class CameraXDeviceStateCallback(
internal var params: CameraParams,
internal var activity: MainActivity,
internal var testConfig: TestConfig
) : CameraDevice.StateCallback() {
/**
* Camera device has opened successfully, record timing and initiate the preview stream.
*/
override fun onOpened(@NonNull cameraDevice: CameraDevice) {
MainActivity.logd("In CameraXStateCallback onOpened: " + cameraDevice.id +
" current test: " + testConfig.currentRunningTest.toString())
params.timer.openEnd = System.currentTimeMillis()
params.isOpen = true
params.device = cameraDevice
when (testConfig.currentRunningTest) {
TestType.INIT -> {
// Camera opened, we're done
testConfig.testFinished = true
closeCameraX(activity, params, testConfig)
}
else -> {
params.timer.previewStart = System.currentTimeMillis()
}
}
}
/**
* Camera device has been closed, recording close timing.
*
* If this is a switch test, swizzle camera ids and move to the next step of the test.
*/
override fun onClosed(camera: CameraDevice?) {
MainActivity.logd("In CameraXStateCallback onClosed.")
if (testConfig.testFinished) {
params.timer.cameraCloseEnd = System.currentTimeMillis()
testConfig.testFinished = false
testEnded(activity, params, testConfig)
return
}
if ((testConfig.currentRunningTest == TestType.SWITCH_CAMERA) ||
(testConfig.currentRunningTest == TestType.MULTI_SWITCH)) {
// First camera closed, now start the second
if (testConfig.switchTestCurrentCamera == testConfig.switchTestCameras.get(0)) {
testConfig.switchTestCurrentCamera = testConfig.switchTestCameras.get(1)
cameraXOpenCamera(activity, params, testConfig)
}
// Second camera closed, now start the first
else if (testConfig.switchTestCurrentCamera == testConfig.switchTestCameras.get(1)) {
testConfig.switchTestCurrentCamera = testConfig.switchTestCameras.get(0)
testConfig.testFinished = true
cameraXOpenCamera(activity, params, testConfig)
}
}
}
/**
* Camera has been disconnected. Whatever was happening, it won't work now.
*/
override fun onDisconnected(@NonNull cameraDevice: CameraDevice) {
MainActivity.logd("In CameraXStateCallback onDisconnected: " + params.id)
testConfig.testFinished = false // Whatever we are doing will fail now, try to exit
closeCameraX(activity, params, testConfig)
}
/**
* Camera device has thrown an error. Try to recover or fail gracefully.
*/
override fun onError(@NonNull cameraDevice: CameraDevice, error: Int) {
MainActivity.logd("In CameraXStateCallback onError: " +
cameraDevice.id + " and error: " + error)
when (error) {
CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE -> {
// Let's try to close an open camera and re-open this one
MainActivity.logd("In CameraXStateCallback too many cameras open, closing one...")
closeACamera(activity, testConfig)
cameraXOpenCamera(activity, params, testConfig)
}
CameraDevice.StateCallback.ERROR_CAMERA_DEVICE -> {
MainActivity.logd("Fatal camerax error, close and try to re-initialize...")
closeCameraX(activity, params, testConfig)
cameraXOpenCamera(activity, params, testConfig)
}
CameraDevice.StateCallback.ERROR_CAMERA_IN_USE -> {
MainActivity.logd("This camera is already open... doing nothing")
}
else -> {
testConfig.testFinished = false // Whatever we are doing will fail now, try to exit
closeCameraX(activity, params, testConfig)
}
}
}
}
|