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