Merge master.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/lib/writer.h
21  *  @brief Writer class.
22  */
23
24 #include <list>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/thread.hpp>
27 #include <boost/thread/condition.hpp>
28 #include "exceptions.h"
29 #include "types.h"
30
31 class Film;
32 class EncodedData;
33 class AudioBuffers;
34 class Job;
35
36 namespace dcp {
37         class MonoPictureMXF;
38         class MonoPictureMXFWriter;
39         class StereoPictureMXF;
40         class StereoPictureMXFWriter;
41         class PictureMXF;
42         class PictureMXFWriter;
43         class SoundMXF;
44         class SoundMXFWriter;
45 }
46
47 struct QueueItem
48 {
49 public:
50         enum Type {
51                 /** a normal frame with some JPEG200 data */
52                 FULL,
53                 /** a frame whose data already exists in the MXF,
54                     and we fake-write it; i.e. we update the writer's
55                     state but we use the data that is already on disk.
56                 */
57                 FAKE,
58         } type;
59
60         /** encoded data for FULL */
61         boost::shared_ptr<const EncodedData> encoded;
62         /** size of data for FAKE */
63         int size;
64         /** frame index */
65         int frame;
66         Eyes eyes;
67 };
68
69 bool operator< (QueueItem const & a, QueueItem const & b);
70 bool operator== (QueueItem const & a, QueueItem const & b);
71
72 /** @class Writer
73  *  @brief Class to manage writing JPEG2000 and audio data to MXFs on disk.
74  *
75  *  This class creates sound and picture MXFs, then takes EncodedData
76  *  or AudioBuffers objects (containing image or sound data respectively)
77  *  and writes them to the MXFs.
78  *
79  *  ::write() for EncodedData can be called out of order, and the Writer
80  *  will sort it out.  write() for AudioBuffers must be called in order.
81  */
82
83 class Writer : public ExceptionStore, public boost::noncopyable
84 {
85 public:
86         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
87         ~Writer ();
88
89         bool can_fake_write (int) const;
90         
91         void write (boost::shared_ptr<const EncodedData>, int, Eyes);
92         void fake_write (int, Eyes);
93         void write (boost::shared_ptr<const AudioBuffers>);
94         void repeat (int f, Eyes);
95         void finish ();
96
97 private:
98
99         void thread ();
100         void terminate_thread (bool);
101         void check_existing_picture_mxf ();
102         bool check_existing_picture_mxf_frame (FILE *, int, Eyes);
103         bool have_sequenced_image_at_queue_head ();
104
105         /** our Film */
106         boost::shared_ptr<const Film> _film;
107         boost::weak_ptr<Job> _job;
108         /** the first frame index that does not already exist in our MXF */
109         int _first_nonexistant_frame;
110
111         /** our thread, or 0 */
112         boost::thread* _thread;
113         /** true if our thread should finish */
114         bool _finish;
115         /** queue of things to write to disk */
116         std::list<QueueItem> _queue;
117         /** number of FULL frames whose JPEG200 data is currently held in RAM */
118         int _queued_full_in_memory;
119         /** mutex for thread state */
120         mutable boost::mutex _mutex;
121         /** condition to manage thread wakeups when we have nothing to do  */
122         boost::condition _empty_condition;
123         /** condition to manage thread wakeups when we have too much to do */
124         boost::condition _full_condition;
125         /** the data of the last written frame, or 0 if there isn't one */
126         boost::shared_ptr<const EncodedData> _last_written[EYES_COUNT];
127         /** the index of the last written frame */
128         int _last_written_frame;
129         Eyes _last_written_eyes;
130         /** maximum number of frames to hold in memory, for when we are managing
131             ordering
132         */
133         static const int _maximum_frames_in_memory;
134
135         /** number of FULL written frames */
136         int _full_written;
137         /** number of FAKE written frames */
138         int _fake_written;
139         /** number of frames pushed to disk and then recovered
140             due to the limit of frames to be held in memory.
141         */
142         int _pushed_to_disk;
143         
144         boost::shared_ptr<dcp::PictureMXF> _picture_mxf;
145         boost::shared_ptr<dcp::PictureMXFWriter> _picture_mxf_writer;
146         boost::shared_ptr<dcp::SoundMXF> _sound_mxf;
147         boost::shared_ptr<dcp::SoundMXFWriter> _sound_mxf_writer;
148 };