summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/video/end_to_end_tests/codec_tests.cc
blob: 60a0bc8a15a259f42b625a8e84a72912b5a0b163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *  Copyright 2018 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 <memory>

#include "absl/types/optional.h"
#include "api/test/video/function_video_encoder_factory.h"
#include "api/video/color_space.h"
#include "api/video/video_rotation.h"
#include "common_video/test/utilities.h"
#include "media/base/codec.h"
#include "media/base/media_constants.h"
#include "media/engine/internal_decoder_factory.h"
#include "media/engine/internal_encoder_factory.h"
#include "modules/video_coding/codecs/h264/include/h264.h"
#include "modules/video_coding/codecs/multiplex/include/multiplex_decoder_adapter.h"
#include "modules/video_coding/codecs/multiplex/include/multiplex_encoder_adapter.h"
#include "modules/video_coding/codecs/vp8/include/vp8.h"
#include "modules/video_coding/codecs/vp9/include/vp9.h"
#include "test/call_test.h"
#include "test/encoder_settings.h"
#include "test/field_trial.h"
#include "test/gtest.h"
#include "test/video_test_constants.h"

namespace webrtc {
namespace {
enum : int {  // The first valid value is 1.
  kColorSpaceExtensionId = 1,
  kVideoRotationExtensionId,
};
}  // namespace

class CodecEndToEndTest : public test::CallTest {
 public:
  CodecEndToEndTest() {
    RegisterRtpExtension(
        RtpExtension(RtpExtension::kColorSpaceUri, kColorSpaceExtensionId));
    RegisterRtpExtension(RtpExtension(RtpExtension::kVideoRotationUri,
                                      kVideoRotationExtensionId));
  }
};

class CodecObserver : public test::EndToEndTest,
                      public rtc::VideoSinkInterface<VideoFrame> {
 public:
  CodecObserver(int no_frames_to_wait_for,
                VideoRotation rotation_to_test,
                absl::optional<ColorSpace> color_space_to_test,
                const std::string& payload_name,
                VideoEncoderFactory* encoder_factory,
                VideoDecoderFactory* decoder_factory)
      : EndToEndTest(4 * test::VideoTestConstants::kDefaultTimeout),
        // TODO(hta): This timeout (120 seconds) is excessive.
        // https://bugs.webrtc.org/6830
        no_frames_to_wait_for_(no_frames_to_wait_for),
        expected_rotation_(rotation_to_test),
        expected_color_space_(color_space_to_test),
        payload_name_(payload_name),
        encoder_factory_(encoder_factory),
        decoder_factory_(decoder_factory),
        frame_counter_(0) {}

  void PerformTest() override {
    EXPECT_TRUE(Wait())
        << "Timed out while waiting for enough frames to be decoded.";
  }

  void ModifyVideoConfigs(
      VideoSendStream::Config* send_config,
      std::vector<VideoReceiveStreamInterface::Config>* receive_configs,
      VideoEncoderConfig* encoder_config) override {
    encoder_config->codec_type = PayloadStringToCodecType(payload_name_);
    send_config->encoder_settings.encoder_factory = encoder_factory_;
    send_config->rtp.payload_name = payload_name_;
    send_config->rtp.payload_type =
        test::VideoTestConstants::kVideoSendPayloadType;

    (*receive_configs)[0].renderer = this;
    (*receive_configs)[0].decoders.resize(1);
    (*receive_configs)[0].decoders[0].payload_type =
        send_config->rtp.payload_type;
    (*receive_configs)[0].decoders[0].video_format =
        SdpVideoFormat(send_config->rtp.payload_name);
    (*receive_configs)[0].decoder_factory = decoder_factory_;
  }

  void OnFrame(const VideoFrame& video_frame) override {
    EXPECT_EQ(expected_rotation_, video_frame.rotation());
    // Test only if explicit color space has been specified since otherwise the
    // color space is codec dependent.
    if (expected_color_space_) {
      EXPECT_EQ(expected_color_space_,
                video_frame.color_space()
                    ? absl::make_optional(*video_frame.color_space())
                    : absl::nullopt);
    }
    if (++frame_counter_ == no_frames_to_wait_for_)
      observation_complete_.Set();
  }

  void OnFrameGeneratorCapturerCreated(
      test::FrameGeneratorCapturer* frame_generator_capturer) override {
    frame_generator_capturer->SetFakeRotation(expected_rotation_);
    frame_generator_capturer->SetFakeColorSpace(expected_color_space_);
  }

 private:
  int no_frames_to_wait_for_;
  VideoRotation expected_rotation_;
  absl::optional<ColorSpace> expected_color_space_;
  std::string payload_name_;
  VideoEncoderFactory* encoder_factory_;
  VideoDecoderFactory* decoder_factory_;
  int frame_counter_;
};

TEST_F(CodecEndToEndTest, SendsAndReceivesVP8) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP8Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP8Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_0, absl::nullopt, "VP8",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_F(CodecEndToEndTest, SendsAndReceivesVP8Rotation90) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP8Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP8Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_90, absl::nullopt, "VP8",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

#if defined(RTC_ENABLE_VP9)
TEST_F(CodecEndToEndTest, SendsAndReceivesVP9) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP9Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP9Decoder::Create(); });
  CodecObserver test(500, kVideoRotation_0, absl::nullopt, "VP9",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_F(CodecEndToEndTest, SendsAndReceivesVP9VideoRotation90) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP9Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP9Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_90, absl::nullopt, "VP9",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_F(CodecEndToEndTest, SendsAndReceivesVP9ExplicitColorSpace) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP9Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP9Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_90,
                     CreateTestColorSpace(/*with_hdr_metadata=*/false), "VP9",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_F(CodecEndToEndTest,
       SendsAndReceivesVP9ExplicitColorSpaceWithHdrMetadata) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return VP9Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return VP9Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_90,
                     CreateTestColorSpace(/*with_hdr_metadata=*/true), "VP9",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

// Mutiplex tests are using VP9 as the underlying implementation.
TEST_F(CodecEndToEndTest, SendsAndReceivesMultiplex) {
  InternalEncoderFactory internal_encoder_factory;
  InternalDecoderFactory internal_decoder_factory;
  test::FunctionVideoEncoderFactory encoder_factory(
      [&internal_encoder_factory]() {
        return std::make_unique<MultiplexEncoderAdapter>(
            &internal_encoder_factory, SdpVideoFormat(cricket::kVp9CodecName));
      });
  test::FunctionVideoDecoderFactory decoder_factory(
      [&internal_decoder_factory]() {
        return std::make_unique<MultiplexDecoderAdapter>(
            &internal_decoder_factory, SdpVideoFormat(cricket::kVp9CodecName));
      });

  CodecObserver test(5, kVideoRotation_0, absl::nullopt, "multiplex",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_F(CodecEndToEndTest, SendsAndReceivesMultiplexVideoRotation90) {
  InternalEncoderFactory internal_encoder_factory;
  InternalDecoderFactory internal_decoder_factory;
  test::FunctionVideoEncoderFactory encoder_factory(
      [&internal_encoder_factory]() {
        return std::make_unique<MultiplexEncoderAdapter>(
            &internal_encoder_factory, SdpVideoFormat(cricket::kVp9CodecName));
      });
  test::FunctionVideoDecoderFactory decoder_factory(
      [&internal_decoder_factory]() {
        return std::make_unique<MultiplexDecoderAdapter>(
            &internal_decoder_factory, SdpVideoFormat(cricket::kVp9CodecName));
      });
  CodecObserver test(5, kVideoRotation_90, absl::nullopt, "multiplex",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

#endif  // defined(RTC_ENABLE_VP9)

#if defined(WEBRTC_USE_H264)
class EndToEndTestH264 : public test::CallTest,
                         public ::testing::WithParamInterface<std::string> {
 public:
  EndToEndTestH264() : field_trial_(GetParam()) {
    RegisterRtpExtension(RtpExtension(RtpExtension::kVideoRotationUri,
                                      kVideoRotationExtensionId));
  }

 private:
  test::ScopedFieldTrials field_trial_;
};

INSTANTIATE_TEST_SUITE_P(
    SpsPpsIdrIsKeyframe,
    EndToEndTestH264,
    ::testing::Values("WebRTC-SpsPpsIdrIsH264Keyframe/Disabled/",
                      "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/"));

TEST_P(EndToEndTestH264, SendsAndReceivesH264) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return H264Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return H264Decoder::Create(); });
  CodecObserver test(500, kVideoRotation_0, absl::nullopt, "H264",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_P(EndToEndTestH264, SendsAndReceivesH264VideoRotation90) {
  test::FunctionVideoEncoderFactory encoder_factory(
      []() { return H264Encoder::Create(); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return H264Decoder::Create(); });
  CodecObserver test(5, kVideoRotation_90, absl::nullopt, "H264",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_P(EndToEndTestH264, SendsAndReceivesH264PacketizationMode0) {
  cricket::VideoCodec codec =
      cricket::CreateVideoCodec(cricket::kH264CodecName);
  codec.SetParam(cricket::kH264FmtpPacketizationMode, "0");
  test::FunctionVideoEncoderFactory encoder_factory(
      [codec]() { return H264Encoder::Create(codec); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return H264Decoder::Create(); });
  CodecObserver test(500, kVideoRotation_0, absl::nullopt, "H264",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}

TEST_P(EndToEndTestH264, SendsAndReceivesH264PacketizationMode1) {
  cricket::VideoCodec codec =
      cricket::CreateVideoCodec(cricket::kH264CodecName);
  codec.SetParam(cricket::kH264FmtpPacketizationMode, "1");
  test::FunctionVideoEncoderFactory encoder_factory(
      [codec]() { return H264Encoder::Create(codec); });
  test::FunctionVideoDecoderFactory decoder_factory(
      []() { return H264Decoder::Create(); });
  CodecObserver test(500, kVideoRotation_0, absl::nullopt, "H264",
                     &encoder_factory, &decoder_factory);
  RunBaseTest(&test);
}
#endif  // defined(WEBRTC_USE_H264)

}  // namespace webrtc