Add basic content information, and some other bits.
[dcpomatic.git] / src / lib / video_content.cc
1 #include <libcxml/cxml.h>
2 #include "video_content.h"
3 #include "video_decoder.h"
4
5 #include "i18n.h"
6
7 int const VideoContentProperty::VIDEO_LENGTH = 0;
8 int const VideoContentProperty::VIDEO_SIZE = 1;
9 int const VideoContentProperty::VIDEO_FRAME_RATE = 2;
10
11 using std::string;
12 using std::stringstream;
13 using std::setprecision;
14 using boost::shared_ptr;
15 using boost::lexical_cast;
16
17 VideoContent::VideoContent (boost::filesystem::path f)
18         : Content (f)
19         , _video_length (0)
20 {
21
22 }
23
24 VideoContent::VideoContent (shared_ptr<const cxml::Node> node)
25         : Content (node)
26 {
27         _video_length = node->number_child<ContentVideoFrame> ("VideoLength");
28         _video_size.width = node->number_child<int> ("VideoWidth");
29         _video_size.height = node->number_child<int> ("VideoHeight");
30         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
31 }
32
33 VideoContent::VideoContent (VideoContent const & o)
34         : Content (o)
35         , _video_length (o._video_length)
36         , _video_size (o._video_size)
37         , _video_frame_rate (o._video_frame_rate)
38 {
39
40 }
41
42 void
43 VideoContent::as_xml (xmlpp::Node* node) const
44 {
45         boost::mutex::scoped_lock lm (_mutex);
46         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
47         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
48         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
49         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
50 }
51
52 void
53 VideoContent::take_from_video_decoder (shared_ptr<VideoDecoder> d)
54 {
55         /* These decoder calls could call other content methods which take a lock on the mutex */
56         libdcp::Size const vs = d->native_size ();
57         float const vfr = d->frames_per_second ();
58         
59         {
60                 boost::mutex::scoped_lock lm (_mutex);
61                 _video_size = vs;
62                 _video_frame_rate = vfr;
63         }
64         
65         Changed (VideoContentProperty::VIDEO_SIZE);
66         Changed (VideoContentProperty::VIDEO_FRAME_RATE);
67 }
68
69
70 string
71 VideoContent::information () const
72 {
73         stringstream s;
74
75         s << String::compose (
76                 _("%1x%2 pixels (%3:1)"),
77                 video_size().width,
78                 video_size().height,
79                 setprecision (3), float (video_size().width) / video_size().height
80                 );
81         
82         return s.str ();
83 }