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