Initial backend support for external export encoder
[ardour.git] / libs / ardour / playlist_source.cc
1 /*
2     Copyright (C) 2011 Paul Davis
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 #ifdef WAF_BUILD
20 #include "libardour-config.h"
21 #endif
22
23 #include <vector>
24 #include <cstdio>
25
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/error.h"
30 #include "pbd/types_convert.h"
31 #include "pbd/enumwriter.h"
32
33 #include "ardour/playlist.h"
34 #include "ardour/playlist_source.h"
35 #include "ardour/playlist_factory.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace std;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 PlaylistSource::PlaylistSource (Session& s, const ID& orig, const std::string& name, boost::shared_ptr<Playlist> p, DataType type,
44                                 sampleoffset_t begin, samplecnt_t len, Source::Flag /*flags*/)
45         : Source (s, type, name)
46         , _playlist (p)
47         , _original (orig)
48 {
49         /* PlaylistSources are never writable, renameable, removable or destructive */
50         _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
51
52         _playlist = p;
53         _playlist_offset = begin;
54         _playlist_length = len;
55
56         _level = _playlist->max_source_level () + 1;
57 }
58
59 PlaylistSource::PlaylistSource (Session& s, const XMLNode& node)
60         : Source (s, DataType::AUDIO, "toBeRenamed")
61 {
62         /* PlaylistSources are never writable, renameable, removable or destructive */
63         _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
64
65
66         if (set_state (node, Stateful::loading_state_version)) {
67                 throw failed_constructor ();
68         }
69 }
70
71 PlaylistSource::~PlaylistSource ()
72 {
73 }
74
75 void
76 PlaylistSource::add_state (XMLNode& node)
77 {
78         node.set_property ("playlist", _playlist->id ());
79         node.set_property ("offset", _playlist_offset);
80         node.set_property ("length", _playlist_length);
81         node.set_property ("original", id());
82
83         node.add_child_nocopy (_playlist->get_state());
84 }
85
86 int
87 PlaylistSource::set_state (const XMLNode& node, int /*version*/)
88 {
89         /* check that we have a playlist ID */
90
91         XMLProperty const * prop = node.property (X_("playlist"));
92
93         if (!prop) {
94                 error << _("No playlist ID in PlaylistSource XML!") << endmsg;
95                 throw failed_constructor ();
96         }
97
98         /* create playlist from child node */
99
100         XMLNodeList nlist;
101         XMLNodeConstIterator niter;
102
103         nlist = node.children();
104
105         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
106                 if ((*niter)->name() == "Playlist") {
107                         _playlist = PlaylistFactory::create (_session, **niter, true, false);
108                         break;
109                 }
110         }
111
112         if (!_playlist) {
113                 error << _("Could not construct playlist for PlaylistSource from session data!") << endmsg;
114                 throw failed_constructor ();
115         }
116
117         /* other properties */
118
119         std::string name;
120         if (!node.get_property (X_("name"), name)) {
121                 throw failed_constructor ();
122         }
123
124         set_name (name);
125
126         if (!node.get_property (X_("offset"), _playlist_offset)) {
127                 throw failed_constructor ();
128         }
129
130         if (!node.get_property (X_("length"), _playlist_length)) {
131                 throw failed_constructor ();
132         }
133
134         /* XXX not quite sure why we set our ID back to the "original" one
135            here. october 2011, paul
136         */
137
138         std::string str;
139         if (!node.get_property (X_("original"), str)) {
140                 throw failed_constructor ();
141         }
142
143         set_id (str);
144
145         _level = _playlist->max_source_level () + 1;
146
147         return 0;
148 }