2 * An example showing how to play a stream sync'd to video, using ffmpeg.
7 #include <condition_variable>
32 _Pragma("GCC diagnostic push")
33 _Pragma("GCC diagnostic ignored \"-Wconversion\"")
34 _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
36 #include "libavcodec/avcodec.h"
37 #include "libavformat/avformat.h"
38 #include "libavformat/avio.h"
39 #include "libavformat/version.h"
40 #include "libavutil/avutil.h"
41 #include "libavutil/error.h"
42 #include "libavutil/frame.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/pixfmt.h"
45 #include "libavutil/rational.h"
46 #include "libavutil/samplefmt.h"
47 #include "libavutil/time.h"
48 #include "libavutil/version.h"
49 #include "libavutil/channel_layout.h"
50 #include "libswscale/swscale.h"
51 #include "libswresample/swresample.h"
53 constexpr auto AVNoPtsValue
= AV_NOPTS_VALUE
;
54 constexpr auto AVErrorEOF
= AVERROR_EOF
;
58 _Pragma("GCC diagnostic pop")
68 #include "common/alhelpers.h"
71 /* Undefine this to disable use of experimental extensions. Don't use for
72 * production code! Interfaces and behavior may change prior to being
75 #define ALLOW_EXPERIMENTAL_EXTS
77 #ifdef ALLOW_EXPERIMENTAL_EXTS
78 #ifndef AL_SOFT_events
79 #define AL_SOFT_events 1
80 #define AL_EVENT_CALLBACK_FUNCTION_SOFT 0x19A2
81 #define AL_EVENT_CALLBACK_USER_PARAM_SOFT 0x19A3
82 #define AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT 0x19A4
83 #define AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT 0x19A5
84 #define AL_EVENT_TYPE_DISCONNECTED_SOFT 0x19A6
85 typedef void (AL_APIENTRY
*ALEVENTPROCSOFT
)(ALenum eventType
, ALuint object
, ALuint param
,
86 ALsizei length
, const ALchar
*message
,
88 typedef void (AL_APIENTRY
*LPALEVENTCONTROLSOFT
)(ALsizei count
, const ALenum
*types
, ALboolean enable
);
89 typedef void (AL_APIENTRY
*LPALEVENTCALLBACKSOFT
)(ALEVENTPROCSOFT callback
, void *userParam
);
90 typedef void* (AL_APIENTRY
*LPALGETPOINTERSOFT
)(ALenum pname
);
91 typedef void (AL_APIENTRY
*LPALGETPOINTERVSOFT
)(ALenum pname
, void **values
);
94 #ifndef AL_SOFT_callback_buffer
95 #define AL_SOFT_callback_buffer
96 typedef unsigned int ALbitfieldSOFT
;
97 #define AL_BUFFER_CALLBACK_FUNCTION_SOFT 0x19A0
98 #define AL_BUFFER_CALLBACK_USER_PARAM_SOFT 0x19A1
99 typedef ALsizei (AL_APIENTRY
*LPALBUFFERCALLBACKTYPESOFT
)(ALvoid
*userptr
, ALvoid
*sampledata
, ALsizei numsamples
);
100 typedef void (AL_APIENTRY
*LPALBUFFERCALLBACKSOFT
)(ALuint buffer
, ALenum format
, ALsizei freq
, LPALBUFFERCALLBACKTYPESOFT callback
, ALvoid
*userptr
, ALbitfieldSOFT flags
);
101 typedef void (AL_APIENTRY
*LPALGETBUFFERPTRSOFT
)(ALuint buffer
, ALenum param
, ALvoid
**value
);
102 typedef void (AL_APIENTRY
*LPALGETBUFFER3PTRSOFT
)(ALuint buffer
, ALenum param
, ALvoid
**value1
, ALvoid
**value2
, ALvoid
**value3
);
103 typedef void (AL_APIENTRY
*LPALGETBUFFERPTRVSOFT
)(ALuint buffer
, ALenum param
, ALvoid
**values
);
105 #endif /* ALLOW_EXPERIMENTAL_EXTS */
110 inline constexpr int64_t operator "" _i64(unsigned long long int n
) noexcept
{ return static_cast<int64_t>(n
); }
113 #define M_PI (3.14159265358979323846)
116 using fixed32
= std::chrono::duration
<int64_t,std::ratio
<1,(1_i64
<<32)>>;
117 using nanoseconds
= std::chrono::nanoseconds
;
118 using microseconds
= std::chrono::microseconds
;
119 using milliseconds
= std::chrono::milliseconds
;
120 using seconds
= std::chrono::seconds
;
121 using seconds_d64
= std::chrono::duration
<double>;
122 using std::chrono::duration_cast
;
124 const std::string AppName
{"alffplay"};
126 ALenum DirectOutMode
{AL_FALSE
};
127 bool EnableWideStereo
{false};
128 bool DisableVideo
{false};
129 LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT
;
130 LPALCGETINTEGER64VSOFT alcGetInteger64vSOFT
;
132 #ifdef AL_SOFT_events
133 LPALEVENTCONTROLSOFT alEventControlSOFT
;
134 LPALEVENTCALLBACKSOFT alEventCallbackSOFT
;
137 #ifdef AL_SOFT_callback_buffer
138 LPALBUFFERCALLBACKSOFT alBufferCallbackSOFT
;
141 const seconds AVNoSyncThreshold
{10};
143 #define VIDEO_PICTURE_QUEUE_SIZE 24
145 const seconds_d64 AudioSyncThreshold
{0.03};
146 const milliseconds AudioSampleCorrectionMax
{50};
147 /* Averaging filter coefficient for audio sync. */
148 #define AUDIO_DIFF_AVG_NB 20
149 const double AudioAvgFilterCoeff
{std::pow(0.01, 1.0/AUDIO_DIFF_AVG_NB
)};
150 /* Per-buffer size, in time */
151 constexpr milliseconds AudioBufferTime
{20};
152 /* Buffer total size, in time (should be divisible by the buffer time) */
153 constexpr milliseconds AudioBufferTotalTime
{800};
154 constexpr auto AudioBufferCount
= AudioBufferTotalTime
/ AudioBufferTime
;
157 FF_MOVIE_DONE_EVENT
= SDL_USEREVENT
160 enum class SyncMaster
{
169 inline microseconds
get_avtime()
170 { return microseconds
{av_gettime()}; }
172 /* Define unique_ptrs to auto-cleanup associated ffmpeg objects. */
173 struct AVIOContextDeleter
{
174 void operator()(AVIOContext
*ptr
) { avio_closep(&ptr
); }
176 using AVIOContextPtr
= std::unique_ptr
<AVIOContext
,AVIOContextDeleter
>;
178 struct AVFormatCtxDeleter
{
179 void operator()(AVFormatContext
*ptr
) { avformat_close_input(&ptr
); }
181 using AVFormatCtxPtr
= std::unique_ptr
<AVFormatContext
,AVFormatCtxDeleter
>;
183 struct AVCodecCtxDeleter
{
184 void operator()(AVCodecContext
*ptr
) { avcodec_free_context(&ptr
); }
186 using AVCodecCtxPtr
= std::unique_ptr
<AVCodecContext
,AVCodecCtxDeleter
>;
188 struct AVFrameDeleter
{
189 void operator()(AVFrame
*ptr
) { av_frame_free(&ptr
); }
191 using AVFramePtr
= std::unique_ptr
<AVFrame
,AVFrameDeleter
>;
193 struct SwrContextDeleter
{
194 void operator()(SwrContext
*ptr
) { swr_free(&ptr
); }
196 using SwrContextPtr
= std::unique_ptr
<SwrContext
,SwrContextDeleter
>;
198 struct SwsContextDeleter
{
199 void operator()(SwsContext
*ptr
) { sws_freeContext(ptr
); }
201 using SwsContextPtr
= std::unique_ptr
<SwsContext
,SwsContextDeleter
>;
204 template<size_t SizeLimit
>
207 std::condition_variable mCondVar
;
208 std::deque
<AVPacket
> mPackets
;
209 size_t mTotalSize
{0};
210 bool mFinished
{false};
212 AVPacket
*getPacket(std::unique_lock
<std::mutex
> &lock
)
214 while(mPackets
.empty() && !mFinished
)
216 return mPackets
.empty() ? nullptr : &mPackets
.front();
221 AVPacket
*pkt
= &mPackets
.front();
222 mTotalSize
-= static_cast<unsigned int>(pkt
->size
);
223 av_packet_unref(pkt
);
224 mPackets
.pop_front();
230 for(AVPacket
&pkt
: mPackets
)
231 av_packet_unref(&pkt
);
236 int sendTo(AVCodecContext
*codecctx
)
238 std::unique_lock
<std::mutex
> lock
{mMutex
};
240 AVPacket
*pkt
{getPacket(lock
)};
241 if(!pkt
) return avcodec_send_packet(codecctx
, nullptr);
243 const int ret
{avcodec_send_packet(codecctx
, pkt
)};
244 if(ret
!= AVERROR(EAGAIN
))
247 std::cerr
<< "Failed to send packet: "<<ret
<<std::endl
;
256 std::lock_guard
<std::mutex
> _
{mMutex
};
259 mCondVar
.notify_one();
262 bool put(const AVPacket
*pkt
)
265 std::unique_lock
<std::mutex
> lock
{mMutex
};
266 if(mTotalSize
>= SizeLimit
)
269 mPackets
.push_back(AVPacket
{});
270 if(av_packet_ref(&mPackets
.back(), pkt
) != 0)
276 mTotalSize
+= static_cast<unsigned int>(mPackets
.back().size
);
278 mCondVar
.notify_one();
289 AVStream
*mStream
{nullptr};
290 AVCodecCtxPtr mCodecCtx
;
292 PacketQueue
<2*1024*1024> mPackets
;
294 /* Used for clock difference average computation */
295 seconds_d64 mClockDiffAvg
{0};
297 /* Time of the next sample to be buffered */
298 nanoseconds mCurrentPts
{0};
300 /* Device clock time that the stream started at. */
301 nanoseconds mDeviceStartTime
{nanoseconds::min()};
303 /* Decompressed sample frame, and swresample context for conversion */
304 AVFramePtr mDecodedFrame
;
305 SwrContextPtr mSwresCtx
;
307 /* Conversion format, for what gets fed to OpenAL */
308 uint64_t mDstChanLayout
{0};
309 AVSampleFormat mDstSampleFmt
{AV_SAMPLE_FMT_NONE
};
311 /* Storage of converted samples */
312 uint8_t *mSamples
{nullptr};
313 int mSamplesLen
{0}; /* In samples */
317 std::unique_ptr
<uint8_t[]> mBufferData
;
318 size_t mBufferDataSize
{0};
319 std::atomic
<size_t> mReadPos
{0};
320 std::atomic
<size_t> mWritePos
{0};
323 ALenum mFormat
{AL_NONE
};
324 ALuint mFrameSize
{0};
326 std::mutex mSrcMutex
;
327 std::condition_variable mSrcCond
;
328 std::atomic_flag mConnected
;
330 std::array
<ALuint
,AudioBufferCount
> mBuffers
{};
331 ALuint mBufferIdx
{0};
333 AudioState(MovieState
&movie
) : mMovie(movie
)
334 { mConnected
.test_and_set(std::memory_order_relaxed
); }
338 alDeleteSources(1, &mSource
);
340 alDeleteBuffers(static_cast<ALsizei
>(mBuffers
.size()), mBuffers
.data());
345 #ifdef AL_SOFT_events
346 static void AL_APIENTRY
EventCallback(ALenum eventType
, ALuint object
, ALuint param
,
347 ALsizei length
, const ALchar
*message
, void *userParam
);
349 #ifdef AL_SOFT_callback_buffer
350 static ALsizei AL_APIENTRY
bufferCallbackC(void *userptr
, void *data
, ALsizei size
)
351 { return static_cast<AudioState
*>(userptr
)->bufferCallback(data
, size
); }
352 ALsizei
bufferCallback(void *data
, ALsizei size
);
355 nanoseconds
getClockNoLock();
356 nanoseconds
getClock()
358 std::lock_guard
<std::mutex
> lock
{mSrcMutex
};
359 return getClockNoLock();
362 bool startPlayback();
366 bool readAudio(uint8_t *samples
, unsigned int length
, int &sample_skip
);
367 void readAudio(int sample_skip
);
375 AVStream
*mStream
{nullptr};
376 AVCodecCtxPtr mCodecCtx
;
378 PacketQueue
<14*1024*1024> mPackets
;
380 /* The pts of the currently displayed frame, and the time (av_gettime) it
381 * was last updated - used to have running video pts
383 nanoseconds mDisplayPts
{0};
384 microseconds mDisplayPtsTime
{microseconds::min()};
385 std::mutex mDispPtsMutex
;
387 /* Swscale context for format conversion */
388 SwsContextPtr mSwscaleCtx
;
392 nanoseconds mPts
{nanoseconds::min()};
394 std::array
<Picture
,VIDEO_PICTURE_QUEUE_SIZE
> mPictQ
;
395 std::atomic
<size_t> mPictQRead
{0u}, mPictQWrite
{1u};
396 std::mutex mPictQMutex
;
397 std::condition_variable mPictQCond
;
399 SDL_Texture
*mImage
{nullptr};
400 int mWidth
{0}, mHeight
{0}; /* Logical image size (actual size may be larger) */
401 bool mFirstUpdate
{true};
403 std::atomic
<bool> mEOS
{false};
404 std::atomic
<bool> mFinalUpdate
{false};
406 VideoState(MovieState
&movie
) : mMovie(movie
) { }
410 SDL_DestroyTexture(mImage
);
414 nanoseconds
getClock();
416 void display(SDL_Window
*screen
, SDL_Renderer
*renderer
);
417 void updateVideo(SDL_Window
*screen
, SDL_Renderer
*renderer
, bool redraw
);
422 AVIOContextPtr mIOContext
;
423 AVFormatCtxPtr mFormatCtx
;
425 SyncMaster mAVSyncType
{SyncMaster::Default
};
427 microseconds mClockBase
{microseconds::min()};
429 std::atomic
<bool> mQuit
{false};
434 std::thread mParseThread
;
435 std::thread mAudioThread
;
436 std::thread mVideoThread
;
438 std::string mFilename
;
440 MovieState(std::string fname
)
441 : mAudio(*this), mVideo(*this), mFilename(std::move(fname
))
446 if(mParseThread
.joinable())
450 static int decode_interrupt_cb(void *ctx
);
452 void setTitle(SDL_Window
*window
);
454 nanoseconds
getClock();
456 nanoseconds
getMasterClock();
458 nanoseconds
getDuration();
460 int streamComponentOpen(unsigned int stream_index
);
465 nanoseconds
AudioState::getClockNoLock()
467 // The audio clock is the timestamp of the sample currently being heard.
468 if(alcGetInteger64vSOFT
)
470 // If device start time = min, we aren't playing yet.
471 if(mDeviceStartTime
== nanoseconds::min())
472 return nanoseconds::zero();
474 // Get the current device clock time and latency.
475 auto device
= alcGetContextsDevice(alcGetCurrentContext());
476 ALCint64SOFT devtimes
[2]{0,0};
477 alcGetInteger64vSOFT(device
, ALC_DEVICE_CLOCK_LATENCY_SOFT
, 2, devtimes
);
478 auto latency
= nanoseconds
{devtimes
[1]};
479 auto device_time
= nanoseconds
{devtimes
[0]};
481 // The clock is simply the current device time relative to the recorded
482 // start time. We can also subtract the latency to get more a accurate
483 // position of where the audio device actually is in the output stream.
484 return device_time
- mDeviceStartTime
- latency
;
487 if(mBufferDataSize
> 0)
489 if(mDeviceStartTime
== nanoseconds::min())
490 return nanoseconds::zero();
492 /* With a callback buffer and no device clock, mDeviceStartTime is
493 * actually the timestamp of the first sample frame played. The audio
494 * clock, then, is that plus the current source offset.
496 ALint64SOFT offset
[2];
497 if(alGetSourcei64vSOFT
)
498 alGetSourcei64vSOFT(mSource
, AL_SAMPLE_OFFSET_LATENCY_SOFT
, offset
);
502 alGetSourcei(mSource
, AL_SAMPLE_OFFSET
, &ioffset
);
503 offset
[0] = ALint64SOFT
{ioffset
} << 32;
506 /* NOTE: The source state must be checked last, in case an underrun
507 * occurs and the source stops between getting the state and retrieving
508 * the offset+latency.
511 alGetSourcei(mSource
, AL_SOURCE_STATE
, &status
);
514 if(status
== AL_PLAYING
|| status
== AL_PAUSED
)
515 pts
= mDeviceStartTime
- nanoseconds
{offset
[1]} +
516 duration_cast
<nanoseconds
>(fixed32
{offset
[0] / mCodecCtx
->sample_rate
});
519 /* If the source is stopped, the pts of the next sample to be heard
520 * is the pts of the next sample to be buffered, minus the amount
521 * already in the buffer ready to play.
523 const size_t woffset
{mWritePos
.load(std::memory_order_acquire
)};
524 const size_t roffset
{mReadPos
.load(std::memory_order_relaxed
)};
525 const size_t readable
{((woffset
>= roffset
) ? woffset
: (mBufferDataSize
+woffset
)) -
528 pts
= mCurrentPts
- nanoseconds
{seconds
{readable
/mFrameSize
}}/mCodecCtx
->sample_rate
;
534 /* The source-based clock is based on 4 components:
535 * 1 - The timestamp of the next sample to buffer (mCurrentPts)
536 * 2 - The length of the source's buffer queue
537 * (AudioBufferTime*AL_BUFFERS_QUEUED)
538 * 3 - The offset OpenAL is currently at in the source (the first value
539 * from AL_SAMPLE_OFFSET_LATENCY_SOFT)
540 * 4 - The latency between OpenAL and the DAC (the second value from
541 * AL_SAMPLE_OFFSET_LATENCY_SOFT)
543 * Subtracting the length of the source queue from the next sample's
544 * timestamp gives the timestamp of the sample at the start of the source
545 * queue. Adding the source offset to that results in the timestamp for the
546 * sample at OpenAL's current position, and subtracting the source latency
547 * from that gives the timestamp of the sample currently at the DAC.
549 nanoseconds pts
{mCurrentPts
};
552 ALint64SOFT offset
[2];
553 if(alGetSourcei64vSOFT
)
554 alGetSourcei64vSOFT(mSource
, AL_SAMPLE_OFFSET_LATENCY_SOFT
, offset
);
558 alGetSourcei(mSource
, AL_SAMPLE_OFFSET
, &ioffset
);
559 offset
[0] = ALint64SOFT
{ioffset
} << 32;
562 ALint queued
, status
;
563 alGetSourcei(mSource
, AL_BUFFERS_QUEUED
, &queued
);
564 alGetSourcei(mSource
, AL_SOURCE_STATE
, &status
);
566 /* If the source is AL_STOPPED, then there was an underrun and all
567 * buffers are processed, so ignore the source queue. The audio thread
568 * will put the source into an AL_INITIAL state and clear the queue
569 * when it starts recovery.
571 if(status
!= AL_STOPPED
)
573 pts
-= AudioBufferTime
*queued
;
574 pts
+= duration_cast
<nanoseconds
>(fixed32
{offset
[0] / mCodecCtx
->sample_rate
});
576 /* Don't offset by the latency if the source isn't playing. */
577 if(status
== AL_PLAYING
)
578 pts
-= nanoseconds
{offset
[1]};
581 return std::max(pts
, nanoseconds::zero());
584 bool AudioState::startPlayback()
586 const size_t woffset
{mWritePos
.load(std::memory_order_acquire
)};
587 const size_t roffset
{mReadPos
.load(std::memory_order_relaxed
)};
588 const size_t readable
{((woffset
>= roffset
) ? woffset
: (mBufferDataSize
+woffset
)) -
591 if(mBufferDataSize
> 0)
595 if(!alcGetInteger64vSOFT
)
596 mDeviceStartTime
= mCurrentPts
-
597 nanoseconds
{seconds
{readable
/mFrameSize
}}/mCodecCtx
->sample_rate
;
602 alGetSourcei(mSource
, AL_BUFFERS_QUEUED
, &queued
);
603 if(queued
== 0) return false;
606 alSourcePlay(mSource
);
607 if(alcGetInteger64vSOFT
)
609 /* Subtract the total buffer queue time from the current pts to get the
610 * pts of the start of the queue.
612 int64_t srctimes
[2]{0,0};
613 alGetSourcei64vSOFT(mSource
, AL_SAMPLE_OFFSET_CLOCK_SOFT
, srctimes
);
614 auto device_time
= nanoseconds
{srctimes
[1]};
615 auto src_offset
= duration_cast
<nanoseconds
>(fixed32
{srctimes
[0]}) /
616 mCodecCtx
->sample_rate
;
618 /* The mixer may have ticked and incremented the device time and sample
619 * offset, so subtract the source offset from the device time to get
620 * the device time the source started at. Also subtract startpts to get
621 * the device time the stream would have started at to reach where it
624 if(mBufferDataSize
> 0)
626 nanoseconds startpts
{mCurrentPts
-
627 nanoseconds
{seconds
{readable
/mFrameSize
}}/mCodecCtx
->sample_rate
};
628 mDeviceStartTime
= device_time
- src_offset
- startpts
;
632 nanoseconds startpts
{mCurrentPts
- AudioBufferTotalTime
};
633 mDeviceStartTime
= device_time
- src_offset
- startpts
;
639 int AudioState::getSync()
641 if(mMovie
.mAVSyncType
== SyncMaster::Audio
)
644 auto ref_clock
= mMovie
.getMasterClock();
645 auto diff
= ref_clock
- getClockNoLock();
647 if(!(diff
< AVNoSyncThreshold
&& diff
> -AVNoSyncThreshold
))
649 /* Difference is TOO big; reset accumulated average */
650 mClockDiffAvg
= seconds_d64::zero();
654 /* Accumulate the diffs */
655 mClockDiffAvg
= mClockDiffAvg
*AudioAvgFilterCoeff
+ diff
;
656 auto avg_diff
= mClockDiffAvg
*(1.0 - AudioAvgFilterCoeff
);
657 if(avg_diff
< AudioSyncThreshold
/2.0 && avg_diff
> -AudioSyncThreshold
)
660 /* Constrain the per-update difference to avoid exceedingly large skips */
661 diff
= std::min
<nanoseconds
>(diff
, AudioSampleCorrectionMax
);
662 return static_cast<int>(duration_cast
<seconds
>(diff
*mCodecCtx
->sample_rate
).count());
665 int AudioState::decodeFrame()
667 while(!mMovie
.mQuit
.load(std::memory_order_relaxed
))
670 while((ret
=avcodec_receive_frame(mCodecCtx
.get(), mDecodedFrame
.get())) == AVERROR(EAGAIN
))
671 mPackets
.sendTo(mCodecCtx
.get());
674 if(ret
== AVErrorEOF
) break;
675 std::cerr
<< "Failed to receive frame: "<<ret
<<std::endl
;
679 if(mDecodedFrame
->nb_samples
<= 0)
682 /* If provided, update w/ pts */
683 if(mDecodedFrame
->best_effort_timestamp
!= AVNoPtsValue
)
684 mCurrentPts
= duration_cast
<nanoseconds
>(seconds_d64
{av_q2d(mStream
->time_base
) *
685 static_cast<double>(mDecodedFrame
->best_effort_timestamp
)});
687 if(mDecodedFrame
->nb_samples
> mSamplesMax
)
690 av_samples_alloc(&mSamples
, nullptr, mCodecCtx
->channels
, mDecodedFrame
->nb_samples
,
692 mSamplesMax
= mDecodedFrame
->nb_samples
;
694 /* Return the amount of sample frames converted */
695 int data_size
{swr_convert(mSwresCtx
.get(), &mSamples
, mDecodedFrame
->nb_samples
,
696 const_cast<const uint8_t**>(mDecodedFrame
->data
), mDecodedFrame
->nb_samples
)};
698 av_frame_unref(mDecodedFrame
.get());
705 /* Duplicates the sample at in to out, count times. The frame size is a
706 * multiple of the template type size.
709 static void sample_dup(uint8_t *out
, const uint8_t *in
, size_t count
, size_t frame_size
)
711 auto *sample
= reinterpret_cast<const T
*>(in
);
712 auto *dst
= reinterpret_cast<T
*>(out
);
713 if(frame_size
== sizeof(T
))
714 std::fill_n(dst
, count
, *sample
);
717 /* NOTE: frame_size is a multiple of sizeof(T). */
718 size_t type_mult
{frame_size
/ sizeof(T
)};
720 std::generate_n(dst
, count
*type_mult
,
721 [sample
,type_mult
,&i
]() -> T
732 bool AudioState::readAudio(uint8_t *samples
, unsigned int length
, int &sample_skip
)
734 unsigned int audio_size
{0};
736 /* Read the next chunk of data, refill the buffer, and queue it
738 length
/= mFrameSize
;
739 while(mSamplesLen
> 0 && audio_size
< length
)
741 unsigned int rem
{length
- audio_size
};
744 const auto len
= static_cast<unsigned int>(mSamplesLen
- mSamplesPos
);
745 if(rem
> len
) rem
= len
;
746 std::copy_n(mSamples
+ static_cast<unsigned int>(mSamplesPos
)*mFrameSize
,
747 rem
*mFrameSize
, samples
);
751 rem
= std::min(rem
, static_cast<unsigned int>(-mSamplesPos
));
753 /* Add samples by copying the first sample */
754 if((mFrameSize
&7) == 0)
755 sample_dup
<uint64_t>(samples
, mSamples
, rem
, mFrameSize
);
756 else if((mFrameSize
&3) == 0)
757 sample_dup
<uint32_t>(samples
, mSamples
, rem
, mFrameSize
);
758 else if((mFrameSize
&1) == 0)
759 sample_dup
<uint16_t>(samples
, mSamples
, rem
, mFrameSize
);
761 sample_dup
<uint8_t>(samples
, mSamples
, rem
, mFrameSize
);
765 mCurrentPts
+= nanoseconds
{seconds
{rem
}} / mCodecCtx
->sample_rate
;
766 samples
+= rem
*mFrameSize
;
769 while(mSamplesPos
>= mSamplesLen
)
771 int frame_len
= decodeFrame();
772 if(frame_len
<= 0) break;
774 mSamplesLen
= frame_len
;
775 mSamplesPos
= std::min(mSamplesLen
, sample_skip
);
776 sample_skip
-= mSamplesPos
;
778 // Adjust the device start time and current pts by the amount we're
779 // skipping/duplicating, so that the clock remains correct for the
780 // current stream position.
781 auto skip
= nanoseconds
{seconds
{mSamplesPos
}} / mCodecCtx
->sample_rate
;
782 mDeviceStartTime
-= skip
;
790 if(audio_size
< length
)
792 const unsigned int rem
{length
- audio_size
};
793 std::fill_n(samples
, rem
*mFrameSize
,
794 (mDstSampleFmt
== AV_SAMPLE_FMT_U8
) ? 0x80 : 0x00);
795 mCurrentPts
+= nanoseconds
{seconds
{rem
}} / mCodecCtx
->sample_rate
;
801 void AudioState::readAudio(int sample_skip
)
803 size_t woffset
{mWritePos
.load(std::memory_order_acquire
)};
804 while(mSamplesLen
> 0)
806 const size_t roffset
{mReadPos
.load(std::memory_order_relaxed
)};
810 size_t rem
{(((roffset
> woffset
) ? roffset
-1
811 : ((roffset
== 0) ? mBufferDataSize
-1
812 : mBufferDataSize
)) - woffset
) / mFrameSize
};
813 rem
= std::min
<size_t>(rem
, static_cast<ALuint
>(-mSamplesPos
));
816 auto *splout
{&mBufferData
[woffset
]};
817 if((mFrameSize
&7) == 0)
818 sample_dup
<uint64_t>(splout
, mSamples
, rem
, mFrameSize
);
819 else if((mFrameSize
&3) == 0)
820 sample_dup
<uint32_t>(splout
, mSamples
, rem
, mFrameSize
);
821 else if((mFrameSize
&1) == 0)
822 sample_dup
<uint16_t>(splout
, mSamples
, rem
, mFrameSize
);
824 sample_dup
<uint8_t>(splout
, mSamples
, rem
, mFrameSize
);
825 woffset
+= rem
* mFrameSize
;
826 if(woffset
== mBufferDataSize
)
828 mWritePos
.store(woffset
, std::memory_order_release
);
829 mSamplesPos
+= static_cast<int>(rem
);
830 mCurrentPts
+= nanoseconds
{seconds
{rem
}} / mCodecCtx
->sample_rate
;
834 const size_t boffset
{static_cast<ALuint
>(mSamplesPos
) * size_t{mFrameSize
}};
835 const size_t nbytes
{static_cast<ALuint
>(mSamplesLen
)*size_t{mFrameSize
} -
837 if(roffset
> woffset
)
839 const size_t writable
{roffset
-woffset
-1};
840 if(writable
< nbytes
) break;
842 memcpy(&mBufferData
[woffset
], mSamples
+boffset
, nbytes
);
847 const size_t writable
{mBufferDataSize
+roffset
-woffset
-1};
848 if(writable
< nbytes
) break;
850 const size_t todo1
{std::min
<size_t>(nbytes
, mBufferDataSize
-woffset
)};
851 const size_t todo2
{nbytes
- todo1
};
853 memcpy(&mBufferData
[woffset
], mSamples
+boffset
, todo1
);
855 if(woffset
== mBufferDataSize
)
860 memcpy(&mBufferData
[woffset
], mSamples
+boffset
+todo1
, todo2
);
865 mWritePos
.store(woffset
, std::memory_order_release
);
866 mCurrentPts
+= nanoseconds
{seconds
{mSamplesLen
-mSamplesPos
}} / mCodecCtx
->sample_rate
;
869 mSamplesLen
= decodeFrame();
870 if(mSamplesLen
<= 0) break;
872 mSamplesPos
= std::min(mSamplesLen
, sample_skip
);
873 sample_skip
-= mSamplesPos
;
875 auto skip
= nanoseconds
{seconds
{mSamplesPos
}} / mCodecCtx
->sample_rate
;
876 mDeviceStartTime
-= skip
;
878 } while(mSamplesPos
>= mSamplesLen
);
883 #ifdef AL_SOFT_events
884 void AL_APIENTRY
AudioState::EventCallback(ALenum eventType
, ALuint object
, ALuint param
,
885 ALsizei length
, const ALchar
*message
, void *userParam
)
887 auto self
= static_cast<AudioState
*>(userParam
);
889 if(eventType
== AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
)
891 /* Temporarily lock the source mutex to ensure it's not between
892 * checking the processed count and going to sleep.
894 std::unique_lock
<std::mutex
>{self
->mSrcMutex
}.unlock();
895 self
->mSrcCond
.notify_one();
899 std::cout
<< "\n---- AL Event on AudioState "<<self
<<" ----\nEvent: ";
902 case AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
: std::cout
<< "Buffer completed"; break;
903 case AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT
: std::cout
<< "Source state changed"; break;
904 case AL_EVENT_TYPE_DISCONNECTED_SOFT
: std::cout
<< "Disconnected"; break;
906 std::cout
<< "0x"<<std::hex
<<std::setw(4)<<std::setfill('0')<<eventType
<<std::dec
<<
907 std::setw(0)<<std::setfill(' '); break;
910 "Object ID: "<<object
<<"\n"
911 "Parameter: "<<param
<<"\n"
912 "Message: "<<std::string
{message
, static_cast<ALuint
>(length
)}<<"\n----"<<
915 if(eventType
== AL_EVENT_TYPE_DISCONNECTED_SOFT
)
918 std::lock_guard
<std::mutex
> lock
{self
->mSrcMutex
};
919 self
->mConnected
.clear(std::memory_order_release
);
921 self
->mSrcCond
.notify_one();
926 #ifdef AL_SOFT_callback_buffer
927 ALsizei
AudioState::bufferCallback(void *data
, ALsizei size
)
931 size_t roffset
{mReadPos
.load(std::memory_order_acquire
)};
934 const size_t woffset
{mWritePos
.load(std::memory_order_relaxed
)};
935 if(woffset
== roffset
) break;
937 size_t todo
{((woffset
< roffset
) ? mBufferDataSize
: woffset
) - roffset
};
938 todo
= std::min
<size_t>(todo
, static_cast<ALuint
>(size
-got
));
940 memcpy(data
, &mBufferData
[roffset
], todo
);
941 data
= static_cast<ALbyte
*>(data
) + todo
;
942 got
+= static_cast<ALsizei
>(todo
);
945 if(roffset
== mBufferDataSize
)
948 mReadPos
.store(roffset
, std::memory_order_release
);
954 int AudioState::handler()
956 std::unique_lock
<std::mutex
> srclock
{mSrcMutex
, std::defer_lock
};
957 milliseconds sleep_time
{AudioBufferTime
/ 3};
960 #ifdef AL_SOFT_events
961 const std::array
<ALenum
,3> evt_types
{{
962 AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
, AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT
,
963 AL_EVENT_TYPE_DISCONNECTED_SOFT
}};
964 if(alEventControlSOFT
)
966 alEventControlSOFT(evt_types
.size(), evt_types
.data(), AL_TRUE
);
967 alEventCallbackSOFT(EventCallback
, this);
968 sleep_time
= AudioBufferTotalTime
;
971 #ifdef AL_SOFT_bformat_ex
972 const bool has_bfmt_ex
{alIsExtensionPresent("AL_SOFT_bformat_ex") != AL_FALSE
};
973 ALenum ambi_layout
{AL_FUMA_SOFT
};
974 ALenum ambi_scale
{AL_FUMA_SOFT
};
977 /* Find a suitable format for OpenAL. */
980 if((mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_FLT
|| mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_FLTP
) &&
981 alIsExtensionPresent("AL_EXT_FLOAT32"))
983 mDstSampleFmt
= AV_SAMPLE_FMT_FLT
;
985 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
986 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
987 (fmt
=alGetEnumValue("AL_FORMAT_71CHN32")) != AL_NONE
&& fmt
!= -1)
989 mDstChanLayout
= mCodecCtx
->channel_layout
;
993 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
994 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
995 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
996 (fmt
=alGetEnumValue("AL_FORMAT_51CHN32")) != AL_NONE
&& fmt
!= -1)
998 mDstChanLayout
= mCodecCtx
->channel_layout
;
1002 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
1004 mDstChanLayout
= mCodecCtx
->channel_layout
;
1006 mFormat
= AL_FORMAT_MONO_FLOAT32
;
1008 /* Assume 3D B-Format (ambisonics) if the channel layout is blank and
1009 * there's 4 or more channels. FFmpeg/libavcodec otherwise seems to
1010 * have no way to specify if the source is actually B-Format (let alone
1011 * if it's 2D or 3D).
1013 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
1014 alIsExtensionPresent("AL_EXT_BFORMAT") &&
1015 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D_FLOAT32")) != AL_NONE
&& fmt
!= -1)
1017 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
1018 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
1019 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
1021 /* OpenAL only supports first-order with AL_EXT_BFORMAT, which
1022 * is 4 channels for 3D buffers.
1030 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
1032 mFormat
= AL_FORMAT_STEREO_FLOAT32
;
1035 if(mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_U8
|| mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_U8P
)
1037 mDstSampleFmt
= AV_SAMPLE_FMT_U8
;
1039 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
1040 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
1041 (fmt
=alGetEnumValue("AL_FORMAT_71CHN8")) != AL_NONE
&& fmt
!= -1)
1043 mDstChanLayout
= mCodecCtx
->channel_layout
;
1047 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
1048 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
1049 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
1050 (fmt
=alGetEnumValue("AL_FORMAT_51CHN8")) != AL_NONE
&& fmt
!= -1)
1052 mDstChanLayout
= mCodecCtx
->channel_layout
;
1056 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
1058 mDstChanLayout
= mCodecCtx
->channel_layout
;
1060 mFormat
= AL_FORMAT_MONO8
;
1062 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
1063 alIsExtensionPresent("AL_EXT_BFORMAT") &&
1064 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D8")) != AL_NONE
&& fmt
!= -1)
1066 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
1067 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
1068 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
1076 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
1078 mFormat
= AL_FORMAT_STEREO8
;
1083 mDstSampleFmt
= AV_SAMPLE_FMT_S16
;
1085 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
1086 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
1087 (fmt
=alGetEnumValue("AL_FORMAT_71CHN16")) != AL_NONE
&& fmt
!= -1)
1089 mDstChanLayout
= mCodecCtx
->channel_layout
;
1093 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
1094 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
1095 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
1096 (fmt
=alGetEnumValue("AL_FORMAT_51CHN16")) != AL_NONE
&& fmt
!= -1)
1098 mDstChanLayout
= mCodecCtx
->channel_layout
;
1102 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
1104 mDstChanLayout
= mCodecCtx
->channel_layout
;
1106 mFormat
= AL_FORMAT_MONO16
;
1108 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
1109 alIsExtensionPresent("AL_EXT_BFORMAT") &&
1110 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D16")) != AL_NONE
&& fmt
!= -1)
1112 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
1113 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
1114 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
1122 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
1124 mFormat
= AL_FORMAT_STEREO16
;
1127 void *samples
{nullptr};
1128 ALsizei buffer_len
{0};
1135 mDecodedFrame
.reset(av_frame_alloc());
1138 std::cerr
<< "Failed to allocate audio frame" <<std::endl
;
1144 /* OpenAL only supports first-order ambisonics with AL_EXT_BFORMAT, so
1145 * we have to drop any extra channels.
1147 mSwresCtx
.reset(swr_alloc_set_opts(nullptr,
1148 (1_i64
<<4)-1, mDstSampleFmt
, mCodecCtx
->sample_rate
,
1149 (1_i64
<<mCodecCtx
->channels
)-1, mCodecCtx
->sample_fmt
, mCodecCtx
->sample_rate
,
1152 /* Note that ffmpeg/libavcodec has no method to check the ambisonic
1153 * channel order and normalization, so we can only assume AmbiX as the
1154 * defacto-standard. This is not true for .amb files, which use FuMa.
1156 std::vector
<double> mtx(64*64, 0.0);
1157 #ifdef AL_SOFT_bformat_ex
1158 ambi_layout
= AL_ACN_SOFT
;
1159 ambi_scale
= AL_SN3D_SOFT
;
1162 /* An identity matrix that doesn't remix any channels. */
1163 std::cout
<< "Found AL_SOFT_bformat_ex" <<std::endl
;
1164 mtx
[0 + 0*64] = 1.0;
1165 mtx
[1 + 1*64] = 1.0;
1166 mtx
[2 + 2*64] = 1.0;
1167 mtx
[3 + 3*64] = 1.0;
1172 std::cout
<< "Found AL_EXT_BFORMAT" <<std::endl
;
1173 /* Without AL_SOFT_bformat_ex, OpenAL only supports FuMa channel
1174 * ordering and normalization, so a custom matrix is needed to
1175 * scale and reorder the source from AmbiX.
1177 mtx
[0 + 0*64] = std::sqrt(0.5);
1178 mtx
[3 + 1*64] = 1.0;
1179 mtx
[1 + 2*64] = 1.0;
1180 mtx
[2 + 3*64] = 1.0;
1182 swr_set_matrix(mSwresCtx
.get(), mtx
.data(), 64);
1185 mSwresCtx
.reset(swr_alloc_set_opts(nullptr,
1186 static_cast<int64_t>(mDstChanLayout
), mDstSampleFmt
, mCodecCtx
->sample_rate
,
1187 mCodecCtx
->channel_layout
? static_cast<int64_t>(mCodecCtx
->channel_layout
)
1188 : av_get_default_channel_layout(mCodecCtx
->channels
),
1189 mCodecCtx
->sample_fmt
, mCodecCtx
->sample_rate
,
1191 if(!mSwresCtx
|| swr_init(mSwresCtx
.get()) != 0)
1193 std::cerr
<< "Failed to initialize audio converter" <<std::endl
;
1197 alGenBuffers(static_cast<ALsizei
>(mBuffers
.size()), mBuffers
.data());
1198 alGenSources(1, &mSource
);
1201 alSourcei(mSource
, AL_DIRECT_CHANNELS_SOFT
, DirectOutMode
);
1202 if(EnableWideStereo
)
1204 const float angles
[2]{static_cast<float>(M_PI
/ 3.0), static_cast<float>(-M_PI
/ 3.0)};
1205 alSourcefv(mSource
, AL_STEREO_ANGLES
, angles
);
1207 #ifdef AL_SOFT_bformat_ex
1210 for(ALuint bufid
: mBuffers
)
1212 alBufferi(bufid
, AL_AMBISONIC_LAYOUT_SOFT
, ambi_layout
);
1213 alBufferi(bufid
, AL_AMBISONIC_SCALING_SOFT
, ambi_scale
);
1218 if(alGetError() != AL_NO_ERROR
)
1221 #ifdef AL_SOFT_callback_buffer
1222 if(alBufferCallbackSOFT
)
1224 alBufferCallbackSOFT(mBuffers
[0], mFormat
, mCodecCtx
->sample_rate
, bufferCallbackC
, this,
1226 alSourcei(mSource
, AL_BUFFER
, static_cast<ALint
>(mBuffers
[0]));
1227 if(alGetError() != AL_NO_ERROR
)
1229 fprintf(stderr
, "Failed to set buffer callback\n");
1230 alSourcei(mSource
, AL_BUFFER
, 0);
1231 buffer_len
= static_cast<int>(duration_cast
<seconds
>(mCodecCtx
->sample_rate
*
1232 AudioBufferTime
).count() * mFrameSize
);
1236 mBufferDataSize
= static_cast<size_t>(duration_cast
<seconds
>(mCodecCtx
->sample_rate
*
1237 AudioBufferTotalTime
).count()) * mFrameSize
;
1238 mBufferData
.reset(new uint8_t[mBufferDataSize
]);
1239 mReadPos
.store(0, std::memory_order_relaxed
);
1240 mWritePos
.store(0, std::memory_order_relaxed
);
1243 alcGetIntegerv(alcGetContextsDevice(alcGetCurrentContext()), ALC_REFRESH
, 1, &refresh
);
1244 sleep_time
= milliseconds
{seconds
{1}} / refresh
;
1249 buffer_len
= static_cast<int>(duration_cast
<seconds
>(mCodecCtx
->sample_rate
*
1250 AudioBufferTime
).count() * mFrameSize
);
1252 samples
= av_malloc(static_cast<ALuint
>(buffer_len
));
1254 /* Prefill the codec buffer. */
1256 const int ret
{mPackets
.sendTo(mCodecCtx
.get())};
1257 if(ret
== AVERROR(EAGAIN
) || ret
== AVErrorEOF
)
1262 if(alcGetInteger64vSOFT
)
1265 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()), ALC_DEVICE_CLOCK_SOFT
,
1267 mDeviceStartTime
= nanoseconds
{devtime
} - mCurrentPts
;
1270 mSamplesLen
= decodeFrame();
1273 mSamplesPos
= std::min(mSamplesLen
, getSync());
1275 auto skip
= nanoseconds
{seconds
{mSamplesPos
}} / mCodecCtx
->sample_rate
;
1276 mDeviceStartTime
-= skip
;
1277 mCurrentPts
+= skip
;
1280 while(!mMovie
.mQuit
.load(std::memory_order_relaxed
)
1281 && mConnected
.test_and_set(std::memory_order_relaxed
))
1284 if(mBufferDataSize
> 0)
1286 alGetSourcei(mSource
, AL_SOURCE_STATE
, &state
);
1287 readAudio(getSync());
1291 ALint processed
, queued
;
1293 /* First remove any processed buffers. */
1294 alGetSourcei(mSource
, AL_BUFFERS_PROCESSED
, &processed
);
1295 while(processed
> 0)
1298 alSourceUnqueueBuffers(mSource
, 1, &bid
);
1302 /* Refill the buffer queue. */
1303 int sync_skip
{getSync()};
1304 alGetSourcei(mSource
, AL_BUFFERS_QUEUED
, &queued
);
1305 while(static_cast<ALuint
>(queued
) < mBuffers
.size())
1307 /* Read the next chunk of data, filling the buffer, and queue
1310 const bool got_audio
{readAudio(static_cast<uint8_t*>(samples
),
1311 static_cast<ALuint
>(buffer_len
), sync_skip
)};
1312 if(!got_audio
) break;
1314 const ALuint bufid
{mBuffers
[mBufferIdx
]};
1315 mBufferIdx
= static_cast<ALuint
>((mBufferIdx
+1) % mBuffers
.size());
1317 alBufferData(bufid
, mFormat
, samples
, buffer_len
, mCodecCtx
->sample_rate
);
1318 alSourceQueueBuffers(mSource
, 1, &bufid
);
1322 /* Check that the source is playing. */
1323 alGetSourcei(mSource
, AL_SOURCE_STATE
, &state
);
1324 if(state
== AL_STOPPED
)
1326 /* AL_STOPPED means there was an underrun. Clear the buffer
1327 * queue since this likely means we're late, and rewind the
1328 * source to get it back into an AL_INITIAL state.
1330 alSourceRewind(mSource
);
1331 alSourcei(mSource
, AL_BUFFER
, 0);
1332 if(alcGetInteger64vSOFT
)
1334 /* Also update the device start time with the current
1335 * device clock, so the decoder knows we're running behind.
1338 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()),
1339 ALC_DEVICE_CLOCK_SOFT
, 1, &devtime
);
1340 mDeviceStartTime
= nanoseconds
{devtime
} - mCurrentPts
;
1346 /* (re)start the source if needed, and wait for a buffer to finish */
1347 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
1349 if(!startPlayback())
1352 if(alGetError() != AL_NO_ERROR
)
1355 mSrcCond
.wait_for(srclock
, sleep_time
);
1358 alSourceRewind(mSource
);
1359 alSourcei(mSource
, AL_BUFFER
, 0);
1365 #ifdef AL_SOFT_events
1366 if(alEventControlSOFT
)
1368 alEventControlSOFT(evt_types
.size(), evt_types
.data(), AL_FALSE
);
1369 alEventCallbackSOFT(nullptr, nullptr);
1377 nanoseconds
VideoState::getClock()
1379 /* NOTE: This returns incorrect times while not playing. */
1380 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1381 if(mDisplayPtsTime
== microseconds::min())
1382 return nanoseconds::zero();
1383 auto delta
= get_avtime() - mDisplayPtsTime
;
1384 return mDisplayPts
+ delta
;
1387 /* Called by VideoState::updateVideo to display the next video frame. */
1388 void VideoState::display(SDL_Window
*screen
, SDL_Renderer
*renderer
)
1393 double aspect_ratio
;
1397 if(mCodecCtx
->sample_aspect_ratio
.num
== 0)
1401 aspect_ratio
= av_q2d(mCodecCtx
->sample_aspect_ratio
) * mCodecCtx
->width
/
1404 if(aspect_ratio
<= 0.0)
1405 aspect_ratio
= static_cast<double>(mCodecCtx
->width
) / mCodecCtx
->height
;
1407 SDL_GetWindowSize(screen
, &win_w
, &win_h
);
1409 w
= (static_cast<int>(std::rint(h
* aspect_ratio
)) + 3) & ~3;
1413 h
= (static_cast<int>(std::rint(w
/ aspect_ratio
)) + 3) & ~3;
1415 x
= (win_w
- w
) / 2;
1416 y
= (win_h
- h
) / 2;
1418 SDL_Rect src_rect
{ 0, 0, mWidth
, mHeight
};
1419 SDL_Rect dst_rect
{ x
, y
, w
, h
};
1420 SDL_RenderCopy(renderer
, mImage
, &src_rect
, &dst_rect
);
1421 SDL_RenderPresent(renderer
);
1424 /* Called regularly on the main thread where the SDL_Renderer was created. It
1425 * handles updating the textures of decoded frames and displaying the latest
1428 void VideoState::updateVideo(SDL_Window
*screen
, SDL_Renderer
*renderer
, bool redraw
)
1430 size_t read_idx
{mPictQRead
.load(std::memory_order_relaxed
)};
1431 Picture
*vp
{&mPictQ
[read_idx
]};
1433 auto clocktime
= mMovie
.getMasterClock();
1434 bool updated
{false};
1437 size_t next_idx
{(read_idx
+1)%mPictQ
.size()};
1438 if(next_idx
== mPictQWrite
.load(std::memory_order_acquire
))
1440 Picture
*nextvp
{&mPictQ
[next_idx
]};
1441 if(clocktime
< nextvp
->mPts
)
1446 read_idx
= next_idx
;
1448 if(mMovie
.mQuit
.load(std::memory_order_relaxed
))
1451 mFinalUpdate
= true;
1452 mPictQRead
.store(read_idx
, std::memory_order_release
);
1453 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1454 mPictQCond
.notify_one();
1460 mPictQRead
.store(read_idx
, std::memory_order_release
);
1461 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1462 mPictQCond
.notify_one();
1464 /* allocate or resize the buffer! */
1465 bool fmt_updated
{false};
1466 if(!mImage
|| mWidth
!= mCodecCtx
->width
|| mHeight
!= mCodecCtx
->height
)
1470 SDL_DestroyTexture(mImage
);
1471 mImage
= SDL_CreateTexture(renderer
, SDL_PIXELFORMAT_IYUV
, SDL_TEXTUREACCESS_STREAMING
,
1472 mCodecCtx
->coded_width
, mCodecCtx
->coded_height
);
1474 std::cerr
<< "Failed to create YV12 texture!" <<std::endl
;
1475 mWidth
= mCodecCtx
->width
;
1476 mHeight
= mCodecCtx
->height
;
1478 if(mFirstUpdate
&& mWidth
> 0 && mHeight
> 0)
1480 /* For the first update, set the window size to the video size. */
1481 mFirstUpdate
= false;
1485 if(mCodecCtx
->sample_aspect_ratio
.den
!= 0)
1487 double aspect_ratio
= av_q2d(mCodecCtx
->sample_aspect_ratio
);
1488 if(aspect_ratio
>= 1.0)
1489 w
= static_cast<int>(w
*aspect_ratio
+ 0.5);
1490 else if(aspect_ratio
> 0.0)
1491 h
= static_cast<int>(h
/aspect_ratio
+ 0.5);
1493 SDL_SetWindowSize(screen
, w
, h
);
1499 AVFrame
*frame
{vp
->mFrame
.get()};
1500 void *pixels
{nullptr};
1503 if(mCodecCtx
->pix_fmt
== AV_PIX_FMT_YUV420P
)
1504 SDL_UpdateYUVTexture(mImage
, nullptr,
1505 frame
->data
[0], frame
->linesize
[0],
1506 frame
->data
[1], frame
->linesize
[1],
1507 frame
->data
[2], frame
->linesize
[2]
1509 else if(SDL_LockTexture(mImage
, nullptr, &pixels
, &pitch
) != 0)
1510 std::cerr
<< "Failed to lock texture" <<std::endl
;
1513 // Convert the image into YUV format that SDL uses
1514 int coded_w
{mCodecCtx
->coded_width
};
1515 int coded_h
{mCodecCtx
->coded_height
};
1516 int w
{mCodecCtx
->width
};
1517 int h
{mCodecCtx
->height
};
1518 if(!mSwscaleCtx
|| fmt_updated
)
1520 mSwscaleCtx
.reset(sws_getContext(
1521 w
, h
, mCodecCtx
->pix_fmt
,
1522 w
, h
, AV_PIX_FMT_YUV420P
, 0,
1523 nullptr, nullptr, nullptr
1527 /* point pict at the queue */
1528 uint8_t *pict_data
[3];
1529 pict_data
[0] = static_cast<uint8_t*>(pixels
);
1530 pict_data
[1] = pict_data
[0] + coded_w
*coded_h
;
1531 pict_data
[2] = pict_data
[1] + coded_w
*coded_h
/4;
1533 int pict_linesize
[3];
1534 pict_linesize
[0] = pitch
;
1535 pict_linesize
[1] = pitch
/ 2;
1536 pict_linesize
[2] = pitch
/ 2;
1538 sws_scale(mSwscaleCtx
.get(), reinterpret_cast<uint8_t**>(frame
->data
), frame
->linesize
,
1539 0, h
, pict_data
, pict_linesize
);
1540 SDL_UnlockTexture(mImage
);
1549 /* Show the picture! */
1550 display(screen
, renderer
);
1555 auto disp_time
= get_avtime();
1557 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1558 mDisplayPts
= vp
->mPts
;
1559 mDisplayPtsTime
= disp_time
;
1561 if(mEOS
.load(std::memory_order_acquire
))
1563 if((read_idx
+1)%mPictQ
.size() == mPictQWrite
.load(std::memory_order_acquire
))
1565 mFinalUpdate
= true;
1566 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1567 mPictQCond
.notify_one();
1572 int VideoState::handler()
1574 std::for_each(mPictQ
.begin(), mPictQ
.end(),
1575 [](Picture
&pict
) -> void
1576 { pict
.mFrame
= AVFramePtr
{av_frame_alloc()}; });
1578 /* Prefill the codec buffer. */
1580 const int ret
{mPackets
.sendTo(mCodecCtx
.get())};
1581 if(ret
== AVERROR(EAGAIN
) || ret
== AVErrorEOF
)
1586 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1587 mDisplayPtsTime
= get_avtime();
1590 auto current_pts
= nanoseconds::zero();
1591 while(!mMovie
.mQuit
.load(std::memory_order_relaxed
))
1593 size_t write_idx
{mPictQWrite
.load(std::memory_order_relaxed
)};
1594 Picture
*vp
{&mPictQ
[write_idx
]};
1596 /* Retrieve video frame. */
1597 AVFrame
*decoded_frame
{vp
->mFrame
.get()};
1599 while((ret
=avcodec_receive_frame(mCodecCtx
.get(), decoded_frame
)) == AVERROR(EAGAIN
))
1600 mPackets
.sendTo(mCodecCtx
.get());
1603 if(ret
== AVErrorEOF
) break;
1604 std::cerr
<< "Failed to receive frame: "<<ret
<<std::endl
;
1608 /* Get the PTS for this frame. */
1609 if(decoded_frame
->best_effort_timestamp
!= AVNoPtsValue
)
1610 current_pts
= duration_cast
<nanoseconds
>(seconds_d64
{av_q2d(mStream
->time_base
) *
1611 static_cast<double>(decoded_frame
->best_effort_timestamp
)});
1612 vp
->mPts
= current_pts
;
1614 /* Update the video clock to the next expected PTS. */
1615 auto frame_delay
= av_q2d(mCodecCtx
->time_base
);
1616 frame_delay
+= decoded_frame
->repeat_pict
* (frame_delay
* 0.5);
1617 current_pts
+= duration_cast
<nanoseconds
>(seconds_d64
{frame_delay
});
1619 /* Put the frame in the queue to be loaded into a texture and displayed
1620 * by the rendering thread.
1622 write_idx
= (write_idx
+1)%mPictQ
.size();
1623 mPictQWrite
.store(write_idx
, std::memory_order_release
);
1625 /* Send a packet now so it's hopefully ready by the time it's needed. */
1626 mPackets
.sendTo(mCodecCtx
.get());
1628 if(write_idx
== mPictQRead
.load(std::memory_order_acquire
))
1630 /* Wait until we have space for a new pic */
1631 std::unique_lock
<std::mutex
> lock
{mPictQMutex
};
1632 while(write_idx
== mPictQRead
.load(std::memory_order_acquire
) &&
1633 !mMovie
.mQuit
.load(std::memory_order_relaxed
))
1634 mPictQCond
.wait(lock
);
1639 std::unique_lock
<std::mutex
> lock
{mPictQMutex
};
1640 while(!mFinalUpdate
) mPictQCond
.wait(lock
);
1646 int MovieState::decode_interrupt_cb(void *ctx
)
1648 return static_cast<MovieState
*>(ctx
)->mQuit
.load(std::memory_order_relaxed
);
1651 bool MovieState::prepare()
1653 AVIOContext
*avioctx
{nullptr};
1654 AVIOInterruptCB intcb
{decode_interrupt_cb
, this};
1655 if(avio_open2(&avioctx
, mFilename
.c_str(), AVIO_FLAG_READ
, &intcb
, nullptr))
1657 std::cerr
<< "Failed to open "<<mFilename
<<std::endl
;
1660 mIOContext
.reset(avioctx
);
1662 /* Open movie file. If avformat_open_input fails it will automatically free
1663 * this context, so don't set it onto a smart pointer yet.
1665 AVFormatContext
*fmtctx
{avformat_alloc_context()};
1666 fmtctx
->pb
= mIOContext
.get();
1667 fmtctx
->interrupt_callback
= intcb
;
1668 if(avformat_open_input(&fmtctx
, mFilename
.c_str(), nullptr, nullptr) != 0)
1670 std::cerr
<< "Failed to open "<<mFilename
<<std::endl
;
1673 mFormatCtx
.reset(fmtctx
);
1675 /* Retrieve stream information */
1676 if(avformat_find_stream_info(mFormatCtx
.get(), nullptr) < 0)
1678 std::cerr
<< mFilename
<<": failed to find stream info" <<std::endl
;
1682 /* Dump information about file onto standard error */
1683 av_dump_format(mFormatCtx
.get(), 0, mFilename
.c_str(), 0);
1685 mParseThread
= std::thread
{std::mem_fn(&MovieState::parse_handler
), this};
1689 void MovieState::setTitle(SDL_Window
*window
)
1691 auto pos1
= mFilename
.rfind('/');
1692 auto pos2
= mFilename
.rfind('\\');
1693 auto fpos
= ((pos1
== std::string::npos
) ? pos2
:
1694 (pos2
== std::string::npos
) ? pos1
:
1695 std::max(pos1
, pos2
)) + 1;
1696 SDL_SetWindowTitle(window
, (mFilename
.substr(fpos
)+" - "+AppName
).c_str());
1699 nanoseconds
MovieState::getClock()
1701 if(mClockBase
== microseconds::min())
1702 return nanoseconds::zero();
1703 return get_avtime() - mClockBase
;
1706 nanoseconds
MovieState::getMasterClock()
1708 if(mAVSyncType
== SyncMaster::Video
)
1709 return mVideo
.getClock();
1710 if(mAVSyncType
== SyncMaster::Audio
)
1711 return mAudio
.getClock();
1715 nanoseconds
MovieState::getDuration()
1716 { return std::chrono::duration
<int64_t,std::ratio
<1,AV_TIME_BASE
>>(mFormatCtx
->duration
); }
1718 int MovieState::streamComponentOpen(unsigned int stream_index
)
1720 if(stream_index
>= mFormatCtx
->nb_streams
)
1723 /* Get a pointer to the codec context for the stream, and open the
1726 AVCodecCtxPtr avctx
{avcodec_alloc_context3(nullptr)};
1727 if(!avctx
) return -1;
1729 if(avcodec_parameters_to_context(avctx
.get(), mFormatCtx
->streams
[stream_index
]->codecpar
))
1732 AVCodec
*codec
{avcodec_find_decoder(avctx
->codec_id
)};
1733 if(!codec
|| avcodec_open2(avctx
.get(), codec
, nullptr) < 0)
1735 std::cerr
<< "Unsupported codec: "<<avcodec_get_name(avctx
->codec_id
)
1736 << " (0x"<<std::hex
<<avctx
->codec_id
<<std::dec
<<")" <<std::endl
;
1740 /* Initialize and start the media type handler */
1741 switch(avctx
->codec_type
)
1743 case AVMEDIA_TYPE_AUDIO
:
1744 mAudio
.mStream
= mFormatCtx
->streams
[stream_index
];
1745 mAudio
.mCodecCtx
= std::move(avctx
);
1748 case AVMEDIA_TYPE_VIDEO
:
1749 mVideo
.mStream
= mFormatCtx
->streams
[stream_index
];
1750 mVideo
.mCodecCtx
= std::move(avctx
);
1757 return static_cast<int>(stream_index
);
1760 int MovieState::parse_handler()
1762 auto &audio_queue
= mAudio
.mPackets
;
1763 auto &video_queue
= mVideo
.mPackets
;
1765 int video_index
{-1};
1766 int audio_index
{-1};
1768 /* Find the first video and audio streams */
1769 for(unsigned int i
{0u};i
< mFormatCtx
->nb_streams
;i
++)
1771 auto codecpar
= mFormatCtx
->streams
[i
]->codecpar
;
1772 if(codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
&& !DisableVideo
&& video_index
< 0)
1773 video_index
= streamComponentOpen(i
);
1774 else if(codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
&& audio_index
< 0)
1775 audio_index
= streamComponentOpen(i
);
1778 if(video_index
< 0 && audio_index
< 0)
1780 std::cerr
<< mFilename
<<": could not open codecs" <<std::endl
;
1784 /* Set the base time 750ms ahead of the current av time. */
1785 mClockBase
= get_avtime() + milliseconds
{750};
1787 if(audio_index
>= 0)
1788 mAudioThread
= std::thread
{std::mem_fn(&AudioState::handler
), &mAudio
};
1789 if(video_index
>= 0)
1790 mVideoThread
= std::thread
{std::mem_fn(&VideoState::handler
), &mVideo
};
1792 /* Main packet reading/dispatching loop */
1793 while(!mQuit
.load(std::memory_order_relaxed
))
1796 if(av_read_frame(mFormatCtx
.get(), &packet
) < 0)
1799 /* Copy the packet into the queue it's meant for. */
1800 if(packet
.stream_index
== video_index
)
1802 while(!mQuit
.load(std::memory_order_acquire
) && !video_queue
.put(&packet
))
1803 std::this_thread::sleep_for(milliseconds
{100});
1805 else if(packet
.stream_index
== audio_index
)
1807 while(!mQuit
.load(std::memory_order_acquire
) && !audio_queue
.put(&packet
))
1808 std::this_thread::sleep_for(milliseconds
{100});
1811 av_packet_unref(&packet
);
1813 /* Finish the queues so the receivers know nothing more is coming. */
1814 if(mVideo
.mCodecCtx
) video_queue
.setFinished();
1815 if(mAudio
.mCodecCtx
) audio_queue
.setFinished();
1817 /* all done - wait for it */
1818 if(mVideoThread
.joinable())
1819 mVideoThread
.join();
1820 if(mAudioThread
.joinable())
1821 mAudioThread
.join();
1824 std::unique_lock
<std::mutex
> lock
{mVideo
.mPictQMutex
};
1825 while(!mVideo
.mFinalUpdate
)
1826 mVideo
.mPictQCond
.wait(lock
);
1830 evt
.user
.type
= FF_MOVIE_DONE_EVENT
;
1831 SDL_PushEvent(&evt
);
1837 // Helper class+method to print the time with human-readable formatting.
1841 std::ostream
&operator<<(std::ostream
&os
, const PrettyTime
&rhs
)
1843 using hours
= std::chrono::hours
;
1844 using minutes
= std::chrono::minutes
;
1846 seconds t
{rhs
.mTime
};
1853 // Only handle up to hour formatting
1855 os
<< duration_cast
<hours
>(t
).count() << 'h' << std::setfill('0') << std::setw(2)
1856 << (duration_cast
<minutes
>(t
).count() % 60) << 'm';
1858 os
<< duration_cast
<minutes
>(t
).count() << 'm' << std::setfill('0');
1859 os
<< std::setw(2) << (duration_cast
<seconds
>(t
).count() % 60) << 's' << std::setw(0)
1860 << std::setfill(' ');
1867 int main(int argc
, char *argv
[])
1869 std::unique_ptr
<MovieState
> movState
;
1873 std::cerr
<< "Usage: "<<argv
[0]<<" [-device <device name>] [-direct] <files...>" <<std::endl
;
1876 /* Register all formats and codecs */
1877 #if !(LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100))
1880 /* Initialize networking protocols */
1881 avformat_network_init();
1883 if(SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_EVENTS
))
1885 std::cerr
<< "Could not initialize SDL - <<"<<SDL_GetError() <<std::endl
;
1889 /* Make a window to put our video */
1890 SDL_Window
*screen
{SDL_CreateWindow(AppName
.c_str(), 0, 0, 640, 480, SDL_WINDOW_RESIZABLE
)};
1893 std::cerr
<< "SDL: could not set video mode - exiting" <<std::endl
;
1896 /* Make a renderer to handle the texture image surface and rendering. */
1897 Uint32 render_flags
{SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC
};
1898 SDL_Renderer
*renderer
{SDL_CreateRenderer(screen
, -1, render_flags
)};
1901 SDL_RendererInfo rinf
{};
1904 /* Make sure the renderer supports IYUV textures. If not, fallback to a
1905 * software renderer. */
1906 if(SDL_GetRendererInfo(renderer
, &rinf
) == 0)
1908 for(Uint32 i
{0u};!ok
&& i
< rinf
.num_texture_formats
;i
++)
1909 ok
= (rinf
.texture_formats
[i
] == SDL_PIXELFORMAT_IYUV
);
1913 std::cerr
<< "IYUV pixelformat textures not supported on renderer "<<rinf
.name
<<std::endl
;
1914 SDL_DestroyRenderer(renderer
);
1920 render_flags
= SDL_RENDERER_SOFTWARE
| SDL_RENDERER_PRESENTVSYNC
;
1921 renderer
= SDL_CreateRenderer(screen
, -1, render_flags
);
1925 std::cerr
<< "SDL: could not create renderer - exiting" <<std::endl
;
1928 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1929 SDL_RenderFillRect(renderer
, nullptr);
1930 SDL_RenderPresent(renderer
);
1932 /* Open an audio device */
1934 if(InitAL(&argv
, &argc
))
1936 std::cerr
<< "Failed to set up audio device" <<std::endl
;
1941 auto device
= alcGetContextsDevice(alcGetCurrentContext());
1942 if(alcIsExtensionPresent(device
, "ALC_SOFT_device_clock"))
1944 std::cout
<< "Found ALC_SOFT_device_clock" <<std::endl
;
1945 alcGetInteger64vSOFT
= reinterpret_cast<LPALCGETINTEGER64VSOFT
>(
1946 alcGetProcAddress(device
, "alcGetInteger64vSOFT")
1951 if(alIsExtensionPresent("AL_SOFT_source_latency"))
1953 std::cout
<< "Found AL_SOFT_source_latency" <<std::endl
;
1954 alGetSourcei64vSOFT
= reinterpret_cast<LPALGETSOURCEI64VSOFT
>(
1955 alGetProcAddress("alGetSourcei64vSOFT")
1958 #ifdef AL_SOFT_events
1959 if(alIsExtensionPresent("AL_SOFTX_events"))
1961 std::cout
<< "Found AL_SOFT_events" <<std::endl
;
1962 alEventControlSOFT
= reinterpret_cast<LPALEVENTCONTROLSOFT
>(
1963 alGetProcAddress("alEventControlSOFT"));
1964 alEventCallbackSOFT
= reinterpret_cast<LPALEVENTCALLBACKSOFT
>(
1965 alGetProcAddress("alEventCallbackSOFT"));
1968 #ifdef AL_SOFT_callback_buffer
1969 if(alIsExtensionPresent("AL_SOFTX_callback_buffer"))
1971 std::cout
<< "Found AL_SOFT_callback_buffer" <<std::endl
;
1972 alBufferCallbackSOFT
= reinterpret_cast<LPALBUFFERCALLBACKSOFT
>(
1973 alGetProcAddress("alBufferCallbackSOFT"));
1978 for(;fileidx
< argc
;++fileidx
)
1980 if(strcmp(argv
[fileidx
], "-direct") == 0)
1982 if(alIsExtensionPresent("AL_SOFT_direct_channels_remix"))
1984 std::cout
<< "Found AL_SOFT_direct_channels_remix" <<std::endl
;
1985 DirectOutMode
= AL_REMIX_UNMATCHED_SOFT
;
1987 else if(alIsExtensionPresent("AL_SOFT_direct_channels"))
1989 std::cout
<< "Found AL_SOFT_direct_channels" <<std::endl
;
1990 DirectOutMode
= AL_DROP_UNMATCHED_SOFT
;
1993 std::cerr
<< "AL_SOFT_direct_channels not supported for direct output" <<std::endl
;
1995 else if(strcmp(argv
[fileidx
], "-wide") == 0)
1997 if(!alIsExtensionPresent("AL_EXT_STEREO_ANGLES"))
1998 std::cerr
<< "AL_EXT_STEREO_ANGLES not supported for wide stereo" <<std::endl
;
2001 std::cout
<< "Found AL_EXT_STEREO_ANGLES" <<std::endl
;
2002 EnableWideStereo
= true;
2005 else if(strcmp(argv
[fileidx
], "-novideo") == 0)
2006 DisableVideo
= true;
2011 while(fileidx
< argc
&& !movState
)
2013 movState
= std::unique_ptr
<MovieState
>{new MovieState
{argv
[fileidx
++]}};
2014 if(!movState
->prepare()) movState
= nullptr;
2018 std::cerr
<< "Could not start a video" <<std::endl
;
2021 movState
->setTitle(screen
);
2023 /* Default to going to the next movie at the end of one. */
2024 enum class EomAction
{
2026 } eom_action
{EomAction::Next
};
2027 seconds last_time
{seconds::min()};
2031 int have_evt
{SDL_WaitEventTimeout(&event
, 10)};
2033 auto cur_time
= std::chrono::duration_cast
<seconds
>(movState
->getMasterClock());
2034 if(cur_time
!= last_time
)
2036 auto end_time
= std::chrono::duration_cast
<seconds
>(movState
->getDuration());
2037 std::cout
<< " \r "<<PrettyTime
{cur_time
}<<" / "<<PrettyTime
{end_time
} <<std::flush
;
2038 last_time
= cur_time
;
2041 bool force_redraw
{false};
2046 switch(event
.key
.keysym
.sym
)
2049 movState
->mQuit
= true;
2050 eom_action
= EomAction::Quit
;
2054 movState
->mQuit
= true;
2055 eom_action
= EomAction::Next
;
2063 case SDL_WINDOWEVENT
:
2064 switch(event
.window
.event
)
2066 case SDL_WINDOWEVENT_RESIZED
:
2067 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
2068 SDL_RenderFillRect(renderer
, nullptr);
2069 force_redraw
= true;
2072 case SDL_WINDOWEVENT_EXPOSED
:
2073 force_redraw
= true;
2082 movState
->mQuit
= true;
2083 eom_action
= EomAction::Quit
;
2086 case FF_MOVIE_DONE_EVENT
:
2088 last_time
= seconds::min();
2089 if(eom_action
!= EomAction::Quit
)
2092 while(fileidx
< argc
&& !movState
)
2094 movState
= std::unique_ptr
<MovieState
>{new MovieState
{argv
[fileidx
++]}};
2095 if(!movState
->prepare()) movState
= nullptr;
2099 movState
->setTitle(screen
);
2104 /* Nothing more to play. Shut everything down and quit. */
2109 SDL_DestroyRenderer(renderer
);
2111 SDL_DestroyWindow(screen
);
2120 } while(SDL_PollEvent(&event
));
2122 movState
->mVideo
.updateVideo(screen
, renderer
, force_redraw
);
2125 std::cerr
<< "SDL_WaitEvent error - "<<SDL_GetError() <<std::endl
;