livekit / client-sdk-swift

LiveKit Swift Client SDK. Easily build live audio or video experiences into your mobile app, game or website.
https://livekit.io
Apache License 2.0
174 stars 84 forks source link

Expose VideoFrame properties #341

Closed hiroshihorie closed 4 months ago

hiroshihorie commented 4 months ago

Exposes buffer, pixelBuffer etc.

sameers27 commented 4 months ago

Also can we make public toRTCType() on VideoFrame?

hiroshihorie commented 4 months ago

We don't expose WebRTC types directly, what is your use case ?

sameers27 commented 4 months ago

We don't expose WebRTC types directly, what is your use case ?

We currently have our own RTCVideoFrame decoder. To continue using it we would need to convert the VideoFrame to RTCVideoFrame

hiroshihorie commented 4 months ago

Your RTCVideoFrame decoder is written for another WebRTC SDK ? Even I expose underlying WebRTC symbol directly it's a LKRTCVideoFrame object I don't think it's compatible to use directly compiler will complain.

What I think will work is to write a converter from VideoFrame to RTCVideoFrame. There shouldn't be much overhead since buffer memory isn't being moved around.

I can write an example snippet.

sameers27 commented 4 months ago

Your RTCVideoFrame decoder is written for another WebRTC SDK ? Even I expose underlying WebRTC symbol directly it's a LKRTCVideoFrame object I don't think it's compatible to use directly compiler will complain.

What I think will work is to write a converter from VideoFrame to RTCVideoFrame. There shouldn't be much overhead since buffer memory isn't being moved around.

I can write an example snippet.

That would be great! Our decoder is written for any webrtc sdk. We also use it for livekit

hiroshihorie commented 4 months ago

Can you try something like :


extension LiveKit.VideoRotation {
    func toWebRTCRotation() -> WebRTC.RTCVideoRotation {
        switch self {
        case ._0: return ._0
        case ._90: return ._90
        case ._180: return ._180
        case ._270: return ._270
        }
    }
}

extension LiveKit.VideoFrame {
    // Example to convert frame
    func toWebRTCVideoFrame() -> WebRTC.RTCVideoFrame {
        let rtcBuffer: RTCVideoFrameBuffer
        if let buffer = buffer as? CVPixelVideoBuffer {
            rtcBuffer = WebRTC.RTCCVPixelBuffer(pixelBuffer: buffer.pixelBuffer)
        } else if let buffer = buffer as? I420VideoBuffer {
            rtcBuffer = WebRTC.RTCI420Buffer(width: buffer.chromaWidth,
                                             height: buffer.chromaHeight,
                                             dataY: buffer.dataY,
                                             dataU: buffer.dataU,
                                             dataV: buffer.dataV)
        } else {
            fatalError("Unsupported type")
        }

        return RTCVideoFrame(buffer: rtcBuffer,
                             rotation: rotation.toWebRTCRotation(),
                             timeStampNs: timeStampNs)
    }
}