Merge master.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <dcp/dcp.h>
21 #include "dcp_content.h"
22 #include "dcp_examiner.h"
23 #include "job.h"
24 #include "film.h"
25 #include "compose.hpp"
26
27 #include "i18n.h"
28
29 using std::string;
30 using boost::shared_ptr;
31
32 DCPContent::DCPContent (shared_ptr<const Film> f, boost::filesystem::path p)
33         : Content (f)
34         , VideoContent (f)
35         , SingleStreamAudioContent (f)
36         , SubtitleContent (f)
37         , _has_subtitles (false)
38         , _directory (p)
39 {
40         read_directory (p);
41 }
42
43 DCPContent::DCPContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
44         : Content (f, node)
45         , VideoContent (f, node, version)
46         , SingleStreamAudioContent (f, node, version)
47         , SubtitleContent (f, node, version)
48 {
49         _name = node->string_child ("Name");
50         _has_subtitles = node->bool_child ("HasSubtitles");
51         _directory = node->string_child ("Directory");
52 }
53
54 void
55 DCPContent::read_directory (boost::filesystem::path p)
56 {
57         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
58                 if (boost::filesystem::is_regular_file (i->path ())) {
59                         _paths.push_back (i->path ());
60                 } else if (boost::filesystem::is_directory (i->path ())) {
61                         read_directory (i->path ());
62                 }
63         }
64 }
65
66 void
67 DCPContent::examine (shared_ptr<Job> job)
68 {
69         job->set_progress_unknown ();
70         Content::examine (job);
71         
72         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
73         take_from_video_examiner (examiner);
74         take_from_audio_examiner (examiner);
75
76         boost::mutex::scoped_lock lm (_mutex);
77         _name = examiner->name ();
78         _has_subtitles = examiner->has_subtitles ();
79 }
80
81 string
82 DCPContent::summary () const
83 {
84         boost::mutex::scoped_lock lm (_mutex);
85         return String::compose (_("%1 [DCP]"), _name);
86 }
87
88 string
89 DCPContent::technical_summary () const
90 {
91         return Content::technical_summary() + " - "
92                 + VideoContent::technical_summary() + " - "
93                 + AudioContent::technical_summary() + " - ";
94 }
95
96 void
97 DCPContent::as_xml (xmlpp::Node* node) const
98 {
99         node->add_child("Type")->add_child_text ("DCP");
100
101         Content::as_xml (node);
102         VideoContent::as_xml (node);
103         SingleStreamAudioContent::as_xml (node);
104         SubtitleContent::as_xml (node);
105
106         boost::mutex::scoped_lock lm (_mutex);
107         node->add_child("Name")->add_child_text (_name);
108         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
109         node->add_child("Directory")->add_child_text (_directory.string ());
110 }
111
112 DCPTime
113 DCPContent::full_length () const
114 {
115         shared_ptr<const Film> film = _film.lock ();
116         assert (film);
117         return DCPTime (video_length (), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
118 }
119
120 string
121 DCPContent::identifier () const
122 {
123         return SubtitleContent::identifier ();
124 }
125
126 bool
127 DCPContent::has_subtitles () const
128 {
129         boost::mutex::scoped_lock lm (_mutex);
130         return _has_subtitles;
131 }