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 /** @file src/lib/writer.h
21 * @brief Writer class.
25 #include "player_subtitles.h"
27 #include "exception_store.h"
28 #include <dcp/picture_asset_writer.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/weak_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
42 class MonoPictureAsset;
43 class MonoPictureAssetWriter;
44 class StereoPictureAsset;
45 class StereoPictureAssetWriter;
47 class PictureAssetWriter;
49 class SoundAssetWriter;
61 /** a normal frame with some JPEG200 data */
63 /** a frame whose data already exists in the MXF,
64 and we fake-write it; i.e. we update the writer's
65 state but we use the data that is already on disk.
71 /** encoded data for FULL */
72 boost::optional<Data> encoded;
73 /** size of data for FAKE */
80 bool operator< (QueueItem const & a, QueueItem const & b);
81 bool operator== (QueueItem const & a, QueueItem const & b);
84 * @brief Class to manage writing JPEG2000 and audio data to assets on disk.
86 * This class creates sound and picture assets, then takes Data
87 * or AudioBuffers objects (containing image or sound data respectively)
88 * and writes them to the assets.
90 * ::write() for Data can be called out of order, and the Writer
91 * will sort it out. write() for AudioBuffers must be called in order.
94 class Writer : public ExceptionStore, public boost::noncopyable
97 Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
102 bool can_fake_write (int) const;
104 void write (Data, int, Eyes);
105 void fake_write (int, Eyes);
106 void repeat (int, Eyes);
107 void write (boost::shared_ptr<const AudioBuffers>);
108 void write (PlayerSubtitles subs);
109 void write (std::list<boost::shared_ptr<Font> > fonts);
112 void set_encoder_threads (int threads);
117 void terminate_thread (bool);
118 void check_existing_picture_asset ();
119 bool check_existing_picture_asset_frame (FILE *, int, Eyes);
120 bool have_sequenced_image_at_queue_head ();
121 void write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const;
122 long frame_info_position (int frame, Eyes eyes) const;
123 dcp::FrameInfo read_frame_info (FILE* file, int frame, Eyes eyes) const;
126 boost::shared_ptr<const Film> _film;
127 boost::weak_ptr<Job> _job;
128 /** the first frame index that does not already exist in our MXF */
129 int _first_nonexistant_frame;
131 /** our thread, or 0 */
132 boost::thread* _thread;
133 /** true if our thread should finish */
135 /** queue of things to write to disk */
136 std::list<QueueItem> _queue;
137 /** number of FULL frames whose JPEG200 data is currently held in RAM */
138 int _queued_full_in_memory;
139 /** mutex for thread state */
140 mutable boost::mutex _state_mutex;
141 /** condition to manage thread wakeups when we have nothing to do */
142 boost::condition _empty_condition;
143 /** condition to manage thread wakeups when we have too much to do */
144 boost::condition _full_condition;
145 /** the data of the last written frame, if there is one */
146 boost::optional<Data> _last_written[EYES_COUNT];
147 /** the index of the last written frame */
148 int _last_written_frame;
149 Eyes _last_written_eyes;
150 /** maximum number of frames to hold in memory, for when we are managing
153 int _maximum_frames_in_memory;
155 /** number of FULL written frames */
157 /** number of FAKE written frames */
160 /** number of frames pushed to disk and then recovered
161 due to the limit of frames to be held in memory.
165 boost::shared_ptr<dcp::PictureAsset> _picture_asset;
166 boost::shared_ptr<dcp::PictureAssetWriter> _picture_asset_writer;
167 boost::shared_ptr<dcp::SoundAsset> _sound_asset;
168 boost::shared_ptr<dcp::SoundAssetWriter> _sound_asset_writer;
169 boost::shared_ptr<dcp::SubtitleAsset> _subtitle_asset;
171 std::list<boost::shared_ptr<Font> > _fonts;