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

media / filters / mac / audio_toolbox_audio_decoder.h [blame]

// Copyright 2022 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_FILTERS_MAC_AUDIO_TOOLBOX_AUDIO_DECODER_H_
#define MEDIA_FILTERS_MAC_AUDIO_TOOLBOX_AUDIO_DECODER_H_

#include <memory>

#include <AudioToolbox/AudioToolbox.h>

#include "base/apple/scoped_typeref.h"
#include "base/memory/free_deleter.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_decoder.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/media_export.h"

namespace media {

class AudioDiscardHelper;
class LimitingAudioQueue;
class MediaLog;

// Audio decoder based on macOS's AudioToolbox API. The AudioToolbox
// API is required to decode codecs that aren't supported by Chromium.
class MEDIA_EXPORT AudioToolboxAudioDecoder : public AudioDecoder {
 public:
  explicit AudioToolboxAudioDecoder(std::unique_ptr<MediaLog> media_log);

  AudioToolboxAudioDecoder(const AudioToolboxAudioDecoder&) = delete;
  AudioToolboxAudioDecoder& operator=(const AudioToolboxAudioDecoder&) = delete;

  ~AudioToolboxAudioDecoder() override;

  // AudioDecoder implementation.
  AudioDecoderType GetDecoderType() const override;
  void Initialize(const AudioDecoderConfig& config,
                  CdmContext* cdm_context,
                  InitCB init_cb,
                  const OutputCB& output_cb,
                  const WaitingCB& waiting_cb) override;
  void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  void Reset(base::OnceClosure reset_cb) override;
  bool NeedsBitstreamConversion() const override;

 private:
  struct ScopedAudioConverterRefTraits {
    static AudioConverterRef InvalidValue() { return nullptr; }
    static AudioConverterRef Retain(AudioConverterRef converter);
    static void Release(AudioConverterRef converter);
  };
  using ScopedAudioConverterRef =
      base::apple::ScopedTypeRef<AudioConverterRef,
                                 ScopedAudioConverterRefTraits>;

  bool CreateDecoder(const AudioDecoderConfig& config);

  void OnOutputReady(DecoderBuffer::TimeInfo time_info,
                     scoped_refptr<AudioBuffer> output_buffer);

  std::unique_ptr<MediaLog> media_log_;

  // "Converter" for turning encoded samples into raw audio.
  ScopedAudioConverterRef decoder_;

  // Actual channel count and layout from decoder, may be different than config.
  uint32_t channel_count_ = 0u;
  ChannelLayout channel_layout_ = CHANNEL_LAYOUT_UNSUPPORTED;

  // Actual sample rate from the decoder, may be different than config.
  uint32_t sample_rate_ = 0u;

  // Callback that delivers output frames.
  OutputCB output_cb_;

  std::unique_ptr<AudioDiscardHelper> discard_helper_;

  std::unique_ptr<LimitingAudioQueue> limiter_queue_;

  // Staging structures for receiving decoded data.
  std::unique_ptr<AudioBus> output_bus_;
  std::unique_ptr<AudioBufferList, base::FreeDeleter> output_buffer_list_;
};

}  // namespace media

#endif  // MEDIA_FILTERS_MAC_AUDIO_TOOLBOX_AUDIO_DECODER_H_