]> git.sesse.net Git - nageru/blob - futatabi/player.h
a581a54f79dd41ae076403bb2a8b3b2831b0d029
[nageru] / futatabi / 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/avformat.h>
10 #include <libavformat/avio.h>
11 }
12
13 #include <condition_variable>
14 #include <functional>
15 #include <mutex>
16 #include <thread>
17
18 class JPEGFrameView;
19 class VideoStream;
20 class QSurface;
21 class QSurfaceFormat;
22
23 double compute_time_left(const std::vector<Clip> &clips, const std::map<size_t, double> &progress);
24
25 class Player : public QueueInterface {
26 public:
27         enum StreamOutput {
28                 NO_STREAM_OUTPUT,
29                 HTTPD_STREAM_OUTPUT,  // Output to global_httpd.
30                 FILE_STREAM_OUTPUT    // Output to file_avctx.
31         };
32         Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
33         ~Player();
34
35         struct ClipWithRow {
36                 Clip clip;
37                 unsigned row;  // Used for progress callback only.
38         };
39         void play(const Clip &clip)
40         {
41                 play({ ClipWithRow{ clip, 0 } });
42         }
43         void play(const std::vector<ClipWithRow> &clips);
44         void override_angle(unsigned stream_idx);  // Assumes one-clip playlist only.
45
46         // Not thread-safe to set concurrently with playing.
47         // Will be called back from the player thread.
48         using done_callback_func = std::function<void()>;
49         void set_done_callback(done_callback_func cb) { done_callback = cb; }
50
51         // Not thread-safe to set concurrently with playing.
52         // Will be called back from the player thread.
53         // The keys in the given map are row members in the vector given to play().
54         using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
55         void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
56
57         // QueueInterface.
58         void take_queue_spot() override;
59         void release_queue_spot() override;
60
61 private:
62         void thread_func(AVFormatContext *file_avctx);
63         void play_playlist_once();
64         void display_single_frame(int primary_stream_idx, const FrameOnDisk &primary_frame, int secondary_stream_idx, const FrameOnDisk &secondary_frame, double fade_alpha, std::chrono::steady_clock::time_point frame_start, bool snapped);
65         void open_output_stream();
66         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
67         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
68
69         // Find the frame immediately before and after this point.
70         // Returns false if pts is after the last frame.
71         bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
72
73         std::thread player_thread;
74         std::atomic<bool> should_quit{ false };
75
76         JPEGFrameView *destination;
77         done_callback_func done_callback;
78         progress_callback_func progress_callback;
79
80         std::mutex queue_state_mu;
81         std::condition_variable new_clip_changed;
82         std::vector<ClipWithRow> queued_clip_list;  // Under queue_state_mu.
83         bool new_clip_ready = false;  // Under queue_state_mu.
84         bool playing = false;  // Under queue_state_mu.
85         int override_stream_idx = -1;  // Under queue_state_mu.
86         int64_t last_pts_played = -1;  // Under queue_state_mu. Used by previews only.
87
88         std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
89
90         std::atomic<int64_t> metric_dropped_interpolated_frame{ 0 };
91         std::atomic<int64_t> metric_dropped_unconditional_frame{ 0 };
92         std::atomic<int64_t> metric_faded_frame{ 0 };
93         std::atomic<int64_t> metric_faded_snapped_frame{ 0 };
94         std::atomic<int64_t> metric_original_frame{ 0 };
95         std::atomic<int64_t> metric_original_snapped_frame{ 0 };
96         std::atomic<int64_t> metric_refresh_frame{ 0 };
97         std::atomic<int64_t> metric_interpolated_frame{ 0 };
98         std::atomic<int64_t> metric_interpolated_faded_frame{ 0 };
99
100         // under queue_state_mu. Part of this instead of VideoStream so that we own
101         // its lock and can sleep on it.
102         size_t num_queued_frames = 0;
103         static constexpr size_t max_queued_frames = 10;
104
105         // State private to the player thread.
106         int64_t pts = 0;
107         const StreamOutput stream_output;
108 };
109
110 #endif  // !defined(_PLAYER_H)