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/ffmpeg/ffmpeg_common.h"
8 #include "media/filters/ffmpeg_glue.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class FFmpegCommonTest
: public testing::Test
{
15 FFmpegCommonTest() { FFmpegGlue::InitializeFFmpeg(); }
16 ~FFmpegCommonTest() override
{};
19 TEST_F(FFmpegCommonTest
, OpusAudioDecoderConfig
) {
20 AVCodecContext context
= {0};
21 context
.codec_type
= AVMEDIA_TYPE_AUDIO
;
22 context
.codec_id
= AV_CODEC_ID_OPUS
;
23 context
.channel_layout
= CHANNEL_LAYOUT_STEREO
;
25 context
.sample_fmt
= AV_SAMPLE_FMT_FLT
;
27 // During conversion this sample rate should be changed to 48kHz.
28 context
.sample_rate
= 44100;
30 AudioDecoderConfig decoder_config
;
31 AVCodecContextToAudioDecoderConfig(&context
, false, &decoder_config
, false);
32 EXPECT_EQ(48000, decoder_config
.samples_per_second());
35 TEST_F(FFmpegCommonTest
, TimeBaseConversions
) {
36 const int64 test_data
[][5] = {
37 {1, 2, 1, 500000, 1 },
38 {1, 3, 1, 333333, 1 },
39 {1, 3, 2, 666667, 2 },
42 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
46 time_base
.num
= static_cast<int>(test_data
[i
][0]);
47 time_base
.den
= static_cast<int>(test_data
[i
][1]);
49 base::TimeDelta time_delta
=
50 ConvertFromTimeBase(time_base
, test_data
[i
][2]);
52 EXPECT_EQ(time_delta
.InMicroseconds(), test_data
[i
][3]);
53 EXPECT_EQ(ConvertToTimeBase(time_base
, time_delta
), test_data
[i
][4]);
57 TEST_F(FFmpegCommonTest
, VerifyFormatSizes
) {
58 for (AVSampleFormat format
= AV_SAMPLE_FMT_NONE
;
59 format
< AV_SAMPLE_FMT_NB
;
60 format
= static_cast<AVSampleFormat
>(format
+ 1)) {
61 SampleFormat sample_format
= AVSampleFormatToSampleFormat(format
);
62 if (sample_format
== kUnknownSampleFormat
) {
63 // This format not supported, so skip it.
67 // Have FFMpeg compute the size of a buffer of 1 channel / 1 frame
68 // with 1 byte alignment to make sure the sizes match.
69 int single_buffer_size
= av_samples_get_buffer_size(NULL
, 1, 1, format
, 1);
70 int bytes_per_channel
= SampleFormatToBytesPerChannel(sample_format
);
71 EXPECT_EQ(bytes_per_channel
, single_buffer_size
);
75 TEST_F(FFmpegCommonTest
, UTCDateToTime_Valid
) {
77 EXPECT_TRUE(FFmpegUTCDateToTime("2012-11-10 12:34:56", &result
));
79 base::Time::Exploded exploded
;
80 result
.UTCExplode(&exploded
);
81 EXPECT_TRUE(exploded
.HasValidValues());
82 EXPECT_EQ(2012, exploded
.year
);
83 EXPECT_EQ(11, exploded
.month
);
84 EXPECT_EQ(6, exploded
.day_of_week
);
85 EXPECT_EQ(10, exploded
.day_of_month
);
86 EXPECT_EQ(12, exploded
.hour
);
87 EXPECT_EQ(34, exploded
.minute
);
88 EXPECT_EQ(56, exploded
.second
);
89 EXPECT_EQ(0, exploded
.millisecond
);
92 #if defined(ALLOCATOR_SHIM) && defined(GTEST_HAS_DEATH_TEST)
93 TEST_F(FFmpegCommonTest
, WinAllocatorShimDeathTest
) {
94 scoped_ptr
<char, base::FreeDeleter
> ptr
;
95 // INT_MAX - 128 is carefully chosen to be below the default limit for
96 // ffmpeg allocations, but above the maximum allowed limit by the allocator
97 // shim, so we can be certain the code is being hit.
98 EXPECT_DEATH(ptr
.reset(static_cast<char*>(av_malloc(INT_MAX
- 128))), "");
103 TEST_F(FFmpegCommonTest
, UTCDateToTime_Invalid
) {
104 const char* invalid_date_strings
[] = {
116 "2012-11-10-12 12:34:56",
118 "2012-11-10 12:34:56:78",
119 "ABCD-11-10 12:34:56",
120 "2012-EF-10 12:34:56",
121 "2012-11-GH 12:34:56",
122 "2012-11-10 IJ:34:56",
123 "2012-11-10 12:JL:56",
124 "2012-11-10 12:34:MN",
125 "2012-11-10 12:34:56.123",
126 "2012-11-1012:34:56",
127 "2012-11-10 12:34:56 UTC",
130 for (size_t i
= 0; i
< arraysize(invalid_date_strings
); ++i
) {
131 const char* date_string
= invalid_date_strings
[i
];
133 EXPECT_FALSE(FFmpegUTCDateToTime(date_string
, &result
))
134 << "date_string '" << date_string
<< "'";
135 EXPECT_TRUE(result
.is_null());