]> git.sesse.net Git - nageru/blobdiff - futatabi/player.h
Fix a minor unneeded unlock/lock.
[nageru] / futatabi / player.h
index c7f8e070a71a0ce697e1c21edd14727a3892762f..41dcfb884ae10ab7f98a72786181eacddc435eea 100644 (file)
@@ -6,24 +6,34 @@
 #include "queue_spot_holder.h"
 
 extern "C" {
+#include <libavformat/avformat.h>
 #include <libavformat/avio.h>
 }
 
 #include <condition_variable>
 #include <functional>
 #include <mutex>
+#include <thread>
 
 class JPEGFrameView;
 class VideoStream;
 class QSurface;
 class QSurfaceFormat;
 
+double compute_time_left(const std::vector<Clip> &clips, const std::map<size_t, double> &progress);
+
 class Player : public QueueInterface {
 public:
-       Player(JPEGFrameView *destination, bool also_output_to_stream);
+       enum StreamOutput {
+               NO_STREAM_OUTPUT,
+               HTTPD_STREAM_OUTPUT,  // Output to global_httpd.
+               FILE_STREAM_OUTPUT    // Output to file_avctx.
+       };
+       Player(JPEGFrameView *destination, StreamOutput stream_output, AVFormatContext *file_avctx = nullptr);
+       ~Player();
 
-       void play_clip(const Clip &clip, size_t clip_idx, unsigned stream_idx);
-       void override_angle(unsigned stream_idx);  // For the current clip only.
+       void play(const std::vector<Clip> &clips);
+       void override_angle(unsigned stream_idx);  // Assumes one-clip playlist only.
 
        // Not thread-safe to set concurrently with playing.
        // Will be called back from the player thread.
@@ -32,12 +42,7 @@ public:
 
        // Not thread-safe to set concurrently with playing.
        // Will be called back from the player thread.
-       // The second parameter is the clip's position in the play list.
-       using next_clip_callback_func = std::function<std::pair<Clip, size_t>()>;
-       void set_next_clip_callback(next_clip_callback_func cb) { next_clip_callback = cb; }
-
-       // Not thread-safe to set concurrently with playing.
-       // Will be called back from the player thread.
+       // The keys in the given map are indexes in the vector given to play().
        using progress_callback_func = std::function<void(const std::map<size_t, double> &progress)>;
        void set_progress_callback(progress_callback_func cb) { progress_callback = cb; }
 
@@ -46,7 +51,7 @@ public:
        void release_queue_spot() override;
 
 private:
-       void thread_func(bool also_output_to_stream);
+       void thread_func(StreamOutput stream_output, AVFormatContext *file_avctx);
        void open_output_stream();
        static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
        int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
@@ -55,24 +60,33 @@ private:
        // Returns false if pts is after the last frame.
        bool find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *frame_lower, FrameOnDisk *frame_upper);
 
+       std::thread player_thread;
+       std::atomic<bool> should_quit{false};
+
        JPEGFrameView *destination;
        done_callback_func done_callback;
-       next_clip_callback_func next_clip_callback;
        progress_callback_func progress_callback;
 
-       std::mutex mu;
-       Clip current_clip;  // Under mu. Can have pts_in = -1 for no clip.
-       size_t current_clip_idx;  // Under mu.
-       unsigned current_stream_idx;  // Under mu.
-
        std::mutex queue_state_mu;
        std::condition_variable new_clip_changed;
+       std::vector<Clip> queued_clip_list;   // Under queue_state_mu.
        bool new_clip_ready = false;  // Under queue_state_mu.
        bool playing = false;  // Under queue_state_mu.
        int override_stream_idx = -1;  // Under queue_state_mu.
+       int64_t last_pts_played = -1;  // Under queue_state_mu. Used by previews only.
 
        std::unique_ptr<VideoStream> video_stream;  // Can be nullptr.
 
+       std::atomic<int64_t> metric_dropped_interpolated_frame{0};
+       std::atomic<int64_t> metric_dropped_unconditional_frame{0};
+       std::atomic<int64_t> metric_faded_frame{0};
+       std::atomic<int64_t> metric_faded_snapped_frame{0};
+       std::atomic<int64_t> metric_original_frame{0};
+       std::atomic<int64_t> metric_original_snapped_frame{0};
+       std::atomic<int64_t> metric_refresh_frame{0};
+       std::atomic<int64_t> metric_interpolated_frame{0};
+       std::atomic<int64_t> metric_interpolated_faded_frame{0};
+
        // under queue_state_mu. Part of this instead of VideoStream so that we own
        // its lock and can sleep on it.
        size_t num_queued_frames = 0;