CameraInput.hpp
1 #pragma once
2 #include <Media/Libav.hpp>
3 #if SCORE_HAS_LIBAV
4 
5 #include <Video/FrameQueue.hpp>
6 #include <Video/Rescale.hpp>
7 #include <Video/VideoInterface.hpp>
8 extern "C" {
9 #include <libavformat/avformat.h>
10 #include <libswscale/swscale.h>
11 }
12 
13 #include <ossia/detail/lockfree_queue.hpp>
14 
15 #include <score_plugin_media_export.h>
16 
17 #include <atomic>
18 #include <condition_variable>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 namespace Video
23 {
24 
25 class SCORE_PLUGIN_MEDIA_EXPORT ExternalInput : public VideoInterface
26 {
27 public:
28  virtual ~ExternalInput();
29  virtual bool start() noexcept = 0;
30  virtual void stop() noexcept = 0;
31 };
32 
33 class SCORE_PLUGIN_MEDIA_EXPORT CameraInput final
34  : public ExternalInput
35  , public LibAVDecoder
36 {
37 public:
38  CameraInput() noexcept;
39  ~CameraInput() noexcept;
40 
41  bool load(
42  const std::string& inputDevice, const std::string& format, int w, int h,
43  double fps, int codec, int pixelfmt) noexcept;
44 
45  bool start() noexcept override;
46  void stop() noexcept override;
47 
48  AVFrame* dequeue_frame() noexcept override;
49  void release_frame(AVFrame* frame) noexcept override;
50 
51 private:
52  void buffer_thread() noexcept;
53  void close_file() noexcept;
54  AVFrame* read_frame_impl() noexcept;
55  bool open_stream() noexcept;
56  void close_stream() noexcept;
57  ReadFrame read_one_frame(AVFramePointer frame, AVPacket& packet);
58 
59  static const constexpr int frames_to_buffer = 1;
60 
61  AVCodecID m_requestedCodec{AV_CODEC_ID_NONE};
62  AVPixelFormat m_requestedPixfmt{AV_PIX_FMT_NONE};
63 
64  std::thread m_thread;
65 
66  std::string m_inputKind;
67  std::string m_inputDevice;
68 
69  std::atomic_bool m_running{};
70 };
71 
72 }
73 #endif
Definition: CameraInput.hpp:36
Definition: CameraInput.hpp:26
Definition: Rescale.hpp:45
Definition: VideoInterface.hpp:38
Definition: VideoInterface.hpp:31