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