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 class Player : public QueueInterface {
27 HTTPD_STREAM_OUTPUT, // Output to global_httpd.
28 FILE_STREAM_OUTPUT // Output to file_avctx.
30 Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
35 size_t row; // Used for progress callback only.
37 void play(const Clip &clip)
39 play({ ClipWithRow{ clip, 0 } });
41 void play(const std::vector<ClipWithRow> &clips);
42 void override_angle(unsigned stream_idx); // Assumes one-clip playlist only.
44 // Not thread-safe to set concurrently with playing.
45 // Will be called back from the player thread.
46 using done_callback_func = std::function<void()>;
47 void set_done_callback(done_callback_func cb) { done_callback = cb; }
49 // Not thread-safe to set concurrently with playing.
50 // Will be called back from the player thread.
51 // The keys in the given map are row members in the vector given to play().
52 using progress_callback_func = std::function<void(const std::map<size_t, double> &progress, double time_remaining)>;
53 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
56 void take_queue_spot() override;
57 void release_queue_spot() override;
60 void thread_func(AVFormatContext *file_avctx);
61 void play_playlist_once();
62 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);
63 void open_output_stream();
64 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
65 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
67 // Find the frame immediately before and after this point.
68 // Returns false if pts is after the last frame.
69 bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
71 std::thread player_thread;
72 std::atomic<bool> should_quit{ false };
74 JPEGFrameView *destination;
75 done_callback_func done_callback;
76 progress_callback_func progress_callback;
78 std::mutex queue_state_mu;
79 std::condition_variable new_clip_changed;
80 std::vector<ClipWithRow> queued_clip_list; // Under queue_state_mu.
81 bool new_clip_ready = false; // Under queue_state_mu.
82 bool playing = false; // Under queue_state_mu.
83 int override_stream_idx = -1; // Under queue_state_mu.
84 int64_t last_pts_played = -1; // Under queue_state_mu. Used by previews only.
86 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
88 std::atomic<int64_t> metric_dropped_interpolated_frame{ 0 };
89 std::atomic<int64_t> metric_dropped_unconditional_frame{ 0 };
90 std::atomic<int64_t> metric_faded_frame{ 0 };
91 std::atomic<int64_t> metric_faded_snapped_frame{ 0 };
92 std::atomic<int64_t> metric_original_frame{ 0 };
93 std::atomic<int64_t> metric_original_snapped_frame{ 0 };
94 std::atomic<int64_t> metric_refresh_frame{ 0 };
95 std::atomic<int64_t> metric_interpolated_frame{ 0 };
96 std::atomic<int64_t> metric_interpolated_faded_frame{ 0 };
98 // under queue_state_mu. Part of this instead of VideoStream so that we own
99 // its lock and can sleep on it.
100 size_t num_queued_frames = 0;
101 static constexpr size_t max_queued_frames = 10;
103 // State private to the player thread.
105 const StreamOutput stream_output;
108 double compute_time_left(const std::vector<Player::ClipWithRow> &clips, size_t currently_playing_idx, double progress_currently_playing);
110 static inline double compute_total_time(const std::vector<Player::ClipWithRow> &clips)
112 return compute_time_left(clips, 0, 0.0);
115 #endif // !defined(_PLAYER_H)