-
Notifications
You must be signed in to change notification settings - Fork 37
/
FlutterLiveStreamView.kt
191 lines (166 loc) · 5.77 KB
/
FlutterLiveStreamView.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
package video.api.flutter.livestream
import android.Manifest
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Size
import android.view.Surface
import androidx.annotation.RequiresPermission
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
import io.flutter.view.TextureRegistry
import io.github.thibaultbee.streampack.data.AudioConfig
import io.github.thibaultbee.streampack.data.VideoConfig
import io.github.thibaultbee.streampack.error.StreamPackError
import io.github.thibaultbee.streampack.ext.rtmp.streamers.CameraRtmpLiveStreamer
import io.github.thibaultbee.streampack.listeners.OnConnectionListener
import io.github.thibaultbee.streampack.listeners.OnErrorListener
import io.github.thibaultbee.streampack.utils.*
import kotlinx.coroutines.runBlocking
class FlutterLiveStreamView(
private val context: Context,
textureRegistry: TextureRegistry,
messenger: BinaryMessenger
) :
OnConnectionListener, OnErrorListener {
private val flutterTexture = textureRegistry.createSurfaceTexture()
val textureId: Long
get() = flutterTexture.id()
private val streamer = CameraRtmpLiveStreamer(
context = context,
initialOnConnectionListener = this,
initialOnErrorListener = this
)
private var _isPreviewing = false
private var _isStreaming = false
val isStreaming: Boolean
get() = _isStreaming
private var eventSink: EventChannel.EventSink? = null
private val eventChannel = EventChannel(messenger, "video.api.livestream/events")
init {
eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
eventSink = events
}
override fun onCancel(arguments: Any?) {
eventSink?.endOfStream()
eventSink = null
}
})
}
var videoConfig = VideoConfig()
set(value) {
if (isStreaming) {
throw UnsupportedOperationException("You have to stop streaming first")
}
eventSink?.success(
mapOf(
"type" to "videoSizeChanged",
"width" to value.resolution.width.toDouble(),
"height" to value.resolution.height.toDouble() // Dart size fields are in double
)
)
val wasPreviewing = _isPreviewing
if (wasPreviewing) {
stopPreview()
}
streamer.configure(value)
field = value
if (wasPreviewing) {
startPreview()
}
}
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
var audioConfig = AudioConfig()
set(value) {
if (isStreaming) {
throw UnsupportedOperationException("You have to stop streaming first")
}
streamer.configure(value)
field = value
}
var isMuted: Boolean
get() = streamer.settings.audio.isMuted
set(value) {
streamer.settings.audio.isMuted = value
}
var camera: String
get() = streamer.camera
set(value) {
streamer.camera = value
}
var cameraPosition: String
get() = when {
context.isFrontCamera(streamer.camera) -> "front"
context.isBackCamera(streamer.camera) -> "back"
context.isExternalCamera(streamer.camera) -> "other"
else -> throw IllegalArgumentException("Invalid camera position for camera ${streamer.camera}")
}
set(value) {
val cameraList = when (value) {
"front" -> context.getFrontCameraList()
"back" -> context.getBackCameraList()
"other" -> context.getExternalCameraList()
else -> throw IllegalArgumentException("Invalid camera position: $value")
}
streamer.camera = cameraList[0]
}
fun dispose() {
streamer.stopStream()
streamer.stopPreview()
flutterTexture.release()
}
fun startStream(url: String) {
runBlocking {
streamer.startStream(url)
_isStreaming = true
}
}
fun stopStream() {
streamer.stopStream()
streamer.disconnect()
_isStreaming = false
}
@RequiresPermission(Manifest.permission.CAMERA)
fun startPreview() {
streamer.startPreview(getSurface(videoConfig.resolution))
_isPreviewing = true
}
fun stopPreview() {
streamer.stopPreview()
_isPreviewing = false
}
private fun getSurface(resolution: Size): Surface {
val surfaceTexture = flutterTexture.surfaceTexture().apply {
setDefaultBufferSize(
resolution.width,
resolution.height
)
}
return Surface(surfaceTexture)
}
override fun onSuccess() {
Handler(Looper.getMainLooper()).post {
eventSink?.success(mapOf("type" to "connected"))
}
}
override fun onLost(message: String) {
Handler(Looper.getMainLooper()).post {
eventSink?.success(mapOf("type" to "disconnected"))
}
}
override fun onFailed(message: String) {
Handler(Looper.getMainLooper()).post {
eventSink?.success(mapOf("type" to "connectionFailed", "message" to message))
}
}
override fun onError(error: StreamPackError) {
_isStreaming = false
Handler(Looper.getMainLooper()).post {
eventSink?.error(error::class.java.name, error.message, error)
}
}
companion object {
private const val TAG = "FlutterLiveStreamView"
}
}