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 #include <list>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/thread.hpp>
23 #include <boost/thread/condition.hpp>
24 #include "exceptions.h"
25
26 class Film;
27 class EncodedData;
28 class AudioBuffers;
29 class Job;
30
31 namespace libdcp {
32         class MonoPictureAsset;
33         class MonoPictureAssetWriter;
34         class SoundAsset;
35         class SoundAssetWriter;
36 }
37
38 struct QueueItem
39 {
40 public:
41         enum Type {
42                 /** a normal frame with some JPEG200 data */
43                 FULL,
44                 /** a frame whose data already exists in the MXF,
45                     and we fake-write it; i.e. we update the writer's
46                     state but we use the data that is already on disk.
47                 */
48                 FAKE,
49                 /** this is a repeat of the last frame to be written */
50                 REPEAT
51         } type;
52
53         /** encoded data for FULL */
54         boost::shared_ptr<const EncodedData> encoded;
55         /** size of data for FAKE */
56         int size;
57         /** frame index */
58         int frame;
59 };
60
61 bool operator< (QueueItem const & a, QueueItem const & b);
62 bool operator== (QueueItem const & a, QueueItem const & b);
63
64 class Writer : public ExceptionStore
65 {
66 public:
67         Writer (boost::shared_ptr<Film>, boost::shared_ptr<Job>);
68
69         bool can_fake_write (int) const;
70         
71         void write (boost::shared_ptr<const EncodedData>, int);
72         void fake_write (int);
73         void write (boost::shared_ptr<const AudioBuffers>);
74         void repeat (int f);
75         void finish ();
76
77 private:
78
79         void thread ();
80         void check_existing_picture_mxf ();
81
82         /** our Film */
83         boost::shared_ptr<Film> _film;
84         boost::shared_ptr<Job> _job;
85         /** the first frame index that does not already exist in our MXF */
86         int _first_nonexistant_frame;
87
88         /** our thread, or 0 */
89         boost::thread* _thread;
90         /** true if our thread should finish */
91         bool _finish;
92         /** queue of things to write to disk */
93         std::list<QueueItem> _queue;
94         /** number of FULL frames whose JPEG200 data is currently held in RAM */
95         int _queued_full_in_memory;
96         /** mutex for thread state */
97         mutable boost::mutex _mutex;
98         /** condition to manage thread wakeups */
99         boost::condition _condition;
100         /** the data of the last written frame, or 0 if there isn't one */
101         boost::shared_ptr<const EncodedData> _last_written;
102         /** the index of the last written frame */
103         int _last_written_frame;
104         /** maximum number of frames to hold in memory, for when we are managing
105             ordering
106         */
107         static const int _maximum_frames_in_memory;
108
109         /** number of FULL written frames */
110         int _full_written;
111         /** number of FAKE written frames */
112         int _fake_written;
113         /** number of REPEAT written frames */
114         int _repeat_written;
115         /** number of frames pushed to disk and then recovered
116             due to the limit of frames to be held in memory.
117         */
118         int _pushed_to_disk;
119         
120         boost::shared_ptr<libdcp::MonoPictureAsset> _picture_asset;
121         boost::shared_ptr<libdcp::MonoPictureAssetWriter> _picture_asset_writer;
122         boost::shared_ptr<libdcp::SoundAsset> _sound_asset;
123         boost::shared_ptr<libdcp::SoundAssetWriter> _sound_asset_writer;
124 };