1 /*
2 * Copyright 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/video_coding/codecs/test/encoded_video_frame_producer.h"
12
13 #include <memory>
14 #include <vector>
15
16 #include "api/test/create_frame_generator.h"
17 #include "api/test/frame_generator_interface.h"
18 #include "api/transport/rtp/dependency_descriptor.h"
19 #include "api/video/video_frame.h"
20 #include "api/video/video_frame_type.h"
21 #include "api/video_codecs/video_encoder.h"
22 #include "modules/video_coding/include/video_codec_interface.h"
23 #include "modules/video_coding/include/video_error_codes.h"
24 #include "rtc_base/checks.h"
25
26 namespace webrtc {
27 namespace {
28
29 class EncoderCallback : public EncodedImageCallback {
30 public:
EncoderCallback(std::vector<EncodedVideoFrameProducer::EncodedFrame> & output_frames)31 explicit EncoderCallback(
32 std::vector<EncodedVideoFrameProducer::EncodedFrame>& output_frames)
33 : output_frames_(output_frames) {}
34
35 private:
OnEncodedImage(const EncodedImage & encoded_image,const CodecSpecificInfo * codec_specific_info)36 Result OnEncodedImage(const EncodedImage& encoded_image,
37 const CodecSpecificInfo* codec_specific_info) override {
38 output_frames_.push_back({encoded_image, *codec_specific_info});
39 return Result(Result::Error::OK);
40 }
41
42 std::vector<EncodedVideoFrameProducer::EncodedFrame>& output_frames_;
43 };
44
45 } // namespace
46
47 std::vector<EncodedVideoFrameProducer::EncodedFrame>
Encode()48 EncodedVideoFrameProducer::Encode() {
49 std::unique_ptr<test::FrameGeneratorInterface> frame_buffer_generator =
50 test::CreateSquareFrameGenerator(
51 resolution_.Width(), resolution_.Height(),
52 test::FrameGeneratorInterface::OutputType::kI420, absl::nullopt);
53
54 std::vector<EncodedFrame> encoded_frames;
55 EncoderCallback encoder_callback(encoded_frames);
56 RTC_CHECK_EQ(encoder_.RegisterEncodeCompleteCallback(&encoder_callback),
57 WEBRTC_VIDEO_CODEC_OK);
58
59 uint32_t rtp_tick = 90000 / framerate_fps_;
60 for (int i = 0; i < num_input_frames_; ++i) {
61 VideoFrame frame =
62 VideoFrame::Builder()
63 .set_video_frame_buffer(frame_buffer_generator->NextFrame().buffer)
64 .set_timestamp_rtp(rtp_timestamp_)
65 .build();
66 rtp_timestamp_ += rtp_tick;
67 RTC_CHECK_EQ(encoder_.Encode(frame, &next_frame_type_),
68 WEBRTC_VIDEO_CODEC_OK);
69 next_frame_type_[0] = VideoFrameType::kVideoFrameDelta;
70 }
71
72 RTC_CHECK_EQ(encoder_.RegisterEncodeCompleteCallback(nullptr),
73 WEBRTC_VIDEO_CODEC_OK);
74 return encoded_frames;
75 }
76
77 } // namespace webrtc
78