Skip to content

Commit

Permalink
sender: fix serializing RTCRtpSendParameters
Browse files Browse the repository at this point in the history
It's possible for user code to replace encodings entirely. Thus, the
resulting array will not have RTCRtpEncodingParameters object instances,
but plain objects.

Handle it by deep-cloning the objects with JSON.parse(JSON.stringify(x))
since that will take care of appropriately serializing them, no matter
the type.
  • Loading branch information
saghul committed Aug 14, 2024
1 parent ac7f578 commit 6cfedd7
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/RTCRtpSendParameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import RTCRtpEncodingParameters, { RTCRtpEncodingParametersInit } from './RTCRtpEncodingParameters';
import RTCRtpParameters, { RTCRtpParametersInit } from './RTCRtpParameters';
import { deepClone } from './RTCUtil';

type DegradationPreferenceType = 'maintain-framerate'
| 'maintain-resolution'
Expand Down Expand Up @@ -31,7 +32,7 @@ export interface RTCRtpSendParametersInit extends RTCRtpParametersInit {

export default class RTCRtpSendParameters extends RTCRtpParameters {
readonly transactionId: string;
readonly encodings: RTCRtpEncodingParameters[];
encodings: (RTCRtpEncodingParameters | RTCRtpEncodingParametersInit)[];
degradationPreference: DegradationPreferenceType | null;

constructor(init: RTCRtpSendParametersInit) {
Expand All @@ -51,8 +52,7 @@ export default class RTCRtpSendParameters extends RTCRtpParameters {
const obj = super.toJSON();

obj['transactionId'] = this.transactionId;

obj['encodings'] = this.encodings.map(e => e.toJSON());
obj['encodings'] = this.encodings.map(e => deepClone(e));

if (this.degradationPreference !== null) {
obj['degradationPreference'] = DegradationPreference.toNative(this.degradationPreference);
Expand Down

0 comments on commit 6cfedd7

Please sign in to comment.