Some subtitle renaming.
[dcpomatic.git] / src / lib / active_text.cc
1 /*
2     Copyright (C) 2017-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 #include "active_text.h"
22 #include "text_content.h"
23 #include <boost/shared_ptr.hpp>
24 #include <boost/weak_ptr.hpp>
25
26 using std::list;
27 using std::pair;
28 using std::make_pair;
29 using boost::weak_ptr;
30 using boost::shared_ptr;
31 using boost::optional;
32
33 void
34 ActiveText::add (DCPTimePeriod period, list<PlayerText>& pc, list<Period> p) const
35 {
36         BOOST_FOREACH (Period i, p) {
37                 DCPTimePeriod test (i.from, i.to.get_value_or(DCPTime::max()));
38                 optional<DCPTimePeriod> overlap = period.overlap (test);
39                 if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
40                         pc.push_back (i.subs);
41                 }
42         }
43 }
44
45 list<PlayerText>
46 ActiveText::get (DCPTimePeriod period) const
47 {
48         boost::mutex::scoped_lock lm (_mutex);
49
50         list<PlayerText> ps;
51
52         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
53
54                 shared_ptr<const TextContent> caption = i->first.lock ();
55                 if (!caption || !caption->use()) {
56                         continue;
57                 }
58
59                 add (period, ps, i->second);
60         }
61
62         return ps;
63 }
64
65 /** Get the open captions that should be burnt into a given period.
66  *  @param period Period of interest.
67  *  @param always_burn_captions Always burn captions even if their content is not set to burn.
68  */
69 list<PlayerText>
70 ActiveText::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
71 {
72         boost::mutex::scoped_lock lm (_mutex);
73
74         list<PlayerText> ps;
75
76         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
77
78                 shared_ptr<const TextContent> caption = i->first.lock ();
79                 if (!caption) {
80                         continue;
81                 }
82
83                 if (!caption->use() || (!always_burn_captions && !caption->burn())) {
84                         /* Not burning this content */
85                         continue;
86                 }
87
88                 add (period, ps, i->second);
89         }
90
91         return ps;
92 }
93
94 /** Remove subtitles that finish before a given time from our list.
95  *  @param time Time to remove before.
96  */
97 void
98 ActiveText::clear_before (DCPTime time)
99 {
100         boost::mutex::scoped_lock lm (_mutex);
101
102         Map updated;
103         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
104                 list<Period> as;
105                 BOOST_FOREACH (Period j, i->second) {
106                         if (!j.to || j.to.get() >= time) {
107                                 as.push_back (j);
108                         }
109                 }
110                 if (!as.empty ()) {
111                         updated[i->first] = as;
112                 }
113         }
114         _data = updated;
115 }
116
117 /** Add a new subtitle with a from time.
118  *  @param content Content that the subtitle is from.
119  *  @param ps Subtitles.
120  *  @param from From time for these subtitles.
121  */
122 void
123 ActiveText::add_from (weak_ptr<const TextContent> content, PlayerText ps, DCPTime from)
124 {
125         boost::mutex::scoped_lock lm (_mutex);
126
127         if (_data.find(content) == _data.end()) {
128                 _data[content] = list<Period>();
129         }
130         _data[content].push_back (Period (ps, from));
131 }
132
133 /** Add the to time for the last subtitle added from a piece of content.
134  *  @param content Content that the subtitle is from.
135  *  @param to To time for the last subtitle submitted to add_from for this content.
136  *  @return Return the corresponding subtitles and their from time.
137  */
138 pair<PlayerText, DCPTime>
139 ActiveText::add_to (weak_ptr<const TextContent> content, DCPTime to)
140 {
141         boost::mutex::scoped_lock lm (_mutex);
142
143         DCPOMATIC_ASSERT (_data.find(content) != _data.end());
144
145         _data[content].back().to = to;
146
147         BOOST_FOREACH (StringText& i, _data[content].back().subs.string) {
148                 i.set_out (dcp::Time(to.seconds(), 1000));
149         }
150
151         return make_pair (_data[content].back().subs, _data[content].back().from);
152 }
153
154 /** @param content Some content.
155  *  @return true if we have any active subtitles from this content.
156  */
157 bool
158 ActiveText::have (weak_ptr<const TextContent> content) const
159 {
160         boost::mutex::scoped_lock lm (_mutex);
161
162         Map::const_iterator i = _data.find(content);
163         if (i == _data.end()) {
164                 return false;
165         }
166
167         return !i->second.empty();
168 }
169
170 void
171 ActiveText::clear ()
172 {
173         boost::mutex::scoped_lock lm (_mutex);
174         _data.clear ();
175 }