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