blob: fd38fb2aba47b65bb51f06b637f378a76575403a [file] [log] [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_AUDIO_FLAC_AUDIO_HANDLER_H_
#define MEDIA_AUDIO_FLAC_AUDIO_HANDLER_H_
#include <cstddef>
#include <memory>
#include "base/strings/string_piece.h"
#include "media/audio/audio_handler.h"
#include "media/base/media_export.h"
#include "third_party/flac/include/FLAC/stream_decoder.h"
namespace media {
class AudioBus;
class AudioFifo;
class FlacStreamDecoderDeleter {
public:
void operator()(FLAC__StreamDecoder* ptr) {
FLAC__stream_decoder_delete(ptr);
}
};
// This class provides the input from flac file format.
class MEDIA_EXPORT FlacAudioHandler : public AudioHandler {
public:
explicit FlacAudioHandler(base::StringPiece data);
FlacAudioHandler(const FlacAudioHandler&) = delete;
FlacAudioHandler& operator=(const FlacAudioHandler&) = delete;
~FlacAudioHandler() override;
// AudioHandler:
bool Initialize() override;
int GetNumChannels() const override;
int GetSampleRate() const override;
base::TimeDelta GetDuration() const override;
bool AtEnd() const override;
bool CopyTo(AudioBus* bus, size_t* frames_written) override;
void Reset() override;
int total_frames_for_testing() const { return total_frames_; }
private:
// Callbacks for the `decoder_`.
static FLAC__StreamDecoderReadStatus ReadCallback(
const FLAC__StreamDecoder* decoder,
FLAC__byte buffer[],
size_t* bytes,
void* client_data);
static FLAC__StreamDecoderWriteStatus WriteCallback(
const FLAC__StreamDecoder* decoder,
const FLAC__Frame* frame,
const FLAC__int32* const buffer[],
void* client_data);
static void MetaCallback(const FLAC__StreamDecoder* decoder,
const FLAC__StreamMetadata* metadata,
void* client_data);
static void ErrorCallback(const FLAC__StreamDecoder* decoder,
FLAC__StreamDecoderErrorStatus status,
void* client_data);
bool is_initialized() const { return !!fifo_; }
// Internal callbacks.
FLAC__StreamDecoderReadStatus ReadCallbackInternal(FLAC__byte buffer[],
size_t* bytes);
FLAC__StreamDecoderWriteStatus WriteCallbackInternal(
const FLAC__Frame* frame,
const FLAC__int32* const buffer[]);
void MetaCallbackInternal(const FLAC__StreamMetadata* metadata);
// Check if the metadata fecthed in `MetaCallback()` is valid or not. This
// function will check the `num_channels_`, `bits_per_sample_`, `sample_rate_`
// and `total_frames_`.
// Note: We only consider 16 bits per second of audio data currently.
bool AreParamsValid() const;
const base::StringPiece flac_data_;
const std::unique_ptr<FLAC__StreamDecoder, FlacStreamDecoderDeleter> decoder_;
// To transfer decoded PCM samples from `WriteCallback` to `CopyTo`.
std::unique_ptr<AudioFifo> fifo_;
// The PCM buffer received in `WriteCallback()` is copied into `bus_`.
std::unique_ptr<AudioBus> bus_;
// Start to read `flac_data_` from `cursor_` position.
size_t cursor_ = 0u;
int num_channels_ = 0;
int sample_rate_ = 0;
int bits_per_sample_ = 0;
// Equal to the total number of samples per channel.
uint64_t total_frames_ = 0u;
};
} // namespace media
#endif // MEDIA_AUDIO_FLAC_AUDIO_HANDLER_H_