Fix bugs in BroadcastInfo, and use it in SndfileSource
[ardour.git] / libs / ardour / broadcast_info.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ardour/broadcast_info.h>
22
23 #include <sstream>
24 #include <iomanip>
25
26 #include <glibmm.h>
27
28 #include <ardour/svn_revision.h>
29 #include <ardour/ardour.h>
30 #include <ardour/session.h>
31
32 #include <pbd/convert.h>
33
34 using namespace PBD;
35
36 namespace ARDOUR
37 {
38
39 static void
40 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
41 {
42         char buf[target_size+1];
43         va_list ap;
44
45         va_start (ap, fmt);
46         vsnprintf (buf, target_size+1, fmt, ap);
47         va_end (ap);
48
49         memset (target, 0, target_size);
50         memcpy (target, buf, target_size);
51
52 }
53
54 BroadcastInfo::BroadcastInfo () :
55   _has_info (false)
56 {
57         info = new SF_BROADCAST_INFO;
58         memset (info, 0, sizeof (*info));
59         
60         // Note: Set version to 1 when UMID is used, otherwise version should stay at 0
61         info->version = 0;
62         
63         time_t rawtime;
64         std::time (&rawtime);
65         _time = *localtime (&rawtime);
66 }
67
68 BroadcastInfo::~BroadcastInfo ()
69 {
70         delete info;
71 }
72
73 void
74 BroadcastInfo::set_from_session (Session const & session, int64_t time_ref)
75 {
76         set_description (session.name());
77         set_time_reference (time_ref);
78         set_origination_time ();
79         set_originator ();
80         set_originator_ref ();
81 }
82
83 bool
84 BroadcastInfo::load_from_file (string const & filename)
85 {
86         SNDFILE * file = 0;
87         SF_INFO info;
88         
89         info.format = 0;
90         
91         if (!(file = sf_open (filename.c_str(), SFM_READ, &info))) {
92                 update_error();
93                 return false;
94         }
95         
96         bool ret = load_from_file (file);
97         
98         sf_close (file);
99         return ret;
100 }
101
102 bool
103 BroadcastInfo::load_from_file (SNDFILE* sf)
104 {
105         if (sf_command (sf, SFC_GET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
106                 update_error();
107                 _has_info = false;
108                 return false;
109         }
110         
111         _has_info = true;
112         return true;
113 }
114
115 string
116 BroadcastInfo::get_description () const
117 {
118         return info->description;
119 }
120
121 int64_t
122 BroadcastInfo::get_time_reference () const
123 {
124         if (!_has_info) {
125                 return 0;
126         }
127         
128         int64_t ret = (uint32_t) info->time_reference_high;
129         ret <<= 32;
130         ret |= (uint32_t) info->time_reference_low;
131         return ret;
132 }
133
134 struct tm
135 BroadcastInfo::get_origination_time () const
136 {
137         struct tm ret;
138         
139         string date = info->origination_date;
140         ret.tm_year = atoi (date.substr (0, 4)) - 1900;
141         ret.tm_mon = atoi (date.substr (5, 2));
142         ret.tm_mday = atoi (date.substr (8, 2));
143         
144         string time = info->origination_time;
145         ret.tm_hour = atoi (time.substr (0,2));
146         ret.tm_min = atoi (time.substr (3,2));
147         ret.tm_sec = atoi (time.substr (6,2));
148         
149         return ret;
150 }
151
152 string
153 BroadcastInfo::get_originator () const
154 {
155         return info->originator;
156 }
157
158 string
159 BroadcastInfo::get_originator_ref () const
160 {
161         return info->originator_reference;
162 }
163
164 bool
165 BroadcastInfo::write_to_file (string const & filename)
166 {
167         SNDFILE * file = 0;
168         SF_INFO info;
169         
170         info.format = 0;
171         
172         if (!(file = sf_open (filename.c_str(), SFM_RDWR, &info))) {
173                 update_error();
174                 return false;
175         }
176         
177         bool ret = write_to_file (file);
178         
179         sf_close (file);
180         return ret;
181 }
182
183 bool
184 BroadcastInfo::write_to_file (SNDFILE* sf)
185 {
186         if (sf_command (sf, SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
187                 update_error();
188                 return false;
189         }
190         
191         return true;
192 }
193
194 void
195 BroadcastInfo::set_description (string const & desc)
196 {
197         _has_info = true;
198         
199         snprintf_bounded_null_filled (info->description, sizeof (info->description), desc.c_str());
200 }
201
202 void
203 BroadcastInfo::set_time_reference (int64_t when)
204 {
205         _has_info = true;
206         
207         info->time_reference_high = (when >> 32);
208         info->time_reference_low = (when & 0xffffffff);
209 }
210
211 void
212 BroadcastInfo::set_origination_time (struct tm * now)
213 {
214         _has_info = true;
215         
216         if (now) {
217                 _time = *now;
218         }
219         
220         snprintf_bounded_null_filled (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
221                   _time.tm_year + 1900,
222                   _time.tm_mon + 1,
223                   _time.tm_mday);
224         
225         snprintf_bounded_null_filled (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
226                   _time.tm_hour,
227                   _time.tm_min,
228                   _time.tm_sec);
229 }
230
231 void
232 BroadcastInfo::set_originator (string const & str)
233 {
234         _has_info = true;
235         
236         if (!str.empty()) {
237                 snprintf_bounded_null_filled (info->originator, sizeof (info->originator), str.c_str());
238                 return;
239         }
240         
241         snprintf_bounded_null_filled (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
242 }
243
244 void
245 BroadcastInfo::set_originator_ref (string const & str)
246 {
247         _has_info = true;
248         
249         if (!str.empty()) {
250                 snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), str.c_str());
251                 return;
252         }
253         
254         /* random code is 9 digits */
255         
256         int random_code = random() % 999999999;
257         
258         /* Serial number is 12 chars */
259         
260         std::ostringstream serial_number;
261         serial_number << "ARDOUR" << "r" <<  std::setfill('0') << std::right << std::setw(5) << svn_revision;
262         
263         snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
264                   Config->get_bwf_country_code().c_str(),
265                   Config->get_bwf_organization_code().c_str(),
266                   serial_number.str().c_str(),
267                   _time.tm_hour,
268                   _time.tm_min,
269                   _time.tm_sec,
270                   random_code);
271         
272 }
273
274 void
275 BroadcastInfo::update_error ()
276 {
277         char errbuf[256];
278         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
279         error = errbuf;
280 }
281
282 } // namespace ARDOUR
283