5 #include "queue_spot_holder.h"
8 #include <libavformat/avio.h>
11 #include <condition_variable>
20 class Player : public QueueInterface {
22 Player(JPEGFrameView *destination, bool also_output_to_stream);
24 void play_clip(const Clip &clip, size_t clip_idx, unsigned stream_idx);
25 void override_angle(unsigned stream_idx); // For the current clip only.
27 // Not thread-safe to set concurrently with playing.
28 // Will be called back from the player thread.
29 using done_callback_func = std::function<void()>;
30 void set_done_callback(done_callback_func cb) { done_callback = cb; }
32 // Not thread-safe to set concurrently with playing.
33 // Will be called back from the player thread.
34 // The second parameter is the clip's position in the play list.
35 using next_clip_callback_func = std::function<std::pair<Clip, size_t>()>;
36 void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
38 // Not thread-safe to set concurrently with playing.
39 // Will be called back from the player thread.
40 using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
41 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
44 void take_queue_spot() override;
45 void release_queue_spot() override;
48 void thread_func(bool also_output_to_stream);
49 void open_output_stream();
50 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
51 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
53 // Find the frame immediately before and after this point.
54 // Returns false if pts is after the last frame.
55 bool find_surrounding_frames(int64_t pts, int stream_idx, int64_t *pts_lower, int64_t *pts_upper);
57 JPEGFrameView *destination;
58 done_callback_func done_callback;
59 next_clip_callback_func next_clip_callback;
60 progress_callback_func progress_callback;
63 Clip current_clip; // Under mu. Can have pts_in = -1 for no clip.
64 size_t current_clip_idx; // Under mu.
65 unsigned current_stream_idx; // Under mu.
67 std::mutex queue_state_mu;
68 std::condition_variable new_clip_changed;
69 bool new_clip_ready = false; // Under queue_state_mu.
70 bool playing = false; // Under queue_state_mu.
71 int override_stream_idx = -1; // Under queue_state_mu.
73 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
75 // under queue_state_mu. Part of this instead of VideoStream so that we own
76 // its lock and can sleep on it.
77 size_t num_queued_frames = 0;
78 static constexpr size_t max_queued_frames = 10;
81 #endif // !defined(_PLAYER_H)