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