BWF USID according to EBU-R99
[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 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
25 #include <vector>
26
27 #include <glibmm.h>
28
29 #include "ardour/revision.h"
30 #include "ardour/session.h"
31 #include "ardour/session_metadata.h"
32
33 using namespace PBD;
34
35 namespace ARDOUR
36 {
37
38 static void
39 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
40 {
41         std::vector<char> buf(target_size+1);
42         va_list ap;
43
44         va_start (ap, fmt);
45         vsnprintf (&buf[0], target_size+1, fmt, ap);
46         va_end (ap);
47
48         memset (target, 0, target_size);
49         memcpy (target, &buf[0], target_size);
50
51 }
52
53 BroadcastInfo::BroadcastInfo ()
54 {
55
56 }
57
58 void
59 BroadcastInfo::set_from_session (Session const & session, int64_t time_ref)
60 {
61         set_description (session.name());
62         set_time_reference (time_ref);
63         set_origination_time ();
64         set_originator ();
65         set_originator_ref_from_session (session);
66 }
67
68 void
69 BroadcastInfo::set_originator (std::string const & str)
70 {
71         _has_info = true;
72
73         if (!str.empty()) {
74                 AudioGrapher::BroadcastInfo::set_originator (str);
75                 return;
76         }
77
78         snprintf_bounded_null_filled (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
79 }
80
81 void
82 BroadcastInfo::set_originator_ref_from_session (Session const & /*session*/)
83 {
84         _has_info = true;
85
86         /* random code is 9 digits */
87
88         int random_code = g_random_int() % 999999999;
89
90         /* Serial number is 12 chars */
91
92         std::ostringstream serial_number;
93         serial_number << PROGRAM_NAME << revision;
94
95         /* https://tech.ebu.ch/docs/r/r099.pdf
96          * CC Country code: (2 characters) based on the ISO 3166-1 standard
97          * OOO Organisation code: (3 characters) based on the EBU facility codes, Tech 3279 
98          * NNNNNNNNNNNN Serial number: (12 characters extracted from the recorder model and serial number) This should identify the machine’s type and serial number.
99          * HHMMSS OriginationTime (6 characters,) from the <OriginationTime> field of the BWF.
100          * RRRRRRRRR Random Number (9 characters 0-9) Generated locally by the recorder using some reasonably random algorithm. 
101          */
102         snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
103                   SessionMetadata::Metadata()->country().substr (0, 2).c_str(),
104                   SessionMetadata::Metadata()->organization().substr (0, 3).c_str(),
105                   serial_number.str().substr (0, 12).c_str(),
106                   _time.tm_hour,
107                   _time.tm_min,
108                   _time.tm_sec,
109                   random_code);
110
111 }
112
113 } // namespace ARDOUR
114