<https://example.livekit.io/中包含虚拟背景功能>,给出他的代码示例
# ask-ai
a
t
Thanks for reaching out, 徐旭. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit provides virtual background functionality across multiple platforms. Below are examples for implementing this feature: JavaScript/Web Implementation: LiveKit offers the
@livekit/track-processors
package with built-in virtual background support: •
BackgroundBlur(blurRadius)
- Applies a blur effect to the background •
VirtualBackground(imagePath)
- Replaces the background with a custom image Basic Usage Example:
Copy code
import { BackgroundBlur, VirtualBackground, supportsBackgroundProcessors } from '@livekit/track-processors';

if(!supportsBackgroundProcessors()) {
  throw new Error("this browser does not support background processors")
}

const videoTrack = await createLocalVideoTrack();
const blur = BackgroundBlur(10);
await videoTrack.setProcessor(blur);
room.localParticipant.publishTrack(videoTrack);

// To disable
async function disableBackgroundBlur() {
  await videoTrack.stopProcessor();
}

// Virtual background
const virtualBg = VirtualBackground('/path/to/image.jpg');
await videoTrack.setProcessor(virtualBg);
React Implementation Example:
Copy code
import { BackgroundBlur, VirtualBackground } from '@livekit/track-processors';
import { isLocalTrack } from 'livekit-client';

export function CameraSettings() {
  const { cameraTrack } = useLocalParticipant();
  const [backgroundType, setBackgroundType] = React.useState<'none' | 'blur' | 'image'>('none');

  React.useEffect(() => {
    if (isLocalTrack(cameraTrack?.track)) {
      if (backgroundType === 'blur') {
        cameraTrack.track?.setProcessor(BackgroundBlur());
      } else if (backgroundType === 'image' && virtualBackgroundImagePath) {
        cameraTrack.track?.setProcessor(VirtualBackground(virtualBackgroundImagePath));
      } else {
        cameraTrack.track?.stopProcessor();
      }
    }
  }, [cameraTrack, backgroundType, virtualBackgroundImagePath]);
}
Android Implementation: LiveKit provides the
VirtualBackgroundVideoProcessor
for Android: Installation: Sources: lib/CameraSettings.tsx | livekit-android-track-processors/src/main/java/io/livekit/android/track/pro... | Development Tools | livekit/livekit_composite | DeepWiki | src/transformers/BackgroundTransformer.ts
a
共享桌面场景下,用户电脑是2k的,前端推流的分辨率能推送2k吗?const room = new Room({ adaptiveStream: false, // adaptiveStream: { pixelDensity: 'screen' }, dynacast: true, publishDefaults: { simulcast: true, videoEncoding: ScreenSharePresets.h1080fps30.encoding, videoSimulcastLayers: [VideoPresets.h180, VideoPresets.h1080], videoCodec: 'h264', screenShareSimulcastLayers: [VideoPresets.h720, VideoPresets.h1080], screenShareEncoding: ScreenSharePresets.h1080fps30.encoding, audioPreset: 'musicHighQualityStereo', // musicHighQualityStereo telephone forceStereo: true // 立体声音轨 },上述是livekit推流配置,
t
Hey there! Want to level up your screen sharing game with LiveKit? I've got some awesome tips to help you support crystal-clear 2K resolution. Let me walk you through it like we're solving this together. 1. Use the "Original" Preset: The JavaScript SDK has this super handy
original
preset in
ScreenSharePresets
that captures your screen exactly as it is, without any annoying resizing:
Copy code
export const ScreenSharePresets = {
  // ... other presets
  // original resolution, without resizing
  original: new VideoPreset(0, 0, 7_000_000, 30, 'medium'),
}
Cool, right? This preset grabs your screen's native resolution and gives you a generous 7 Mbps bitrate to handle those high-res details. 2. Specify a Custom 2K Resolution: Want more control? No problem! You can easily define a custom 2K resolution (2560x1440) using `ScreenShareCaptureOptions`:
Copy code
const screenShareOptions = {
  resolution: {
    width: 2560,
    height: 1440,
    frameRate: 30
  }
};
3. Ensure Sufficient Bitrate: Pro tip for 2K content: allocate 6-8 Mbps to keep things looking sharp. You can set this up in your encoding settings:
Copy code
const publishOptions = {
  screenShareEncoding: ScreenSharePresets.original.encoding
};
And just like that, you're all set to push stunning 2K screen sharing! Let me know if you need any more help along the way. Sources: examples/demo/demo.ts | lib/src/types/video_parameters.dart
a
screenShareSimulcastLayers: [VideoPresets.h720, VideoPresets.h1080], screenShareEncoding: ScreenSharePresets.h1080fps30.encoding,我想问的是如果设置1080p分辨率,用户的电脑是2k,实际推流分辨率是多少?
t
Hey there! Let me break down what happens when your screen sharing gets a bit technical. When you set
screenShareEncoding
to 1080p but you've got a super crisp 2K display, LiveKit's got a smart solution for you. Resolution Scaling Process: • First, LiveKit grabs your entire screen at its full, beautiful 2K resolution. • Then, it gently scales things down to fit that 1080p streaming sweet spot. • Think of it like taking a wide landscape photo and cropping it to fit a standard frame - your 2K screen (2560x1440) gets smoothly adjusted to 1080p with just a bit of resizing magic. Encoding Parameters: • Just to be clear,
screenShareEncoding
is more about controlling the quality (bitrate and framerate) rather than the actual capture size. • So with the
h1080fps30
preset, you're looking at a max bitrate of 5,000,000 bps and a smooth 30 frames per second. Actual Streaming Resolution: The end result? A crisp 1080p stream that looks great, scaled down from your original 2K capture. Alternative: Want to keep things exactly as they appear on your screen? No problem! Just use the
original
preset, which lets you stream at your native resolution without any resizing. Bottom line: Your 2K screen gets a friendly little makeover to become a 1080p stream, keeping things looking sharp and professional! Sources: lib/src/participant/local.dart | src/room/track/options.ts
a
INFO livekit.pub rtc/participant.go:1676 pending track added {"room": "0ceacb42d41b4bd89eb941be1c60698c", "roomID": "RM_HzcrDUrw9LKK", "participant": "f0053cb5d4304acfb2c569b848e3fd67", "pID": "PA_Fv6ssKQSfE2C", "remote": false, "trackID": "TR_VSgoRHJ2SurTQy", "track": {"sid": "TR_VSgoRHJ2SurTQy", "type": 1, "width": 1536, "height": 864, "source": 3, "layers": [{"width": 1280, "height": 720, "bitrate": 1700000}, {"quality": 1, "width": 1536, "height": 864, "bitrate": 3000000}, {"quality": 2, "width": 1536, "height": 864, "bitrate": 5000000}], "codecs": [{"mimeType": "video/AV1", "cid": "a1386ba2-4a43-441c-82bf-ab0ada67cd49"}, {"mimeType": "video/vp8"}], "stream": "screen"}, "request": {"cid": "a1386ba2-4a43-441c-82bf-ab0ada67cd49", "type": 1, "width": 1536, "height": 864, "source": 3, "layers": [{"width": 1280, "height": 720, "bitrate": 1700000}, {"quality": 1, "width": 1536, "height": 864, "bitrate": 3000000}, {"quality": 2, "width": 1536, "height": 864, "bitrate": 5000000}], "simulcastCodecs": [{"codec": "AV1", "cid": "a1386ba2-4a43-441c-82bf-ab0ada67cd49"}, {"codec": "vp8"}]}}这是livekit的日志,上面的864分辨率是用户电脑的分辨率打印,还是实际推流到room的流就是864分辨率?
t
Hey there! Let me break down how LiveKit handles resolution in its logs. Think of it like this: the resolution you see (like 864p) is actually showing the real resolution being captured and streamed to the room - not just what's on your screen. Here's the scoop: • When you're screen sharing, the resolution reflects either the capture settings you've chosen or just the raw size of your display. • For camera tracks, it pulls directly from the actual camera settings. • If simulcast is on, the log shows the highest-quality layer being published. So when you see that 864p, it's basically telling you the exact resolution being encoded and sent to the room. This might be a bit different from your full screen resolution because of things like capture settings, internet bandwidth, or encoding preferences. Pretty cool, right? Sources: livekit/src/room/options.rs | livekit-android-sdk/src/main/java/io/livekit/android/room/track/LocalScreen... | lib/src/participant/local.dart | Codecs and more | LiveKit Docs