1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
Research Center Canada)
Copyright (C) 2013 Matthias P. Braendli
http://mpb.li
ZeroMQ input. see www.zeromq.org for more info
From the ZeroMQ manpage 'zmq':
The 0MQ lightweight messaging kernel is a library which extends the standard
socket interfaces with features traditionally provided by specialised
messaging middleware products. 0MQ sockets provide an abstraction of
asynchronous message queues, multiple messaging patterns, message filtering
(subscriptions), seamless access to multiple transport protocols and more.
*/
/*
This file is part of CRC-DabMux.
CRC-DabMux is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CRC-DabMux is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CRC-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dabInput.h"
#include "dabInputZmq.h"
#include "dabInputFifo.h"
#include "StatsServer.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_INPUT_ZEROMQ
#include <stdio.h>
#include <zmq.h>
#include <list>
#include <string.h>
#include <string>
#include <limits.h>
#ifdef __MINGW32__
# define bzero(s, n) memset(s, 0, n)
#endif
extern StatsServer global_stats;
struct dabInputOperations dabInputZmqOperations = {
dabInputZmqInit,
dabInputZmqOpen,
dabInputSetbuf,
NULL,
NULL,
NULL,
dabInputZmqReadFrame,
dabInputSetbitrate,
dabInputZmqClose,
dabInputZmqClean,
NULL
};
int dabInputZmqInit(void** args)
{
dabInputZmqData* input = new dabInputZmqData;
input->zmq_context = zmq_ctx_new();
if (input->zmq_context == NULL) {
etiLog.log(error, "Failed to initialise ZeroMQ context: %s\n", zmq_strerror(errno));
return 1;
}
input->zmq_sock = zmq_socket(input->zmq_context, ZMQ_SUB);
if (input->zmq_sock == NULL) {
etiLog.log(error, "Failed to initialise ZeroMQ socket: %s\n", zmq_strerror(errno));
return 1;
}
input->prebuffering = INPUT_ZMQ_PREBUFFERING;
*args = input;
return 0;
}
int dabInputZmqOpen(void* args, const char* inputUri)
{
dabInputZmqData* input = (dabInputZmqData*)args;
std::string uri = "tcp://" + std::string(inputUri);
int connect_error = zmq_bind(input->zmq_sock, uri.c_str());
if (connect_error < 0) {
etiLog.log(error, "Failed to connect socket to uri '%s': %s\n", uri.c_str(), zmq_strerror(errno));
return 1;
}
connect_error = zmq_setsockopt(input->zmq_sock, ZMQ_SUBSCRIBE, NULL, 0);
if (connect_error < 0) {
etiLog.log(error, "Failed to subscribe to zmq messages: %s\n", zmq_strerror(errno));
return 1;
}
global_stats.registerInput(uri);
input->uri = uri;
return 0;
}
// size corresponds to a frame size. It is constant for a given bitrate
int dabInputZmqReadFrame(dabInputOperations* ops, void* args, void* buffer, int size)
{
int rc;
dabInputZmqData* input = (dabInputZmqData*)args;
/* We must *always* read data from the ZMQ socket,
* to make sure that ZMQ internal buffers are emptied
* quickly. It's the only way to control the buffers
* of the whole path from encoder to our frame_buffer.
*/
rc = dabInputZmqReadFromSocket(input, size);
/* Notify of a buffer overrun, and drop some frames */
if (input->frame_buffer.size() >= INPUT_ZMQ_MAX_BUFFER_SIZE) {
global_stats.notifyOverrun(input->uri);
/* If the buffer is really too full, we drop as many frames as needed
* to get down to the prebuffering size. We would like to have our buffer
* filled to the prebuffering length.
*/
if (input->frame_buffer.size() >= 1.5*INPUT_ZMQ_MAX_BUFFER_SIZE) {
size_t over_max = input->frame_buffer.size() - INPUT_ZMQ_PREBUFFERING;
while (over_max--) {
input->frame_buffer.pop_front();
}
}
else {
/* Our frame_buffer contains DAB logical frames. Five of these make one
* AAC superframe.
*
* Dropping this superframe amounts to dropping 120ms of audio. */
input->frame_buffer.pop_front();
input->frame_buffer.pop_front();
input->frame_buffer.pop_front();
input->frame_buffer.pop_front();
input->frame_buffer.pop_front();
}
}
if (input->prebuffering > 0) {
if (rc > 0)
input->prebuffering--;
if (input->prebuffering == 0)
etiLog.log(info, "inputZMQ %s input pre-buffering complete\n",
input->uri.c_str());
/* During prebuffering, give a zeroed frame to the mux */
global_stats.notifyUnderrun(input->uri);
memset(buffer, 0, size);
return size;
}
// Save stats data in bytes, not in frames
global_stats.notifyBuffer(input->uri, input->frame_buffer.size() * size);
if (input->frame_buffer.empty()) {
etiLog.log(warn, "inputZMQ %s input empty, re-enabling pre-buffering\n",
input->uri.c_str());
// reset prebuffering
input->prebuffering = INPUT_ZMQ_PREBUFFERING;
/* We have no data to give, we give a zeroed frame */
global_stats.notifyUnderrun(input->uri);
memset(buffer, 0, size);
return size;
}
else
{
/* Normal situation, give a frame from the frame_buffer */
char* newframe = input->frame_buffer.front();
memcpy(buffer, newframe, size);
delete[] newframe;
input->frame_buffer.pop_front();
return size;
}
}
// Read a superframe from the socket, cut it into five frames, and push to list
int dabInputZmqReadFromSocket(dabInputZmqData* input, int framesize)
{
int rc;
zmq_msg_t msg;
rc = zmq_msg_init(&msg);
if (rc == -1) {
etiLog.log(error, "Failed to init zmq message: %s\n", zmq_strerror(errno));
return 0;
}
int nBytes = zmq_msg_recv(&msg, input->zmq_sock, ZMQ_DONTWAIT);
if (nBytes == -1) {
if (errno != EAGAIN) {
etiLog.log(error, "Failed to receive zmq message: %s\n", zmq_strerror(errno));
}
zmq_msg_close(&msg);
return 0;
}
char* data = (char*)zmq_msg_data(&msg);
/* TS 102 563, Section 6:
* Audio super frames are transported in five successive DAB logical frames
* with additional error protection.
*/
if (nBytes == 5*framesize)
{
if (input->frame_buffer.size() > INPUT_ZMQ_MAX_BUFFER_SIZE) {
etiLog.level(warn) <<
"inputZMQ " << input->uri <<
" buffer full (" << input->frame_buffer.size() << "),"
" dropping incoming superframe !";
nBytes = 0;
}
else {
// copy the input frame blockwise into the frame_buffer
for (char* framestart = data;
framestart < &data[5*framesize];
framestart += framesize) {
char* frame = new char[framesize];
memcpy(frame, framestart, framesize);
input->frame_buffer.push_back(frame);
}
}
}
else
{
etiLog.level(error) <<
"inputZMQ " << input->uri <<
" wrong data size: recv'd" << nBytes <<
", need " << 5*framesize << ".";
nBytes = 0;
}
zmq_msg_close(&msg);
return nBytes;
}
int dabInputZmqClose(void* args)
{
dabInputZmqData* input = (dabInputZmqData*)args;
zmq_close(input->zmq_sock);
return 0;
}
int dabInputZmqClean(void** args)
{
dabInputZmqData* input = (dabInputZmqData*)(*args);
zmq_ctx_term(input->zmq_context);
delete input;
return 0;
}
#endif
|