Audaspace  1.6.0
A high level audio library.
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 
29 #include <condition_variable>
30 #include <thread>
31 #include <pipewire/pipewire.h>
32 #include <spa/utils/ringbuffer.h>
33 
34 #include "devices/SoftwareDevice.h"
35 
37 
42 {
43 private:
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 
109 protected:
110  virtual void playing(bool playing);
111 
112 public:
120  PipeWireDevice(const std::string& name, DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE);
121 
125  virtual ~PipeWireDevice();
126 
127  virtual ISynchronizer* getSynchronizer();
131  static void registerPlugin();
132 };
133 
#define AUD_NAMESPACE_BEGIN
Opens the audaspace namespace aud.
Definition: Audaspace.h:116
This class enables global synchronization of several audio applications if supported.
Definition: ISynchronizer.h:38
virtual ISynchronizer * getSynchronizer()
Retrieves the synchronizer for this device, which enables accurate synchronization between audio play...
#define AUD_DEFAULT_BUFFER_SIZE
The default playback buffer size of a device.
Definition: Audaspace.h:103
virtual void playing(bool playing)=0
This function tells the device, to start or pause playback.
This class is a simple buffer in RAM which is 32 Byte aligned and provides resize functionality...
Definition: Buffer.h:33
Specification of a sound device.
Definition: Specification.h:128
This device plays back through PipeWire, the simple direct media layer.
Definition: PipeWireDevice.h:41
#define AUD_NAMESPACE_END
Closes the audaspace namespace aud.
Definition: Audaspace.h:119
#define AUD_PLUGIN_API
Used for exporting symbols in the shared library.
Definition: Audaspace.h:94
The software device is a generic device with software mixing.
Definition: SoftwareDevice.h:51
#define AUD_LOCAL
Used for hiding symbols from export in the shared library.
Definition: Audaspace.h:80
This class is a default ISynchronizer implementation that actually does no synchronization and is int...
Definition: DefaultSynchronizer.h:33
The SoftwareDevice class.