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