Loading...
Searching...
No Matches
LibavInterrupt.hpp
1#pragma once
2#include <Media/Libav.hpp>
3#if SCORE_HAS_LIBAV
4
5extern "C" {
6#include <libavformat/avformat.h>
7}
8
9#include <atomic>
10#include <chrono>
11
12namespace Video
13{
14
30{
31public:
32 using clock = std::chrono::steady_clock;
33
34 void install(AVFormatContext& ctx) noexcept
35 {
36 ctx.interrupt_callback.callback = &LibavInterrupt::on_interrupt;
37 ctx.interrupt_callback.opaque = this;
38 }
39
40 void arm(std::chrono::milliseconds timeout) noexcept
41 {
42 m_deadline.store(
43 (clock::now() + timeout).time_since_epoch().count(), std::memory_order_release);
44 }
45
46 void disarm() noexcept { m_deadline.store(none, std::memory_order_release); }
47
49 void abort() noexcept { m_aborted.store(true, std::memory_order_release); }
50
51 void reset() noexcept
52 {
53 m_aborted.store(false, std::memory_order_release);
54 disarm();
55 }
56
57 bool aborted() const noexcept { return m_aborted.load(std::memory_order_acquire); }
58
59 bool expired() const noexcept
60 {
61 if(aborted())
62 return true;
63
64 const auto deadline = m_deadline.load(std::memory_order_acquire);
65 return deadline != none && clock::now().time_since_epoch().count() >= deadline;
66 }
67
68private:
69 static constexpr int64_t none = 0;
70
71 static int on_interrupt(void* opaque) noexcept
72 {
73 return static_cast<const LibavInterrupt*>(opaque)->expired() ? 1 : 0;
74 }
75
76 std::atomic<int64_t> m_deadline{none};
77 std::atomic_bool m_aborted{};
78};
79
82{
83 explicit LibavTimeout(LibavInterrupt& itr, std::chrono::milliseconds t) noexcept
84 : m_itr{itr}
85 {
86 m_itr.arm(t);
87 }
88 ~LibavTimeout() { m_itr.disarm(); }
89
90 LibavTimeout(const LibavTimeout&) = delete;
91 LibavTimeout& operator=(const LibavTimeout&) = delete;
92
93private:
94 LibavInterrupt& m_itr;
95};
96
97}
98#endif
Deadline for the blocking libav I/O of one AVFormatContext.
Definition LibavInterrupt.hpp:30
void abort() noexcept
Unblocks the pending call and every subsequent one until reset()
Definition LibavInterrupt.hpp:49
Arms an interrupt for the duration of a scope.
Definition LibavInterrupt.hpp:82