-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathCamEngine.kt
More file actions
368 lines (305 loc) · 12.6 KB
/
CamEngine.kt
File metadata and controls
368 lines (305 loc) · 12.6 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.samsung.android.scan3d.serv
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.ImageFormat
import android.hardware.camera2.CameraCaptureSession
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraDevice
import android.hardware.camera2.CameraManager
import android.hardware.camera2.CaptureRequest
import android.hardware.camera2.TotalCaptureResult
import android.hardware.camera2.params.OutputConfiguration
import android.hardware.camera2.params.SessionConfiguration
import android.media.ImageReader
import android.media.MediaCodec
import android.media.MediaCodecInfo
import android.media.MediaCodecList
import android.media.MediaFormat
import android.os.Handler
import android.os.HandlerThread
import android.os.Parcelable
import android.util.Log
import android.util.Size
import android.view.Surface
import com.samsung.android.scan3d.fragments.CameraFragment
import com.samsung.android.scan3d.http.HttpService
import com.samsung.android.scan3d.util.Selector
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.parcelize.Parcelize
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
class CamEngine(val context: Context) {
var http: HttpService? = null
var resW = 1280
var resH = 720
var insidePause = false
var isShowingPreview: Boolean = false
private var cameraManager: CameraManager =
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
private var cameraList: List<Selector.SensorDesc> =
Selector.enumerateCameras(cameraManager)
val camOutPutFormat = ImageFormat.JPEG // ImageFormat.YUV_420_888// ImageFormat.JPEG
val executor = Executors.newSingleThreadExecutor()
fun getEncoder(mimeType: String, resW: Int, resH: Int): MediaCodec? {
fun selectCodec(mimeType: String, needEncoder: Boolean): MediaCodecInfo? {
val list = MediaCodecList(0).getCodecInfos()
list.forEach {
if (it.isEncoder) {
Log.i(
"CODECS",
"We got type " + it.name + " " + it.supportedTypes.contentToString()
)
if (it.supportedTypes.any { e -> e.equals(mimeType, ignoreCase = true) }) {
return it
}
}
}
return null
}
val colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatCbYCrY;
val codec = selectCodec("video/avc", true) ?: return null
val format = MediaFormat.createVideoFormat("video/avc", resW, resH)
format.setString(MediaFormat.KEY_MIME, "video/avc");
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
format.setInteger(MediaFormat.KEY_BIT_RATE, 2_000_000);
Log.i("CODECS", "video/avc: " + codec)
val encoder = MediaCodec.createByCodecName(codec.getName());
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
encoder.start();
return encoder
}
var viewState: CameraFragment.Companion.ViewState = CameraFragment.Companion.ViewState(
true,
stream = false,
cameraId = cameraList.first().cameraId,
quality = 80,
resolutionIndex = null
)
/** [CameraCharacteristics] corresponding to the provided Camera ID */
var characteristics: CameraCharacteristics =
cameraManager.getCameraCharacteristics(viewState.cameraId)
var sizes = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP
)!!.getOutputSizes(camOutPutFormat).reversed()
private lateinit var imageReader: ImageReader
private val cameraThread = HandlerThread("CameraThread").apply { start() }
private val cameraHandler = Handler(cameraThread.looper)
private lateinit var camera: CameraDevice
var previewSurface: Surface? = null
private var session: CameraCaptureSession? = null
private fun stopRunning() {
if (session != null) {
Log.i("CAMERA", "close")
session!!.stopRepeating()
session!!.close()
session = null
camera.close()
imageReader.close()
}
}
fun restart() {
stopRunning()
runBlocking { initializeCamera() }
}
@SuppressLint("MissingPermission")
private suspend fun openCamera(
manager: CameraManager,
cameraId: String,
logicalCameraId: String?,
handler: Handler? = null
): CameraDevice = suspendCancellableCoroutine { cont ->
val idToOpen = logicalCameraId ?: cameraId;
manager.openCamera(idToOpen, object : CameraDevice.StateCallback() {
override fun onOpened(device: CameraDevice) = cont.resume(device)
override fun onDisconnected(device: CameraDevice) {
Log.w("CamEngine", "Camera $cameraId has been disconnected")
}
override fun onError(device: CameraDevice, error: Int) {
val msg = when (error) {
ERROR_CAMERA_DEVICE -> "Fatal (device)"
ERROR_CAMERA_DISABLED -> "Device policy"
ERROR_CAMERA_IN_USE -> "Camera in use"
ERROR_CAMERA_SERVICE -> "Fatal (service)"
ERROR_MAX_CAMERAS_IN_USE -> "Maximum cameras in use"
else -> "Unknown"
}
val exc = RuntimeException("Camera $cameraId error: ($error) $msg")
Log.e("CamEngine", exc.message, exc)
if (cont.isActive) cont.resumeWithException(exc)
}
}, handler)
}
/**
* Starts a [CameraCaptureSession] and returns the configured session (as the result of the
* suspend coroutine
*/
private suspend fun createCaptureSession(
device: CameraDevice,
physicalCamId: String?,
targets: List<Surface>,
handler: Handler? = null
): CameraCaptureSession = suspendCoroutine { cont ->
val outputConfigs = mutableListOf<OutputConfiguration>();
targets.forEach {
outputConfigs.add(OutputConfiguration(it).apply {
// If physical camera id is not null, it's a logical cam, you should set it
if (physicalCamId != null) {
setPhysicalCameraId(physicalCamId)
}
})
}
// Create a capture session using the predefined targets; this also involves defining the
// session state callback to be notified of when the session is ready
val sessionConfiguration = SessionConfiguration(
SessionConfiguration.SESSION_REGULAR,
outputConfigs,
Executors.newSingleThreadExecutor(),
object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) = cont.resume(session)
override fun onConfigureFailed(session: CameraCaptureSession) {
val exc = RuntimeException("Camera ${device.id} session configuration failed")
Log.e("CamEngine", exc.message, exc)
cont.resumeWithException(exc)
}
}
)
device.createCaptureSession(sessionConfiguration)
}
suspend fun initializeCamera() {
Log.i("CAMERA", "initializeCamera")
val showLiveSurface = viewState.preview && !insidePause && previewSurface != null
isShowingPreview = showLiveSurface
stopRunning()
characteristics = cameraManager.getCameraCharacteristics(viewState.cameraId)
sizes = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP
)!!.getOutputSizes(camOutPutFormat).reversed()
if (viewState.resolutionIndex == null) {
var selIndex = 0
for (formatIndex in 0 until sizes.size) {
if (sizes[formatIndex].height <= 720) {
selIndex = formatIndex
}
}
viewState.resolutionIndex = selIndex
}
resW = sizes[viewState.resolutionIndex!!].width
resH = sizes[viewState.resolutionIndex!!].height
val sensor = cameraList.find { it.cameraId == viewState.cameraId }!!
camera = openCamera(cameraManager, sensor.cameraId, sensor.logicalCameraId, cameraHandler)
imageReader = ImageReader.newInstance(
resW, resH, camOutPutFormat, 4
)
var targets = listOf(imageReader.surface)
if (showLiveSurface) {
targets = targets.plus(previewSurface!!)
}
session = createCaptureSession(
camera,
if (sensor.logicalCameraId == null) null else sensor.cameraId,
targets,
cameraHandler
)
val captureRequest = camera.createCaptureRequest(
CameraDevice.TEMPLATE_RECORD //TEMPLATE_PREVIEW
)
if (showLiveSurface) {
captureRequest.addTarget(previewSurface!!)
}
captureRequest.addTarget(imageReader.surface)
captureRequest.set(CaptureRequest.JPEG_QUALITY, viewState.quality.toByte())
var lastTime = System.currentTimeMillis()
var kodd = 0
var aquired = AtomicInteger(0)
session!!.setRepeatingRequest(
captureRequest.build(),
object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
super.onCaptureCompleted(session, request, result)
var lastImg = imageReader.acquireNextImage()
if (aquired.get() > 1 && lastImg != null) {
lastImg.close()
Log.i("COM", "EARLY CLOSE")
lastImg = null
}
val img = lastImg ?: return
aquired.incrementAndGet()
var curTime = System.currentTimeMillis()
val delta = curTime - lastTime
lastTime = curTime
kodd += 1
if (camOutPutFormat == ImageFormat.JPEG) {
// executor.execute(Runnable {
val buffer = img.planes[0].buffer
val bytes = ByteArray(buffer.remaining()).apply { buffer.get(this) }
if (kodd % 10 == 0) {
updateViewQuick(
DataQuick(
delta.toInt(),
(30 * bytes.size / 1000)
)
)
}
img.close()
aquired.decrementAndGet()
if (viewState.stream) {
http?.channel?.trySend(
bytes
)
}
}
}
},
cameraHandler
)
updateView()
}
fun destroy() {
stopRunning()
cameraThread.quitSafely()
}
fun updateView() {
val intent = Intent("UpdateFromCameraEngine") //FILTER is a string to identify this intent
intent.putExtra(
"data",
Data(
cameraList,
cameraList.find { it.cameraId == viewState.cameraId }!!,
resolutions = sizes,
resolutionSelected = viewState.resolutionIndex!!
)
)
context.sendBroadcast(intent)
}
fun updateViewQuick(dq: DataQuick) {
val intent = Intent("UpdateFromCameraEngine") //FILTER is a string to identify this intent
intent.putExtra(
"dataQuick", dq
)
context.sendBroadcast(intent)
}
companion object {
@Parcelize
data class Data(
val sensors: List<Selector.SensorDesc>,
val sensorSelected: Selector.SensorDesc,
val resolutions: List<Size>,
val resolutionSelected: Int,
) : Parcelable
@Parcelize
data class DataQuick(
val ms: Int,
val rateKbs: Int
) : Parcelable
}
}