chromium/media/filters/audio_decoder_unittest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string_view>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/md5.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_hash.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_util.h"
#include "media/base/supported_types.h"
#include "media/base/test_data_util.h"
#include "media/base/test_helpers.h"
#include "media/base/timestamp_constants.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/ffmpeg/scoped_av_packet.h"
#include "media/filters/audio_file_reader.h"
#include "media/filters/ffmpeg_audio_decoder.h"
#include "media/filters/in_memory_url_protocol.h"
#include "media/media_buildflags.h"
#include "media/mojo/services/gpu_mojo_media_client_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "media/base/android/media_codec_util.h"
#include "media/filters/android/media_codec_audio_decoder.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "media/filters/mac/audio_toolbox_audio_decoder.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_com_initializer.h"
#include "media/filters/win/media_foundation_audio_decoder.h"
#endif

#if BUILDFLAG(USE_PROPRIETARY_CODECS)
#include "media/formats/mpeg/adts_stream_parser.h"
#endif

Combine;
TestWithParam;
Values;
ValuesIn;

namespace media {

namespace {

// The number of packets to read and then decode from each file.
constexpr size_t kDecodeRuns =;

struct DecodedBufferExpectations {};

DataExpectations;

struct TestParams {};

// Tells gtest how to print our TestParams structure.
std::ostream& operator<<(std::ostream& os, const TestParams& params) {}

// Marks negative timestamp buffers for discard or transfers FFmpeg's built in
// discard metadata in favor of setting DiscardPadding on the DecoderBuffer.
// Allows better testing of AudioDiscardHelper usage.
void SetDiscardPadding(AVPacket* packet,
                       DecoderBuffer* buffer,
                       double samples_per_second) {}

}  // namespace

class AudioDecoderTest
    : public TestWithParam<std::tuple<AudioDecoderType, TestParams>> {};

constexpr DataExpectations kBearOpusExpectations =;

// Test params to test decoder reinitialization. Choose opus because it is
// supported on all platforms we test on.
constexpr TestParams kReinitializeTestParams =;

#if BUILDFLAG(IS_ANDROID)
constexpr TestParams kMediaCodecTestParams[] = {
    {AudioCodec::kOpus, "bear-opus.ogg", kBearOpusExpectations, 24, 48000,
     CHANNEL_LAYOUT_STEREO},
#if BUILDFLAG(USE_PROPRIETARY_CODECS)
    {AudioCodec::kAAC,
     "sfx.adts",
     {{
         {0, 23219, "-1.80,-1.49,-0.23,1.11,1.54,-0.11,"},
         {23219, 23219, "-1.90,-1.53,-0.15,1.28,1.23,-0.33,"},
         {46439, 23219, "0.54,0.88,2.19,3.54,3.24,1.63,"},
     }},
     0,
     44100,
     CHANNEL_LAYOUT_MONO},
    {AudioCodec::kAAC,
     "bear-audio-implicit-he-aac-v2.aac",
     {{
         {0, 42666, "-1.76,-0.12,1.72,1.45,0.10,-1.32,"},
         {42666, 42666, "-1.78,-0.13,1.70,1.44,0.09,-1.32,"},
         {85333, 42666, "-1.78,-0.13,1.70,1.44,0.08,-1.33,"},
     }},
     0,
     24000,
     CHANNEL_LAYOUT_MONO},
#endif  // defined(USE_PROPRIETARY_CODECS)
};
#endif  // BUILDFLAG(IS_ANDROID)

#if (BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)) && \
    BUILDFLAG(USE_PROPRIETARY_CODECS)
// Note: We don't test hashes for xHE-AAC content since the decoder is provided
// by the operating system and will apply DRC based on device specific params.
constexpr TestParams kXheAacTestParams[] = {
    {AudioCodec::kAAC,
     "noise-xhe-aac.mp4",
     {{
         {0, 42666, nullptr},
         {42666, 42666, nullptr},
         {85333, 42666, nullptr},
     }},
     0,
     48000,
     CHANNEL_LAYOUT_STEREO,
     AudioCodecProfile::kXHE_AAC},
// Windows doesn't support 29.4kHz
#if !BUILDFLAG(IS_WIN)
    {AudioCodec::kAAC,
     "noise-xhe-aac-mono.mp4",
     {{
         {0, 34829, nullptr},
         {34829, 34829, nullptr},
         {69659, 34829, nullptr},
     }},
     0,
     29400,
     CHANNEL_LAYOUT_UNSUPPORTED,
     AudioCodecProfile::kXHE_AAC},
#endif
    {AudioCodec::kAAC,
     "noise-xhe-aac-44kHz.mp4",
     {{
         {0, 23219, nullptr},
         {23219, 23219, nullptr},
         {46439, 23219, nullptr},
     }},
     0,
     44100,
     CHANNEL_LAYOUT_STEREO,
     AudioCodecProfile::kXHE_AAC},
};
#endif  // (BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)) &&
        // BUILDFLAG(USE_PROPRIETARY_CODECS)

constexpr DataExpectations kSfxFlacExpectations =;

constexpr TestParams kFFmpegTestParams[] =;

void AudioDecoderTest::SetReinitializeParams() {}

TEST_P(AudioDecoderTest, Initialize) {}

TEST_P(AudioDecoderTest, Reinitialize_AfterInitialize) {}

TEST_P(AudioDecoderTest, Reinitialize_AfterDecode) {}

TEST_P(AudioDecoderTest, Reinitialize_AfterReset) {}

// Verifies decode audio as well as the Decode() -> Reset() sequence.
TEST_P(AudioDecoderTest, ProduceAudioSamples) {}

TEST_P(AudioDecoderTest, Decode) {}

TEST_P(AudioDecoderTest, MismatchedSubsampleBuffer) {}

// The AudioDecoders do not support encrypted buffers since they were
// initialized without cdm_context.
TEST_P(AudioDecoderTest, EncryptedBuffer) {}

TEST_P(AudioDecoderTest, Reset) {}

TEST_P(AudioDecoderTest, NoTimestamp) {}

TEST_P(AudioDecoderTest, EOSBuffer) {}

INSTANTIATE_TEST_SUITE_P();

#if BUILDFLAG(IS_ANDROID)
std::vector<TestParams> GetAndroidParams() {
  std::vector<TestParams> params;
  params.insert(params.end(), std::cbegin(kMediaCodecTestParams),
                std::cend(kMediaCodecTestParams));
#if BUILDFLAG(USE_PROPRIETARY_CODECS)
  params.insert(params.end(), std::cbegin(kXheAacTestParams),
                std::cend(kXheAacTestParams));
#endif
  return params;
}

INSTANTIATE_TEST_SUITE_P(MediaCodec,
                         AudioDecoderTest,
                         Combine(Values(AudioDecoderType::kMediaCodec),
                                 ValuesIn(GetAndroidParams())));
#endif

#if BUILDFLAG(USE_PROPRIETARY_CODECS)
#if BUILDFLAG(IS_MAC)
INSTANTIATE_TEST_SUITE_P(AudioToolbox,
                         AudioDecoderTest,
                         Combine(Values(AudioDecoderType::kAudioToolbox),
                                 ValuesIn(kXheAacTestParams)));
#elif BUILDFLAG(IS_WIN)
INSTANTIATE_TEST_SUITE_P(MediaFoundation,
                         AudioDecoderTest,
                         Combine(Values(AudioDecoderType::kMediaFoundation),
                                 ValuesIn(kXheAacTestParams)));
#endif
#endif

}  // namespace media