QAudioBufferInput Class | Qt Multimedia | Qt 6.11.1
Description: The QAudioBufferInput class is used for providing custom audio buffers to QMediaRecorder through QMediaCaptureSession.

- Qt 6.11
- Qt Multimedia
- C++ Classes
- QtMultimedia
- QAudioBufferInput
- Qt 6.11.1 Reference Documentation

# QAudioBufferInput Class

The QAudioBufferInput class is used for providing custom audio buffers to QMediaRecorder through QMediaCaptureSession. More...

Header: | `#include <QAudioBufferInput>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Multimedia)` `target_link_libraries(mytarget PRIVATE Qt6::Multimedia)`
qmake: | `QT += multimedia`
Since: | Qt 6.8
Inherits: | QObject

- List of all members, including inherited members

## Public Functions

| QAudioBufferInput(QObject *parent = nullptr)
| QAudioBufferInput(const QAudioFormat &format, QObject *parent = nullptr)
virtual | ~QAudioBufferInput() override
QMediaCaptureSession * | captureSession() const
QAudioFormat | format() const
bool | sendAudioBuffer(const QAudioBuffer &audioBuffer)

## Signals

void | readyToSendAudioBuffer()

## Detailed Description

QAudioBufferInput is only supported with the FFmpeg backend.

Custom audio buffers can be recorded by connecting a QAudioBufferInput and a QMediaRecorder to a QMediaCaptureSession. For a pull mode implementation, call sendAudioBuffer() in response to the readyToSendAudioBuffer() signal. In the snippet below this is done by connecting the signal to a slot in a custom media generator class. The slot function emits another signal with a new audio buffer, which is connected to sendAudioBuffer():

     QMediaCaptureSession session;
     QMediaRecorder recorder;
     QAudioBufferInput audioInput;

     session.setRecorder(&recorder);
     session.setAudioBufferInput(&audioInput);

     MediaGenerator generator; // Custom class providing audio buffers

     connect(&audioInput, &QAudioBufferInput::readyToSendAudioBuffer,
             &generator, &MediaGenerator::nextAudioBuffer);
     connect(&generator, &MediaGenerator::audioBufferReady,
             &audioInput, &QAudioBufferInput::sendAudioBuffer);

     recorder.record();

Here's a minimal implementation of the slot function that provides audio buffers:

     void MediaGenerator::nextAudioBuffer()
     {
         QAudioBuffer buffer = nextBuffer();
         emit audioBufferReady(buffer);
     }

For more details see readyToSendAudioBuffer() and sendAudioBuffer().

See also QMediaRecorder and QMediaCaptureSession.

## Member Function Documentation

### `[explicit]` QAudioBufferInput::QAudioBufferInput(QObject *parent = nullptr)

Constructs a new QAudioBufferInput object with parent.

### `[explicit]` QAudioBufferInput::QAudioBufferInput(const QAudioFormat &format, QObject *parent = nullptr)

Constructs a new QAudioBufferInput object with audio format and parent.

The specified format will work as a hint for the initialization of the matching audio encoder upon invoking QMediaRecorder::record(). If the format is not specified or not valid, the audio encoder will be initialized upon sending the first audio buffer.

We recommend specifying the format if you know in advance what kind of audio buffers you're going to send.

### `[override virtual noexcept]` QAudioBufferInput::~QAudioBufferInput()

Destroys the object.

### QMediaCaptureSession *QAudioBufferInput::captureSession() const

Returns the capture session this audio buffer input is connected to, or a `nullptr` if the audio buffer input is not connected to a capture session.

Use QMediaCaptureSession::setAudioBufferInput() to connect the audio buffer input to a session.

### QAudioFormat QAudioBufferInput::format() const

Returns the audio format that was specified upon construction of the audio buffer input.

### `[signal]` void QAudioBufferInput::readyToSendAudioBuffer()

Signals that a new audio buffer can be sent to the audio buffer input. After receiving the signal, if you have audio date to be sent, invoke sendAudioBuffer once or in a loop until it returns `false`.

See also sendAudioBuffer().

### bool QAudioBufferInput::sendAudioBuffer(const QAudioBuffer &audioBuffer)

Sends QAudioBuffer to QMediaRecorder through QMediaCaptureSession.

Returns `true` if the specified audioBuffer has been sent successfully to the destination. Returns `false`, if the buffer hasn't been sent, which can happen if the instance is not assigned to QMediaCaptureSession, the session doesn't have a media recorder, the media recorder is not started or its queue is full. The readyToSendAudioBuffer() signal will be emitted as soon as the destination is able to handle a new audio buffer.

Sending of an empty audio buffer is treated by QMediaRecorder as an end of the input stream. QMediaRecorder stops the recording automatically if QMediaRecorder::autoStop is `true` and all the inputs have reported the end of the stream.