libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
sdlplayer.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <promeki/atomic.h>
11#include <promeki/audiodesc.h>
12#include <promeki/basicthread.h>
14#include <promeki/framerate.h>
15#include <promeki/framesync.h>
16#include <promeki/mediaio.h>
17#include <promeki/mutex.h>
18#include <promeki/namespace.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class SDLPlayerWidget;
24class SDLAudioOutput;
25class Clock;
26
62class SDLPlayerMediaIO : public DedicatedThreadMediaIO {
63 PROMEKI_OBJECT(SDLPlayerMediaIO, DedicatedThreadMediaIO)
64 friend class SDLPlayerWidget;
65
66 public:
68 using UPtr = UniquePtr<SDLPlayerMediaIO>;
69
70 ~SDLPlayerMediaIO() override;
71
73 SDLPlayerWidget *widget() const { return _widget; }
74
76 SDLAudioOutput *audioOutput() const { return _audioOutput; }
77
79 bool useAudioClock() const { return _useAudioClock; }
80
91 void pause();
92
103 void resume();
104
107
109 bool isPaused() const;
110
111 public:
112 Error describe(MediaIODescription *out) const override;
113 Error proposeInput(const MediaDesc &offered, MediaDesc *preferred) const override;
114
115 // MediaIO::close(false) (the watchdog's forced-close
116 // path) calls this from a thread that's NOT the
117 // worker — we're typically deadlocked with the
118 // worker blocked inside FrameSync::pushFrame on
119 // queue backpressure while the submitted CmdClose
120 // sits behind it in the worker queue. Interrupting
121 // FrameSync unblocks the pending pushFrame so the
122 // worker can drain to CmdClose.
123 void cancelBlockingWork() override;
124
125 protected:
126 Error executeCmd(MediaIOCommandOpen &cmd) override;
127 Error executeCmd(MediaIOCommandClose &cmd) override;
128 Error executeCmd(MediaIOCommandWrite &cmd) override;
129
130 private:
142 SDLPlayerMediaIO(SDLPlayerWidget *widget, SDLAudioOutput *audio, bool useAudioClock);
143
144 PixelFormat pickNativePixelFormat(const PixelFormat &offered) const;
145
146 // Spawns the pull thread. Caller must hold
147 // @ref _clockMutex and guarantee no thread is already
148 // running (i.e. @ref _pullThread is not joinable).
149 void startPullThread();
150
151 // Sets the running flag false, interrupts FrameSync,
152 // and joins the pull thread if any. Caller must hold
153 // @ref _clockMutex.
154 void stopPullThread();
155
156 void pullLoop();
157
158 // Owner back-pointer. The widget outlives the task
159 // (widget owns the MediaIO which owns us) so this
160 // pointer is always valid for the task's lifetime.
161 SDLPlayerWidget *_widget = nullptr;
162 SDLAudioOutput *_audioOutput = nullptr;
163
164 bool _useAudioClock = true;
165
166 // Per-open state.
167 bool _audioConfigured = false;
168 FrameRate _frameRate;
169 AudioDesc _audioDesc;
170 Clock::Ptr _clock;
171 mutable Mutex _clockMutex;
172 FrameSync _sync;
173
174 // Pull-thread state.
175 BasicThread _pullThread;
176 Atomic<bool> _pullRunning;
177 int _instanceId = 0;
178};
179
180PROMEKI_NAMESPACE_END
Manages an SDL3 audio output device for playback.
Definition sdlaudiooutput.h:64
MediaIO sink that plays frames through SDL via FrameSync.
Definition sdlplayer.h:62
bool useAudioClock() const
True when the audio device drives timing.
Definition sdlplayer.h:79
bool isPaused() const
Returns true when the playback clock is paused.
SDLPlayerWidget * widget() const
Returns the widget that owns this task.
Definition sdlplayer.h:73
void pause()
Pauses playback.
void resume()
Resumes playback after a pause.
void togglePause()
Convenience: calls pause or resume.
UniquePtr< SDLPlayerMediaIO > UPtr
Unique-ownership pointer to an SDLPlayerMediaIO.
Definition sdlplayer.h:68
SDLAudioOutput * audioOutput() const
Returns the configured audio output.
Definition sdlplayer.h:76
Widget that plays frames through an SDLPlayerMediaIO.
Definition sdlplayerwidget.h:71