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"
41 using std::stringstream;
42 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::lexical_cast;
48 : _audio_from (AUDIO_FFMPEG)
54 Playlist::Playlist (shared_ptr<const Playlist> other)
55 : _audio_from (other->_audio_from)
56 , _loop (other->_loop)
58 for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
59 _content.push_back ((*i)->clone ());
68 _audio_from = AUDIO_FFMPEG;
73 for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
77 _content_connections.clear ();
79 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
82 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
84 _video.push_back (vc);
87 /* FFmpegContent is audio if we are doing AUDIO_FFMPEG */
88 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
89 if (fc && _audio_from == AUDIO_FFMPEG) {
90 _audio.push_back (fc);
93 /* SndfileContent trumps FFmpegContent for audio */
94 shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
96 if (_audio_from == AUDIO_FFMPEG) {
97 /* This is our fist SndfileContent; clear any FFmpegContent and
98 say that we are using Sndfile.
101 _audio_from = AUDIO_SNDFILE;
104 _audio.push_back (sc);
107 _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
111 /** @return Length of our audio */
113 Playlist::audio_length () const
115 ContentAudioFrame len = 0;
117 switch (_audio_from) {
119 /* FFmpeg content is sequential */
120 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
121 len += (*i)->audio_length ();
125 /* Sndfile content is simultaneous */
126 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
127 len = max (len, (*i)->audio_length ());
135 /** @return number of audio channels */
137 Playlist::audio_channels () const
141 switch (_audio_from) {
143 /* FFmpeg audio is sequential, so use the maximum channel count */
144 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
145 channels = max (channels, (*i)->audio_channels ());
149 /* Sndfile audio is simultaneous, so it's the sum of the channel counts */
150 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
151 channels += (*i)->audio_channels ();
160 Playlist::audio_frame_rate () const
162 if (_audio.empty ()) {
166 /* XXX: assuming that all content has the same rate */
167 return _audio.front()->audio_frame_rate ();
171 Playlist::video_frame_rate () const
173 if (_video.empty ()) {
177 /* XXX: assuming all the same */
178 return _video.front()->video_frame_rate ();
182 Playlist::video_size () const
184 if (_video.empty ()) {
185 return libdcp::Size ();
188 /* XXX: assuming all the same */
189 return _video.front()->video_size ();
193 Playlist::video_length () const
195 ContentVideoFrame len = 0;
196 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
197 len += (*i)->video_length ();
204 Playlist::has_audio () const
206 return !_audio.empty ();
210 Playlist::content_changed (weak_ptr<Content> c, int p)
212 ContentChanged (c, p);
216 Playlist::default_audio_mapping () const
219 if (_audio.empty ()) {
223 switch (_audio_from) {
226 /* XXX: assumes all the same */
227 if (_audio.front()->audio_channels() == 1) {
228 /* Map mono sources to centre */
229 m.add (AudioMapping::Channel (_audio.front(), 0), libdcp::CENTRE);
231 int const N = min (_audio.front()->audio_channels (), MAX_AUDIO_CHANNELS);
232 /* Otherwise just start with a 1:1 mapping */
233 for (int i = 0; i < N; ++i) {
234 m.add (AudioMapping::Channel (_audio.front(), i), (libdcp::Channel) i);
243 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
244 for (int j = 0; j < (*i)->audio_channels(); ++j) {
245 m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
247 if (n >= MAX_AUDIO_CHANNELS) {
251 if (n >= MAX_AUDIO_CHANNELS) {
263 Playlist::audio_digest () const
267 for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
268 t += (*i)->digest ();
270 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
272 t += lexical_cast<string> (fc->audio_stream()->id);
276 t += lexical_cast<string> (_loop);
278 return md5_digest (t.c_str(), t.length());
282 Playlist::video_digest () const
286 for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
287 t += (*i)->digest ();
288 shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
289 if (fc && fc->subtitle_stream()) {
290 t += fc->subtitle_stream()->id;
294 t += lexical_cast<string> (_loop);
296 return md5_digest (t.c_str(), t.length());
300 Playlist::content_length () const
302 float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24;
303 int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000;
307 ContentVideoFrame (audio_length() * vfr / afr)
312 Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
314 list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
315 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
317 string const type = (*i)->string_child ("Type");
318 boost::shared_ptr<Content> c;
320 if (type == "FFmpeg") {
321 c.reset (new FFmpegContent (*i));
322 } else if (type == "ImageMagick") {
323 c.reset (new ImageMagickContent (*i));
324 } else if (type == "Sndfile") {
325 c.reset (new SndfileContent (*i));
328 _content.push_back (c);
331 _loop = node->number_child<int> ("Loop");
337 Playlist::as_xml (xmlpp::Node* node)
339 for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
340 (*i)->as_xml (node->add_child ("Content"));
343 node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
347 Playlist::add (shared_ptr<Content> c)
349 _content.push_back (c);
355 Playlist::remove (shared_ptr<Content> c)
357 ContentList::iterator i = find (_content.begin(), _content.end(), c);
358 if (i != _content.end ()) {
367 Playlist::move_earlier (shared_ptr<Content> c)
369 ContentList::iterator i = find (_content.begin(), _content.end(), c);
370 if (i == _content.begin () || i == _content.end()) {
374 ContentList::iterator j = i;
384 Playlist::move_later (shared_ptr<Content> c)
386 ContentList::iterator i = find (_content.begin(), _content.end(), c);
387 if (i == _content.end()) {
391 ContentList::iterator j = i;
393 if (j == _content.end ()) {
404 Playlist::set_loop (int l)
410 shared_ptr<FFmpegContent>
411 Playlist::ffmpeg () const
413 for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
414 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
420 return shared_ptr<FFmpegContent> ();
424 Playlist::has_subtitles () const
426 shared_ptr<FFmpegContent> fc = ffmpeg ();
431 return !fc->subtitle_streams().empty();