]> git.sesse.net Git - nageru/blob - futatabi/player.h
Fix some Futatabi shutdown problems.
[nageru] / futatabi / player.h
1 #ifndef _PLAYER_H
2 #define _PLAYER_H 1
3
4 #include "clip_list.h"
5 #include "frame_on_disk.h"
6 #include "queue_spot_holder.h"
7
8 extern "C" {
9 #include <libavformat/avio.h>
10 }
11
12 #include <condition_variable>
13 #include <functional>
14 #include <mutex>
15 #include <thread>
16
17 class JPEGFrameView;
18 class VideoStream;
19 class QSurface;
20 class QSurfaceFormat;
21
22 class Player : public QueueInterface {
23 public:
24         Player(JPEGFrameView *destination, bool also_output_to_stream);
25         ~Player();
26
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.
29
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; }
34
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; }
40
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; }
45
46         // QueueInterface.
47         void take_queue_spot() override;
48         void release_queue_spot() override;
49
50 private:
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);
55
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);
59
60         std::thread player_thread;
61         std::atomic<bool> should_quit{false};
62
63         JPEGFrameView *destination;
64         done_callback_func done_callback;
65         next_clip_callback_func next_clip_callback;
66         progress_callback_func progress_callback;
67
68         std::mutex mu;
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.
72
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.
78
79         std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
80
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;
85 };
86
87 #endif  // !defined(_PLAYER_H)