Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ spec:webidl; type:dfn; text:resolve
</pre>
<pre class=link-defaults>
spec:streams; type:interface; text:ReadableStream
spec:infra; type:dfn; text:list
</pre>

# Introduction # {#introduction}
Expand Down Expand Up @@ -1003,32 +1004,35 @@ The <dfn abstract-op>generate key frame algorithm</dfn>, given |promise|, |encod
in Section 10 of [[!RFC8851]], then reject |promise| with {{TypeError}} and abort
these steps.
1. [=In parallel=], run the following steps:
1. Gather a list of video encoders, named |videoEncoders| from |encoder|, ordered according negotiated RIDs if any.
1. If |rid| is defined, remove from |videoEncoders| any video encoder that does not match |rid|.
1. If |rid| is undefined, remove from |videoEncoders| all video encoders except the first one.
1. If |videoEncoders| is empty, [=queue a task=] to reject |promise| with {{NotFoundError}} and abort these steps.
|videoEncoders| is expected to be empty if the corresponding {{RTCRtpSender}} is not active, or the corresponding {{RTCRtpSender}} track is ended.
1. Let |videoEncoder| be the first encoder in |videoEncoders|.
1. If |rid| is undefined, set |rid| to the RID value corresponding to |videoEncoder|.
1. Create a pending key frame task called |task| with |task|.`[[rid]]` set to rid and |task|.`[[promise]]`| set to |promise|.
1. Let |layers| be a new [=list=] of the layers for this |encoder|, ordered by negotiated encoding index.
1. Remove from |layers| all layers that are not {{RTCRtpEncodingParameters/active}}, or whose
corresponding {{RTCRtpSender}} track has ended.
1. If |rid| is not undefined, remove from |layers| all layers whose RID is not |rid|.

Note: If no |rid| is passed in, keyframes are generated for all active layers.

1. If |layers| is now empty, [=queue a task=] to reject |promise| with {{NotFoundError}} and abort these steps.
1. Create a pending key frame task called |task| with |task|.`[[layers]]` set to |layers| and |task|.`[[promise]]` set to |promise|.
1. If |encoder|.`[[pendingKeyFrameTasks]]` is undefined, initialize |encoder|.`[[pendingKeyFrameTasks]]` to an empty set.
1. Let |shouldTriggerKeyFrame| be <code>false</code> if |encoder|.`[[pendingKeyFrameTasks]]` contains a task whose `[[rid]]`
value is equal to |rid|, and <code>true</code> otherwise.
1. Remove from |layers| all layers already [=list/contained|found=] in any `[[layers]]` of any tasks in |encoder|.`[[pendingKeyFrameTasks]]`.
Copy link
Collaborator

@youennf youennf Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can do that step first before creating the pending key frame task.

1. Add |task| to |encoder|.`[[pendingKeyFrameTasks]]`.
1. If |shouldTriggerKeyFrame| is <code>true</code>, instruct |videoEncoder| to generate a key frame for the next provided video frame.
1. For each |layer| in |layers| (if any), instruct |encoder| to generate a key frame for its
next provided video frame to that |layer|.

For any {{RTCRtpScriptTransformer}} named |transformer|, the following steps are run just before any |frame| is enqueued in |transformer|.`[[readable]]`:
1. Let |encoder| be |transformer|.`[[encoder]]`.
1. If |encoder| or |encoder|.`[[pendingKeyFrameTasks]]` is undefined, abort these steps.
1. If |frame| is not a video {{RTCEncodedVideoFrameType/"key"}} frame, abort these steps.
1. For each |task| in |encoder|.`[[pendingKeyFrameTasks]]`, run the following steps:
1. If |frame| was generated by a video encoder identified by |task|.`[[rid]]`, run the following steps:
1. If |frame| was generated for a layer
[=list/contained=] in |task|.`[[layers]]`, remove that layer from |task|.`[[layers]]`.
If this causes |task|.`[[layers]]` to become empty, then run the following steps:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's resolve the promise as soon as we have a frame, that way, whether there are multiple key frames or just one, it is consistent.

1. Remove |task| from |encoder|.`[[pendingKeyFrameTasks]]`.
1. Resolve |task|.`[[promise]]` with |frame|'s timestamp.

By resolving the promises just before enqueuing the corresponding key frame in a {{RTCRtpScriptTransformer}}'s readable,
the resolution callbacks of the promises are always executed just before the corresponding key frame is exposed.
If the promise is associated to several rid values, it will be resolved when the first key frame corresponding to one the rid value is enqueued.
If the promise is associated with several layers, it will be resolved once key frames have been enqueued for all of them.

The <dfn abstract-op>send request key frame algorithm</dfn>, given |promise| and |depacketizer|, is defined by running these steps:
1. If |depacketizer| is undefined, reject |promise| with {{InvalidStateError}}, abort these steps.
Expand Down