FrameQueue.hpp
1 #pragma once
2 #include <Media/Libav.hpp>
3 
4 #if SCORE_HAS_LIBAV
5 #include <Video/VideoInterface.hpp>
6 
7 #include <ossia/detail/lockfree_queue.hpp>
8 
9 #include <score_plugin_media_export.h>
10 
11 #if defined(SCORE_LIBAV_FRAME_DEBUGGING)
12 #include <mutex>
13 #define BOOST_STACKTRACE_USE_BACKTRACE 1
14 #include <boost/stacktrace.hpp>
15 #endif
16 
17 #include <atomic>
18 #include <vector>
19 
20 extern "C" {
21 struct AVFrame;
22 }
23 namespace Video
24 {
25 
26 #if defined(SCORE_LIBAV_FRAME_DEBUGGING)
27 inline struct frame_counters
28 {
29  std::mutex mtx;
30  struct fc
31  {
32  AVFrame* frame{};
33  boost::stacktrace::stacktrace st;
34  };
35  std::vector<fc> allocated;
36  void allocate(AVFrame*);
37  void deallocate(AVFrame*);
38 } frame_counts;
39 #define SCORE_LIBAV_FRAME_ALLOC_CHECK(f) frame_counts.allocate(f)
40 #define SCORE_LIBAV_FRAME_DEALLOC_CHECK(f) frame_counts.deallocate(f)
41 #else
42 #define SCORE_LIBAV_FRAME_ALLOC_CHECK(f) \
43  do \
44  { \
45  } while(0)
46 #define SCORE_LIBAV_FRAME_DEALLOC_CHECK(f) \
47  do \
48  { \
49  } while(0)
50 #endif
51 
52 struct SCORE_PLUGIN_MEDIA_EXPORT FrameQueue
53 {
54 public:
55  FrameQueue();
56  ~FrameQueue();
57 
58  FrameQueue(const FrameQueue&) = delete;
59  FrameQueue(FrameQueue&&) = delete;
60  FrameQueue& operator=(const FrameQueue&) = delete;
61  FrameQueue& operator=(FrameQueue&&) = delete;
62 
63  AVFramePointer newFrame() noexcept;
64 
65  void enqueue_decoding_error(AVFrame* f);
66  void enqueue(AVFrame* f);
67  AVFrame* dequeue() noexcept;
68  AVFrame* dequeue_one() noexcept;
69  AVFrame* discard_and_dequeue() noexcept;
70  AVFrame* discard_and_dequeue_one() noexcept;
71 
72  void set_discard_frame(AVFrame*);
73  void release(AVFrame* frame) noexcept;
74  void drain();
75 
76  std::size_t size() const noexcept { return available.size_approx(); }
77 
78 private:
79  ossia::mpmc_queue<AVFrame*> available;
80  ossia::mpmc_queue<AVFrame*> released;
81 
82  std::vector<AVFrame*> m_decodeThreadFrameBuffer;
83  std::atomic<AVFrame*> m_discardUntil{};
84 };
85 
86 SCORE_PLUGIN_MEDIA_EXPORT
87 uint8_t* initFrameBuffer(AVFrame& frame, std::size_t bytes);
88 
89 }
90 #endif
Definition: FrameQueue.hpp:53