5 #include "frame_on_disk.h"
6 #include "queue_spot_holder.h"
9 #include <libavformat/avio.h>
12 #include <condition_variable>
22 class Player : public QueueInterface {
24 Player(JPEGFrameView *destination, bool also_output_to_stream);
27 void play_clip(const Clip &clip, size_t clip_idx, unsigned stream_idx);
28 void override_angle(unsigned stream_idx); // For the current clip only.
30 // Not thread-safe to set concurrently with playing.
31 // Will be called back from the player thread.
32 using done_callback_func = std::function<void()>;
33 void set_done_callback(done_callback_func cb) { done_callback = cb; }
35 // Not thread-safe to set concurrently with playing.
36 // Will be called back from the player thread.
37 // The second parameter is the clip's position in the play list.
38 using next_clip_callback_func = std::function<std::pair<Clip, size_t>()>;
39 void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
41 // Not thread-safe to set concurrently with playing.
42 // Will be called back from the player thread.
43 using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
44 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
47 void take_queue_spot() override;
48 void release_queue_spot() override;
51 void thread_func(bool also_output_to_stream);
52 void open_output_stream();
53 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
54 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
56 // Find the frame immediately before and after this point.
57 // Returns false if pts is after the last frame.
58 bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
60 std::thread player_thread;
61 std::atomic<bool> should_quit{false};
63 JPEGFrameView *destination;
64 done_callback_func done_callback;
65 next_clip_callback_func next_clip_callback;
66 progress_callback_func progress_callback;
69 Clip current_clip; // Under mu. Can have pts_in = -1 for no clip.
70 size_t current_clip_idx; // Under mu.
71 unsigned current_stream_idx; // Under mu.
73 std::mutex queue_state_mu;
74 std::condition_variable new_clip_changed;
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.
79 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
81 // under queue_state_mu. Part of this instead of VideoStream so that we own
82 // its lock and can sleep on it.
83 size_t num_queued_frames = 0;
84 static constexpr size_t max_queued_frames = 10;
87 #endif // !defined(_PLAYER_H)