Merge master into direct-mxf.
[dcpomatic.git] / src / lib / stream.cc
1 /*
2     Copyright (C) 2012 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 <sstream>
21 #include "compose.hpp"
22 #include "stream.h"
23 #include "ffmpeg_decoder.h"
24 #include "external_audio_decoder.h"
25
26 using std::string;
27 using std::stringstream;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 /** Construct a SubtitleStream from a value returned from to_string().
32  *  @param t String returned from to_string().
33  *  @param v State file version.
34  */
35 SubtitleStream::SubtitleStream (string t, boost::optional<int>)
36 {
37         stringstream n (t);
38         n >> _id;
39
40         size_t const s = t.find (' ');
41         if (s != string::npos) {
42                 _name = t.substr (s + 1);
43         }
44 }
45
46 /** @return A canonical string representation of this stream */
47 string
48 SubtitleStream::to_string () const
49 {
50         return String::compose ("%1 %2", _id, _name);
51 }
52
53 /** Create a SubtitleStream from a value returned from to_string().
54  *  @param t String returned from to_string().
55  *  @param v State file version.
56  */
57 shared_ptr<SubtitleStream>
58 SubtitleStream::create (string t, optional<int> v)
59 {
60         return shared_ptr<SubtitleStream> (new SubtitleStream (t, v));
61 }
62
63 /** Create an AudioStream from a string returned from to_string().
64  *  @param t String returned from to_string().
65  *  @param v State file version.
66  *  @return AudioStream, or 0.
67  */
68 shared_ptr<AudioStream>
69 audio_stream_factory (string t, optional<int> v)
70 {
71         shared_ptr<AudioStream> s;
72
73         s = FFmpegAudioStream::create (t, v);
74         if (!s) {
75                 s = ExternalAudioStream::create (t, v);
76         }
77
78         return s;
79 }
80
81 /** Create a SubtitleStream from a string returned from to_string().
82  *  @param t String returned from to_string().
83  *  @param v State file version.
84  *  @return SubtitleStream, or 0.
85  */
86 shared_ptr<SubtitleStream>
87 subtitle_stream_factory (string t, optional<int> v)
88 {
89         return SubtitleStream::create (t, v);
90 }