Clean up of 3D->2D conversion.
[dcpomatic.git] / src / lib / encoder.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_ENCODER_H
22 #define DCPOMATIC_ENCODER_H
23
24 /** @file  src/encoder.h
25  *  @brief Encoder class.
26  */
27
28 #include "util.h"
29 #include "cross.h"
30 #include "exception_store.h"
31 #include <boost/shared_ptr.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/thread/condition.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/optional.hpp>
36 #include <boost/signals2.hpp>
37 #include <list>
38 #include <stdint.h>
39
40 class Film;
41 class EncodeServerDescription;
42 class DCPVideo;
43 class Writer;
44 class Job;
45 class PlayerVideo;
46
47 /** @class Encoder
48  *  @brief Class to manage encoding to JPEG2000.
49  *
50  *  This class keeps a queue of frames to be encoded and distributes
51  *  the work around threads and encoding servers.
52  */
53
54 class Encoder : public boost::noncopyable, public ExceptionStore
55 {
56 public:
57         Encoder (boost::shared_ptr<const Film>, boost::shared_ptr<Writer>);
58         ~Encoder ();
59
60         /** Called to indicate that a processing run is about to begin */
61         void begin ();
62
63         /** Called to pass a bit of video to be encoded as the next DCP frame */
64         void encode (boost::shared_ptr<PlayerVideo> f);
65
66         /** Called when a processing run has finished */
67         void end ();
68
69         float current_encoding_rate () const;
70         int video_frames_enqueued () const;
71
72 private:
73
74         void frame_done ();
75
76         void encoder_thread (boost::optional<EncodeServerDescription>);
77         void terminate_threads ();
78         void servers_list_changed ();
79
80         /** Film that we are encoding */
81         boost::shared_ptr<const Film> _film;
82
83         /** Mutex for _time_history */
84         mutable boost::mutex _state_mutex;
85         /** List of the times of completion of the last _history_size frames;
86             first is the most recently completed.
87         */
88         std::list<struct timeval> _time_history;
89         /** Number of frames that we should keep history for */
90         static int const _history_size;
91
92         /** Mutex for _threads */
93         mutable boost::mutex _threads_mutex;
94         std::list<boost::thread *> _threads;
95         mutable boost::mutex _queue_mutex;
96         std::list<boost::shared_ptr<DCPVideo> > _queue;
97         /** condition to manage thread wakeups when we have nothing to do */
98         boost::condition _empty_condition;
99         /** condition to manage thread wakeups when we have too much to do */
100         boost::condition _full_condition;
101
102         boost::shared_ptr<Writer> _writer;
103         Waker _waker;
104
105         boost::shared_ptr<PlayerVideo> _last_player_video;
106
107         boost::signals2::scoped_connection _server_found_connection;
108 };
109
110 #endif