1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "media/base/media.h"
8 #include "media/ffmpeg/ffmpeg_common.h"
9 #include "media/filters/ffmpeg_glue.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class FFmpegCommonTest
: public testing::Test
{
17 FFmpegGlue::InitializeFFmpeg();
19 ~FFmpegCommonTest() override
{};
22 TEST_F(FFmpegCommonTest
, OpusAudioDecoderConfig
) {
23 AVCodecContext context
= {0};
24 context
.codec_type
= AVMEDIA_TYPE_AUDIO
;
25 context
.codec_id
= AV_CODEC_ID_OPUS
;
26 context
.channel_layout
= CHANNEL_LAYOUT_STEREO
;
28 context
.sample_fmt
= AV_SAMPLE_FMT_FLT
;
30 // During conversion this sample rate should be changed to 48kHz.
31 context
.sample_rate
= 44100;
33 AudioDecoderConfig decoder_config
;
34 AVCodecContextToAudioDecoderConfig(&context
, false, &decoder_config
, false);
35 EXPECT_EQ(48000, decoder_config
.samples_per_second());
38 TEST_F(FFmpegCommonTest
, TimeBaseConversions
) {
39 const int64 test_data
[][5] = {
40 {1, 2, 1, 500000, 1 },
41 {1, 3, 1, 333333, 1 },
42 {1, 3, 2, 666667, 2 },
45 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
49 time_base
.num
= static_cast<int>(test_data
[i
][0]);
50 time_base
.den
= static_cast<int>(test_data
[i
][1]);
52 base::TimeDelta time_delta
=
53 ConvertFromTimeBase(time_base
, test_data
[i
][2]);
55 EXPECT_EQ(time_delta
.InMicroseconds(), test_data
[i
][3]);
56 EXPECT_EQ(ConvertToTimeBase(time_base
, time_delta
), test_data
[i
][4]);
60 TEST_F(FFmpegCommonTest
, VerifyFormatSizes
) {
61 for (AVSampleFormat format
= AV_SAMPLE_FMT_NONE
;
62 format
< AV_SAMPLE_FMT_NB
;
63 format
= static_cast<AVSampleFormat
>(format
+ 1)) {
64 SampleFormat sample_format
= AVSampleFormatToSampleFormat(format
);
65 if (sample_format
== kUnknownSampleFormat
) {
66 // This format not supported, so skip it.
70 // Have FFMpeg compute the size of a buffer of 1 channel / 1 frame
71 // with 1 byte alignment to make sure the sizes match.
72 int single_buffer_size
= av_samples_get_buffer_size(NULL
, 1, 1, format
, 1);
73 int bytes_per_channel
= SampleFormatToBytesPerChannel(sample_format
);
74 EXPECT_EQ(bytes_per_channel
, single_buffer_size
);
78 TEST_F(FFmpegCommonTest
, UTCDateToTime_Valid
) {
80 EXPECT_TRUE(FFmpegUTCDateToTime("2012-11-10 12:34:56", &result
));
82 base::Time::Exploded exploded
;
83 result
.UTCExplode(&exploded
);
84 EXPECT_TRUE(exploded
.HasValidValues());
85 EXPECT_EQ(2012, exploded
.year
);
86 EXPECT_EQ(11, exploded
.month
);
87 EXPECT_EQ(6, exploded
.day_of_week
);
88 EXPECT_EQ(10, exploded
.day_of_month
);
89 EXPECT_EQ(12, exploded
.hour
);
90 EXPECT_EQ(34, exploded
.minute
);
91 EXPECT_EQ(56, exploded
.second
);
92 EXPECT_EQ(0, exploded
.millisecond
);
95 TEST_F(FFmpegCommonTest
, UTCDateToTime_Invalid
) {
96 const char* invalid_date_strings
[] = {
108 "2012-11-10-12 12:34:56",
110 "2012-11-10 12:34:56:78",
111 "ABCD-11-10 12:34:56",
112 "2012-EF-10 12:34:56",
113 "2012-11-GH 12:34:56",
114 "2012-11-10 IJ:34:56",
115 "2012-11-10 12:JL:56",
116 "2012-11-10 12:34:MN",
117 "2012-11-10 12:34:56.123",
118 "2012-11-1012:34:56",
119 "2012-11-10 12:34:56 UTC",
122 for (size_t i
= 0; i
< arraysize(invalid_date_strings
); ++i
) {
123 const char* date_string
= invalid_date_strings
[i
];
125 EXPECT_FALSE(FFmpegUTCDateToTime(date_string
, &result
))
126 << "date_string '" << date_string
<< "'";
127 EXPECT_TRUE(result
.is_null());