Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcoreaaudiosink_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QCOREAAUDIOSINK_P_H
4#define QCOREAAUDIOSINK_P_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <QtMultimedia/private/qaudio_platform_implementation_support_p.h>
18
19#include <AudioUnit/AudioUnit.h>
20#include <QtMultimedia/private/qcoreaudioutils_p.h>
21#include <QtMultimedia/private/qcoreaudiodevices_p.h>
22#ifdef Q_OS_MACOS
23# include <QtMultimedia/private/qmacosaudiodatautils_p.h>
24#else
25# include <QtMultimedia/private/qavaudiosessionrecovery_p.h>
26#endif
27
28QT_BEGIN_NAMESPACE
29
30class QCoreAudioSink;
31
32class QCoreAudioSinkStream final : public std::enable_shared_from_this<QCoreAudioSinkStream>,
33 public QtMultimediaPrivate::QPlatformAudioSinkStream
34{
35 using QPlatformAudioSinkStream = QtMultimediaPrivate::QPlatformAudioSinkStream;
36 using AudioEndpointRole = QtMultimediaPrivate::AudioEndpointRole;
37
38public:
39 using SinkType = QCoreAudioSink;
40
41 explicit QCoreAudioSinkStream(QAudioDevice, const QAudioFormat &,
42 std::optional<qsizetype> ringbufferSize, QCoreAudioSink *parent,
43 float volume, std::optional<QtMultimediaPrivate::NativePeriodFrames> nativePeriodFrames,
45 Q_DISABLE_COPY_MOVE(QCoreAudioSinkStream)
46 ~QCoreAudioSinkStream();
47
48 bool open();
49 bool start(QIODevice *device);
51 bool start(AudioCallback cb);
52 void stop(ShutdownPolicy policy);
54 void stopStream();
55
56 void suspend();
57 bool resume();
58
59 using QPlatformAudioSinkStream::bytesFree;
60 using QPlatformAudioSinkStream::processedDuration;
61 using QPlatformAudioSinkStream::ringbufferSizeInBytes;
62 using QPlatformAudioSinkStream::setVolume;
63
64 bool resumeIfNecessary();
65
66 void reportIOError();
68
69private:
70 OSStatus processRingbuffer(uint32_t numberOfFrames,
71 AudioBufferList *ioData) noexcept Q_DECL_NONBLOCKING_FUNCTION;
72 OSStatus processAudioCallback(uint32_t numberOfFrames,
73 AudioBufferList *ioData) noexcept Q_DECL_NONBLOCKING_FUNCTION;
74
75 void updateStreamIdle(bool arg) override;
76
77 bool startAudioUnit();
78 void stopAudioUnit();
79
80
81#ifdef Q_OS_MACOS
82 bool setDisconnectListener(AudioObjectID id);
83
84 QCoreAudioUtils::DeviceDisconnectMonitor m_disconnectMonitor;
85 QFuture<void> m_stopOnDisconnected;
86#endif
87
88 std::unique_ptr<QIODevice> m_reader;
89 QCoreAudioUtils::AudioUnitHandle m_audioUnit;
90
91 QCoreAudioSink *m_parent;
92
93 std::optional<AudioCallback> m_audioCallback;
94
95#ifndef Q_OS_MACOS
96 // Held for as long as this stream wants the shared AVAudioSession active; acquired in
97 // start(), released in stopStream(). suspend()/resume() (interruption handling) must not
98 // touch it: the stream still wants the session active across a suspend/resume cycle, only
99 // the OS-level session state changed.
100 std::optional<QtMultimediaPrivate::QAVAudioSessionManager::ActivationToken> m_sessionActivation;
101#endif
102};
103
104class QCoreAudioSink final
105 : public QtMultimediaPrivate::QPlatformAudioSinkImplementation<QCoreAudioSinkStream,
106 QCoreAudioSink>
107{
108 using BaseClass = QtMultimediaPrivate::QPlatformAudioSinkImplementation<QCoreAudioSinkStream,
109 QCoreAudioSink>;
110
111public:
112 QCoreAudioSink(QAudioDevice device, const QAudioFormat &format, QObject *parent);
114
116
117#ifndef Q_OS_MACOS
118 void resume() override;
119
120 // Interface required by QAVAudioSessionRecovery.
121 QCoreAudioSinkStream *currentStream() const { return m_stream.get(); }
122 using BaseClass::updateStreamState;
123
124 void notifyStreamStopped() { m_recovery.streamStopped(); }
125
126private:
127 QtMultimediaPrivate::QAVAudioSessionRecovery<QCoreAudioSink> m_recovery{ *this };
128#endif
129};
130
131QT_END_NAMESPACE
132
133#endif // QCOREAAUDIOSINK_P_H
QCoreAudioSinkStream(QAudioDevice, const QAudioFormat &, std::optional< qsizetype > ringbufferSize, QCoreAudioSink *parent, float volume, std::optional< QtMultimediaPrivate::NativePeriodFrames > nativePeriodFrames, AudioEndpointRole)
void stop(ShutdownPolicy policy)
bool start(AudioCallback cb)
bool start(QIODevice *device)
void updateStreamIdle(bool arg) override
void resumeStreamIfNecessary()
QCoreAudioSinkStream * currentStream() const
void resume() override
~QCoreAudioSink() override
QCoreAudioSink(QAudioDevice device, const QAudioFormat &format, QObject *parent)