can we pass a custom, non-camera, video feed to a ...
# ask-ai
q
can we pass a custom, non-camera, video feed to a livekit room with the react native sdk
t
Thanks for reaching out, Mitchell. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can pass a custom, non-camera video feed to a LiveKit room using the React Native SDK. The SDK gives you access to WebRTC primitives through
@livekit/react-native-webrtc
, which lets you create and publish your own custom video tracks. Here’s how you can do it: 1. First, generate a custom video stream using a native module or library that can produce a WebRTC-compatible
MediaStreamTrack
. 2. Next, use the
LocalParticipant.publishTrack()
method to publish the
MediaStreamTrack
you created to the LiveKit room. 3. Optionally, you can add metadata like the track name and source (for example,
Track.Source.Unknown
). The React Native SDK’s high-level API makes common tasks like camera and screen sharing really straightforward, but working with custom video sources means you’ll be interacting directly with the WebRTC layer. If you’d like, I’m here to help guide you through the implementation—just let me know! Sources: React Native SDK | livekit/livekit_composite | DeepWiki | src/RTCPeerConnection.ts | docs/functions/VideoView.html | Streaming video from a canvas with WebRTC and React
q
how do i use react native webrtc to take my buffer and create a new track with the video
t
Thanks for reaching out! I understand you're looking to create a
MediaStreamTrack
from a video buffer in React Native WebRTC. Currently, there isn't a direct way to do this purely in the JavaScript layer. Usually, we use
mediaDevices.getUserMedia()
or
mediaDevices.getDisplayMedia()
to get media streams from the camera or screen capture. For working with custom video buffers, you'll want to dive into the native WebRTC layer. On iOS, this might mean using
CVPixelBufferRef
and
RTCVideoFrame
, while on Android, you could explore
VideoCapturer
and
VideoSource
. These native components can help process your video buffer and then expose it as a WebRTC-compatible track through the React Native bridge. If you'd like, I’m here to help guide you through the implementation or brainstorm other options that might work for your project. Just let me know! Sources: src/RTCRtpSender.ts | Streaming video from a canvas with WebRTC and React
q
we already have our stream passing into CVPixelBufferRef, how would we get it from there to our javascript layer and then take that and register it as a new track
t
I'd be happy to guide you through passing your
CVPixelBufferRef
to the JavaScript layer and registering it as a new track. Here's a simple way to approach it: 1. Create a Native Capturer: Start by implementing a custom video capturer in iOS that inherits from
RTCVideoCapturer
. This will help you manage your
CVPixelBufferRef
frames smoothly. 2. Convert to RTCVideoFrame: Wrap your pixel buffer using
RTCCVPixelBuffer
and create an
RTCVideoFrame
with the right timestamps and rotation. For example:
Copy code
RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer];
RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
                                                         rotation:RTCVideoRotation_0
                                                      timeStampNs:timeStampNs];
3. Pass via Delegate: Then, use the
capturer:didCaptureVideoFrame:
method to send these frames into WebRTC. 4. Access in JavaScript: Finally, the frames will be accessible as a
MediaStreamTrack
in your JavaScript layer. From there, you can easily publish the track using LiveKit's
LocalParticipant.publishTrack()
method. If any of these steps feel tricky or you'd like me to walk you through them in more detail, please don’t hesitate to ask. I’m here to help! Sources: Documentation/BasicUsage.md | Streaming video from a canvas with WebRTC and React | webrtc-sys/src/objc_video_frame_buffer.mm | React Native SDK | livekit/livekit_composite | DeepWiki
q
can we list available mediaStreamTracks on the javascript side so we can see our stream
t
I'd love to help you list the available
MediaStreamTracks
on the JavaScript side in React Native WebRTC. You can use these handy methods from the
MediaStream
class: • `getTracks()`: This gives you all the tracks, both audio and video. • `getAudioTracks()`: This one returns just the audio tracks. • `getVideoTracks()`: And this returns only the video tracks. • `getTrackById(trackId)`: If you want a specific track, you can get it by its ID. Here’s a simple example to illustrate:
Copy code
// List all tracks
const allTracks = mediaStream.getTracks();
const audioTracks = mediaStream.getAudioTracks();
const videoTracks = mediaStream.getVideoTracks();

console.log('All Tracks:', allTracks);
console.log('Audio Tracks:', audioTracks);
console.log('Video Tracks:', videoTracks);

// Access a specific track by ID
if (allTracks.length > 0) {
  const specificTrack = mediaStream.getTrackById(allTracks[0].id);
  console.log('Specific Track:', specificTrack);
}
If you have any questions or need a hand with this, just let me know—I’m here to help! Sources: android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java | src/components/VideoTrack.tsx
q
Can you give me an example of the swift file that sends the frames to Webrtc? Am I assigning the media stream class there or only on the JavaScript side?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!