]> git.sesse.net Git - nageru/blob - futatabi/player.h
33a83e5c550101870036b1e9297202e42cd82958
[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/avformat.h>
10 #include <libavformat/avio.h>
11 }
12
13 #include <condition_variable>
14 #include <functional>
15 #include <mutex>
16 #include <thread>
17
18 class JPEGFrameView;
19 class VideoStream;
20 class QSurface;
21 class QSurfaceFormat;
22
23 double compute_time_left(const std::vector<Clip> &clips, const std::map<size_t, double> &progress);
24
25 class Player : public QueueInterface {
26 public:
27         enum StreamOutput {
28                 NO_STREAM_OUTPUT,
29                 HTTPD_STREAM_OUTPUT,  // Output to global_httpd.
30                 FILE_STREAM_OUTPUT    // Output to file_avctx.
31         };
32         Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
33         ~Player();
34
35         void play_clip(const Clip &clip, size_t clip_idx, unsigned stream_idx);
36         void override_angle(unsigned stream_idx);  // For the current clip only.
37
38         // Not thread-safe to set concurrently with playing.
39         // Will be called back from the player thread.
40         using done_callback_func = std::function<void()>;
41         void set_done_callback(done_callback_func cb) { done_callback = cb; }
42
43         // Not thread-safe to set concurrently with playing.
44         // Will be called back from the player thread.
45         // The second parameter is the clip's position in the play list.
46         using next_clip_callback_func = std::function<std::pair<Clip, size_t>()>;
47         void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
48
49         // Not thread-safe to set concurrently with playing.
50         // Will be called back from the player thread.
51         using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
52         void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
53
54         // QueueInterface.
55         void take_queue_spot() override;
56         void release_queue_spot() override;
57
58 private:
59         void thread_func(StreamOutput stream_output, AVFormatContext *file_avctx);
60         void open_output_stream();
61         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
62         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
63
64         // Find the frame immediately before and after this point.
65         // Returns false if pts is after the last frame.
66         bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
67
68         std::thread player_thread;
69         std::atomic<bool> should_quit{false};
70
71         JPEGFrameView *destination;
72         done_callback_func done_callback;
73         next_clip_callback_func next_clip_callback;
74         progress_callback_func progress_callback;
75
76         std::mutex mu;
77         Clip current_clip;  // Under mu. Can have pts_in = -1 for no clip.
78         size_t current_clip_idx;  // Under mu.
79         unsigned current_stream_idx;  // Under mu.
80
81         std::mutex queue_state_mu;
82         std::condition_variable new_clip_changed;
83         bool new_clip_ready = false;  // Under queue_state_mu.
84         bool playing = false;  // Under queue_state_mu.
85         int override_stream_idx = -1;  // Under queue_state_mu.
86
87         std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
88
89         std::atomic<int64_t> metric_dropped_interpolated_frame{0};
90         std::atomic<int64_t> metric_dropped_unconditional_frame{0};
91         std::atomic<int64_t> metric_faded_frame{0};
92         std::atomic<int64_t> metric_faded_snapped_frame{0};
93         std::atomic<int64_t> metric_original_frame{0};
94         std::atomic<int64_t> metric_original_snapped_frame{0};
95         std::atomic<int64_t> metric_refresh_frame{0};
96         std::atomic<int64_t> metric_interpolated_frame{0};
97         std::atomic<int64_t> metric_interpolated_faded_frame{0};
98
99         // under queue_state_mu. Part of this instead of VideoStream so that we own
100         // its lock and can sleep on it.
101         size_t num_queued_frames = 0;
102         static constexpr size_t max_queued_frames = 10;
103 };
104
105 #endif  // !defined(_PLAYER_H)