Missed update to private test repo version.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2018 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 /** @file  src/lib/writer.h
22  *  @brief Writer class.
23  */
24
25 #include "types.h"
26 #include "player_text.h"
27 #include "exception_store.h"
28 #include "dcp_text_track.h"
29 #include <boost/shared_ptr.hpp>
30 #include <boost/weak_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
33 #include <list>
34
35 namespace dcp {
36         class Data;
37 }
38
39 class Film;
40 class AudioBuffers;
41 class Job;
42 class Font;
43 class ReferencedReelAsset;
44 class ReelWriter;
45
46 struct QueueItem
47 {
48 public:
49         QueueItem ()
50                 : size (0)
51                 , reel (0)
52                 , frame (0)
53                 , eyes (EYES_BOTH)
54         {}
55
56         enum Type {
57                 /** a normal frame with some JPEG200 data */
58                 FULL,
59                 /** a frame whose data already exists in the MXF,
60                     and we fake-write it; i.e. we update the writer's
61                     state but we use the data that is already on disk.
62                 */
63                 FAKE,
64                 REPEAT,
65         } type;
66
67         /** encoded data for FULL */
68         boost::optional<dcp::Data> encoded;
69         /** size of data for FAKE */
70         int size;
71         /** reel index */
72         size_t reel;
73         /** frame index within the reel */
74         int frame;
75         /** eyes for FULL, FAKE and REPEAT */
76         Eyes eyes;
77 };
78
79 bool operator< (QueueItem const & a, QueueItem const & b);
80 bool operator== (QueueItem const & a, QueueItem const & b);
81
82 /** @class Writer
83  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
84  *
85  *  This class creates sound and picture assets, then takes Data
86  *  or AudioBuffers objects (containing image or sound data respectively)
87  *  and writes them to the assets.
88  *
89  *  write() for Data (picture) can be called out of order, and the Writer
90  *  will sort it out.  write() for AudioBuffers must be called in order.
91  */
92
93 class Writer : public ExceptionStore, public boost::noncopyable
94 {
95 public:
96         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
97         ~Writer ();
98
99         void start ();
100
101         bool can_fake_write (Frame) const;
102
103         void write (dcp::Data, Frame, Eyes);
104         void fake_write (Frame, Eyes);
105         bool can_repeat (Frame) const;
106         void repeat (Frame, Eyes);
107         void write (boost::shared_ptr<const AudioBuffers>, DCPTime time);
108         void write (PlayerText text, TextType type, boost::optional<DCPTextTrack>, DCPTimePeriod period);
109         void write (std::list<boost::shared_ptr<Font> > fonts);
110         void write (ReferencedReelAsset asset);
111         void finish ();
112
113         void set_encoder_threads (int threads);
114
115 private:
116         void thread ();
117         void terminate_thread (bool);
118         bool have_sequenced_image_at_queue_head ();
119         size_t video_reel (int frame) const;
120         void set_digest_progress (Job* job, float progress);
121         void write_cover_sheet ();
122
123         /** our Film */
124         boost::shared_ptr<const Film> _film;
125         boost::weak_ptr<Job> _job;
126         std::vector<ReelWriter> _reels;
127         std::vector<ReelWriter>::iterator _audio_reel;
128         std::vector<ReelWriter>::iterator _subtitle_reel;
129         std::map<DCPTextTrack, std::vector<ReelWriter>::iterator> _caption_reels;
130
131         /** our thread, or 0 */
132         boost::thread* _thread;
133         /** true if our thread should finish */
134         bool _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         /** maximum number of frames to hold in memory, for when we are managing
146          *  ordering
147          */
148         int _maximum_frames_in_memory;
149         unsigned int _maximum_queue_size;
150
151         /** number of FULL written frames */
152         int _full_written;
153         /** number of FAKE written frames */
154         int _fake_written;
155         int _repeat_written;
156         /** number of frames pushed to disk and then recovered
157             due to the limit of frames to be held in memory.
158         */
159         int _pushed_to_disk;
160
161         boost::mutex _digest_progresses_mutex;
162         std::map<boost::thread::id, float> _digest_progresses;
163
164         std::list<ReferencedReelAsset> _reel_assets;
165
166         std::list<boost::shared_ptr<Font> > _fonts;
167 };