Skip to content

Commit 980e437

Browse files
authored
Merge pull request #510 from rtl-airband/gs/fix-stereo-mp3
Delay initialization of lame (mp3) context until after config parsing is complete
2 parents 21a8caf + fe9e939 commit 980e437

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

src/config.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using namespace std;
3434
static int parse_outputs(libconfig::Setting& outs, channel_t* channel, int i, int j, bool parsing_mixers) {
3535
int oo = 0;
3636
for (int o = 0; o < channel->output_count; o++) {
37+
channel->outputs[oo].has_mp3_output = false;
3738
channel->outputs[oo].lame = NULL;
3839
channel->outputs[oo].lamebuf = NULL;
3940

@@ -95,8 +96,7 @@ static int parse_outputs(libconfig::Setting& outs, channel_t* channel, int i, in
9596
}
9697
#endif /* LIBSHOUT_HAS_TLS */
9798

98-
channel->outputs[oo].lame = airlame_init(channel->mode, channel->highpass, channel->lowpass);
99-
channel->outputs[oo].lamebuf = (unsigned char*)malloc(sizeof(unsigned char) * LAMEBUF_SIZE);
99+
channel->outputs[oo].has_mp3_output = true;
100100
} else if (!strncmp(outs[o]["type"], "file", 4)) {
101101
channel->outputs[oo].data = XCALLOC(1, sizeof(struct file_data));
102102
channel->outputs[oo].type = O_FILE;
@@ -122,8 +122,7 @@ static int parse_outputs(libconfig::Setting& outs, channel_t* channel, int i, in
122122
fdata->split_on_transmission = outs[o].exists("split_on_transmission") ? (bool)(outs[o]["split_on_transmission"]) : false;
123123
fdata->include_freq = outs[o].exists("include_freq") ? (bool)(outs[o]["include_freq"]) : false;
124124

125-
channel->outputs[oo].lame = airlame_init(channel->mode, channel->highpass, channel->lowpass);
126-
channel->outputs[oo].lamebuf = (unsigned char*)malloc(sizeof(unsigned char) * LAMEBUF_SIZE);
125+
channel->outputs[oo].has_mp3_output = true;
127126

128127
if (fdata->split_on_transmission) {
129128
if (parsing_mixers) {

src/rtl_airband.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,29 @@ void init_demod(demod_params_t* params, Signal* signal, int device_start, int de
265265
#endif /* WITH_BCM_VC */
266266
}
267267

268-
void init_output(output_params_t* params, int device_start, int device_end, int mixer_start, int mixer_end) {
268+
bool init_output(channel_t* channel, output_t* output) {
269+
if (output->has_mp3_output) {
270+
output->lame = airlame_init(channel->mode, channel->highpass, channel->lowpass);
271+
output->lamebuf = (unsigned char*)malloc(sizeof(unsigned char) * LAMEBUF_SIZE);
272+
}
273+
if (output->type == O_ICECAST) {
274+
shout_setup((icecast_data*)(output->data), channel->mode);
275+
} else if (output->type == O_UDP_STREAM) {
276+
udp_stream_data* sdata = (udp_stream_data*)(output->data);
277+
if (!udp_stream_init(sdata, channel->mode, (size_t)WAVE_BATCH * sizeof(float))) {
278+
return false;
279+
}
280+
#ifdef WITH_PULSEAUDIO
281+
} else if (output->type == O_PULSE) {
282+
pulse_init();
283+
pulse_setup((pulse_data*)(output->data), channel->mode);
284+
#endif /* WITH_PULSEAUDIO */
285+
}
286+
287+
return true;
288+
}
289+
290+
void init_output_params(output_params_t* params, int device_start, int device_end, int mixer_start, int mixer_end) {
269291
assert(params != NULL);
270292

271293
params->mp3_signal = new Signal;
@@ -949,19 +971,9 @@ int main(int argc, char* argv[]) {
949971
channel_t* channel = &mixers[i].channel;
950972
for (int k = 0; k < channel->output_count; k++) {
951973
output_t* output = channel->outputs + k;
952-
if (output->type == O_ICECAST) {
953-
shout_setup((icecast_data*)(output->data), channel->mode);
954-
} else if (output->type == O_UDP_STREAM) {
955-
udp_stream_data* sdata = (udp_stream_data*)(output->data);
956-
if (!udp_stream_init(sdata, channel->mode, (size_t)WAVE_BATCH * sizeof(float))) {
957-
cerr << "Failed to initialize mixer " << i << " output " << k << " - aborting\n";
958-
error();
959-
}
960-
#ifdef WITH_PULSEAUDIO
961-
} else if (output->type == O_PULSE) {
962-
pulse_init();
963-
pulse_setup((pulse_data*)(output->data), channel->mode);
964-
#endif /* WITH_PULSEAUDIO */
974+
if (!init_output(channel, output)) {
975+
cerr << "Failed to initialize mixer " << i << " output " << k << " - aborting\n";
976+
error();
965977
}
966978
}
967979
}
@@ -972,19 +984,9 @@ int main(int argc, char* argv[]) {
972984

973985
for (int k = 0; k < channel->output_count; k++) {
974986
output_t* output = channel->outputs + k;
975-
if (output->type == O_ICECAST) {
976-
shout_setup((icecast_data*)(output->data), channel->mode);
977-
} else if (output->type == O_UDP_STREAM) {
978-
udp_stream_data* sdata = (udp_stream_data*)(output->data);
979-
if (!udp_stream_init(sdata, channel->mode, (size_t)WAVE_BATCH * sizeof(float))) {
980-
cerr << "Failed to initialize device " << i << " channel " << j << " output " << k << " - aborting\n";
981-
error();
982-
}
983-
#ifdef WITH_PULSEAUDIO
984-
} else if (output->type == O_PULSE) {
985-
pulse_init();
986-
pulse_setup((pulse_data*)(output->data), channel->mode);
987-
#endif /* WITH_PULSEAUDIO */
987+
if (!init_output(channel, output)) {
988+
cerr << "Failed to initialize device " << i << " channel " << j << " output " << k << " - aborting\n";
989+
error();
988990
}
989991
}
990992
}
@@ -1055,7 +1057,7 @@ int main(int argc, char* argv[]) {
10551057

10561058
// Setup the output and demod threads
10571059
if (multiple_output_threads == false) {
1058-
init_output(&output_params[0], 0, device_count, 0, mixer_count);
1060+
init_output_params(&output_params[0], 0, device_count, 0, mixer_count);
10591061

10601062
if (multiple_demod_threads == false) {
10611063
init_demod(&demod_params[0], output_params[0].mp3_signal, 0, device_count);
@@ -1066,16 +1068,16 @@ int main(int argc, char* argv[]) {
10661068
}
10671069
} else {
10681070
if (multiple_demod_threads == false) {
1069-
init_output(&output_params[0], 0, device_count, 0, 0);
1071+
init_output_params(&output_params[0], 0, device_count, 0, 0);
10701072
init_demod(&demod_params[0], output_params[0].mp3_signal, 0, device_count);
10711073
} else {
10721074
for (int i = 0; i < device_count; i++) {
1073-
init_output(&output_params[i], i, i + 1, 0, 0);
1075+
init_output_params(&output_params[i], i, i + 1, 0, 0);
10741076
init_demod(&demod_params[i], output_params[i].mp3_signal, i, i + 1);
10751077
}
10761078
}
10771079
if (mixer_count > 0) {
1078-
init_output(&output_params[output_thread_count - 1], 0, 0, 0, mixer_count);
1080+
init_output_params(&output_params[output_thread_count - 1], 0, 0, 0, mixer_count);
10791081
}
10801082
}
10811083

src/rtl_airband.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,14 @@ struct output_t {
184184
bool active;
185185
void* data;
186186

187-
// set if this output performs mp3 encoding
187+
// set to true in order to initialize `lame` and `lamebuf` after config parsing
188+
// is complete
189+
bool has_mp3_output;
190+
191+
// lame encoder and buffer for mp3 output. initialized after config parsing
192+
// if `uses_mp3_output` is true
188193
lame_t lame;
189-
unsigned char* lamebuf; // temporary buffer used for lame encoding
194+
unsigned char* lamebuf;
190195
};
191196

192197
struct freq_tag {

0 commit comments

Comments
 (0)