Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / dcp_examiner.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_examiner.h"
21 #include "dcp_content.h"
22 #include "exceptions.h"
23 #include "image.h"
24 #include "config.h"
25 #include <dcp/dcp.h>
26 #include <dcp/decrypted_kdm.h>
27 #include <dcp/cpl.h>
28 #include <dcp/reel.h>
29 #include <dcp/reel_picture_asset.h>
30 #include <dcp/reel_sound_asset.h>
31 #include <dcp/mono_picture_asset.h>
32 #include <dcp/mono_picture_frame.h>
33 #include <dcp/stereo_picture_asset.h>
34 #include <dcp/stereo_picture_frame.h>
35 #include <dcp/sound_asset.h>
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::list;
41 using std::cout;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44
45 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
46         : _video_length (0)
47         , _audio_length (0)
48         , _has_subtitles (false)
49         , _encrypted (false)
50         , _kdm_valid (false)
51 {
52         dcp::DCP dcp (content->directory ());
53         dcp.read ();
54
55         if (content->kdm ()) {
56                 dcp.add (dcp::DecryptedKDM (content->kdm().get(), Config::instance()->decryption_chain()->key().get ()));
57         }
58
59         if (dcp.cpls().size() == 0) {
60                 throw DCPError ("No CPLs found in DCP");
61         } else if (dcp.cpls().size() > 1) {
62                 throw DCPError ("Multiple CPLs found in DCP");
63         }
64
65         _name = dcp.cpls().front()->content_title_text ();
66
67         list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
68         for (list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin(); i != reels.end(); ++i) {
69
70                 if ((*i)->main_picture ()) {
71                         dcp::Fraction const frac = (*i)->main_picture()->frame_rate ();
72                         float const fr = float(frac.numerator) / frac.denominator;
73                         if (!_video_frame_rate) {
74                                 _video_frame_rate = fr;
75                         } else if (_video_frame_rate.get() != fr) {
76                                 throw DCPError (_("Mismatched frame rates in DCP"));
77                         }
78
79                         shared_ptr<dcp::PictureAsset> asset = (*i)->main_picture()->asset ();
80                         if (!_video_size) {
81                                 _video_size = asset->size ();
82                         } else if (_video_size.get() != asset->size ()) {
83                                 throw DCPError (_("Mismatched video sizes in DCP"));
84                         }
85
86                         _video_length += (*i)->main_picture()->duration();
87                 }
88
89                 if ((*i)->main_sound ()) {
90                         shared_ptr<dcp::SoundAsset> asset = (*i)->main_sound()->asset ();
91
92                         if (!_audio_channels) {
93                                 _audio_channels = asset->channels ();
94                         } else if (_audio_channels.get() != asset->channels ()) {
95                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
96                         }
97
98                         if (!_audio_frame_rate) {
99                                 _audio_frame_rate = asset->sampling_rate ();
100                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
101                                 throw DCPError (_("Mismatched audio frame rates in DCP"));
102                         }
103
104                         _audio_length += (*i)->main_sound()->duration();
105                 }
106
107                 if ((*i)->main_subtitle ()) {
108                         _has_subtitles = true;
109                 }
110         }
111
112         _encrypted = dcp.encrypted ();
113         _kdm_valid = true;
114
115         /* Check that we can read the first picture frame */
116         try {
117                 if (!dcp.cpls().empty () && !dcp.cpls().front()->reels().empty ()) {
118                         shared_ptr<dcp::PictureAsset> asset = dcp.cpls().front()->reels().front()->main_picture()->asset ();
119                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
120                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
121
122                         if (mono) {
123                                 mono->get_frame(0)->xyz_image ();
124                         } else {
125                                 stereo->get_frame(0)->xyz_image (dcp::EYE_LEFT);
126                         }
127
128                 }
129         } catch (dcp::DCPReadError& e) {
130                 _kdm_valid = false;
131                 if (_encrypted && content->kdm ()) {
132                         /* XXX: maybe don't use an exception for this */
133                         throw StringError (_("The KDM does not decrypt the DCP.  Perhaps it is targeted at the wrong CPL."));
134                 }
135         }
136 }