Skip to content

Commit

Permalink
fix(core): screen source: do not throw error when stopStream is cal…
Browse files Browse the repository at this point in the history
…led explicitly
  • Loading branch information
ThibaultBee committed Nov 11, 2023
1 parent 2ac4b7a commit 72e0268
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class ScreenSource(
var activityResult: ActivityResult? = null
var onErrorListener: OnErrorListener? = null

/**
* Avoid to trigger `onError` when screen source `stopStream` has been called.
*/
private var isExplicitelyStopped = false

private val mediaProjectionManager =
context.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
private var virtualDisplay: VirtualDisplay? = null
Expand All @@ -70,15 +75,21 @@ class ScreenSource(
override fun onStopped() {
super.onStopped()
Logger.i(this@ScreenSource.TAG, "onStopped")
onErrorListener?.onError(StreamPackError("Screen source has been stopped"))

if (!isExplicitelyStopped) {
onErrorListener?.onError(StreamPackError("Screen source virtual display has been stopped"))
}
}
}

private val mediaProjectionCallback = object : MediaProjection.Callback() {
override fun onStop() {
super.onStop()
Logger.i(this@ScreenSource.TAG, "onStop")
onErrorListener?.onError(StreamPackError("Screen source has been stopped"))

if (!isExplicitelyStopped) {
onErrorListener?.onError(StreamPackError("Screen source media projection has been stopped"))
}
}
}

Expand All @@ -97,6 +108,8 @@ class ScreenSource(
val resultCode = activityResult!!.resultCode
val resultData = activityResult!!.data!!

isExplicitelyStopped = false

val orientedSize = orientationProvider.getOrientedSize(videoConfig!!.resolution)
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, resultData).apply {
virtualDisplay = createVirtualDisplay(
Expand All @@ -115,6 +128,8 @@ class ScreenSource(


override fun stopStream() {
isExplicitelyStopped = true

virtualDisplay?.release()
virtualDisplay = null
mediaProjection?.stop()
Expand Down

0 comments on commit 72e0268

Please sign in to comment.