2 Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "active_captions.h"
22 #include "caption_content.h"
23 #include <boost/shared_ptr.hpp>
24 #include <boost/weak_ptr.hpp>
29 using boost::weak_ptr;
30 using boost::shared_ptr;
31 using boost::optional;
34 ActiveCaptions::add (DCPTimePeriod period, list<PlayerCaption>& pc, list<Period> p) const
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);
46 ActiveCaptions::get (DCPTimePeriod period) const
48 list<PlayerCaption> ps;
50 for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
52 shared_ptr<const CaptionContent> caption = i->first.lock ();
53 if (!caption || !caption->use()) {
57 add (period, ps, i->second);
63 /** Get the open captions that should be burnt into a given period.
64 * @param period Period of interest.
65 * @param always_burn_captions Always burn captions even if their content is not set to burn.
68 ActiveCaptions::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
70 list<PlayerCaption> ps;
72 for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
74 shared_ptr<const CaptionContent> caption = i->first.lock ();
79 if (!caption->use() || (!always_burn_captions && !caption->burn())) {
80 /* Not burning this content */
84 add (period, ps, i->second);
90 /** Remove subtitles that finish before a given time from our list.
91 * @param time Time to remove before.
94 ActiveCaptions::clear_before (DCPTime time)
97 for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
99 BOOST_FOREACH (Period j, i->second) {
100 if (!j.to || j.to.get() >= time) {
105 updated[i->first] = as;
111 /** Add a new subtitle with a from time.
112 * @param content Content that the subtitle is from.
113 * @param ps Subtitles.
114 * @param from From time for these subtitles.
117 ActiveCaptions::add_from (weak_ptr<const CaptionContent> content, PlayerCaption ps, DCPTime from)
119 if (_data.find(content) == _data.end()) {
120 _data[content] = list<Period>();
122 _data[content].push_back (Period (ps, from));
125 /** Add the to time for the last subtitle added from a piece of content.
126 * @param content Content that the subtitle is from.
127 * @param to To time for the last subtitle submitted to add_from for this content.
128 * @return Return the corresponding subtitles and their from time.
130 pair<PlayerCaption, DCPTime>
131 ActiveCaptions::add_to (weak_ptr<const CaptionContent> content, DCPTime to)
133 DCPOMATIC_ASSERT (_data.find(content) != _data.end());
135 _data[content].back().to = to;
137 BOOST_FOREACH (TextCaption& i, _data[content].back().subs.text) {
138 i.set_out (dcp::Time(to.seconds(), 1000));
141 return make_pair (_data[content].back().subs, _data[content].back().from);
144 /** @param content Some content.
145 * @return true if we have any active subtitles from this content.
148 ActiveCaptions::have (weak_ptr<const CaptionContent> content) const
150 Map::const_iterator i = _data.find(content);
151 if (i == _data.end()) {
155 return !i->second.empty();
159 ActiveCaptions::clear ()