UnityRenderStreaming icon indicating copy to clipboard operation
UnityRenderStreaming copied to clipboard

[REQUEST] Need a way to adjust encoding bitrate

Open kevinfoley opened this issue 4 years ago • 22 comments

In the Unity WebRTC package, the encoding bitrate can normally be adjusted from an RTCRtpSender instance. However, the RTCRtpSender is not exposed directly in the Render Streaming package. In version 3.0.1 and earlier of Render Streaming, the AddTrack() function in SignalingHandlerBase returned the Transceiver, and it was possible to adjust the video quality in a custom Broadcast class like this:

private void ApplyBitrate(RTCRtpTransceiver transceiver) {
	var parameters = transceiver.Sender.GetParameters();
	for (int i = 0; i < parameters.encodings.Length; i++) {
		RTCRtpEncodingParameters encoding = parameters.encodings[i];
		encoding.maxBitrate = this.maxBitrate;
		encoding.minBitrate = this.minBitrate;
		parameters.encodings[i] = encoding;
	}
	transceiver.Sender.SetParameters(parameters);
}

As of version 3.1, SignalingHandlerBase no longer returns the Transceiver. There is no way to access the Transceiver from any code outside of the Render Streaming package, and therefore no way to access the RTCRtpSender and adjust the encoding bitrate.

kevinfoley avatar Dec 11 '21 02:12 kevinfoley

you can inherit from any StreamSender class you need, and then override SetSender method, so that you can get the sender and its parameter, finally you can set bitrate of encoding from parameter

Minamiyama avatar Dec 12 '21 09:12 Minamiyama

@kevinfoley Right, we will add the API to control bitrate and framerate of video stream.

karasusan avatar Dec 13 '21 01:12 karasusan

memo: URS-330

karasusan avatar Dec 13 '21 01:12 karasusan

@karasusan When will we be able to use the API to control bitrate based on your plan?

xiaolongrenlzj avatar Jan 18 '22 20:01 xiaolongrenlzj

same problem, now only 300kbps as default

chopin1998 avatar Jan 24 '22 07:01 chopin1998

@chopin1998 We will try to support this next milestone.

karasusan avatar Jan 25 '22 02:01 karasusan

@kevinfoley @chopin1998 I checked this issue. How about VideoStreamSender.ChangeVideoParameters method for your use?

karasusan avatar Feb 02 '22 07:02 karasusan

FYI https://github.com/Unity-Technologies/UnityRenderStreaming/issues/453#issuecomment-884873236

karasusan avatar Feb 02 '22 07:02 karasusan

@chopin1998 We will try to support this next milestone.

thanks! can i know the time for next milestone?

chopin1998 avatar Feb 07 '22 03:02 chopin1998

@chopin1998 The next milestone is in this month but we noticed that this issue should be fixed at first. https://github.com/Unity-Technologies/com.unity.webrtc/issues/552

karasusan avatar Feb 07 '22 06:02 karasusan

@chopin1998 I think you have the same issue reported here. What are your thoughts on? https://github.com/Unity-Technologies/com.unity.webrtc/issues/552

karasusan avatar Feb 07 '22 10:02 karasusan

@karasusan

i have upgraded to 3.1.0-exp.3, no bitrate options found.

bitrate still too low ~300k,

chopin1998 avatar Feb 17 '22 03:02 chopin1998

@chopin1998 Sorry but we omitted the feature this version. Could you read this thread and try? https://forum.unity.com/threads/webrtc-renderstreaming-code-improvements.1145162/

karasusan avatar Feb 17 '22 08:02 karasusan

@karasusan

i believe there is a simply way to modify code (hardcode) to change the default bitrate setting, could you please tell me where to change it?

and will 3.1.0~exp.4 support birate interface?

chopin1998 avatar Feb 17 '22 10:02 chopin1998

@chopin1998 Have you checked this? https://github.com/Unity-Technologies/UnityRenderStreaming/issues/611#issuecomment-1027647660

karasusan avatar Feb 17 '22 23:02 karasusan

@karasusan you mean file: com.unity.renderstreaming/Runtime/Scripts/VideoStreamSender.cs Line 43, right?

i noticed the function, and do some modify on this, but nothing happen. even i write a Debug.Log at begin of the function, no output in console, so i believe this function is not called.

i wanna call it by myself, but i donot know how to get "connectionId" for its first parameter.

chopin1998 avatar Feb 18 '22 05:02 chopin1998

@chopin1998 Sorry, we have not provided the sample for controlling setting of encoder. I promise that we will make the sample for the future release.

I guess you can see the "Bidirectional" sample to understand how to use connectionId.

karasusan avatar Feb 22 '22 02:02 karasusan

In the Unity WebRTC package, the encoding bitrate can normally be adjusted from an RTCRtpSender instance. However, the RTCRtpSender is not exposed directly in the Render Streaming package. In version 3.0.1 and earlier of Render Streaming, the AddTrack() function in SignalingHandlerBase returned the Transceiver, and it was possible to adjust the video quality in a custom Broadcast class like this:

private void ApplyBitrate(RTCRtpTransceiver transceiver) {
	var parameters = transceiver.Sender.GetParameters();
	for (int i = 0; i < parameters.encodings.Length; i++) {
		RTCRtpEncodingParameters encoding = parameters.encodings[i];
		encoding.maxBitrate = this.maxBitrate;
		encoding.minBitrate = this.minBitrate;
		parameters.encodings[i] = encoding;
	}
	transceiver.Sender.SetParameters(parameters);
}

As of version 3.1, SignalingHandlerBase no longer returns the Transceiver. There is no way to access the Transceiver from any code outside of the Render Streaming package, and therefore no way to access the RTCRtpSender and adjust the encoding bitrate.

@kevinfoley I also need to adjust the bitrate, and I tried above on 3.0.1, and set breakpoint in AddTrack(), but found that transceiver.Sender.GetParameters().encodings have zero element. And therefore I'm not able to set any parameter for encoder. I was always seeing empty encodings no matter I enabled hardware encoding or not. Also @karasusan did you ever hit this? Any tip about what's wrong?

yinhew avatar Mar 16 '22 06:03 yinhew

@yinhew I am not sure the situation. It might be possible to found values after completing signaling process.

karasusan avatar Mar 23 '22 02:03 karasusan

@chopin1998 Sorry, we have not provided the sample for controlling setting of encoder. I promise that we will make the sample for the future release.

I guess you can see the "Bidirectional" sample to understand how to use connectionId.

is next miltstone coming soon? ;-)

chopin1998 avatar Apr 01 '22 07:04 chopin1998

@chopin1998 It should be add to the next milestone but the time is a few month later.

karasusan avatar Apr 05 '22 02:04 karasusan

bled hardware encoding or not. Also @karasusan did you ever hit this? Any tip about what's wrong?

I had the same problem too. I solved this problem by doing this bitrate modificatino in "OnAnswer()" where Sender.GetParameters().encodings is not empty.

TDFranger avatar Jul 11 '22 08:07 TDFranger