Skip to content

Commit fde423a

Browse files
committed
fix: mark encoded frames as the end of their temporal unit
Since M152 WebRTC expects encoders to set EncodedImage's end-of-temporal-unit flag, and to report every frame they drop through EncodedImageCallback::OnFrameDropped, so that it can release what it keeps of a frame (the captured frame held for corruption detection, for one) as soon as nothing more of it will come out. All encoders here produce a single layer, so every frame ends its temporal unit. EncoderOutputProcessor sets the flag for the hardware encoders, and the Java encoder wrapper sets it for the frames of Java encoders. The wrapper also reports the frames it gives up on: frames a Java encoder produced no output for, the oldest frame when too many are pending, and frames that cannot be converted for the Java encoder. The Media Foundation and VideoToolbox encoders report their drops already; NVENC and VA-API drop none.
1 parent 43b7420 commit fde423a

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎webrtc-jni/src/main/cpp/include/media/video/codec/VideoEncoderWrapper.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ namespace jni
8080
int32_t HandleReturnCode(JNIEnv * env, int32_t status, const char * method);
8181
void InvalidateCallback(JNIEnv * env);
8282
void UpdateEncoderInfo(JNIEnv * env);
83+
// Tells WebRTC that a frame given to the encoder will not come out.
84+
void ReportDroppedFrame(uint32_t rtpTimestamp);
8385

8486
ScalingSettings GetScalingSettings(JNIEnv * env) const;
8587
std::vector<ResolutionBitrateLimits> GetResolutionBitrateLimits(JNIEnv * env) const;

‎webrtc-jni/src/main/cpp/src/media/video/codec/EncoderOutputProcessor.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ namespace jni
164164
}
165165

166166
image.SetEncodedData(webrtc::EncodedImageBuffer::Create(output.data(), output.size()));
167+
// A stream without layers: nothing else follows for this frame, which
168+
// lets WebRTC release what it keeps of the frame until then.
169+
image.set_end_of_temporal_unit(true);
167170
image.set_frame_type(keyFrame
168171
? webrtc::VideoFrameType::kVideoFrameKey
169172
: webrtc::VideoFrameType::kVideoFrameDelta);

‎webrtc-jni/src/main/cpp/src/media/video/codec/VideoEncoderWrapper.cpp‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ namespace jni
239239
if (jFrame == nullptr) {
240240
RTC_LOG(LS_WARNING) << "Java encoder: dropping a frame that cannot be converted to I420";
241241

242+
ReportDroppedFrame(frame.rtp_timestamp());
243+
242244
return WEBRTC_VIDEO_CODEC_OK;
243245
}
244246

@@ -268,10 +270,13 @@ namespace jni
268270
result = WEBRTC_VIDEO_CODEC_ERROR;
269271
}
270272
else {
273+
std::optional<uint32_t> givenUp;
274+
271275
{
272276
webrtc::MutexLock lock(&frameExtraInfosLock);
273277

274278
if (frameExtraInfos.size() >= kMaxPendingFrames) {
279+
givenUp = frameExtraInfos.front().rtpTimestamp;
275280
frameExtraInfos.pop_front();
276281
}
277282

@@ -281,6 +286,10 @@ namespace jni
281286
});
282287
}
283288

289+
if (givenUp) {
290+
ReportDroppedFrame(*givenUp);
291+
}
292+
284293
jobject status = env->CallObjectMethod(encoder, javaClass->encode, jFrame, info);
285294
result = ToNativeCodecStatus(env, status, "encode");
286295
}
@@ -359,29 +368,40 @@ namespace jni
359368
webrtc::EncodedImage image = EncodedImage::toNative(env, imageRef);
360369
const int64_t captureTimeNs = EncodedImage::getCaptureTimeNs(env, imageRef);
361370

362-
FrameExtraInfo extraInfo;
371+
std::optional<FrameExtraInfo> extraInfo;
372+
std::vector<uint32_t> dropped;
363373

364374
{
365375
webrtc::MutexLock lock(&frameExtraInfosLock);
366376

367377
// Frames come out in the order they went in, though an encoder
368378
// may drop some, so the records of older frames are stale.
369379
while (!frameExtraInfos.empty() && frameExtraInfos.front().captureTimeNs < captureTimeNs) {
380+
dropped.push_back(frameExtraInfos.front().rtpTimestamp);
370381
frameExtraInfos.pop_front();
371382
}
372383

373-
if (frameExtraInfos.empty() || frameExtraInfos.front().captureTimeNs != captureTimeNs) {
374-
RTC_LOG(LS_WARNING) << "Java encoder produced a frame for no frame it was given, capture time "
375-
<< captureTimeNs << " ns";
376-
return;
384+
if (!frameExtraInfos.empty() && frameExtraInfos.front().captureTimeNs == captureTimeNs) {
385+
extraInfo = frameExtraInfos.front();
386+
frameExtraInfos.pop_front();
377387
}
388+
}
378389

379-
extraInfo = frameExtraInfos.front();
380-
frameExtraInfos.pop_front();
390+
for (uint32_t rtpTimestamp : dropped) {
391+
ReportDroppedFrame(rtpTimestamp);
381392
}
382393

383-
image.SetRtpTimestamp(extraInfo.rtpTimestamp);
394+
if (!extraInfo) {
395+
RTC_LOG(LS_WARNING) << "Java encoder produced a frame for no frame it was given, capture time "
396+
<< captureTimeNs << " ns";
397+
return;
398+
}
399+
400+
image.SetRtpTimestamp(extraInfo->rtpTimestamp);
384401
image.capture_time_ms_ = captureTimeNs / webrtc::kNumNanosecsPerMillisec;
402+
// The Java encoders produce a single layer, so every frame ends its
403+
// temporal unit.
404+
image.set_end_of_temporal_unit(true);
385405

386406
if (image.qp_ < 0) {
387407
image.qp_ = ParseQp(image);
@@ -395,6 +415,16 @@ namespace jni
395415
}
396416
}
397417

418+
void VideoEncoderWrapper::ReportDroppedFrame(uint32_t rtpTimestamp)
419+
{
420+
webrtc::EncodedImageCallback * encodeCallback = callback.load();
421+
422+
if (encodeCallback != nullptr) {
423+
// A single layer, so nothing else follows for the frame.
424+
encodeCallback->OnFrameDropped(rtpTimestamp, /*spatial_id=*/0, /*is_end_of_temporal_unit=*/true);
425+
}
426+
}
427+
398428
int32_t VideoEncoderWrapper::HandleReturnCode(JNIEnv * env, int32_t status, const char * method)
399429
{
400430
if (status >= 0) {

‎webrtc/src/test/java/dev/onvoid/webrtc/VideoCodecFactoryTests.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ private void assertRoundTrip(VideoCodecInfo codec, boolean javaBuffers) throws E
228228
assertTrue(decoderFactory.last.get().released.get(), "decoder was never released");
229229
}
230230

231+
@Test
232+
void javaEncoderDroppingFramesKeepsSending() throws Exception {
233+
// A Java encoder drops a frame by producing nothing for it. WebRTC is
234+
// told about the frames dropped once a later one comes out, and the
235+
// frames encoded still arrive.
236+
TestEncoderFactory encoderFactory = new TestEncoderFactory();
237+
encoderFactory.dropEveryOtherFrame = true;
238+
239+
PeerConnectionFactory built = testCodecFactory(encoderFactory, new TestDecoderFactory());
240+
241+
try {
242+
assertFramesReceived(built, TEST_CODEC);
243+
}
244+
finally {
245+
built.dispose();
246+
}
247+
248+
assertTrue(encoderFactory.dropped.get() > 0, "the encoder dropped no frames");
249+
}
250+
231251
@Test
232252
void factoryDelegatesToBuiltInCodec() throws Exception {
233253
AtomicInteger encoders = new AtomicInteger();
@@ -499,10 +519,14 @@ static class TestEncoderFactory implements VideoEncoderFactory {
499519
final DefaultVideoEncoderFactory builtIn = new DefaultVideoEncoderFactory();
500520
final AtomicInteger created = new AtomicInteger();
501521
final AtomicReference<TestEncoder> last = new AtomicReference<>();
522+
final AtomicInteger dropped = new AtomicInteger();
502523
final VideoCodecInfo codec;
503524

504525
volatile EncodeListener onEncode;
505526

527+
/** Makes the encoders produce nothing for every other frame. */
528+
volatile boolean dropEveryOtherFrame;
529+
506530

507531
TestEncoderFactory() {
508532
this(new VideoCodecInfo(TEST_CODEC));
@@ -600,6 +624,8 @@ static class TestEncoder implements VideoEncoder {
600624

601625
private Callback callback;
602626

627+
private int frames;
628+
603629

604630
TestEncoder(TestEncoderFactory factory, boolean h265) {
605631
this.factory = factory;
@@ -621,6 +647,12 @@ public VideoCodecStatus release() {
621647

622648
@Override
623649
public VideoCodecStatus encode(VideoFrame frame, EncodeInfo info) {
650+
if (factory.dropEveryOtherFrame && frames++ % 2 == 1) {
651+
// Dropped: no output, as a rate controlled encoder would do.
652+
factory.dropped.incrementAndGet();
653+
return VideoCodecStatus.OK;
654+
}
655+
624656
int width = frame.buffer.getWidth();
625657
int height = frame.buffer.getHeight();
626658
int luma = frame.buffer.toI420().getDataY().get(0) & 0xFF;

0 commit comments

Comments
 (0)