]> git.sesse.net Git - nageru/blob - futatabi/player.h
Move VADisplayWithCleanup into its own header file, in shared/.
[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 #include "shared/metrics.h"
8
9 extern "C" {
10 #include <libavformat/avformat.h>
11 #include <libavformat/avio.h>
12 }
13
14 #include <condition_variable>
15 #include <functional>
16 #include <mutex>
17 #include <thread>
18
19 class JPEGFrameView;
20 class VideoStream;
21 class QSurface;
22 class QSurfaceFormat;
23
24 struct TimeRemaining {
25         size_t num_infinite;
26         double t;
27 };
28
29 class Player : public QueueInterface {
30 public:
31         enum StreamOutput {
32                 NO_STREAM_OUTPUT,
33                 HTTPD_STREAM_OUTPUT,  // Output to global_httpd.
34                 FILE_STREAM_OUTPUT    // Output to file_avctx.
35         };
36         Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
37         ~Player();
38
39         void play(const Clip &clip)
40         {
41                 play({ ClipWithID{ clip, 0 } });
42         }
43         void play(const std::vector<ClipWithID> &clips);
44         void override_angle(unsigned stream_idx);  // Assumes one-clip playlist only.
45
46         // Replace the part of the playlist that we haven't started playing yet
47         // (ie., from the point immediately after the last current playing clip
48         // and to the end) with the given one.
49         //
50         // E.g., if we have the playlist A, B, C, D, E, F, we're currently in a fade
51         // from B to C and run splice_play() with the list G, C, H, I, the resulting
52         // list will be A, B, C, H, I. (If the new list doesn't contain B nor C,
53         // there will be some heuristics.) Note that we always compare on ID only;
54         // changes will be ignored for the purposes of setting the split point,
55         // although the newly-spliced entries will of course get the new in/out points
56         // etc., which is the main reason for going through this exercise in the first
57         // place.
58         //
59         // If nothing is playing, the call will be ignored.
60         void splice_play(const std::vector<ClipWithID> &clips);
61
62         // Set the status string that will be used for the video stream's status subtitles
63         // whenever we are not playing anything.
64         void set_pause_status(const std::string &status)
65         {
66                 std::lock_guard<std::mutex> lock(queue_state_mu);
67                 pause_status = status;
68         }
69
70         void skip_to_next()
71         {
72                 should_skip_to_next = true;
73         }
74
75         void set_master_speed(float speed)
76         {
77                 start_master_speed = speed;
78                 change_master_speed = speed;
79         }
80
81         // Not thread-safe to set concurrently with playing.
82         // Will be called back from the player thread.
83         using done_callback_func = std::function<void()>;
84         void set_done_callback(done_callback_func cb) { done_callback = cb; }
85
86         // Not thread-safe to set concurrently with playing.
87         // Will be called back from the player thread.
88         // The keys in the given map are row members in the vector given to play().
89         using progress_callback_func = std::function<void(const std::map<uint64_t, double> &progress, TimeRemaining time_remaining)>;
90         void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
91
92         // QueueInterface.
93         void take_queue_spot() override;
94         void release_queue_spot() override;
95
96 private:
97         void thread_func(AVFormatContext *file_avctx);
98         void play_playlist_once();
99         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, const std::string &subtitle, bool play_audio);
100         void open_output_stream();
101         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
102         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
103
104         // Find the frame immediately before and after this point.
105         // Returns false if pts is after the last frame.
106         bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
107
108         std::thread player_thread;
109         std::atomic<bool> should_quit{ false };
110         std::atomic<bool> should_skip_to_next{ false };
111         std::atomic<float> start_master_speed{ 1.0f };
112         std::atomic<float> change_master_speed{ 0.0f / 0.0f };
113
114         JPEGFrameView *destination;
115         done_callback_func done_callback;
116         progress_callback_func progress_callback;
117
118         std::mutex queue_state_mu;
119         std::condition_variable new_clip_changed;
120         std::vector<ClipWithID> queued_clip_list;  // Under queue_state_mu.
121         bool new_clip_ready = false;  // Under queue_state_mu.
122         bool playing = false;  // Under queue_state_mu.
123         int override_stream_idx = -1;  // Under queue_state_mu.
124         int64_t last_pts_played = -1;  // Under queue_state_mu. Used by previews only.
125
126         bool splice_ready = false;  // Under queue_state_mu.
127         std::vector<ClipWithID> to_splice_clip_list;  // Under queue_state_mu.
128         std::string pause_status = "paused";  // Under queue_state_mu.
129
130         std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
131
132         std::atomic<int64_t> metric_dropped_interpolated_frame{ 0 };
133         std::atomic<int64_t> metric_dropped_unconditional_frame{ 0 };
134         std::atomic<int64_t> metric_faded_frame{ 0 };
135         std::atomic<int64_t> metric_faded_snapped_frame{ 0 };
136         std::atomic<int64_t> metric_original_frame{ 0 };
137         std::atomic<int64_t> metric_original_snapped_frame{ 0 };
138         std::atomic<int64_t> metric_refresh_frame{ 0 };
139         std::atomic<int64_t> metric_interpolated_frame{ 0 };
140         std::atomic<int64_t> metric_interpolated_faded_frame{ 0 };
141         Summary metric_player_ahead_seconds;
142
143         // under queue_state_mu. Part of this instead of VideoStream so that we own
144         // its lock and can sleep on it.
145         size_t num_queued_frames = 0;
146         static constexpr size_t max_queued_frames = 10;
147
148         // State private to the player thread.
149         int64_t pts = 0;
150         const StreamOutput stream_output;
151 };
152
153 TimeRemaining compute_time_left(const std::vector<ClipWithID> &clips, size_t currently_playing_idx, double progress_currently_playing);
154
155 static inline TimeRemaining compute_total_time(const std::vector<ClipWithID> &clips)
156 {
157         return compute_time_left(clips, 0, 0.0);
158 }
159
160 std::string format_duration(TimeRemaining t);
161
162 #endif  // !defined(_PLAYER_H)