davidliu
Committed by GitHub

Add self camera preview to basic example app (#479)

* Self camera

* spotless
/*
* Copyright 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.sample.basic
import android.Manifest
... ... @@ -13,7 +29,9 @@ import io.livekit.android.LiveKit
import io.livekit.android.events.RoomEvent
import io.livekit.android.events.collect
import io.livekit.android.renderer.SurfaceViewRenderer
import io.livekit.android.renderer.TextureViewRenderer
import io.livekit.android.room.Room
import io.livekit.android.room.track.LocalVideoTrack
import io.livekit.android.room.track.Track
import io.livekit.android.room.track.VideoTrack
import kotlinx.coroutines.launch
... ... @@ -32,6 +50,7 @@ class MainActivity : AppCompatActivity() {
// Setup the video renderer
room.initVideoRenderer(findViewById<SurfaceViewRenderer>(R.id.renderer))
room.initVideoRenderer(findViewById<TextureViewRenderer>(R.id.local_camera))
requestNeededPermissions { connectToRoom() }
}
... ... @@ -62,6 +81,12 @@ class MainActivity : AppCompatActivity() {
localParticipant.setMicrophoneEnabled(true)
localParticipant.setCameraEnabled(true)
// Attach local video camera
val localTrack = localParticipant.getTrackPublication(Track.Source.CAMERA)?.track as? LocalVideoTrack
if (localTrack != null) {
attachLocalVideo(localTrack)
}
// Attach video of remote participant if already available.
val remoteVideoTrack = room.remoteParticipants.values.firstOrNull()
?.getTrackPublication(Track.Source.CAMERA)
... ... @@ -85,6 +110,10 @@ class MainActivity : AppCompatActivity() {
findViewById<View>(R.id.progress).visibility = View.GONE
}
private fun attachLocalVideo(videoTrack: VideoTrack) {
videoTrack.addRenderer(findViewById<SurfaceViewRenderer>(R.id.local_camera))
}
private fun requestNeededPermissions(onHasPermissions: () -> Unit) {
val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { grants ->
... ...
... ... @@ -10,6 +10,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<io.livekit.android.renderer.TextureViewRenderer
android:id="@+id/local_camera"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="bottom|end"
android:layout_margin="10dp" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
... ...