davidliu
Committed by GitHub

Remove compose from livekit-android-sdk (#355)

... ... @@ -50,14 +50,16 @@ dependencies {
}
```
Compose-based apps should check out our [Android Components SDK](https://github.com/livekit/components-android) for composables support.
You'll also need jitpack as one of your repositories.
```groovy
subprojects {
```groovy title="settings.gradle"
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
// ...
//...
maven { url 'https://jitpack.io' }
// For SNAPSHOT access
... ...
... ... @@ -57,12 +57,8 @@ android {
}
buildFeatures {
compose true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_version
}
kotlinOptions {
freeCompilerArgs = ["-Xinline-classes", "-opt-in=kotlin.RequiresOptIn"]
jvmTarget = java_version
... ... @@ -153,7 +149,6 @@ 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-2024 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)
}
/**
* To be called from a compose view, using `Modifier.onGloballyPositioned`.
*
* Example:
* ```
* modifier = Modifier.onGloballyPositioned { videoSinkVisibility.onGloballyPositioned(it) }
* ```
*/
fun onGloballyPositioned(layoutCoordinates: LayoutCoordinates) {
// Note, LayoutCoordinates are mutable and may be reused.
coordinates = layoutCoordinates
val visible = isVisible()
val size = size()
if (lastVisible != visible || lastSize != size) {
notifyChanged()
}
lastVisible = visible
lastSize = size
}
/**
* To be called when the associated compose view no longer exists.
*
* Example:
* ```
* DisposableEffect(room, videoTrack) {
* onDispose {
* videoSinkVisibility.onDispose()
* }
* }
* ```
*/
fun onDispose() {
if (coordinates == null) {
return
}
coordinates = null
notifyChanged()
}
}
... ... @@ -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.compose.VideoRenderer
import io.livekit.android.composesample.ui.VideoRenderer
import io.livekit.android.room.Room
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.track.CameraPosition
... ...