Check for a minimum PipeWire version
[openal-soft.git] / al / event.cpp
blobe5923c436a769079e3becb1550fcff25b0644fe6
2 #include "config.h"
4 #include "event.h"
6 #include <algorithm>
7 #include <atomic>
8 #include <cstring>
9 #include <exception>
10 #include <memory>
11 #include <mutex>
12 #include <new>
13 #include <string>
14 #include <thread>
15 #include <utility>
17 #include "AL/al.h"
18 #include "AL/alc.h"
20 #include "albyte.h"
21 #include "alc/context.h"
22 #include "alc/effects/base.h"
23 #include "alc/inprogext.h"
24 #include "almalloc.h"
25 #include "core/async_event.h"
26 #include "core/except.h"
27 #include "core/logging.h"
28 #include "core/voice_change.h"
29 #include "opthelpers.h"
30 #include "ringbuffer.h"
31 #include "threads.h"
34 static int EventThread(ALCcontext *context)
36 RingBuffer *ring{context->mAsyncEvents.get()};
37 bool quitnow{false};
38 while(likely(!quitnow))
40 auto evt_data = ring->getReadVector().first;
41 if(evt_data.len == 0)
43 context->mEventSem.wait();
44 continue;
47 std::lock_guard<std::mutex> _{context->mEventCbLock};
48 do {
49 auto *evt_ptr = reinterpret_cast<AsyncEvent*>(evt_data.buf);
50 evt_data.buf += sizeof(AsyncEvent);
51 evt_data.len -= 1;
53 AsyncEvent evt{*evt_ptr};
54 al::destroy_at(evt_ptr);
55 ring->readAdvance(1);
57 quitnow = evt.EnumType == AsyncEvent::KillThread;
58 if(unlikely(quitnow)) break;
60 if(evt.EnumType == AsyncEvent::ReleaseEffectState)
62 evt.u.mEffectState->release();
63 continue;
66 uint enabledevts{context->mEnabledEvts.load(std::memory_order_acquire)};
67 if(!context->mEventCb) continue;
69 if(evt.EnumType == AsyncEvent::SourceStateChange)
71 if(!(enabledevts&AsyncEvent::SourceStateChange))
72 continue;
73 ALuint state{};
74 std::string msg{"Source ID " + std::to_string(evt.u.srcstate.id)};
75 msg += " state has changed to ";
76 switch(evt.u.srcstate.state)
78 case AsyncEvent::SrcState::Reset:
79 msg += "AL_INITIAL";
80 state = AL_INITIAL;
81 break;
82 case AsyncEvent::SrcState::Stop:
83 msg += "AL_STOPPED";
84 state = AL_STOPPED;
85 break;
86 case AsyncEvent::SrcState::Play:
87 msg += "AL_PLAYING";
88 state = AL_PLAYING;
89 break;
90 case AsyncEvent::SrcState::Pause:
91 msg += "AL_PAUSED";
92 state = AL_PAUSED;
93 break;
95 context->mEventCb(AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT, evt.u.srcstate.id,
96 state, static_cast<ALsizei>(msg.length()), msg.c_str(), context->mEventParam);
98 else if(evt.EnumType == AsyncEvent::BufferCompleted)
100 if(!(enabledevts&AsyncEvent::BufferCompleted))
101 continue;
102 std::string msg{std::to_string(evt.u.bufcomp.count)};
103 if(evt.u.bufcomp.count == 1) msg += " buffer completed";
104 else msg += " buffers completed";
105 context->mEventCb(AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, evt.u.bufcomp.id,
106 evt.u.bufcomp.count, static_cast<ALsizei>(msg.length()), msg.c_str(),
107 context->mEventParam);
109 else if(evt.EnumType == AsyncEvent::Disconnected)
111 if(!(enabledevts&AsyncEvent::Disconnected))
112 continue;
113 context->mEventCb(AL_EVENT_TYPE_DISCONNECTED_SOFT, 0, 0,
114 static_cast<ALsizei>(strlen(evt.u.disconnect.msg)), evt.u.disconnect.msg,
115 context->mEventParam);
117 } while(evt_data.len != 0);
119 return 0;
122 void StartEventThrd(ALCcontext *ctx)
124 try {
125 ctx->mEventThread = std::thread{EventThread, ctx};
127 catch(std::exception& e) {
128 ERR("Failed to start event thread: %s\n", e.what());
130 catch(...) {
131 ERR("Failed to start event thread! Expect problems.\n");
135 void StopEventThrd(ALCcontext *ctx)
137 RingBuffer *ring{ctx->mAsyncEvents.get()};
138 auto evt_data = ring->getWriteVector().first;
139 if(evt_data.len == 0)
141 do {
142 std::this_thread::yield();
143 evt_data = ring->getWriteVector().first;
144 } while(evt_data.len == 0);
146 al::construct_at(reinterpret_cast<AsyncEvent*>(evt_data.buf), AsyncEvent::KillThread);
147 ring->writeAdvance(1);
149 ctx->mEventSem.post();
150 if(ctx->mEventThread.joinable())
151 ctx->mEventThread.join();
154 AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable)
155 START_API_FUNC
157 ContextRef context{GetContextRef()};
158 if(unlikely(!context)) return;
160 if(count < 0) context->setError(AL_INVALID_VALUE, "Controlling %d events", count);
161 if(count <= 0) return;
162 if(!types) SETERR_RETURN(context, AL_INVALID_VALUE,, "NULL pointer");
164 uint flags{0};
165 const ALenum *types_end = types+count;
166 auto bad_type = std::find_if_not(types, types_end,
167 [&flags](ALenum type) noexcept -> bool
169 if(type == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT)
170 flags |= AsyncEvent::BufferCompleted;
171 else if(type == AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT)
172 flags |= AsyncEvent::SourceStateChange;
173 else if(type == AL_EVENT_TYPE_DISCONNECTED_SOFT)
174 flags |= AsyncEvent::Disconnected;
175 else
176 return false;
177 return true;
180 if(bad_type != types_end)
181 SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid event type 0x%04x", *bad_type);
183 if(enable)
185 uint enabledevts{context->mEnabledEvts.load(std::memory_order_relaxed)};
186 while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts|flags,
187 std::memory_order_acq_rel, std::memory_order_acquire) == 0)
189 /* enabledevts is (re-)filled with the current value on failure, so
190 * just try again.
194 else
196 uint enabledevts{context->mEnabledEvts.load(std::memory_order_relaxed)};
197 while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts&~flags,
198 std::memory_order_acq_rel, std::memory_order_acquire) == 0)
201 /* Wait to ensure the event handler sees the changed flags before
202 * returning.
204 std::lock_guard<std::mutex> _{context->mEventCbLock};
207 END_API_FUNC
209 AL_API void AL_APIENTRY alEventCallbackSOFT(ALEVENTPROCSOFT callback, void *userParam)
210 START_API_FUNC
212 ContextRef context{GetContextRef()};
213 if(unlikely(!context)) return;
215 std::lock_guard<std::mutex> _{context->mPropLock};
216 std::lock_guard<std::mutex> __{context->mEventCbLock};
217 context->mEventCb = callback;
218 context->mEventParam = userParam;
220 END_API_FUNC