5 #include "frame_on_disk.h"
6 #include "queue_spot_holder.h"
9 #include <libavformat/avformat.h>
10 #include <libavformat/avio.h>
13 #include <condition_variable>
23 double compute_time_left(const std::vector<Clip> &clips, const std::map<size_t, double> &progress);
25 class Player : public QueueInterface {
29 HTTPD_STREAM_OUTPUT, // Output to global_httpd.
30 FILE_STREAM_OUTPUT // Output to file_avctx.
32 Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
35 void play(const std::vector<Clip> &clips);
36 void override_angle(unsigned stream_idx); // Assumes one-clip playlist only.
38 // Not thread-safe to set concurrently with playing.
39 // Will be called back from the player thread.
40 using done_callback_func = std::function<void()>;
41 void set_done_callback(done_callback_func cb) { done_callback = cb; }
43 // Not thread-safe to set concurrently with playing.
44 // Will be called back from the player thread.
45 // The keys in the given map are indexes in the vector given to play().
46 using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
47 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
50 void take_queue_spot() override;
51 void release_queue_spot() override;
54 void thread_func(AVFormatContext *file_avctx);
55 void play_playlist_once();
56 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);
57 void open_output_stream();
58 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
59 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
61 // Find the frame immediately before and after this point.
62 // Returns false if pts is after the last frame.
63 bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
65 std::thread player_thread;
66 std::atomic<bool> should_quit{false};
68 JPEGFrameView *destination;
69 done_callback_func done_callback;
70 progress_callback_func progress_callback;
72 std::mutex queue_state_mu;
73 std::condition_variable new_clip_changed;
74 std::vector<Clip> queued_clip_list; // Under queue_state_mu.
75 bool new_clip_ready = false; // Under queue_state_mu.
76 bool playing = false; // Under queue_state_mu.
77 int override_stream_idx = -1; // Under queue_state_mu.
78 int64_t last_pts_played = -1; // Under queue_state_mu. Used by previews only.
80 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
82 std::atomic<int64_t> metric_dropped_interpolated_frame{0};
83 std::atomic<int64_t> metric_dropped_unconditional_frame{0};
84 std::atomic<int64_t> metric_faded_frame{0};
85 std::atomic<int64_t> metric_faded_snapped_frame{0};
86 std::atomic<int64_t> metric_original_frame{0};
87 std::atomic<int64_t> metric_original_snapped_frame{0};
88 std::atomic<int64_t> metric_refresh_frame{0};
89 std::atomic<int64_t> metric_interpolated_frame{0};
90 std::atomic<int64_t> metric_interpolated_faded_frame{0};
92 // under queue_state_mu. Part of this instead of VideoStream so that we own
93 // its lock and can sleep on it.
94 size_t num_queued_frames = 0;
95 static constexpr size_t max_queued_frames = 10;
97 // State private to the player thread.
99 const StreamOutput stream_output;
102 #endif // !defined(_PLAYER_H)