davidliu
Committed by GitHub

Put back composables for now (#308)

* Put back composables for now

* Spotless
ext {
android_build_tools_version = '8.0.2'
compose_version = '1.4.2'
compose_compiler_version = '1.4.6'
compose_version = '1.2.1'
compose_compiler_version = '1.4.5'
kotlin_version = '1.8.20'
java_version = JavaVersion.VERSION_17
dokka_version = '1.5.0'
... ... @@ -69,4 +69,4 @@ ext {
]
annotations = [
]
}
\ No newline at end of file
}
... ...
... ... @@ -57,8 +57,12 @@ android {
}
buildFeatures {
compose true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_version
}
kotlinOptions {
freeCompilerArgs = ["-Xinline-classes", "-opt-in=kotlin.RequiresOptIn"]
jvmTarget = java_version
... ... @@ -149,6 +153,7 @@ dependencies {
implementation deps.androidx.annotation
implementation "androidx.core:core:${versions.androidx_core}"
implementation "com.google.protobuf:protobuf-javalite:${versions.protobuf}"
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'javax.sip:android-jain-sip-ri:1.3.0-91'
... ...
/*
* Copyright 2023 LiveKit, Inc.
*
* 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.livekit.android.compose
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.currentCompositeKeyHash
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.viewinterop.AndroidView
import io.livekit.android.renderer.TextureViewRenderer
import io.livekit.android.room.Room
import io.livekit.android.room.track.RemoteVideoTrack
import io.livekit.android.room.track.VideoTrack
import io.livekit.android.room.track.video.ComposeVisibility
/**
* Widget for displaying a VideoTrack. Handles the Compose <-> AndroidView interop needed to use
* [TextureViewRenderer].
*/
@Composable
fun VideoRenderer(
room: Room,
videoTrack: VideoTrack?,
modifier: Modifier = Modifier,
mirror: Boolean = false,
) {
val videoSinkVisibility = remember(room, videoTrack) { ComposeVisibility() }
var boundVideoTrack by remember { mutableStateOf<VideoTrack?>(null) }
var view: TextureViewRenderer? by remember { mutableStateOf(null) }
fun cleanupVideoTrack() {
view?.let { boundVideoTrack?.removeRenderer(it) }
boundVideoTrack = null
}
fun setupVideoIfNeeded(videoTrack: VideoTrack?, view: TextureViewRenderer) {
if (boundVideoTrack == videoTrack) {
return
}
cleanupVideoTrack()
boundVideoTrack = videoTrack
if (videoTrack != null) {
if (videoTrack is RemoteVideoTrack) {
videoTrack.addRenderer(view, videoSinkVisibility)
} else {
videoTrack.addRenderer(view)
}
}
}
DisposableEffect(view, mirror) {
view?.setMirror(mirror)
onDispose { }
}
DisposableEffect(room, videoTrack) {
onDispose {
videoSinkVisibility.onDispose()
cleanupVideoTrack()
}
}
DisposableEffect(currentCompositeKeyHash.toString()) {
onDispose {
view?.release()
}
}
AndroidView(
factory = { context ->
TextureViewRenderer(context).apply {
room.initVideoRenderer(this)
setupVideoIfNeeded(videoTrack, this)
view = this
}
},
update = { v -> setupVideoIfNeeded(videoTrack, v) },
modifier = modifier
.onGloballyPositioned { videoSinkVisibility.onGloballyPositioned(it) },
)
}
... ...
/*
* Copyright 2023 LiveKit, Inc.
*
* 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.livekit.android.room.track.video
import androidx.compose.ui.layout.LayoutCoordinates
import io.livekit.android.room.track.Track
/**
* A [VideoSinkVisibility] for compose views.
*
* To use, pass an `onGloballyPositioned` modifier your composable like so:
* ```
* modifier = Modifier.onGloballyPositioned { videoSinkVisibility.onGloballyPositioned(it) }
* ```
*/
class ComposeVisibility : VideoSinkVisibility() {
private var coordinates: LayoutCoordinates? = null
private var lastVisible = isVisible()
private var lastSize = size()
override fun isVisible(): Boolean {
return (coordinates?.isAttached == true &&
coordinates?.size?.width != 0 &&
coordinates?.size?.height != 0)
}
override fun size(): Track.Dimensions {
val width = coordinates?.size?.width ?: 0
val height = coordinates?.size?.height ?: 0
return Track.Dimensions(width, height)
}
// Note, LayoutCoordinates are mutable and may be reused.
fun onGloballyPositioned(layoutCoordinates: LayoutCoordinates) {
coordinates = layoutCoordinates
val visible = isVisible()
val size = size()
if (lastVisible != visible || lastSize != size) {
notifyChanged()
}
lastVisible = visible
lastSize = size
}
fun onDispose() {
if (coordinates == null) {
return
}
coordinates = null
notifyChanged()
}
}
... ...
... ... @@ -26,6 +26,11 @@ import io.livekit.android.room.track.Track
import io.livekit.android.room.track.video.ViewVisibility.Notifier
import java.util.Observable
/**
* Provides the visibility and dimensions of the video sink, allowing LiveKit
* to automatically manage the quality of the stream when adaptive streaming
* is used.
*/
abstract class VideoSinkVisibility : Observable() {
abstract fun isVisible(): Boolean
abstract fun size(): Track.Dimensions
... ... @@ -83,9 +88,12 @@ class ViewVisibility(private val view: View) : VideoSinkVisibility() {
private fun scheduleRecalculate() {
handler.removeCallbacksAndMessages(null)
handler.postDelayed({
recalculate()
}, 2000)
handler.postDelayed(
{
recalculate()
},
2000,
)
}
fun recalculate() {
... ...
... ... @@ -29,7 +29,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import io.livekit.android.composesample.ui.VideoRenderer
import io.livekit.android.compose.VideoRenderer
import io.livekit.android.room.Room
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.track.CameraPosition
... ...