]> git.sesse.net Git - nageru/blob - player.h
Add a queue of frames going into VideoStream.
[nageru] / player.h
1 #ifndef _PLAYER_H
2 #define _PLAYER_H 1
3
4 #include "clip_list.h"
5
6 extern "C" {
7 #include <libavformat/avio.h>
8 }
9
10 #include <condition_variable>
11 #include <functional>
12 #include <mutex>
13
14 class JPEGFrameView;
15 class VideoStream;
16 class QSurface;
17 class QSurfaceFormat;
18
19 class Player {
20 public:
21         Player(JPEGFrameView *destination, bool also_output_to_stream);
22
23         void play_clip(const Clip &clip, unsigned stream_idx);
24         void override_angle(unsigned stream_idx);  // For the current clip only.
25
26         // Not thread-safe to set concurrently with playing.
27         // Will be called back from the player thread.
28         using done_callback_func = std::function<void()>;
29         void set_done_callback(done_callback_func cb) { done_callback = cb; }
30
31         // Not thread-safe to set concurrently with playing.
32         // Will be called back from the player thread.
33         using next_clip_callback_func = std::function<Clip()>;
34         void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
35
36         // Not thread-safe to set concurrently with playing.
37         // Will be called back from the player thread.
38         using progress_callback_func = std::function<void(double played_this_clip, double total_length)>;
39         void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
40
41 private:
42         void thread_func(bool also_output_to_stream);
43         void open_output_stream();
44         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
45         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
46
47         // Find the frame immediately before and after this point.
48         // Returns false if pts is after the last frame.
49         bool find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper);
50
51         JPEGFrameView *destination;
52         done_callback_func done_callback;
53         next_clip_callback_func next_clip_callback;
54         progress_callback_func progress_callback;
55
56         std::mutex mu;
57         Clip current_clip;  // Under mu. Can have pts_in = -1 for no clip.
58         unsigned current_stream_idx;  // Under mu.
59
60         std::mutex queue_state_mu;
61         std::condition_variable new_clip_changed;
62         bool new_clip_ready = false;  // Under queue_state_mu.
63         bool playing = false;  // Under queue_state_mu.
64         int override_stream_idx = -1;  // Under queue_state_mu.
65
66         std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
67
68         // under queue_state_mu. Part of this instead of VideoStream so that we own
69         // its lock and can sleep on it.
70         size_t num_queued_frames = 0;
71         static constexpr size_t max_queued_frames = 10;
72 };
73
74 #endif  // !defined(_PLAYER_H)