diff --git a/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerActivityLifeCycleObserver.kt b/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerActivityLifeCycleObserver.kt new file mode 100644 index 000000000..97b27d55b --- /dev/null +++ b/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerActivityLifeCycleObserver.kt @@ -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() + } +} \ No newline at end of file diff --git a/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerLifeCycleObserver.kt b/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerViewModelLifeCycleObserver.kt similarity index 70% rename from core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerLifeCycleObserver.kt rename to core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerViewModelLifeCycleObserver.kt index 5a69e608d..36a8e9811 100644 --- a/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerLifeCycleObserver.kt +++ b/core/src/main/java/io/github/thibaultbee/streampack/core/streamers/observers/StreamerViewModelLifeCycleObserver.kt @@ -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. @@ -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) { @@ -45,8 +44,4 @@ open class StreamerLifeCycleObserver(protected val streamer: ICoroutineStreamer) } } } - - override fun onDestroy(owner: LifecycleOwner) { - streamer.release() - } } \ No newline at end of file diff --git a/demos/camera/src/main/java/io/github/thibaultbee/streampack/app/ui/main/PreviewViewModel.kt b/demos/camera/src/main/java/io/github/thibaultbee/streampack/app/ui/main/PreviewViewModel.kt index 847c1fdf8..7c6689829 100644 --- a/demos/camera/src/main/java/io/github/thibaultbee/streampack/app/ui/main/PreviewViewModel.kt +++ b/demos/camera/src/main/java/io/github/thibaultbee/streampack/app/ui/main/PreviewViewModel.kt @@ -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 @@ -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 @@ -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 @@ -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 - } - } }