diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 72e143c..ff36834 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -32,10 +32,15 @@ android { main.java.srcDirs += 'src/main/kotlin' } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "com.example.livekit_example" - minSdkVersion 16 + applicationId "io.livekit.flutter_example" + minSdkVersion 21 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 2c41092..fec9da2 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,7 +1,16 @@ + + + + + + + + + + createState() { return _PreConnectState( - 'ws://localhost:7880', - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5ODQzNDMyMzUsImlzcyI6IkFQSU1teGlMOHJxdUt6dFpFb1pKVjlGYiIsImp0aSI6InNlY29uZCIsIm5iZiI6MTYyNDM0MzIzNSwidmlkZW8iOnsicm9vbSI6Im15cm9vbSIsInJvb21Kb2luIjp0cnVlfX0.USJmnqcLabtsX5A0ZIjguG7jnbVkbZxi5RyPxLOXgOg', + '', + '', ); } } diff --git a/example/lib/room.dart b/example/lib/room.dart index 0d81f74..f33fc36 100644 --- a/example/lib/room.dart +++ b/example/lib/room.dart @@ -32,6 +32,20 @@ class _RoomState extends State with RoomDelegate { super.dispose(); } + _onConnected() async { + // video will fail when running in ios simulator + try { + var localVideo = await LocalVideoTrack.createCameraTrack(); + await widget.room.localParticipant.publishVideoTrack(localVideo); + } catch (e) { + print('could not publish video: $e'); + } + + var localAudio = await LocalAudioTrack.createTrack(); + await widget.room.localParticipant.publishAudioTrack(localAudio); + sortParticipants(); + } + void _onChange() { sortParticipants(); } @@ -92,20 +106,6 @@ class _RoomState extends State with RoomDelegate { } } - _onConnected() async { - // video will fail when running in ios simulator - try { - var localVideo = await LocalVideoTrack.createCameraTrack(); - await widget.room.localParticipant.publishVideoTrack(localVideo); - } catch (e) { - print('could not publish video: $e'); - } - - var localAudio = await LocalAudioTrack.createTrack(); - await widget.room.localParticipant.publishAudioTrack(localAudio); - sortParticipants(); - } - @override Widget build(BuildContext context) { _lastContext = context; @@ -184,6 +184,14 @@ class _VideoViewState extends State with ParticipantDelegate { super.dispose(); } + @override + void didUpdateWidget(covariant VideoView oldWidget) { + oldWidget.participant.removeListener(_onParticipantChanged); + widget.participant.addListener(_onParticipantChanged); + _onParticipantChanged(); + super.didUpdateWidget(oldWidget); + } + // register for change so Flutter will re-build the widget upon change void _onParticipantChanged() { var subscribedVideos = widget.participant.videoTracks.values.where((pub) { @@ -198,10 +206,13 @@ class _VideoViewState extends State with ParticipantDelegate { if (videoPub is RemoteTrackPublication) { videoPub.videoQuality = widget.quality; } - this.videoPub = videoPub; - } else { - this.videoPub = null; + // when muted, show placeholder + if (!videoPub.muted) { + this.videoPub = videoPub; + return; + } } + this.videoPub = null; }); } diff --git a/example/lib/src/controls.dart b/example/lib/src/controls.dart index cc5e7dd..4aa4630 100644 --- a/example/lib/src/controls.dart +++ b/example/lib/src/controls.dart @@ -14,15 +14,17 @@ class Controls extends StatefulWidget { } class _ControlsState extends State { + CameraPosition position = CameraPosition.FRONT; + @override void initState() { super.initState(); - widget.participant.addListener(_onChange); + participant.addListener(_onChange); } @override void dispose() { - widget.participant.removeListener(_onChange); + participant.removeListener(_onChange); super.dispose(); } @@ -69,6 +71,31 @@ class _ControlsState extends State { } } + _setCameraPosition(TrackPublication? pub, CameraPosition position) async { + if (this.position == position) { + return; + } + LocalVideoTrack? track; + if (pub?.track is LocalVideoTrack) { + track = pub!.track as LocalVideoTrack; + } + + if (track == null) { + return; + } + + try { + await track.restartTrack(LocalVideoTrackOptions(position: position)); + } catch (e) { + print('could not restart track: $e'); + return; + } + + setState(() { + this.position = position; + }); + } + _exit() { widget.room.disconnect(); } @@ -100,7 +127,8 @@ class _ControlsState extends State { videoPub = participant.videoTracks.values.first; } - if (videoPub != null && videoPub.muted) { + var videoEnabled = videoPub != null && !videoPub.muted; + if (videoEnabled) { buttons.add(IconButton( onPressed: _muteVideo, icon: const Icon(Icons.videocam_rounded), @@ -112,11 +140,34 @@ class _ControlsState extends State { )); } + if (position == CameraPosition.FRONT) { + buttons.add(IconButton( + icon: const Icon(Icons.video_camera_front_rounded), + onPressed: videoEnabled + ? () { + _setCameraPosition(videoPub, CameraPosition.BACK); + } + : null, + )); + } else { + buttons.add(IconButton( + icon: const Icon(Icons.video_camera_back_rounded), + onPressed: videoEnabled + ? () { + _setCameraPosition(videoPub, CameraPosition.FRONT); + } + : null, + )); + } + buttons.add(IconButton( onPressed: _exit, icon: const Icon(Icons.close_rounded), )); - return Row(children: buttons); + return Row( + mainAxisAlignment: MainAxisAlignment.center, + children: buttons, + ); } }