Skip to content

Commit

Permalink
refactor(lib): externalize constructor with initial parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Feb 15, 2024
1 parent 6b76696 commit 895ae9b
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions livestream/src/main/java/video/api/livestream/ApiVideoLiveStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ import video.api.livestream.models.AudioConfig
import video.api.livestream.models.VideoConfig
import video.api.livestream.views.ApiVideoView


/**
* @param context application context
* @param apiVideoView where to display preview. Could be null if you don't have a preview.
* @param connectionListener connection callbacks
* @param initialAudioConfig initial audio configuration. Could be change later with [audioConfig] field.
* @param initialVideoConfig initial video configuration. Could be change later with [videoConfig] field.
* @param initialCameraPosition initial camera. Could be change later with [cameraPosition] field.
* @param permissionRequester permission requester. Called when permissions are required. Always call [onGranted] when permissions are granted.
*/
@RequiresPermission(allOf = [Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA])
fun ApiVideoLiveStream(
context: Context,
apiVideoView: ApiVideoView,
connectionListener: IConnectionListener,
initialAudioConfig: AudioConfig? = null,
initialVideoConfig: VideoConfig? = null,
initialCameraPosition: CameraFacingDirection = CameraFacingDirection.BACK,
permissionRequester: (List<String>, onGranted: () -> Unit) -> Unit = { _, onGranted -> onGranted() }
): ApiVideoLiveStream {
return ApiVideoLiveStream(
context,
apiVideoView,
connectionListener,
permissionRequester
).apply {
audioConfig = initialAudioConfig
videoConfig = initialVideoConfig
cameraPosition = initialCameraPosition
}
}

/**
* Manages both livestream and camera preview.
*/
Expand All @@ -27,19 +59,13 @@ class ApiVideoLiveStream
* @param context application context
* @param apiVideoView where to display preview. Could be null if you don't have a preview.
* @param connectionListener connection callbacks
* @param initialAudioConfig initial audio configuration. Could be change later with [audioConfig] field.
* @param initialVideoConfig initial video configuration. Could be change later with [videoConfig] field.
* @param initialCamera initial camera. Could be change later with [cameraPosition] field.
* @param permissionRequester permission requester. Called when permissions are required. Always call [onGranted] when permissions are granted.
*/
@RequiresPermission(allOf = [Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA])
constructor(
private val context: Context,
private val apiVideoView: ApiVideoView,
private val connectionListener: IConnectionListener,
private val initialAudioConfig: AudioConfig? = null,
private val initialVideoConfig: VideoConfig? = null,
private val initialCamera: CameraFacingDirection = CameraFacingDirection.BACK,
private val permissionRequester: (List<String>, onGranted: () -> Unit) -> Unit = { _, onGranted -> onGranted() }
) {
companion object {
Expand All @@ -51,7 +77,7 @@ constructor(
/**
* Set/get audio configuration once you have created the a [ApiVideoLiveStream] instance.
*/
var audioConfig: AudioConfig? = initialAudioConfig
var audioConfig: AudioConfig? = null
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
set(value) {
require(value != null) { "Audio config must not be null" }
Expand All @@ -72,7 +98,7 @@ constructor(
/**
* Set/get video configuration once you have created the a [ApiVideoLiveStream] instance.
*/
var videoConfig: VideoConfig? = initialVideoConfig
var videoConfig: VideoConfig? = null
/**
* Set new video configuration.
* It will restart preview if resolution has been changed.
Expand Down Expand Up @@ -232,19 +258,6 @@ constructor(

init {
apiVideoView.holder.addCallback(surfaceCallback)
cameraPosition = initialCamera
audioConfig?.let {
streamer.configure(it.toSdkConfig())
}
videoConfig?.let {
streamer.configure(it.toSdkConfig())
// Try to start in case [SurfaceView] has already been created
try {
startPreview()
} catch (e: Exception) {
Log.i(TAG, "Can't start preview in constructor: ${e.message}")
}
}
}

/**
Expand Down

0 comments on commit 895ae9b

Please sign in to comment.