Audaspace 1.6.0
A high level audio library.
Loading...
Searching...
No Matches
PipeWireDevice.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright 2009-2024 Jörg Müller
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17#pragma once
18
19#ifdef PIPEWIRE_PLUGIN
20#define AUD_BUILD_PLUGIN
21#endif
22
28
29#include <condition_variable>
30#include <thread>
31#include <pipewire/pipewire.h>
32#include <spa/utils/ringbuffer.h>
33
35
37
41class AUD_PLUGIN_API PipeWireDevice : public SoftwareDevice
42{
43private:
44 class PipeWireSynchronizer : public DefaultSynchronizer
45 {
46 PipeWireDevice* m_device;
47 bool m_playing = false;
48 bool m_get_tick_start = false;
49 int64_t m_tick_start = 0.0f;
50 double m_seek_pos = 0.0f;
51
52 public:
53 PipeWireSynchronizer(PipeWireDevice* device);
54
55 void updateTickStart();
56 virtual void play();
57 virtual void stop();
58 virtual void seek(std::shared_ptr<IHandle> handle, double time);
59 virtual double getPosition(std::shared_ptr<IHandle> handle);
60 };
61
63 PipeWireSynchronizer m_synchronizer;
64
68 bool m_fill_ringbuffer;
69
70 pw_stream* m_stream;
71 pw_thread_loop* m_thread;
72 std::unique_ptr<pw_stream_events> m_events;
73
77 std::thread m_mixingThread;
78 bool m_run_mixing_thread;
79
83 std::mutex m_mixingLock;
84
88 spa_ringbuffer m_ringbuffer;
89 Buffer m_ringbuffer_data;
90 std::condition_variable m_mixingCondition;
91
92 AUD_LOCAL static void handleStateChanged(void* device_ptr, enum pw_stream_state old, enum pw_stream_state state, const char* error);
93
97 AUD_LOCAL void updateRingBuffers();
98
103 AUD_LOCAL static void mixAudioBuffer(void* device_ptr);
104
105 // delete copy constructor and operator=
106 PipeWireDevice(const PipeWireDevice&) = delete;
107 PipeWireDevice& operator=(const PipeWireDevice&) = delete;
108
109protected:
110 virtual void playing(bool playing);
111
112public:
120 PipeWireDevice(const std::string& name, DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE);
121
126
131 static void registerPlugin();
132};
133
#define AUD_DEFAULT_BUFFER_SIZE
The default playback buffer size of a device.
Definition Audaspace.h:103
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition Audaspace.h:119
#define AUD_LOCAL
Used for hiding symbols from export in the shared library.
Definition Audaspace.h:80
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition Audaspace.h:116
#define AUD_PLUGIN_API
Used for exporting symbols in the shared library.
Definition Audaspace.h:94
The SoftwareDevice class.
This class is a simple buffer in RAM which is 32 Byte aligned and provides resize functionality.
Definition Buffer.h:34
This class is a default ISynchronizer implementation that actually does no synchronization and is int...
Definition DefaultSynchronizer.h:34
This class enables global synchronization of several audio applications if supported.
Definition ISynchronizer.h:39
PipeWireDevice(const std::string &name, DeviceSpecs specs, int buffersize=AUD_DEFAULT_BUFFER_SIZE)
Opens the PipeWire audio device for playback.
static void registerPlugin()
Registers this plugin.
virtual ~PipeWireDevice()
Closes the PipeWire audio device.
virtual void playing(bool playing)
This function tells the device, to start or pause playback.
virtual ISynchronizer * getSynchronizer()
Retrieves the synchronizer for this device, which enables accurate synchronization between audio play...
virtual std::shared_ptr< IHandle > play(std::shared_ptr< IReader > reader, bool keep=false)
Plays a sound source.
SoftwareDevice()
Empty default constructor.
Specification of a sound device.
Definition Specification.h:129