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