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);
33 void play(const Clip &clip)
35 play({ ClipWithID{ clip, 0 } });
37 void play(const std::vector<ClipWithID> &clips);
38 void override_angle(unsigned stream_idx); // Assumes one-clip playlist only.
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.
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
53 // If nothing is playing, the call will be ignored.
54 void splice_play(const std::vector<ClipWithID> &clips);
56 // Not thread-safe to set concurrently with playing.
57 // Will be called back from the player thread.
58 using done_callback_func = std::function<void()>;
59 void set_done_callback(done_callback_func cb) { done_callback = cb; }
61 // Not thread-safe to set concurrently with playing.
62 // Will be called back from the player thread.
63 // The keys in the given map are row members in the vector given to play().
64 using progress_callback_func = std::function<void(const std::map<uint64_t, double> &progress, double time_remaining)>;
65 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
68 void take_queue_spot() override;
69 void release_queue_spot() override;
72 void thread_func(AVFormatContext *file_avctx);
73 void play_playlist_once();
74 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);
75 void open_output_stream();
76 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
77 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
79 // Find the frame immediately before and after this point.
80 // Returns false if pts is after the last frame.
81 bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
83 std::thread player_thread;
84 std::atomic<bool> should_quit{ false };
86 JPEGFrameView *destination;
87 done_callback_func done_callback;
88 progress_callback_func progress_callback;
90 std::mutex queue_state_mu;
91 std::condition_variable new_clip_changed;
92 std::vector<ClipWithID> queued_clip_list; // Under queue_state_mu.
93 bool new_clip_ready = false; // Under queue_state_mu.
94 bool playing = false; // Under queue_state_mu.
95 int override_stream_idx = -1; // Under queue_state_mu.
96 int64_t last_pts_played = -1; // Under queue_state_mu. Used by previews only.
98 bool splice_ready = false; // Under queue_state_mu.
99 std::vector<ClipWithID> to_splice_clip_list; // Under queue_state_mu.
101 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
103 std::atomic<int64_t> metric_dropped_interpolated_frame{ 0 };
104 std::atomic<int64_t> metric_dropped_unconditional_frame{ 0 };
105 std::atomic<int64_t> metric_faded_frame{ 0 };
106 std::atomic<int64_t> metric_faded_snapped_frame{ 0 };
107 std::atomic<int64_t> metric_original_frame{ 0 };
108 std::atomic<int64_t> metric_original_snapped_frame{ 0 };
109 std::atomic<int64_t> metric_refresh_frame{ 0 };
110 std::atomic<int64_t> metric_interpolated_frame{ 0 };
111 std::atomic<int64_t> metric_interpolated_faded_frame{ 0 };
113 // under queue_state_mu. Part of this instead of VideoStream so that we own
114 // its lock and can sleep on it.
115 size_t num_queued_frames = 0;
116 static constexpr size_t max_queued_frames = 10;
118 // State private to the player thread.
120 const StreamOutput stream_output;
123 double compute_time_left(const std::vector<ClipWithID> &clips, size_t currently_playing_idx, double progress_currently_playing);
125 static inline double compute_total_time(const std::vector<ClipWithID> &clips)
127 return compute_time_left(clips, 0, 0.0);
130 #endif // !defined(_PLAYER_H)