2019-03-22 14:45:23 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef VideoFrameConverter_h
|
|
|
|
#define VideoFrameConverter_h
|
|
|
|
|
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "ImageToI420.h"
|
2021-11-03 18:23:30 +03:00
|
|
|
#include "Pacer.h"
|
2019-03-22 14:45:23 +03:00
|
|
|
#include "VideoSegment.h"
|
|
|
|
#include "VideoUtils.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2021-11-03 18:23:26 +03:00
|
|
|
#include "jsapi/RTCStatsReport.h"
|
2019-03-22 14:45:23 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
#include "mozilla/dom/ImageBitmapBinding.h"
|
|
|
|
#include "mozilla/dom/ImageUtils.h"
|
2020-10-23 21:02:54 +03:00
|
|
|
#include "api/video/video_frame.h"
|
2022-03-08 06:15:45 +03:00
|
|
|
#include "common_video/include/video_frame_buffer_pool.h"
|
2020-10-23 21:02:54 +03:00
|
|
|
#include "common_video/include/video_frame_buffer.h"
|
2019-03-22 14:45:23 +03:00
|
|
|
|
|
|
|
// The number of frame buffers VideoFrameConverter may create before returning
|
|
|
|
// errors.
|
|
|
|
// Sometimes these are released synchronously but they can be forwarded all the
|
|
|
|
// way to the encoder for asynchronous encoding. With a pool size of 5,
|
|
|
|
// we allow 1 buffer for the current conversion, and 4 buffers to be queued at
|
|
|
|
// the encoder.
|
|
|
|
#define CONVERTER_BUFFER_POOL_SIZE 5
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2019-03-22 14:45:27 +03:00
|
|
|
static mozilla::LazyLogModule gVideoFrameConverterLog("VideoFrameConverter");
|
2019-03-22 14:45:23 +03:00
|
|
|
|
|
|
|
// An async video frame format converter.
|
|
|
|
//
|
2019-10-02 13:23:02 +03:00
|
|
|
// Input is typically a MediaTrackListener driven by MediaTrackGraph.
|
2019-03-22 14:45:23 +03:00
|
|
|
//
|
2021-11-03 18:23:30 +03:00
|
|
|
// Output is passed through to VideoFrameConvertedEvent() whenever a frame is
|
|
|
|
// converted.
|
2019-03-22 14:45:23 +03:00
|
|
|
class VideoFrameConverter {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VideoFrameConverter)
|
|
|
|
|
2021-11-03 18:23:26 +03:00
|
|
|
explicit VideoFrameConverter(
|
|
|
|
const dom::RTCStatsTimestampMaker& aTimestampMaker)
|
|
|
|
: mTimestampMaker(aTimestampMaker),
|
2022-05-02 23:37:34 +03:00
|
|
|
mTaskQueue(TaskQueue::Create(
|
2021-11-03 18:23:30 +03:00
|
|
|
GetMediaThreadPool(MediaThreadType::WEBRTC_WORKER),
|
|
|
|
"VideoFrameConverter")),
|
|
|
|
mPacer(MakeAndAddRef<Pacer<FrameToProcess>>(
|
|
|
|
mTaskQueue, TimeDuration::FromSeconds(1))),
|
|
|
|
mBufferPool(false, CONVERTER_BUFFER_POOL_SIZE) {
|
2019-03-22 14:45:23 +03:00
|
|
|
MOZ_COUNT_CTOR(VideoFrameConverter);
|
2021-11-03 18:23:30 +03:00
|
|
|
|
|
|
|
mPacingListener = mPacer->PacedItemEvent().Connect(
|
|
|
|
mTaskQueue, [self = RefPtr<VideoFrameConverter>(this), this](
|
|
|
|
FrameToProcess aFrame, TimeStamp aTime) {
|
|
|
|
QueueForProcessing(std::move(aFrame.mImage), aTime, aFrame.mSize,
|
|
|
|
aFrame.mForceBlack);
|
|
|
|
});
|
2019-03-22 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueueVideoChunk(const VideoChunk& aChunk, bool aForceBlack) {
|
|
|
|
gfx::IntSize size = aChunk.mFrame.GetIntrinsicSize();
|
2019-03-25 17:25:11 +03:00
|
|
|
if (size.width == 0 || size.height == 0) {
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp t = aChunk.mTimeStamp;
|
|
|
|
MOZ_ASSERT(!t.IsNull());
|
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
mPacer->Enqueue(
|
|
|
|
FrameToProcess(aChunk.mFrame.GetImage(), t, size, aForceBlack), t);
|
2019-03-22 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
/**
|
|
|
|
* An active VideoFrameConverter actively converts queued video frames.
|
|
|
|
* While inactive, we keep track of the frame most recently queued for
|
|
|
|
* processing, so it can be immediately sent out once activated.
|
|
|
|
*/
|
|
|
|
void SetActive(bool aActive) {
|
2021-11-03 18:23:29 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mTaskQueue->Dispatch(NS_NewRunnableFunction(
|
2021-11-03 18:23:30 +03:00
|
|
|
__func__, [self = RefPtr<VideoFrameConverter>(this), this, aActive,
|
|
|
|
time = TimeStamp::Now()] {
|
2019-08-05 21:00:48 +03:00
|
|
|
if (mActive == aActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Debug,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p is now %s", this,
|
2019-08-05 21:00:48 +03:00
|
|
|
aActive ? "active" : "inactive"));
|
|
|
|
mActive = aActive;
|
|
|
|
if (aActive && mLastFrameQueuedForProcessing.Serial() != -2) {
|
|
|
|
// After activating, we re-process the last image that was queued
|
|
|
|
// for processing so it can be immediately sent.
|
2021-11-03 18:23:30 +03:00
|
|
|
mLastFrameQueuedForProcessing.mTime = time;
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mTaskQueue->Dispatch(
|
|
|
|
NewRunnableMethod<StoreCopyPassByLRef<FrameToProcess>>(
|
|
|
|
"VideoFrameConverter::ProcessVideoFrame", this,
|
|
|
|
&VideoFrameConverter::ProcessVideoFrame,
|
|
|
|
mLastFrameQueuedForProcessing)));
|
2019-08-05 21:00:48 +03:00
|
|
|
}
|
2021-11-03 18:23:29 +03:00
|
|
|
})));
|
2019-08-05 21:00:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetTrackEnabled(bool aTrackEnabled) {
|
2021-11-03 18:23:29 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mTaskQueue->Dispatch(NS_NewRunnableFunction(
|
2021-11-03 18:23:30 +03:00
|
|
|
__func__, [self = RefPtr<VideoFrameConverter>(this), this,
|
|
|
|
aTrackEnabled, time = TimeStamp::Now()] {
|
2019-08-05 21:00:48 +03:00
|
|
|
if (mTrackEnabled == aTrackEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-22 14:45:15 +03:00
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Debug,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p Track is now %s", this,
|
2019-08-05 21:00:48 +03:00
|
|
|
aTrackEnabled ? "enabled" : "disabled"));
|
|
|
|
mTrackEnabled = aTrackEnabled;
|
2020-08-20 01:18:30 +03:00
|
|
|
if (!aTrackEnabled) {
|
|
|
|
// After disabling we immediately send a frame as black, so it can
|
2021-11-03 18:23:30 +03:00
|
|
|
// be seen quickly, even if no frames are flowing. If no frame has
|
|
|
|
// been queued for processing yet, we use the FrameToProcess default
|
|
|
|
// size (640x480).
|
|
|
|
mLastFrameQueuedForProcessing.mTime = time;
|
|
|
|
mLastFrameQueuedForProcessing.mForceBlack = true;
|
|
|
|
mLastFrameQueuedForProcessing.mImage = nullptr;
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mTaskQueue->Dispatch(
|
|
|
|
NewRunnableMethod<StoreCopyPassByLRef<FrameToProcess>>(
|
|
|
|
"VideoFrameConverter::ProcessVideoFrame", this,
|
|
|
|
&VideoFrameConverter::ProcessVideoFrame,
|
|
|
|
mLastFrameQueuedForProcessing)));
|
2019-03-22 14:45:15 +03:00
|
|
|
}
|
2021-11-03 18:23:29 +03:00
|
|
|
})));
|
2019-03-22 14:45:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-02 18:53:17 +03:00
|
|
|
void Shutdown() {
|
2021-11-03 18:23:30 +03:00
|
|
|
mPacer->Shutdown()->Then(mTaskQueue, __func__,
|
|
|
|
[self = RefPtr<VideoFrameConverter>(this), this] {
|
|
|
|
mPacingListener.DisconnectIfExists();
|
|
|
|
mBufferPool.Release();
|
|
|
|
mLastFrameQueuedForProcessing = FrameToProcess();
|
|
|
|
mLastFrameConverted = Nothing();
|
|
|
|
});
|
2021-11-02 18:53:17 +03:00
|
|
|
}
|
2021-11-02 17:36:00 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
MediaEventSourceExc<webrtc::VideoFrame>& VideoFrameConvertedEvent() {
|
|
|
|
return mVideoFrameConvertedEvent;
|
|
|
|
}
|
|
|
|
|
2021-11-02 18:53:17 +03:00
|
|
|
protected:
|
|
|
|
struct FrameToProcess {
|
2021-11-03 18:23:30 +03:00
|
|
|
FrameToProcess() = default;
|
|
|
|
|
|
|
|
FrameToProcess(RefPtr<layers::Image> aImage, TimeStamp aTime,
|
|
|
|
gfx::IntSize aSize, bool aForceBlack)
|
|
|
|
: mImage(std::move(aImage)),
|
|
|
|
mTime(aTime),
|
|
|
|
mSize(aSize),
|
|
|
|
mForceBlack(aForceBlack) {}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
RefPtr<layers::Image> mImage;
|
|
|
|
TimeStamp mTime = TimeStamp::Now();
|
2021-11-03 18:23:30 +03:00
|
|
|
gfx::IntSize mSize = gfx::IntSize(640, 480);
|
2019-08-05 21:00:48 +03:00
|
|
|
bool mForceBlack = false;
|
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
int32_t Serial() const {
|
2019-08-05 21:00:48 +03:00
|
|
|
if (mForceBlack) {
|
|
|
|
// Set the last-img check to indicate black.
|
|
|
|
// -1 is not a guaranteed invalid serial. See bug 1262134.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!mImage) {
|
|
|
|
// Set the last-img check to indicate reset.
|
|
|
|
// -2 is not a guaranteed invalid serial. See bug 1262134.
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return mImage->GetSerial();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
struct FrameConverted {
|
|
|
|
FrameConverted(webrtc::VideoFrame aFrame, int32_t aSerial)
|
|
|
|
: mFrame(std::move(aFrame)), mSerial(aSerial) {}
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
webrtc::VideoFrame mFrame;
|
|
|
|
int32_t mSerial;
|
|
|
|
};
|
2021-11-02 18:53:17 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
MOZ_COUNTED_DTOR_VIRTUAL(VideoFrameConverter)
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
void VideoFrameConverted(webrtc::VideoFrame aVideoFrame, int32_t aSerial) {
|
2019-03-22 14:45:23 +03:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
|
2021-11-03 18:23:29 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Verbose,
|
|
|
|
("VideoFrameConverter %p: Converted a frame. Diff from last: %.3fms",
|
|
|
|
this,
|
|
|
|
static_cast<double>(aVideoFrame.timestamp_us() -
|
|
|
|
(mLastFrameConverted
|
2021-11-03 18:23:30 +03:00
|
|
|
? mLastFrameConverted->mFrame.timestamp_us()
|
2021-11-03 18:23:29 +03:00
|
|
|
: aVideoFrame.timestamp_us())) /
|
|
|
|
1000));
|
|
|
|
|
2021-11-03 18:23:28 +03:00
|
|
|
// Check that time doesn't go backwards
|
2021-11-03 18:23:30 +03:00
|
|
|
MOZ_ASSERT_IF(mLastFrameConverted,
|
|
|
|
aVideoFrame.timestamp_us() >
|
|
|
|
mLastFrameConverted->mFrame.timestamp_us());
|
2021-11-03 18:23:28 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
mLastFrameConverted = Some(FrameConverted(aVideoFrame, aSerial));
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
mVideoFrameConvertedEvent.Notify(std::move(aVideoFrame));
|
2019-03-22 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 14:44:40 +03:00
|
|
|
void QueueForProcessing(RefPtr<layers::Image> aImage, TimeStamp aTime,
|
|
|
|
gfx::IntSize aSize, bool aForceBlack) {
|
2019-03-22 14:45:27 +03:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
FrameToProcess frame{std::move(aImage), aTime, aSize,
|
|
|
|
aForceBlack || !mTrackEnabled};
|
2019-03-22 14:45:27 +03:00
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
if (frame.mTime <= mLastFrameQueuedForProcessing.mTime) {
|
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Debug,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Dropping a frame because time did not "
|
|
|
|
"progress (%.3fs)",
|
|
|
|
this,
|
2019-08-05 21:00:48 +03:00
|
|
|
(mLastFrameQueuedForProcessing.mTime - frame.mTime).ToSeconds()));
|
2019-03-22 14:44:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
if (frame.Serial() == mLastFrameQueuedForProcessing.Serial()) {
|
|
|
|
// This is the same frame as the last one. We limit the same-frame rate to
|
|
|
|
// 1 second, and rewrite the time so the frame-gap is in whole seconds.
|
|
|
|
//
|
|
|
|
// The pacer only starts duplicating frames every second if there is no
|
|
|
|
// flow of frames into it. There are other reasons the same frame could
|
|
|
|
// repeat here, and at a shorter interval than one second. For instance
|
|
|
|
// after the sender is disabled (SetTrackEnabled) but there is still a
|
|
|
|
// flow of frames into the pacer. All disabled frames have the same
|
|
|
|
// serial.
|
|
|
|
if (int32_t diffSec = static_cast<int32_t>(
|
|
|
|
(frame.mTime - mLastFrameQueuedForProcessing.mTime).ToSeconds());
|
|
|
|
diffSec != 0) {
|
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Verbose,
|
|
|
|
("VideoFrameConverter %p: Rewrote time interval for a duplicate "
|
|
|
|
"frame from %.3fs to %.3fs",
|
|
|
|
this,
|
|
|
|
(frame.mTime - mLastFrameQueuedForProcessing.mTime).ToSeconds(),
|
|
|
|
static_cast<float>(diffSec)));
|
|
|
|
frame.mTime = mLastFrameQueuedForProcessing.mTime +
|
|
|
|
TimeDuration::FromSeconds(diffSec);
|
|
|
|
} else {
|
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Verbose,
|
|
|
|
("VideoFrameConverter %p: Dropping a duplicate frame because a "
|
|
|
|
"second hasn't passed (%.3fs)",
|
|
|
|
this,
|
|
|
|
(frame.mTime - mLastFrameQueuedForProcessing.mTime).ToSeconds()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
mLastFrameQueuedForProcessing = std::move(frame);
|
|
|
|
|
|
|
|
if (!mActive) {
|
2021-11-03 18:23:29 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Debug,
|
|
|
|
("VideoFrameConverter %p: Ignoring a frame because we're inactive",
|
|
|
|
this));
|
2019-08-05 21:00:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-03-22 14:44:40 +03:00
|
|
|
|
2021-11-03 18:23:29 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mTaskQueue->Dispatch(
|
2019-08-05 21:00:48 +03:00
|
|
|
NewRunnableMethod<StoreCopyPassByLRef<FrameToProcess>>(
|
2019-03-22 14:44:40 +03:00
|
|
|
"VideoFrameConverter::ProcessVideoFrame", this,
|
2019-08-05 21:00:48 +03:00
|
|
|
&VideoFrameConverter::ProcessVideoFrame,
|
2021-11-03 18:23:29 +03:00
|
|
|
mLastFrameQueuedForProcessing)));
|
2019-03-22 14:44:40 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
void ProcessVideoFrame(const FrameToProcess& aFrame) {
|
2019-03-22 14:44:40 +03:00
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
if (aFrame.mTime < mLastFrameQueuedForProcessing.mTime) {
|
|
|
|
MOZ_LOG(
|
|
|
|
gVideoFrameConverterLog, LogLevel::Debug,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Dropping a frame that is %.3f seconds "
|
|
|
|
"behind latest",
|
|
|
|
this,
|
2019-08-05 21:00:48 +03:00
|
|
|
(mLastFrameQueuedForProcessing.mTime - aFrame.mTime).ToSeconds()));
|
2019-03-22 14:44:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:23:28 +03:00
|
|
|
const webrtc::Timestamp time =
|
|
|
|
mTimestampMaker.ConvertMozTimeToRealtime(aFrame.mTime);
|
2021-11-02 17:36:00 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
if (mLastFrameConverted &&
|
|
|
|
aFrame.Serial() == mLastFrameConverted->mSerial) {
|
|
|
|
// This is the same input frame as last time. Avoid a conversion.
|
|
|
|
webrtc::VideoFrame frame = mLastFrameConverted->mFrame;
|
|
|
|
frame.set_timestamp_us(time.us());
|
2021-11-03 18:23:30 +03:00
|
|
|
VideoFrameConverted(std::move(frame), mLastFrameConverted->mSerial);
|
2021-11-03 18:23:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
if (aFrame.mForceBlack) {
|
2019-03-22 14:45:23 +03:00
|
|
|
// Send a black image.
|
|
|
|
rtc::scoped_refptr<webrtc::I420Buffer> buffer =
|
2022-03-08 06:15:45 +03:00
|
|
|
mBufferPool.CreateI420Buffer(aFrame.mSize.width, aFrame.mSize.height);
|
2019-03-22 14:45:23 +03:00
|
|
|
if (!buffer) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false,
|
|
|
|
"Buffers not leaving scope except for "
|
|
|
|
"reconfig, should never leak");
|
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Warning,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Creating a buffer for a black video "
|
|
|
|
"frame failed",
|
|
|
|
this));
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 14:45:21 +03:00
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Verbose,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Sending a black video frame", this));
|
2022-07-15 00:48:45 +03:00
|
|
|
webrtc::I420Buffer::SetBlack(buffer.get());
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2021-11-03 18:23:28 +03:00
|
|
|
VideoFrameConverted(webrtc::VideoFrame::Builder()
|
|
|
|
.set_video_frame_buffer(buffer)
|
|
|
|
.set_timestamp_us(time.us())
|
2021-11-03 18:23:30 +03:00
|
|
|
.build(),
|
|
|
|
aFrame.Serial());
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
if (!aFrame.mImage) {
|
2019-03-22 14:45:51 +03:00
|
|
|
// Don't send anything for null images.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 21:00:48 +03:00
|
|
|
MOZ_ASSERT(aFrame.mImage->GetSize() == aFrame.mSize);
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2022-05-04 21:54:07 +03:00
|
|
|
RefPtr<layers::PlanarYCbCrImage> image =
|
|
|
|
aFrame.mImage->AsPlanarYCbCrImage();
|
|
|
|
if (image) {
|
2019-03-22 14:45:23 +03:00
|
|
|
dom::ImageUtils utils(image);
|
|
|
|
if (utils.GetFormat() == dom::ImageBitmapFormat::YUV420P &&
|
|
|
|
image->GetData()) {
|
|
|
|
const layers::PlanarYCbCrData* data = image->GetData();
|
2020-10-30 18:39:15 +03:00
|
|
|
rtc::scoped_refptr<webrtc::I420BufferInterface> video_frame_buffer =
|
|
|
|
webrtc::WrapI420Buffer(
|
2019-08-05 21:00:48 +03:00
|
|
|
aFrame.mImage->GetSize().width, aFrame.mImage->GetSize().height,
|
2019-03-22 14:45:23 +03:00
|
|
|
data->mYChannel, data->mYStride, data->mCbChannel,
|
|
|
|
data->mCbCrStride, data->mCrChannel, data->mCbCrStride,
|
2022-05-04 21:54:07 +03:00
|
|
|
[image] { /* keep reference alive*/ });
|
2019-03-22 14:45:23 +03:00
|
|
|
|
2019-03-22 14:45:21 +03:00
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Verbose,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Sending an I420 video frame", this));
|
2021-11-03 18:23:28 +03:00
|
|
|
VideoFrameConverted(webrtc::VideoFrame::Builder()
|
|
|
|
.set_video_frame_buffer(video_frame_buffer)
|
|
|
|
.set_timestamp_us(time.us())
|
2021-11-03 18:23:30 +03:00
|
|
|
.build(),
|
|
|
|
aFrame.Serial());
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rtc::scoped_refptr<webrtc::I420Buffer> buffer =
|
2022-03-08 06:15:45 +03:00
|
|
|
mBufferPool.CreateI420Buffer(aFrame.mSize.width, aFrame.mSize.height);
|
2019-03-22 14:45:23 +03:00
|
|
|
if (!buffer) {
|
2019-10-31 15:12:19 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
++mFramesDropped;
|
|
|
|
#endif
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mFramesDropped <= 100, "Buffers must be leaking");
|
2019-03-22 14:45:23 +03:00
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Warning,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Creating a buffer failed", this));
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:19:01 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
mFramesDropped = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-03-22 14:45:23 +03:00
|
|
|
nsresult rv =
|
2019-08-05 21:00:48 +03:00
|
|
|
ConvertToI420(aFrame.mImage, buffer->MutableDataY(), buffer->StrideY(),
|
2019-03-22 14:45:23 +03:00
|
|
|
buffer->MutableDataU(), buffer->StrideU(),
|
|
|
|
buffer->MutableDataV(), buffer->StrideV());
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_LOG(gVideoFrameConverterLog, LogLevel::Warning,
|
2021-11-03 18:23:29 +03:00
|
|
|
("VideoFrameConverter %p: Image conversion failed", this));
|
2019-03-22 14:45:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:23:28 +03:00
|
|
|
VideoFrameConverted(webrtc::VideoFrame::Builder()
|
|
|
|
.set_video_frame_buffer(buffer)
|
|
|
|
.set_timestamp_us(time.us())
|
2021-11-03 18:23:30 +03:00
|
|
|
.build(),
|
|
|
|
aFrame.Serial());
|
2019-03-22 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
public:
|
2021-11-03 18:23:26 +03:00
|
|
|
const dom::RTCStatsTimestampMaker mTimestampMaker;
|
|
|
|
|
2019-03-22 14:45:23 +03:00
|
|
|
const RefPtr<TaskQueue> mTaskQueue;
|
2019-03-22 14:45:27 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
protected:
|
2019-03-22 14:45:27 +03:00
|
|
|
// Used to pace future frames close to their rendering-time. Thread-safe.
|
2021-11-03 18:23:30 +03:00
|
|
|
const RefPtr<Pacer<FrameToProcess>> mPacer;
|
2021-11-02 17:36:01 +03:00
|
|
|
|
2021-11-03 18:23:30 +03:00
|
|
|
MediaEventProducerExc<webrtc::VideoFrame> mVideoFrameConvertedEvent;
|
|
|
|
|
2019-03-22 14:45:23 +03:00
|
|
|
// Accessed only from mTaskQueue.
|
2021-11-03 18:23:30 +03:00
|
|
|
MediaEventListener mPacingListener;
|
2022-03-08 06:15:45 +03:00
|
|
|
webrtc::VideoFrameBufferPool mBufferPool;
|
2019-08-05 21:00:48 +03:00
|
|
|
FrameToProcess mLastFrameQueuedForProcessing;
|
2021-11-03 18:23:30 +03:00
|
|
|
Maybe<FrameConverted> mLastFrameConverted;
|
|
|
|
bool mActive = false;
|
|
|
|
bool mTrackEnabled = true;
|
2019-06-20 18:19:01 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
size_t mFramesDropped = 0;
|
|
|
|
#endif
|
2019-03-22 14:45:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // VideoFrameConverter_h
|