i am using livekit sdk in flutter , for live stre...
# ask-ai
g
i am using livekit sdk in flutter , for live streaming but unable to achieve native camera like zoom or focus
t
Thanks for reaching out, bisharat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you achieve native camera-like zoom and focus functionality in the LiveKit Flutter SDK, here are some straightforward approaches you can try: 1. Zoom Control: • You can use the
setZoom
method to programmatically adjust the camera’s zoom level. A slider could be a great way to make this dynamic and user-friendly. • Here’s a simple example to get you started:
Copy code
void setZoom(double zoomLevel) async {
  final videoTrack = _renderer?.srcObject!.getVideoTracks().first;
  if (videoTrack == null) return;
  await rtc.Helper.setZoom(videoTrack, zoomLevel);
}
2. Tap-to-Focus: • To enable tap-to-focus, try wrapping your
VideoTrackRenderer
with a
GestureDetector
. Then, use the
setFocusPoint
and
setExposurePoint
methods to set the focus based on where the user taps. • Here’s an example implementation for this:
Copy code
`void _handleTapToFocus(TapDownDetails details, BoxConstraints constraints) {
  final videoTrack = _getVideoTrack(); // Get your video track
  if (videoTrack == null) return;

  final point = Point(
    details.localPosition.dx / constraints.maxWidth,
    details.localPosition.dy / constraints.maxHeight,
  );

  rtc.Helper.setFocusPoint(videoTrack, point);
  rtc.Helper.setExposurePoint(videoTrack, point);
}`
If you have any questions or need a hand with implementing these features, please don’t hesitate to reach out. I’m here to help!
Sources: lib/src/track/options.dart
g
when i zoom on iphone 16 pro max up to 4x video is pixelated or distorted , same thing when i am doin on native camera app it works fine i need same camera results in live streaming , is there any way to acheive this
t
I'm sorry to hear you're experiencing pixelation issues during zoom on your iPhone 16 Pro Max. This is often due to some limitations in the current Flutter SDK's zoom functionality, which doesn't automatically enhance video quality when zooming in. But don’t worry, there are several ways we can work on improving this together: 1. High-Resolution Capture: Try configuring your camera to capture video at the highest possible resolution using
VideoCaptureOptions.resolution
. This can make a noticeable difference. 2. Adaptive Streaming: Enabling adaptive streaming allows the system to request higher-quality video layers when the video element gets larger during zoom, which helps maintain clarity. 3. Simulcast Configuration: Setting up multiple simulcast layers ensures that higher-quality streams are available when you need them, especially during zoom. 4. Codec Optimization: Using the VP9 codec can offer better video quality at the same bitrate, which is great for smoother zoom experiences. 5. Native iOS Features: For even more advanced zoom features like smooth transitions and hardware-level improvements, you might consider using platform channels to access native iOS capabilities such as
AVCaptureDevice.videoZoomFactor
. If you'd like, I’m here to help guide you through implementing any of these options or answer any questions you have. Just let me know! Sources: lib/src/options.dart | Sources/LiveKit/Views/VideoView+PinchToZoom.swift | Bringing Zoom's end-to-end optimizations to WebRTC | Configuring the Client SDK for Optimal Video Quality
g
how to acheive this Native iOS Features: For even more advanced zoom features like smooth transitions and hardware-level improvements, you might consider using platform channels to access native iOS capabilities such as
t
To help you achieve those advanced zoom features like smooth transitions and hardware-level improvements using native iOS capabilities, you can tap into Flutter's platform channels to access native APIs such as
AVCaptureDevice.videoZoomFactor
. Let me walk you through how to implement this step-by-step: 1. Create a MethodChannel: First, set up a method channel in your Flutter app to communicate with the native iOS code.
Copy code
class Native {
  static final channel = MethodChannel('livekit_client');
}
2. Implement Native iOS Code: On the iOS side, you can control the zoom level using
AVCaptureDevice.videoZoomFactor
. Here’s an example:
Copy code
@objc func handleZoom(_ zoomFactor: CGFloat) {
    guard let device = currentCaptureDevice else { return }
    do {
        try device.lockForConfiguration()
        device.videoZoomFactor = zoomFactor
        device.unlockForConfiguration()
    } catch {
        print("Failed to set zoom factor")
    }
}
3. Connect Flutter to Native Code: Next, use the method channel to call the native zoom function from your Flutter code like this:
Copy code
`Future setZoom(double zoomFactor) async {
  await Native.channel.invokeMethod('setZoom', {'zoomFactor': zoomFactor});
}`
*4. Handle Gestures in Flutter:* Finally, you can capture pinch gestures with a `GestureDetector` to dynamically adjust the zoom level:
`GestureDetector(
  onScaleUpdate: (details) {
    if (details.scale != 1.0) {
      setZoom(details.scale);
    }
  },
  child: VideoTrackRenderer(widget.track),
)`
This method lets you fully utilize the iOS camera hardware capabilities while keeping your UI in Flutter. If you have any questions or need help with any part of this, please don’t hesitate to ask—I’m here to support you!
Sources: shared_swift/LiveKitPlugin.swift | Sources/LiveKit/Views/VideoView+PinchToZoom.swift
g
can i achieve like i have opened real mobile camera app in photo mode and sharing video from there b/c real camera works perfect in zoom focus like instant results and better lens switching to adjust zoom and focus
t
Thank you for reaching out! I wanted to share that the LiveKit Flutter SDK doesn’t support using the native camera app (like the default iOS or Android camera UI) for video streaming. Instead, it captures video directly through WebRTC, which gives you great programmatic control over features like zoom, focus, and exposure. I know the native camera app offers handy features like instant zoom, focus, and lens switching, but these aren’t available for real-time streaming in LiveKit. The good news is, you can still achieve similar effects by: • Using
setZoom
to control zoom programmatically. • Adding tap-to-focus and exposure with
onViewFinderTap
. • Setting up
CameraCaptureOptions
for automatic focus and exposure modes. If you’re looking for that native camera feel and performance, building a custom native module for iOS or Android and connecting it with Flutter via platform channels could be a great path. I’m here to help if you’d like some guidance on that! Sources: lib/src/hardware/hardware.dart | README.md
g
help me building a custom native module for iOS or Android and connecting it with Flutter via platform channels ,
t
I'm happy to guide you through building a custom native module for iOS or Android and connecting it with Flutter via platform channels to achieve advanced camera functionality. Here’s a friendly step-by-step approach to help you along: 1. Create a Flutter Plugin: • Start by running
flutter create --template=plugin my_camera_plugin
to set up your plugin project. • Then, update your
pubspec.yaml
to declare the platform-specific plugin classes. 2. Set Up Platform Channels: • In your Dart code, create a method channel to enable communication with the native side:
Copy code
static const MethodChannel channel = MethodChannel('my_camera_plugin');
• On the native side, make sure to register method call handlers that can process requests coming from Flutter. 3. Implement Native Camera Logic: • Android: Utilize CameraX for advanced camera controls like zoom and focus. Remember to implement lifecycle-aware camera usage and handle permissions properly in
AndroidManifest.xml
. • iOS: Use AVFoundation APIs to manage the camera, including
AVCaptureDevice.videoZoomFactor
for zoom and focus adjustments. 4. Handle Permissions: • It’s important to request camera and microphone permissions on both platforms. For iOS, add the necessary usage descriptions in
Info.plist
. For Android, you can request permissions at runtime using the
permission_handler
package. 5. Test and Integrate: • Be sure to verify that the method channel communication and camera functionality are working smoothly. • If you like, you can also integrate with LiveKit for real-time video streaming. Please don’t hesitate to reach out if you’d like detailed code examples or need a hand with any specific step—I’m here to help! Sources: lib/src/support/native.dart | livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXCapturer.kt | src/Permissions.ts | React Native SDK | livekit/livekit_composite | DeepWiki | pubspec.yaml