-
Notifications
You must be signed in to change notification settings - Fork 0
/
OffscreenActivity.kt
217 lines (181 loc) · 6.67 KB
/
OffscreenActivity.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
package com.banuba.sdk.example.offscreen
import android.Manifest
import android.app.Application
import android.content.pm.PackageManager
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.util.Size
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.banuba.sdk.effect_player.ConsistencyMode
import com.banuba.sdk.effect_player.EffectPlayer
import com.banuba.sdk.effect_player.EffectPlayerConfiguration
import com.banuba.sdk.example.BanubaEffectHelper
import com.banuba.sdk.example.R
import com.banuba.sdk.example.SampleApp
import com.banuba.sdk.example.VideoEditorLaunchContract
import com.banuba.sdk.manager.BanubaSdkManager
import com.banuba.sdk.offscreen.ImageProcessResult
import com.banuba.sdk.offscreen.OffscreenEffectPlayer
import com.banuba.sdk.offscreen.OffscreenSimpleConfig
class OffscreenActivity : AppCompatActivity() {
companion object {
private const val TAG = "OffscreenActivity"
private const val REQUEST_CODE_PERMISSIONS = 1001
private const val SAMPLE_EFFECT_NAME = "AsaiLines"
private val CAMERA_CAPTURE_SIZE = Size(1280, 720)
private val REQUIRED_PERMISSIONS = arrayOf(
Manifest.permission.CAMERA,
)
}
private val buffersQueue = BuffersQueue()
private var glI420Renderer: GLI420Renderer = GLI420Renderer()
private var camera: Camera2Simple? = null
private var offscreenEffectPlayer: OffscreenEffectPlayer? = null
private var glSurfaceView: GLSurfaceView? = null
private var loadEffect = false
private val effectHelper = BanubaEffectHelper()
private val videoEditorLauncher = registerForActivityResult<Application, String?>(
VideoEditorLaunchContract()
) {}
private val frameReadyCallback =
Camera2Simple.FrameReadyCallback { image, imageOrientation ->
offscreenEffectPlayer?.processImage(
image,
imageOrientation
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_offscreen)
glSurfaceView = findViewById<GLSurfaceView>(R.id.surfaceView)?.apply {
setEGLContextClientVersion(3)
setRenderer(glI420Renderer)
renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
}
findViewById<Button>(R.id.loadEffect).setOnClickListener {
toggleEffect()
}
findViewById<Button>(R.id.startVideoEditorButton).setOnClickListener {
startVideoEditor()
}
}
override fun onStart() {
super.onStart()
Log.d(TAG, "onStart")
if (checkAllPermissionsGranted()) {
handleGrantedPermissions()
} else {
Log.d(TAG, "Request permissions")
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
}
override fun onStop() {
super.onStop()
Log.d(TAG, "onStop")
stopCameraPreview()
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy")
releaseOffscreen()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
results: IntArray
) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (!checkAllPermissionsGranted()) {
Toast.makeText(
applicationContext,
"Please grant permissions",
Toast.LENGTH_LONG
).show()
} else {
handleGrantedPermissions()
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, results)
}
}
private fun toggleEffect() {
loadEffect = !loadEffect
if (loadEffect) {
val effect = effectHelper.prepareEffect(assets, SAMPLE_EFFECT_NAME)
offscreenEffectPlayer?.loadEffect(effect.uri.toString())
} else {
offscreenEffectPlayer?.unloadEffect()
}
}
private fun startVideoEditor() {
Log.d(TAG, "Start Video Editor")
releaseOffscreen()
videoEditorLauncher.launch(application)
}
private fun prepareOffscreen() {
Log.d(TAG, "Prepare Offscreen")
camera = Camera2Simple(
applicationContext,
frameReadyCallback,
CAMERA_CAPTURE_SIZE
)
BanubaSdkManager.deinitialize()
BanubaSdkManager.initialize(applicationContext, SampleApp.LICENSE_TOKEN)
val effectPlayerConfig = EffectPlayerConfiguration.create(
CAMERA_CAPTURE_SIZE.width,
CAMERA_CAPTURE_SIZE.height
)
offscreenEffectPlayer = EffectPlayer.create(effectPlayerConfig)?.let { player ->
val oepConfig = OffscreenSimpleConfig.newBuilder(buffersQueue).build()
player.setRenderConsistencyMode(ConsistencyMode.ASYNCHRONOUS_CONSISTENT)
OffscreenEffectPlayer(
this@OffscreenActivity.applicationContext,
player,
CAMERA_CAPTURE_SIZE,
oepConfig
)
}
offscreenEffectPlayer?.setImageProcessListener({ result ->
handleProcessedImageResult(result)
}, Handler(Looper.getMainLooper()))
}
private fun releaseOffscreen() {
Log.d(TAG, "Release Offscreen")
offscreenEffectPlayer = null
BanubaSdkManager.deinitialize()
}
private fun startCameraPreview() {
Log.d(TAG, "Start Camera")
camera?.openCameraAndStartPreview()
}
private fun stopCameraPreview() {
Log.d(TAG, "Stop Camera")
camera?.closeCamera()
camera = null
}
private fun handleGrantedPermissions() {
Log.d(TAG, "Handle Granted permissions")
prepareOffscreen()
startCameraPreview()
}
private fun checkAllPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
private fun handleProcessedImageResult(result: ImageProcessResult) {
buffersQueue.retainBuffer(result.buffer)
glI420Renderer.drawI420Image(
result.getPlaneBuffer(0), result.getBytesPerRowOfPlane(0),
result.getPlaneBuffer(1), result.getBytesPerRowOfPlane(1),
result.getPlaneBuffer(2), result.getBytesPerRowOfPlane(2),
result.width, result.height,
result.orientation.imageOrientationAngle
)
glSurfaceView?.requestRender()
}
}