probably fix cleanup issues, but testing required
[ardour.git] / libs / ardour / source.cc
1 /*
2     Copyright (C) 2000 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
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <float.h>
25 #include <cerrno>
26 #include <ctime>
27 #include <cmath>
28 #include <iomanip>
29 #include <algorithm>
30
31 #include <glibmm/thread.h>
32 #include <pbd/xml++.h>
33 #include <pbd/pthread_utils.h>
34
35 #include <ardour/source.h>
36 #include <ardour/playlist.h>
37
38 #include "i18n.h"
39
40 using std::min;
41 using std::max;
42
43 using namespace ARDOUR;
44
45 Source::Source (Session& s, string name)
46         : _session (s)
47 {
48         _name = name;
49         _timestamp = 0;
50         _in_use = 0;
51 }
52
53 Source::Source (Session& s, const XMLNode& node) 
54         : _session (s)
55 {
56         _timestamp = 0;
57         _in_use = 0;
58
59         if (set_state (node)) {
60                 throw failed_constructor();
61         }
62 }
63
64 Source::~Source ()
65 {
66         notify_callbacks ();
67 }
68
69 XMLNode&
70 Source::get_state ()
71 {
72         XMLNode *node = new XMLNode ("Source");
73         char buf[64];
74
75         node->add_property ("name", _name);
76         _id.print (buf, sizeof (buf));
77         node->add_property ("id", buf);
78
79         if (_timestamp != 0) {
80                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
81                 node->add_property ("timestamp", buf);
82         }
83
84         return *node;
85 }
86
87 int
88 Source::set_state (const XMLNode& node)
89 {
90         const XMLProperty* prop;
91
92         if ((prop = node.property ("name")) != 0) {
93                 _name = prop->value();
94         } else {
95                 return -1;
96         }
97         
98         if ((prop = node.property ("id")) != 0) {
99                 _id = prop->value ();
100         } else {
101                 return -1;
102         }
103
104         if ((prop = node.property ("timestamp")) != 0) {
105                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
106         }
107
108         return 0;
109 }
110
111 void
112 Source::add_playlist (boost::shared_ptr<Playlist> pl)
113 {
114         std::pair<PlaylistMap::iterator,bool> res;
115         std::pair<boost::shared_ptr<Playlist>, uint32_t> newpair (pl, 1);
116         Glib::Mutex::Lock lm (playlist_lock);
117
118         res = _playlists.insert (newpair);
119
120         if (!res.second) {
121                 /* it already existed, bump count */
122                 res.first->second++;
123         }
124                 
125         pl->GoingAway.connect (bind (mem_fun (*this, &Source::remove_playlist), boost::weak_ptr<Playlist> (pl)));
126 }
127
128 void
129 Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
130 {
131         boost::shared_ptr<Playlist> pl (wpl.lock());
132
133         if (!pl) {
134                 return;
135         }
136
137         PlaylistMap::iterator x;
138         Glib::Mutex::Lock lm (playlist_lock);
139
140         if ((x = _playlists.find (pl)) != _playlists.end()) {
141                 if (x->second > 1) {
142                         x->second--;
143                 } else {
144                         _playlists.erase (x);
145                 }
146         }
147 }
148
149 uint32_t
150 Source::used () const
151 {
152         return _playlists.size();
153 }