Factor encoded frame generation code into own target
Show it can make vp9 tests cleaner too. Bug: None Change-Id: I8333a61dec1ef90ade9faffea94e1555ccbfcfaa Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/177013 Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Reviewed-by: Philip Eliasson <philipel@webrtc.org> Cr-Commit-Position: refs/heads/master@{#31523}
This commit is contained in:
committed by
Commit Bot
parent
9465978a3b
commit
90dc5dae2c
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "modules/video_coding/codecs/test/encoded_video_frame_producer.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "api/test/create_frame_generator.h"
|
||||
#include "api/test/frame_generator_interface.h"
|
||||
#include "api/transport/rtp/dependency_descriptor.h"
|
||||
#include "api/video/video_frame.h"
|
||||
#include "api/video/video_frame_type.h"
|
||||
#include "api/video_codecs/video_encoder.h"
|
||||
#include "modules/video_coding/include/video_codec_interface.h"
|
||||
#include "modules/video_coding/include/video_error_codes.h"
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
|
||||
class EncoderCallback : public EncodedImageCallback {
|
||||
public:
|
||||
explicit EncoderCallback(
|
||||
std::vector<EncodedVideoFrameProducer::EncodedFrame>& output_frames)
|
||||
: output_frames_(output_frames) {}
|
||||
|
||||
private:
|
||||
Result OnEncodedImage(
|
||||
const EncodedImage& encoded_image,
|
||||
const CodecSpecificInfo* codec_specific_info,
|
||||
const RTPFragmentationHeader* /*fragmentation*/) override {
|
||||
output_frames_.push_back({encoded_image, *codec_specific_info});
|
||||
return Result(Result::Error::OK);
|
||||
}
|
||||
|
||||
std::vector<EncodedVideoFrameProducer::EncodedFrame>& output_frames_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<EncodedVideoFrameProducer::EncodedFrame>
|
||||
EncodedVideoFrameProducer::Encode() {
|
||||
std::unique_ptr<test::FrameGeneratorInterface> frame_buffer_generator =
|
||||
test::CreateSquareFrameGenerator(
|
||||
resolution_.Width(), resolution_.Height(),
|
||||
test::FrameGeneratorInterface::OutputType::kI420, absl::nullopt);
|
||||
|
||||
std::vector<EncodedFrame> encoded_frames;
|
||||
EncoderCallback encoder_callback(encoded_frames);
|
||||
RTC_CHECK_EQ(encoder_.RegisterEncodeCompleteCallback(&encoder_callback),
|
||||
WEBRTC_VIDEO_CODEC_OK);
|
||||
|
||||
uint32_t rtp_tick = 90000 / framerate_fps_;
|
||||
std::vector<VideoFrameType> frame_types = {VideoFrameType::kVideoFrameDelta};
|
||||
for (int i = 0; i < num_input_frames_; ++i) {
|
||||
VideoFrame frame =
|
||||
VideoFrame::Builder()
|
||||
.set_video_frame_buffer(frame_buffer_generator->NextFrame().buffer)
|
||||
.set_timestamp_rtp(rtp_timestamp_)
|
||||
.build();
|
||||
rtp_timestamp_ += rtp_tick;
|
||||
RTC_CHECK_EQ(encoder_.Encode(frame, &frame_types), WEBRTC_VIDEO_CODEC_OK);
|
||||
}
|
||||
|
||||
RTC_CHECK_EQ(encoder_.RegisterEncodeCompleteCallback(nullptr),
|
||||
WEBRTC_VIDEO_CODEC_OK);
|
||||
return encoded_frames;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_
|
||||
#define MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "api/transport/rtp/dependency_descriptor.h"
|
||||
#include "api/video/encoded_image.h"
|
||||
#include "api/video_codecs/video_encoder.h"
|
||||
#include "modules/video_coding/include/video_codec_interface.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Wrapper around VideoEncoder::Encode for convenient input (generates frames)
|
||||
// and output (returns encoded frames instead of passing them to callback)
|
||||
class EncodedVideoFrameProducer {
|
||||
public:
|
||||
struct EncodedFrame {
|
||||
EncodedImage encoded_image;
|
||||
CodecSpecificInfo codec_specific_info;
|
||||
};
|
||||
|
||||
// `encoder` should be initialized, but shouldn't have `EncoderCallback` set.
|
||||
explicit EncodedVideoFrameProducer(VideoEncoder& encoder)
|
||||
: encoder_(encoder) {}
|
||||
EncodedVideoFrameProducer(const EncodedVideoFrameProducer&) = delete;
|
||||
EncodedVideoFrameProducer& operator=(const EncodedVideoFrameProducer&) =
|
||||
delete;
|
||||
|
||||
// Number of the input frames to pass to the encoder.
|
||||
EncodedVideoFrameProducer& SetNumInputFrames(int value);
|
||||
// Resolution of the input frames.
|
||||
EncodedVideoFrameProducer& SetResolution(RenderResolution value);
|
||||
|
||||
// Generates input video frames and encodes them with `encoder` provided in
|
||||
// the constructor. Returns frame passed to the `OnEncodedImage` by wraping
|
||||
// `EncodedImageCallback` underneath.
|
||||
std::vector<EncodedFrame> Encode();
|
||||
|
||||
private:
|
||||
VideoEncoder& encoder_;
|
||||
|
||||
uint32_t rtp_timestamp_ = 1000;
|
||||
int num_input_frames_ = 1;
|
||||
int framerate_fps_ = 30;
|
||||
RenderResolution resolution_ = {320, 180};
|
||||
};
|
||||
|
||||
inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetNumInputFrames(
|
||||
int value) {
|
||||
RTC_DCHECK_GT(value, 0);
|
||||
num_input_frames_ = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetResolution(
|
||||
RenderResolution value) {
|
||||
resolution_ = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
#endif // MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_
|
||||
Reference in New Issue
Block a user