Skip to content

Commit

Permalink
feat(core): add a lifecycle observer for view models
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Dec 19, 2024
1 parent 772e026 commit bed2a6d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2022 Thibault B.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.thibaultbee.streampack.core.streamers.observers

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import io.github.thibaultbee.streampack.core.streamers.interfaces.ICoroutineStreamer

/**
* A [DefaultLifecycleObserver] to control a streamer on [Activity] lifecycle.
*
* It stops streamer when application goes to background and release it when application is destroyed.
*
* To use it, call:
* - `lifeCycle.addObserver(StreamerActivityLifeCycleObserver(streamer))`
*
* @param streamer The streamer to control
*/
open class StreamerActivityLifeCycleObserver(streamer: ICoroutineStreamer) :
StreamerViewModelLifeCycleObserver(streamer) {
override fun onDestroy(owner: LifecycleOwner) {
streamer.release()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Thibault B.
* Copyright (C) 2024 Thibault B.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,17 +22,16 @@ import io.github.thibaultbee.streampack.core.streamers.interfaces.ICoroutineStre
import kotlinx.coroutines.runBlocking

/**
* Add [DefaultLifecycleObserver] to a streamer.
* A [DefaultLifecycleObserver] to control a streamer on [Activity] lifecycle in a ViewModel.
*
* You will not have to call [ICoroutineStreamer.release] when application is destroyed nor to to call
* [ICoroutineStreamer.stopStream] when application goes to background.
* It stops streamer when application goes to background.
*
* To use it, call:
* - `lifeCycle.addObserver(StreamerLifeCycleObserver(streamer))`
* - `lifeCycle.addObserver(StreamerActivityLifeCycleObserver(streamer))`
*
* @param streamer The streamer to observe
* @param streamer The streamer to control
*/
open class StreamerLifeCycleObserver(protected val streamer: ICoroutineStreamer) :
open class StreamerViewModelLifeCycleObserver(protected val streamer: ICoroutineStreamer) :
DefaultLifecycleObserver {
override fun onPause(owner: LifecycleOwner) {
if (streamer is ICameraStreamer) {
Expand All @@ -45,8 +44,4 @@ open class StreamerLifeCycleObserver(protected val streamer: ICoroutineStreamer)
}
}
}

override fun onDestroy(owner: LifecycleOwner) {
streamer.release()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import android.util.Rational
import androidx.annotation.RequiresPermission
import androidx.core.app.ActivityCompat
import androidx.databinding.Bindable
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
Expand All @@ -44,9 +44,8 @@ import io.github.thibaultbee.streampack.core.data.mediadescriptor.UriMediaDescri
import io.github.thibaultbee.streampack.core.internal.endpoints.MediaSinkType
import io.github.thibaultbee.streampack.core.internal.sources.video.camera.CameraSettings
import io.github.thibaultbee.streampack.core.streamers.interfaces.ICameraStreamer
import io.github.thibaultbee.streampack.core.streamers.interfaces.ICoroutineStreamer
import io.github.thibaultbee.streampack.core.streamers.interfaces.startStream
import io.github.thibaultbee.streampack.core.streamers.observers.StreamerLifeCycleObserver
import io.github.thibaultbee.streampack.core.streamers.observers.StreamerViewModelLifeCycleObserver
import io.github.thibaultbee.streampack.core.utils.extensions.isClosedException
import io.github.thibaultbee.streampack.core.utils.extensions.isFrameRateSupported
import io.github.thibaultbee.streampack.ext.srt.regulator.controllers.DefaultSrtBitrateRegulatorController
Expand All @@ -65,8 +64,8 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
private val buildStreamerUseCase = BuildStreamerUseCase(application, storageRepository)

private var streamer = buildStreamerUseCase()
val streamerLifeCycleObserver: StreamerLifeCycleObserver
get() = ViewModelStreamerLifeCycleObserver(streamer)
val streamerLifeCycleObserver: DefaultLifecycleObserver
get() = StreamerViewModelLifeCycleObserver(streamer)
private val cameraSettings: CameraSettings?
get() = (streamer as? ICameraStreamer)?.videoSource?.settings

Expand Down Expand Up @@ -406,12 +405,4 @@ class PreviewViewModel(private val application: Application) : ObservableViewMod
companion object {
private const val TAG = "PreviewViewModel"
}

class ViewModelStreamerLifeCycleObserver(streamer: ICoroutineStreamer) :
StreamerLifeCycleObserver(streamer) {
override fun onDestroy(owner: LifecycleOwner) {
// Do nothing
// The ViewModel onCleared() method will call release() method
}
}
}

0 comments on commit bed2a6d

Please sign in to comment.