-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArScreenCapturer.kt
387 lines (353 loc) · 14.2 KB
/
ArScreenCapturer.kt
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import package_abc
import android.content.Context
import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.graphics.Matrix
import android.graphics.Rect
import android.opengl.GLES20
import android.opengl.GLUtils
import android.os.*
import android.util.Log
import android.view.PixelCopy
import android.view.SurfaceView
import androidx.annotation.RequiresApi
import kotlinx.coroutines.*
import org.webrtc.*
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
@RequiresApi(api = Build.VERSION_CODES.N)
class ArScreenCapturer(view: SurfaceView, framePerSecond: Int) : VideoCapturer, VideoSink {
private var width = 0
private var height = 0
private val view: SurfaceView
private var context: Context? = null
private var capturerObserver: CapturerObserver? = null
private var surfaceTextureHelper: SurfaceTextureHelper? = null
private var isDisposed = false
private var viewBitmap: Bitmap? = null
private val handlerPixelCopy = Handler(Looper.getMainLooper())
private var handler = Handler(Looper.getMainLooper())
private val started = AtomicBoolean(false)
val numCapturedFrames: Long = 0
private val yuvConverter = YuvConverter()
private var buffer: TextureBufferImpl? = null
private val start = System.nanoTime()
private var frameSenderJob: Job? = null
private var isStarted = true
private val handlerThread = HandlerThread(ArSessionActivity::class.java.simpleName)
private fun checkNotDisposed() {
if (isDisposed) {
throw RuntimeException(context?.getString(R.string.capture_is_disposed))
}
}
fun stopStream() {
frameSenderJob?.cancel()
try {
handlerThread.quitSafely()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
isStarted = false
}
@Synchronized
override fun initialize(surfaceTextureHelper: SurfaceTextureHelper, context: Context, capturerObserver: CapturerObserver) {
checkNotDisposed()
if (capturerObserver == null) {
throw RuntimeException(context.getString(R.string.captue_observer_not_set))
} else {
this.context = context
this.capturerObserver = capturerObserver
if (surfaceTextureHelper == null) {
throw RuntimeException(context.getString(R.string.surface_texture_helper_not_set))
} else {
this.surfaceTextureHelper = surfaceTextureHelper
}
}
handlerThread.start()
handler = Handler(handlerThread.looper)
}
override fun startCapture(width: Int, height: Int, fps: Int) {
checkNotDisposed()
started.set(true)
this.width = width
this.height = height
capturerObserver!!.onCapturerStarted(true)
surfaceTextureHelper!!.startListening(this)
frameSenderJob = CoroutineScope(Dispatchers.IO).launch {
while (isStarted) {
try {
var bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
PixelCopy.request(view, bitmap, { copyResult: Int ->
if (copyResult == PixelCopy.SUCCESS) {
/*viewBitmap = getResizedBitmap(bitmap)
if (viewBitmap != null) {
sendToServer(viewBitmap!!, yuvConverter, start)
try {
viewBitmap?.recycle()
viewBitmap = null
} catch (e: Exception) {
e.printStackTrace()
}
try {
bitmap?.recycle()
bitmap = null
} catch (e: Exception) {
e.printStackTrace()
}
}*/
sendArView(bitmap)
bitmap?.recycle()
} else {
Log.e("Pixel_copy-->", "Couldn't create bitmap of the SurfaceView")
}
// handlerThread.quitSafely()
}, handler)
} catch (e: Exception) {
if (!ArSessionActivity.active) {
isStarted = false
}
e.printStackTrace()
}
delay(VIEW_CAPTURER_FRAMERATE_MS.toLong())
}
}
}
companion object {
private var VIEW_CAPTURER_FRAMERATE_MS = 15
}
private fun createFlippedBitmap(source: Bitmap?, xFlip: Boolean, yFlip: Boolean): Bitmap? {
return try {
val matrix = Matrix()
matrix.postScale(-1f, 1f, source!!.width / 2f, source.height / 2f)
Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
} catch (e: Exception) {
null
}
}
private fun sendToServer(bitmap: Bitmap, yuvConverter: YuvConverter, start: Long) {
try {
val textures = IntArray(1)
GLES20.glGenTextures(0, textures, 0)
buffer = TextureBufferImpl(width, height, VideoFrame.TextureBuffer.Type.RGB, textures[0], Matrix(), surfaceTextureHelper!!.handler, yuvConverter, null)
val flippedBitmap = createFlippedBitmap(bitmap, true, false)
surfaceTextureHelper!!.handler.post {
if (flippedBitmap != null) {
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST)
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST)
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flippedBitmap, 0)
val i420Buf = yuvConverter.convert(buffer)
val frameTime = System.nanoTime() - start
val videoFrame = VideoFrame(i420Buf, 180, frameTime)
capturerObserver!!.onFrameCaptured(videoFrame)
videoFrame.release()
try {
flippedBitmap.recycle()
// viewBitmap?.recycle()
} catch (e: Exception) {
}
}
}
} catch (ignored: Exception) {
}
}
@Throws(InterruptedException::class)
override fun stopCapture() {
checkNotDisposed()
surfaceTextureHelper!!.stopListening()
capturerObserver!!.onCapturerStopped()
started.set(false)
handler.removeCallbacksAndMessages(null)
handlerPixelCopy.removeCallbacksAndMessages(null)
}
override fun changeCaptureFormat(width: Int, height: Int, framerate: Int) {
checkNotDisposed()
this.width = width
this.height = height
}
override fun dispose() {
isDisposed = true
}
override fun isScreencast(): Boolean {
return true
}
/**
* reduces the size of the image
*
* @param image
* @param maxSize
* @return
*/
private fun getResizedBitmap(image: Bitmap): Bitmap? {
val width = image.width
val height = image.height
val bitmap = Bitmap.createScaledBitmap(image, width, height, true)
// bitmap.recycle()
return bitmap
}
override fun onFrame(videoFrame: VideoFrame) {}
init {
require(framePerSecond > 0) { "framePersecond must be greater than 0" }
this.view = view
val tmp = 1f / framePerSecond * 1000
VIEW_CAPTURER_FRAMERATE_MS = Math.round(tmp)
}
fun sendFrameExternal(arSurfaceView: SurfaceView) {
width = arSurfaceView.width
height = arSurfaceView.height
val outBitmap = Bitmap.createBitmap(
width,
height,
Bitmap.Config.ARGB_8888
)
PixelCopy.request(arSurfaceView, outBitmap,
{ copyResult ->
if (copyResult == PixelCopy.SUCCESS) {
sendToServer(outBitmap, yuvConverter, start)
// sendArView(outBitmap)
outBitmap.recycle()
}
}, handler
)
}
fun sendArView(arBitmap: Bitmap?) {
if (arBitmap == null) return
val argb = IntArray(arBitmap.width * arBitmap.height)
arBitmap.getPixels(argb, 0, arBitmap.width, 0, 0, arBitmap.width, arBitmap.height)
val yuv = ByteArray(arBitmap.width * arBitmap.height * 3 / 2)
// val nv21 = yuv420toNV21(yuv, arBitmap.width, arBitmap.height)
encodeYUV420SP(yuv, argb, arBitmap.width, arBitmap.height)
val timestampNS = TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime())
val buffer = NV21Buffer(yuv, arBitmap.width, arBitmap.height, null)
val videoFrame = VideoFrame(buffer, 0, timestampNS)
sendToServer(videoFrame)
arBitmap.recycle()
}
private fun sendToServer(videoFrame: VideoFrame) {
try {
surfaceTextureHelper!!.handler.post {
capturerObserver!!.onFrameCaptured(videoFrame)
videoFrame.release()
}
} catch (ignored: Exception) {
}
}
fun encodeYUV420SP(yuv420sp: ByteArray, argb: IntArray, width: Int, height: Int) {
val frameSize = width * height
var yIndex = 0
var uvIndex = frameSize
var a: Int
var R: Int
var G: Int
var B: Int
var Y: Int
var U: Int
var V: Int
var index = 0
for (j in 0 until height) {
for (i in 0 until width) {
a = argb[index] and -0x1000000 shr 24 // a is not used obviously
R = argb[index] and 0xff0000 shr 16
G = argb[index] and 0xff00 shr 8
B = argb[index] and 0xff shr 0
// well known RGB to YUV algorithm
Y = (66 * R + 129 * G + 25 * B + 128 shr 8) + 16
U = (-38 * R - 74 * G + 112 * B + 128 shr 8) + 128
V = (112 * R - 94 * G - 18 * B + 128 shr 8) + 128
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (if (Y < 0) 0 else if (Y > 255) 255 else Y).toByte()
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (if (V < 0) 0 else if (V > 255) 255 else V).toByte()
yuv420sp[uvIndex++] = (if (U < 0) 0 else if (U > 255) 255 else U).toByte()
}
index++
}
}
}
fun yuv420toNV21(data: ByteArray?, width: Int, height: Int): ByteArray? {
val buffer: JavaI420Buffer = createYUV(data, width, height)!!
return yuv420toNV21(buffer as VideoFrame.I420Buffer, width, height)
}
fun yuv420toNV21(i420: VideoFrame.I420Buffer, width: Int, height: Int): ByteArray? {
val crop = Rect(0, 0, width, height)
val format = 35
val planes: Array<Plane?> =
arrayOfNulls<Plane>(3)
val yPlane: Plane =
Plane(i420.getDataY(), i420.getStrideY(), 1)
val uPlane: Plane =
Plane(i420.getDataU(), i420.getStrideU(), 1)
val vPlane: Plane =
Plane(i420.getDataV(), i420.getStrideV(), 1)
planes[0] = yPlane
planes[1] = uPlane
planes[2] = vPlane
val data = ByteArray(width * height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8)
val rowData = ByteArray(planes[0]!!.rowStride)
var channelOffset = 0
var outputStride = 1
for (i in planes.indices) {
when (i) {
0 -> {
channelOffset = 0
outputStride = 1
}
1 -> {
channelOffset = width * height + 1
outputStride = 2
}
2 -> {
channelOffset = width * height
outputStride = 2
}
}
val buffer: ByteBuffer = planes[i]!!.buffer
val rowStride: Int = planes[i]!!.rowStride
val pixelStride: Int = planes[i]!!.pixelStride
val shift = if (i == 0) 0 else 1
val w = width shr shift
val h = height shr shift
buffer.position(rowStride * (crop.top shr shift) + pixelStride * (crop.left shr shift))
for (row in 0 until h) {
var length: Int
if (pixelStride == 1 && outputStride == 1) {
length = w
buffer[data, channelOffset, w]
channelOffset += w
} else {
length = (w - 1) * pixelStride + 1
buffer[rowData, 0, length]
for (col in 0 until w) {
data[channelOffset] = rowData[col * pixelStride]
channelOffset += outputStride
}
}
if (row < h - 1) {
buffer.position(buffer.position() + rowStride - length)
}
}
}
return data
}
fun createYUV(data: ByteArray?, width: Int, height: Int): JavaI420Buffer? {
return if (data != null && data.size != 0) {
val buffer = JavaI420Buffer.allocate(width, height)
val dataY = buffer.dataY
val dataU = buffer.dataU
val dataV = buffer.dataV
val chromaHeight = (height + 1) / 2
val sizeY = height * buffer.strideY
val sizeU = chromaHeight * buffer.strideU
val sizeV = chromaHeight * buffer.strideV
dataY.put(data, 0, sizeY)
dataU.put(data, sizeY, sizeU)
dataV.put(data, sizeY + sizeU, sizeV)
buffer
} else {
null
}
}
class Plane(val buffer: ByteBuffer, val rowStride: Int, val pixelStride: Int)
}