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

media / mojo / common / audio_data_s16_converter.h [blame]

// Copyright 2021 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_MOJO_COMMON_AUDIO_DATA_S16_CONVERTER_H_
#define MEDIA_MOJO_COMMON_AUDIO_DATA_S16_CONVERTER_H_

#include <memory>

#include "media/base/audio_buffer.h"
#include "media/base/audio_bus.h"
#include "media/mojo/mojom/media_types.mojom.h"

namespace media {

class ChannelMixer;

// Converts AudioBuffer or AudioBus into mojom::AudioDataS16.
class AudioDataS16Converter {
 public:
  AudioDataS16Converter();
  virtual ~AudioDataS16Converter();
  AudioDataS16Converter(const AudioDataS16Converter&) = delete;
  AudioDataS16Converter& operator=(const AudioDataS16Converter&) = delete;

  mojom::AudioDataS16Ptr ConvertToAudioDataS16(
      scoped_refptr<AudioBuffer> buffer,
      bool is_multichannel_supported);

  mojom::AudioDataS16Ptr ConvertToAudioDataS16(
      std::unique_ptr<AudioBus> audio_bus,
      int sample_rate,
      ChannelLayout channel_layout,
      bool is_multichannel_supported);

  mojom::AudioDataS16Ptr ConvertToAudioDataS16(const AudioBus& audio_bus,
                                               int sample_rate,
                                               ChannelLayout channel_layout,
                                               bool is_multichannel_supported);

 private:
  // Recreates the temporary audio bus if the frame count or channel count
  // changed and reads the frames from the buffer into the temporary audio bus.
  void CopyBufferToTempAudioBus(const AudioBuffer& buffer);

  // Resets the temporary monaural audio bus and the channel mixer used to
  // combine multiple audio channels.
  void ResetChannelMixerIfNeeded(int frame_count,
                                 ChannelLayout channel_layout,
                                 int channel_count);

  // The temporary audio bus used to convert the raw audio to the appropriate
  // format.
  std::unique_ptr<AudioBus> temp_audio_bus_;

  // The temporary audio bus used to mix multichannel audio into a single
  // channel.
  std::unique_ptr<AudioBus> monaural_audio_bus_;

  std::unique_ptr<ChannelMixer> channel_mixer_;

  // The layout used to instantiate the channel mixer.
  ChannelLayout channel_layout_ = ChannelLayout::CHANNEL_LAYOUT_NONE;

  // The number of channels of the audio output.
  int channel_count_ = 0;
};

}  // namespace media

#endif  // MEDIA_MOJO_COMMON_AUDIO_DATA_S16_CONVERTER_H_