2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef DCPOMATIC_ENCODER_H
21 #define DCPOMATIC_ENCODER_H
23 /** @file src/encoder.h
24 * @brief Encoder class.
30 #include "exceptions.h"
32 #include <libavutil/samplefmt.h>
33 #include <libswresample/swresample.h>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/thread/mutex.hpp>
37 #include <boost/thread/condition.hpp>
38 #include <boost/thread.hpp>
39 #include <boost/optional.hpp>
46 class ServerDescription;
53 * @brief Class to manage encoding to JPEG2000.
55 * This class keeps a queue of frames to be encoded and distributes
56 * the work around threads and encoding servers.
59 class Encoder : public boost::noncopyable, public ExceptionStore
62 Encoder (boost::shared_ptr<const Film>, boost::weak_ptr<Job>, boost::shared_ptr<Writer>);
65 /** Called to indicate that a processing run is about to begin */
68 /** Call with a frame of video.
69 * @param f Video frame.
71 void enqueue (boost::shared_ptr<PlayerVideo> f);
73 /** Called when a processing run has finished */
76 float current_encoding_rate () const;
77 int video_frames_out () const;
83 void encoder_thread (boost::optional<ServerDescription>);
84 void terminate_threads ();
85 void add_worker_threads (ServerDescription);
86 void servers_list_changed ();
88 /** Film that we are encoding */
89 boost::shared_ptr<const Film> _film;
90 boost::weak_ptr<Job> _job;
92 /** Mutex for _time_history and _last_frame */
93 mutable boost::mutex _state_mutex;
94 /** List of the times of completion of the last _history_size frames;
95 first is the most recently completed.
97 std::list<struct timeval> _time_history;
98 /** Number of frames that we should keep history for */
99 static int const _history_size;
101 /** Number of video frames enqueued so far */
102 int _video_frames_enqueued;
105 std::list<boost::shared_ptr<DCPVideo> > _queue;
106 std::list<boost::thread *> _threads;
107 mutable boost::mutex _mutex;
108 /** condition to manage thread wakeups when we have nothing to do */
109 boost::condition _empty_condition;
110 /** condition to manage thread wakeups when we have too much to do */
111 boost::condition _full_condition;
113 boost::shared_ptr<Writer> _writer;
116 boost::shared_ptr<PlayerVideo> _last_player_video;
118 boost::signals2::scoped_connection _server_found_connection;