Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC: How to pass RTCVideoEncoderSettings into RTCVideoEncoder

I am working on a webrtc screensharing app. Therefore I am utilizing the objective-c webrtc framework.

Now I have problems on how to pass in the RTCVideoEncoderSettings (http://cocoadocs.org/docsets/GoogleWebRTC/1.1.20266/Classes/RTCVideoEncoderSettings.html) into an encoder (VP9). This is what I currently have:

public class CustomVideoEncoderFactory : NSObject, RTCVideoEncoderFactory {

var encoder: RTCVideoEncoder?
var preferredCodec: RTCVideoCodecInfo = RTCVideoCodecInfo(name: "VP9")

public func createEncoder(_ info: RTCVideoCodecInfo) -> RTCVideoEncoder? {

    let vp9Encoder = RTCVideoEncoderVP9.vp9Encoder()!
    // How to pass the RTCVideoEncoderSettings into this encoder???
    return vp9Encoder
}

public func supportedCodecs() -> [RTCVideoCodecInfo] {
    return [RTCVideoCodecInfo(name: "VP9")]
}

}

There is a method called startEncodeWithSettings (http://cocoadocs.org/docsets/GoogleWebRTC/1.1.20266/Protocols/RTCVideoEncoder.html) but I am unsure how to integrate this with my current code. I tried to subclass (public class CustomVideoEncoder: NSObject, RTCVideoEncoder { ... }) which did not work.

Thank you for your help!

like image 691
Silicium Avatar asked Nov 06 '22 08:11

Silicium


1 Answers

Ok, I found a solution. It turns out that for VP9 and VP8 there are the objective-c wrappings missing. VP9 and Vp8 are directly link to the native implementation! Therefore it is ONLY possible to subclass if you are using h264. For changing the settings on VP9 and VP8 it is necessary to modify the settings inside the c++ code!

Example of a custom Encoder Factory:

public class CustomVideoEncoderFactory : NSObject, RTCVideoEncoderFactory {

public func createEncoder(_ info: RTCVideoCodecInfo) -> RTCVideoEncoder? {

    let encoder = super.createEncoder(info) // will create the h264 encoder
    let customEncoder = CustomVideoEncoder()
    self.encoder = customEncoder
    return encoder
}

public func supportedCodecs() -> [RTCVideoCodecInfo] {

    return [RTCVideoCodecInfo(name: kRTCVideoCodecH264Name)] }}

Example of a custom Encoder:

public class CustomVideoEncoder: NSObject, RTCVideoEncoder {

public var encoder: RTCVideoEncoder? // ONLY WORKS WITH h264

public func setCallback(_ callback: @escaping RTCVideoEncoderCallback) {

    return encoder!.setCallback(callback)
}

 public func startEncode(with settings: RTCVideoEncoderSettings, numberOfCores: Int32) -> Int {

     // Change settings here !
    let res = encoder!.startEncode(with: settings, numberOfCores: numberOfCores) 
}

public func release() -> Int {

    return encoder!.release()
}

 public func encode(_ frame: RTCVideoFrame, codecSpecificInfo info: RTCCodecSpecificInfo?, frameTypes: [NSNumber]) -> Int {

     return encoder!.encode(frame, codecSpecificInfo: info, frameTypes: frameTypes)
 }

public func setBitrate(_ bitrateKbit: UInt32, framerate: UInt32) -> Int32 {

    return encoder!.setBitrate(bitrateKbit, framerate: framerate)
}

public func implementationName() -> String {

    return encoder!.implementationName()
}

public func scalingSettings() -> RTCVideoEncoderQpThresholds? {

    return encoder!.scalingSettings()
}}
like image 162
Silicium Avatar answered Nov 15 '22 05:11

Silicium