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
qwindowsaudiosink_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
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists for the convenience
9// of other Qt classes. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef QWINDOWSAUDIOSINK_H
16#define QWINDOWSAUDIOSINK_H
17
18#include <QtCore/qthread.h>
19#include <QtCore/qtclasshelpermacros.h>
20#include <QtCore/private/qcomptr_p.h>
21#include <QtMultimedia/private/qaudiosystem_p.h>
22#include <QtMultimedia/private/qaudiosystem_platform_stream_support_p.h>
23#include <QtMultimedia/private/qaudio_platform_implementation_support_p.h>
24#include <QtMultimedia/private/qwindowsaudioutils_p.h>
25
26#include <atomic>
27#include <memory>
28#include <memory_resource>
29
30struct IAudioRenderClient;
31
32QT_BEGIN_NAMESPACE
33
34class QWindowsResampler;
35
36namespace QtWASAPI {
37
38class QWindowsAudioSink;
39using namespace QtMultimediaPrivate;
40
41///////////////////////////////////////////////////////////////////////////////////////////////////
42
45{
47 using SinkType = QWindowsAudioSink;
48
53
54 QWASAPIAudioSinkStream(QAudioDevice, const QAudioFormat &,
55 std::optional<qsizetype> ringbufferSize, QWindowsAudioSink *parent,
56 float volume, std::optional<int32_t> hardwareBufferSize,
60
61 bool open();
62
67
68 bool start(QIODevice *);
70 bool start(AudioCallback);
71
72 void suspend();
73 void resume();
74 void stop(ShutdownPolicy);
75
76 void updateStreamIdle(bool) override;
77
78private:
79 bool openAudioClient(ComPtr<IMMDevice>, AudioEndpointRole);
80 bool startAudioClient(StreamType);
81
82 template <typename Functor>
83 bool visitAudioClientBuffer(Functor &&f);
84
85 void fillInitialHostBuffer();
86 void runProcessRingbufferLoop();
87 void runProcessCallbackLoop();
88 bool processRingbuffer() noexcept QT_MM_NONBLOCKING;
89 bool processCallback() noexcept QT_MM_NONBLOCKING;
90
91 void handleAudioClientError();
92
93 ComPtr<IAudioClient3> m_audioClient;
94 ComPtr<IAudioRenderClient> m_renderClient;
95
96 QWindowsAudioUtils::reference_time m_periodSize;
97 qsizetype m_audioClientFrames;
98
99 std::atomic_bool m_suspended{};
100 std::atomic<ShutdownPolicy> m_shutdownPolicy{ ShutdownPolicy::DiscardRingbuffer };
101 QAutoResetEvent m_ringbufferDrained;
102
103 const AudioEndpointRole m_role;
104
105 const QUniqueWin32NullHandle m_wasapiHandle;
106 std::unique_ptr<QThread> m_workerThread;
107
108 AudioCallback m_audioCallback;
109
110 QWindowsAudioSink *m_parent;
111
112 QAudioFormat m_hostFormat;
113 std::unique_ptr<char[]> m_preallocatedBuffer;
114 std::unique_ptr<std::pmr::memory_resource> m_memoryResource;
115 std::unique_ptr<QWindowsResampler> m_resampler;
116};
117
118///////////////////////////////////////////////////////////////////////////////////////////////////
119
120class QWindowsAudioSink final
121 : public QPlatformAudioSinkImplementation<QWASAPIAudioSinkStream, QWindowsAudioSink>
122{
123 using BaseClass = QPlatformAudioSinkImplementation<QWASAPIAudioSinkStream, QWindowsAudioSink>;
124
125public:
126 QWindowsAudioSink(QAudioDevice, const QAudioFormat &, QObject *parent);
127};
128
129} // namespace QtWASAPI
130
131QT_END_NAMESPACE
132
133#endif // QWINDOWSAUDIOSINK_H
QWindowsAudioSink(QAudioDevice, const QAudioFormat &, QObject *parent)
QWASAPIAudioSinkStream(QAudioDevice, const QAudioFormat &, std::optional< qsizetype > ringbufferSize, QWindowsAudioSink *parent, float volume, std::optional< int32_t > hardwareBufferSize, AudioEndpointRole)
QAudioFormat::SampleFormat SampleFormat