davidliu
Committed by GitHub

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

* Self camera

* spotless
  1 +/*
  2 + * Copyright 2024 LiveKit, Inc.
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
1 package io.livekit.android.sample.basic 17 package io.livekit.android.sample.basic
2 18
3 import android.Manifest 19 import android.Manifest
@@ -13,7 +29,9 @@ import io.livekit.android.LiveKit @@ -13,7 +29,9 @@ import io.livekit.android.LiveKit
13 import io.livekit.android.events.RoomEvent 29 import io.livekit.android.events.RoomEvent
14 import io.livekit.android.events.collect 30 import io.livekit.android.events.collect
15 import io.livekit.android.renderer.SurfaceViewRenderer 31 import io.livekit.android.renderer.SurfaceViewRenderer
  32 +import io.livekit.android.renderer.TextureViewRenderer
16 import io.livekit.android.room.Room 33 import io.livekit.android.room.Room
  34 +import io.livekit.android.room.track.LocalVideoTrack
17 import io.livekit.android.room.track.Track 35 import io.livekit.android.room.track.Track
18 import io.livekit.android.room.track.VideoTrack 36 import io.livekit.android.room.track.VideoTrack
19 import kotlinx.coroutines.launch 37 import kotlinx.coroutines.launch
@@ -32,6 +50,7 @@ class MainActivity : AppCompatActivity() { @@ -32,6 +50,7 @@ class MainActivity : AppCompatActivity() {
32 50
33 // Setup the video renderer 51 // Setup the video renderer
34 room.initVideoRenderer(findViewById<SurfaceViewRenderer>(R.id.renderer)) 52 room.initVideoRenderer(findViewById<SurfaceViewRenderer>(R.id.renderer))
  53 + room.initVideoRenderer(findViewById<TextureViewRenderer>(R.id.local_camera))
35 54
36 requestNeededPermissions { connectToRoom() } 55 requestNeededPermissions { connectToRoom() }
37 } 56 }
@@ -62,6 +81,12 @@ class MainActivity : AppCompatActivity() { @@ -62,6 +81,12 @@ class MainActivity : AppCompatActivity() {
62 localParticipant.setMicrophoneEnabled(true) 81 localParticipant.setMicrophoneEnabled(true)
63 localParticipant.setCameraEnabled(true) 82 localParticipant.setCameraEnabled(true)
64 83
  84 + // Attach local video camera
  85 + val localTrack = localParticipant.getTrackPublication(Track.Source.CAMERA)?.track as? LocalVideoTrack
  86 + if (localTrack != null) {
  87 + attachLocalVideo(localTrack)
  88 + }
  89 +
65 // Attach video of remote participant if already available. 90 // Attach video of remote participant if already available.
66 val remoteVideoTrack = room.remoteParticipants.values.firstOrNull() 91 val remoteVideoTrack = room.remoteParticipants.values.firstOrNull()
67 ?.getTrackPublication(Track.Source.CAMERA) 92 ?.getTrackPublication(Track.Source.CAMERA)
@@ -85,6 +110,10 @@ class MainActivity : AppCompatActivity() { @@ -85,6 +110,10 @@ class MainActivity : AppCompatActivity() {
85 findViewById<View>(R.id.progress).visibility = View.GONE 110 findViewById<View>(R.id.progress).visibility = View.GONE
86 } 111 }
87 112
  113 + private fun attachLocalVideo(videoTrack: VideoTrack) {
  114 + videoTrack.addRenderer(findViewById<SurfaceViewRenderer>(R.id.local_camera))
  115 + }
  116 +
88 private fun requestNeededPermissions(onHasPermissions: () -> Unit) { 117 private fun requestNeededPermissions(onHasPermissions: () -> Unit) {
89 val requestPermissionLauncher = 118 val requestPermissionLauncher =
90 registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { grants -> 119 registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { grants ->
@@ -10,10 +10,18 @@ @@ -10,10 +10,18 @@
10 android:layout_width="match_parent" 10 android:layout_width="match_parent"
11 android:layout_height="match_parent" /> 11 android:layout_height="match_parent" />
12 12
  13 +
  14 + <io.livekit.android.renderer.TextureViewRenderer
  15 + android:id="@+id/local_camera"
  16 + android:layout_width="150dp"
  17 + android:layout_height="150dp"
  18 + android:layout_gravity="bottom|end"
  19 + android:layout_margin="10dp" />
  20 +
13 <ProgressBar 21 <ProgressBar
14 android:id="@+id/progress" 22 android:id="@+id/progress"
15 android:layout_width="wrap_content" 23 android:layout_width="wrap_content"
16 android:layout_height="wrap_content" 24 android:layout_height="wrap_content"
17 android:layout_gravity="center" /> 25 android:layout_gravity="center" />
18 26
19 -</FrameLayout>  
  27 +</FrameLayout>