2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2010-07-23 07:58:39 +02:00
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file threaded_frame_source.h
|
|
|
|
/// @see threaded_frame_source.cpp
|
|
|
|
/// @ingroup video
|
|
|
|
///
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/exception.h>
|
|
|
|
#include <libaegisub/fs_fwd.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
2012-12-17 17:27:11 +01:00
|
|
|
#include <deque>
|
2012-09-25 01:35:27 +02:00
|
|
|
#include <memory>
|
2012-12-17 17:27:11 +01:00
|
|
|
#include <set>
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
#include <wx/event.h>
|
2011-12-22 22:29:29 +01:00
|
|
|
|
2012-12-17 17:27:11 +01:00
|
|
|
class AssEntry;
|
2010-07-23 07:58:39 +02:00
|
|
|
class AssFile;
|
|
|
|
class SubtitlesProvider;
|
|
|
|
class VideoProvider;
|
2010-08-02 08:32:01 +02:00
|
|
|
class VideoProviderError;
|
2013-07-01 05:15:43 +02:00
|
|
|
struct VideoFrame;
|
2013-01-04 16:01:50 +01:00
|
|
|
namespace agi { namespace dispatch { class Queue; } }
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
/// @class ThreadedFrameSource
|
|
|
|
/// @brief An asynchronous video decoding and subtitle rendering wrapper
|
2013-01-04 16:01:50 +01:00
|
|
|
class ThreadedFrameSource {
|
|
|
|
/// Asynchronous work queue
|
|
|
|
std::unique_ptr<agi::dispatch::Queue> worker;
|
|
|
|
|
2010-07-23 07:58:39 +02:00
|
|
|
/// Subtitles provider
|
2013-01-04 16:01:50 +01:00
|
|
|
std::unique_ptr<SubtitlesProvider> subs_provider;
|
2010-07-23 07:58:39 +02:00
|
|
|
/// Video provider
|
2013-01-04 16:01:50 +01:00
|
|
|
std::unique_ptr<VideoProvider> video_provider;
|
2010-07-23 07:58:39 +02:00
|
|
|
/// Event handler to send FrameReady events to
|
|
|
|
wxEvtHandler *parent;
|
|
|
|
|
2013-12-12 01:29:48 +01:00
|
|
|
int frame_number = -1; ///< Last frame number requested
|
|
|
|
double time = -1.; ///< Time of the frame to pass to the subtitle renderer
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
/// Copy of the subtitles file to avoid having to touch the project context
|
2012-11-16 00:46:56 +01:00
|
|
|
std::unique_ptr<AssFile> subs;
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
/// If >= 0, the subtitles provider current has just the lines visible on
|
|
|
|
/// that frame loaded. If -1, the entire file is loaded. If -2, the
|
|
|
|
/// currently loaded file is out of date.
|
2013-12-12 01:29:48 +01:00
|
|
|
int single_frame = -1;
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> ProcFrame(int frame, double time, bool raw = false);
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
/// Produce a frame if req_version is still the current version
|
|
|
|
void ProcAsync(uint_fast32_t req_version);
|
|
|
|
|
|
|
|
/// Monotonic counter used to drop frames when changes arrive faster than
|
|
|
|
/// they can be rendered
|
2013-12-12 01:29:48 +01:00
|
|
|
std::atomic<uint_fast32_t> version{ 0 };
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// @brief Load the passed subtitle file
|
|
|
|
/// @param subs File to load
|
|
|
|
///
|
|
|
|
/// This function blocks until is it is safe for the calling thread to
|
|
|
|
/// modify subs
|
2012-12-17 17:27:11 +01:00
|
|
|
void LoadSubtitles(const AssFile *subs) throw();
|
|
|
|
|
|
|
|
/// @brief Update a previously loaded subtitle file
|
|
|
|
/// @param subs Subtitle file which was last passed to LoadSubtitles
|
|
|
|
/// @param changes Set of lines which have changed
|
|
|
|
///
|
|
|
|
/// This function only supports changes to existing lines, and not
|
|
|
|
/// insertions or deletions.
|
2013-01-04 16:01:50 +01:00
|
|
|
void UpdateSubtitles(const AssFile *subs, std::set<const AssEntry *> const& changes) throw();
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
/// @brief Queue a request for a frame
|
|
|
|
/// @brief frame Frame number
|
|
|
|
/// @brief time Exact start time of the frame in seconds
|
|
|
|
///
|
|
|
|
/// This merely queues up a request and deletes any pending requests; there
|
|
|
|
/// is no guarantee that the requested frame will ever actually be produced
|
|
|
|
void RequestFrame(int frame, double time) throw();
|
|
|
|
|
|
|
|
/// @brief Synchronously get a frame
|
|
|
|
/// @brief frame Frame number
|
|
|
|
/// @brief time Exact start time of the frame in seconds
|
|
|
|
/// @brief raw Get raw frame without subtitles
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> GetFrame(int frame, double time, bool raw = false);
|
2010-07-23 07:58:39 +02:00
|
|
|
|
2011-12-06 01:17:36 +01:00
|
|
|
/// Get a reference to the video provider this is using
|
2013-01-04 16:01:50 +01:00
|
|
|
VideoProvider *GetVideoProvider() const { return video_provider.get(); }
|
2010-07-23 07:58:39 +02:00
|
|
|
|
|
|
|
/// @brief Constructor
|
|
|
|
/// @param videoFileName File to open
|
|
|
|
/// @param parent Event handler to send FrameReady events to
|
2013-10-17 16:23:45 +02:00
|
|
|
ThreadedFrameSource(agi::fs::path const& filename, std::string const& colormatrix, wxEvtHandler *parent);
|
2010-07-23 07:58:39 +02:00
|
|
|
~ThreadedFrameSource();
|
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
/// Event which signals that a requested frame is ready
|
|
|
|
struct FrameReadyEvent : public wxEvent {
|
2010-09-16 00:10:48 +02:00
|
|
|
/// Frame which is ready
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> frame;
|
2010-07-23 07:58:39 +02:00
|
|
|
/// Time which was used for subtitle rendering
|
|
|
|
double time;
|
2013-11-21 18:13:36 +01:00
|
|
|
wxEvent *Clone() const override { return new FrameReadyEvent(*this); };
|
2013-07-01 05:15:43 +02:00
|
|
|
FrameReadyEvent(std::shared_ptr<VideoFrame> frame, double time)
|
2013-11-21 18:13:36 +01:00
|
|
|
: frame(std::move(frame)), time(time) { }
|
2010-07-23 07:58:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// These exceptions are wxEvents so that they can be passed directly back to
|
|
|
|
// the parent thread as events
|
2013-01-04 16:01:50 +01:00
|
|
|
struct VideoProviderErrorEvent : public wxEvent, public agi::Exception {
|
2013-11-21 18:13:36 +01:00
|
|
|
const char * GetName() const override { return "video/error"; }
|
|
|
|
wxEvent *Clone() const override { return new VideoProviderErrorEvent(*this); };
|
|
|
|
agi::Exception *Copy() const override { return new VideoProviderErrorEvent(*this); };
|
2010-08-02 08:32:01 +02:00
|
|
|
VideoProviderErrorEvent(VideoProviderError const& err);
|
2010-07-23 07:58:39 +02:00
|
|
|
};
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
struct SubtitlesProviderErrorEvent : public wxEvent, public agi::Exception {
|
2013-11-21 18:13:36 +01:00
|
|
|
const char * GetName() const override { return "subtitles/error"; }
|
|
|
|
wxEvent *Clone() const override { return new SubtitlesProviderErrorEvent(*this); };
|
|
|
|
agi::Exception *Copy() const override { return new SubtitlesProviderErrorEvent(*this); };
|
2013-01-04 16:01:50 +01:00
|
|
|
SubtitlesProviderErrorEvent(std::string const& msg);
|
2010-07-23 07:58:39 +02:00
|
|
|
};
|
|
|
|
|
2010-07-23 08:21:22 +02:00
|
|
|
wxDECLARE_EVENT(EVT_FRAME_READY, FrameReadyEvent);
|
|
|
|
wxDECLARE_EVENT(EVT_VIDEO_ERROR, VideoProviderErrorEvent);
|
|
|
|
wxDECLARE_EVENT(EVT_SUBTITLES_ERROR, SubtitlesProviderErrorEvent);
|