2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <libcxml/cxml.h>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/lexical_cast.hpp>
24 #include "sndfile_content.h"
25 #include "sndfile_decoder.h"
26 #include "video_content.h"
27 #include "ffmpeg_decoder.h"
28 #include "ffmpeg_content.h"
29 #include "imagemagick_decoder.h"
30 #include "imagemagick_content.h"
39 using boost::shared_ptr;
40 using boost::weak_ptr;
41 using boost::dynamic_pointer_cast;
42 using boost::lexical_cast;
45 : _audio_from (AUDIO_FFMPEG)
51 Playlist::Playlist (shared_ptr<const Playlist> other)
52 : _audio_from (other->_audio_from)
53 , _loop (other->_loop)
55 for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
56 _content.push_back ((*i)->clone ());
65 _audio_from = AUDIO_FFMPEG;
70 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
74 _content_connections.clear ();
76 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
79 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
81 _video.push_back (vc);
84 /* FFmpegContent is audio if we are doing AUDIO_FFMPEG */
85 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
86 if (fc && _audio_from == AUDIO_FFMPEG) {
87 _audio.push_back (fc);
90 /* SndfileContent trumps FFmpegContent for audio */
91 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
93 if (_audio_from == AUDIO_FFMPEG) {
94 /* This is our fist SndfileContent; clear any FFmpegContent and
95 say that we are using Sndfile.
98 _audio_from = AUDIO_SNDFILE;
101 _audio.push_back (sc);
104 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
108 /** @return Length of our audio */
110 Playlist::audio_length () const
112 ContentAudioFrame len = 0;
114 switch (_audio_from) {
116 /* FFmpeg content is sequential */
117 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
118 len += (*i)->audio_length ();
122 /* Sndfile content is simultaneous */
123 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
124 len = max (len, (*i)->audio_length ());
132 /** @return number of audio channels */
134 Playlist::audio_channels () const
138 switch (_audio_from) {
140 /* FFmpeg audio is sequential, so use the maximum channel count */
141 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
142 channels = max (channels, (*i)->audio_channels ());
146 /* Sndfile audio is simultaneous, so it's the sum of the channel counts */
147 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
148 channels += (*i)->audio_channels ();
157 Playlist::audio_frame_rate () const
159 if (_audio.empty ()) {
163 /* XXX: assuming that all content has the same rate */
164 return _audio.front()->audio_frame_rate ();
168 Playlist::video_frame_rate () const
170 if (_video.empty ()) {
174 /* XXX: assuming all the same */
175 return _video.front()->video_frame_rate ();
179 Playlist::video_size () const
181 if (_video.empty ()) {
182 return libdcp::Size ();
185 /* XXX: assuming all the same */
186 return _video.front()->video_size ();
190 Playlist::video_length () const
192 ContentVideoFrame len = 0;
193 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
194 len += (*i)->video_length ();
201 Playlist::has_audio () const
203 return !_audio.empty ();
207 Playlist::content_changed (weak_ptr<Content> c, int p)
209 ContentChanged (c, p);
213 Playlist::default_audio_mapping () const
216 if (_audio.empty ()) {
220 switch (_audio_from) {
223 /* XXX: assumes all the same */
224 if (_audio.front()->audio_channels() == 1) {
225 /* Map mono sources to centre */
226 m.add (AudioMapping::Channel (_audio.front(), 0), libdcp::CENTRE);
228 int const N = min (_audio.front()->audio_channels (), MAX_AUDIO_CHANNELS);
229 /* Otherwise just start with a 1:1 mapping */
230 for (int i = 0; i < N; ++i) {
231 m.add (AudioMapping::Channel (_audio.front(), i), (libdcp::Channel) i);
240 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
241 for (int j = 0; j < (*i)->audio_channels(); ++j) {
242 m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
244 if (n >= MAX_AUDIO_CHANNELS) {
248 if (n >= MAX_AUDIO_CHANNELS) {
260 Playlist::audio_digest () const
264 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
265 t += (*i)->digest ();
267 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
269 t += lexical_cast<string> (fc->audio_stream()->id);
273 t += lexical_cast<string> (_loop);
275 return md5_digest (t.c_str(), t.length());
279 Playlist::video_digest () const
283 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
284 t += (*i)->digest ();
285 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
286 if (fc && fc->subtitle_stream()) {
287 t += fc->subtitle_stream()->id;
291 t += lexical_cast<string> (_loop);
293 return md5_digest (t.c_str(), t.length());
297 Playlist::content_length () const
299 float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24;
300 int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000;
304 ContentVideoFrame (audio_length() * vfr / afr)
309 Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
311 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
312 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
314 string const type = (*i)->string_child ("Type");
315 boost::shared_ptr<Content> c;
317 if (type == "FFmpeg") {
318 c.reset (new FFmpegContent (*i));
319 } else if (type == "ImageMagick") {
320 c.reset (new ImageMagickContent (*i));
321 } else if (type == "Sndfile") {
322 c.reset (new SndfileContent (*i));
325 _content.push_back (c);
328 _loop = node->number_child<int> ("Loop");
334 Playlist::as_xml (xmlpp::Node* node)
336 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
337 (*i)->as_xml (node->add_child ("Content"));
340 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
344 Playlist::add (shared_ptr<Content> c)
346 _content.push_back (c);
352 Playlist::remove (shared_ptr<Content> c)
354 ContentList::iterator i = find (_content.begin(), _content.end(), c);
355 if (i != _content.end ()) {
364 Playlist::move_earlier (shared_ptr<Content> c)
366 ContentList::iterator i = find (_content.begin(), _content.end(), c);
367 if (i == _content.begin () || i == _content.end()) {
371 ContentList::iterator j = i;
381 Playlist::move_later (shared_ptr<Content> c)
383 ContentList::iterator i = find (_content.begin(), _content.end(), c);
384 if (i == _content.end()) {
388 ContentList::iterator j = i;
390 if (j == _content.end ()) {
401 Playlist::set_loop (int l)
407 shared_ptr<FFmpegContent>
408 Playlist::ffmpeg () const
410 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
411 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
417 return shared_ptr<FFmpegContent> ();