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_clip(const Clip &clip, size_t clip_idx, unsigned stream_idx);
34 void override_angle(unsigned stream_idx); // For the current clip only.
36 // Not thread-safe to set concurrently with playing.
37 // Will be called back from the player thread.
38 using done_callback_func = std::function<void()>;
39 void set_done_callback(done_callback_func cb) { done_callback = cb; }
41 // Not thread-safe to set concurrently with playing.
42 // Will be called back from the player thread.
43 // The second parameter is the clip's position in the play list.
44 using next_clip_callback_func = std::function<std::pair<Clip, size_t>()>;
45 void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
47 // Not thread-safe to set concurrently with playing.
48 // Will be called back from the player thread.
49 using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
50 void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
53 void take_queue_spot() override;
54 void release_queue_spot() override;
57 void thread_func(StreamOutput stream_output, AVFormatContext *file_avctx);
58 void open_output_stream();
59 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
60 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
62 // Find the frame immediately before and after this point.
63 // Returns false if pts is after the last frame.
64 bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
66 std::thread player_thread;
67 std::atomic<bool> should_quit{false};
69 JPEGFrameView *destination;
70 done_callback_func done_callback;
71 next_clip_callback_func next_clip_callback;
72 progress_callback_func progress_callback;
75 Clip current_clip; // Under mu. Can have pts_in = -1 for no clip.
76 size_t current_clip_idx; // Under mu.
77 unsigned current_stream_idx; // Under mu.
79 std::mutex queue_state_mu;
80 std::condition_variable new_clip_changed;
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.
85 std::unique_ptr<VideoStream> video_stream; // Can be nullptr.
87 // under queue_state_mu. Part of this instead of VideoStream so that we own
88 // its lock and can sleep on it.
89 size_t num_queued_frames = 0;
90 static constexpr size_t max_queued_frames = 10;
93 #endif // !defined(_PLAYER_H)