2009-08-24 18:57:06 +02:00
|
|
|
/*
|
2013-04-16 23:55:26 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-08-24 18:57:06 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-04-16 23:55:26 +02:00
|
|
|
#include "RecorderOutputPlugin.hxx"
|
2009-08-24 18:57:06 +02:00
|
|
|
#include "output_api.h"
|
|
|
|
#include "encoder_plugin.h"
|
|
|
|
#include "encoder_list.h"
|
2009-11-07 18:55:16 +01:00
|
|
|
#include "fd_util.h"
|
2010-05-20 06:59:25 +02:00
|
|
|
#include "open.h"
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "recorder"
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
struct RecorderOutput {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2009-08-24 18:57:06 +02:00
|
|
|
/**
|
|
|
|
* The configured encoder plugin.
|
|
|
|
*/
|
|
|
|
struct encoder *encoder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The destination file name.
|
|
|
|
*/
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The destination file descriptor.
|
|
|
|
*/
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The buffer for encoder_read().
|
|
|
|
*/
|
|
|
|
char buffer[32768];
|
2013-04-16 23:55:26 +02:00
|
|
|
|
|
|
|
bool Initialize(const config_param *param, GError **error_r) {
|
|
|
|
return ao_base_init(&base, &recorder_output_plugin, param,
|
|
|
|
error_r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deinitialize() {
|
|
|
|
ao_base_finish(&base);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configure(const config_param *param, GError **error_r);
|
|
|
|
|
|
|
|
bool WriteToFile(const void *data, size_t length, GError **error_r);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes pending data from the encoder to the output file.
|
|
|
|
*/
|
|
|
|
bool EncoderToFile(GError **error_r);
|
2009-08-24 18:57:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The quark used for GError.domain.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
|
|
|
recorder_output_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("recorder_output");
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
inline bool
|
|
|
|
RecorderOutput::Configure(const config_param *param, GError **error_r)
|
2009-08-24 18:57:06 +02:00
|
|
|
{
|
|
|
|
/* read configuration */
|
|
|
|
|
2012-10-01 23:56:10 +02:00
|
|
|
const char *encoder_name =
|
|
|
|
config_get_block_string(param, "encoder", "vorbis");
|
|
|
|
const struct encoder_plugin *encoder_plugin =
|
|
|
|
encoder_plugin_get(encoder_name);
|
2013-04-16 23:55:26 +02:00
|
|
|
if (encoder_plugin == nullptr) {
|
2009-08-24 18:57:06 +02:00
|
|
|
g_set_error(error_r, recorder_output_quark(), 0,
|
|
|
|
"No such encoder: %s", encoder_name);
|
2013-04-16 23:55:26 +02:00
|
|
|
return false;
|
2009-08-24 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
path = config_get_block_string(param, "path", nullptr);
|
|
|
|
if (path == nullptr) {
|
2009-08-24 18:57:06 +02:00
|
|
|
g_set_error(error_r, recorder_output_quark(), 0,
|
|
|
|
"'path' not configured");
|
2013-04-16 23:55:26 +02:00
|
|
|
return false;
|
2009-08-24 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize encoder */
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
encoder = encoder_init(encoder_plugin, param, error_r);
|
|
|
|
if (encoder == nullptr)
|
|
|
|
return false;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static audio_output *
|
|
|
|
recorder_output_init(const config_param *param, GError **error_r)
|
|
|
|
{
|
|
|
|
RecorderOutput *recorder = new RecorderOutput();
|
|
|
|
|
|
|
|
if (!recorder->Initialize(param, error_r)) {
|
|
|
|
delete recorder;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recorder->Configure(param, error_r)) {
|
|
|
|
recorder->Deinitialize();
|
|
|
|
delete recorder;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-07-18 15:39:19 +02:00
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
return &recorder->base;
|
2009-08-24 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
recorder_output_finish(struct audio_output *ao)
|
2009-08-24 18:57:06 +02:00
|
|
|
{
|
2013-04-16 23:55:26 +02:00
|
|
|
RecorderOutput *recorder = (RecorderOutput *)ao;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
encoder_finish(recorder->encoder);
|
2013-04-16 23:55:26 +02:00
|
|
|
recorder->Deinitialize();
|
|
|
|
delete recorder;
|
2009-08-24 18:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
inline bool
|
|
|
|
RecorderOutput::WriteToFile(const void *_data, size_t length, GError **error_r)
|
2009-08-24 18:57:06 +02:00
|
|
|
{
|
2012-10-01 23:59:50 +02:00
|
|
|
assert(length > 0);
|
2009-08-24 18:57:06 +02:00
|
|
|
|
2012-10-01 23:59:50 +02:00
|
|
|
const uint8_t *data = (const uint8_t *)_data, *end = data + length;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
while (true) {
|
2012-10-01 23:59:50 +02:00
|
|
|
ssize_t nbytes = write(fd, data, end - data);
|
2009-08-24 18:57:06 +02:00
|
|
|
if (nbytes > 0) {
|
2012-10-01 23:59:50 +02:00
|
|
|
data += nbytes;
|
|
|
|
if (data == end)
|
2009-08-24 18:57:06 +02:00
|
|
|
return true;
|
|
|
|
} else if (nbytes == 0) {
|
|
|
|
/* shouldn't happen for files */
|
|
|
|
g_set_error(error_r, recorder_output_quark(), 0,
|
|
|
|
"write() returned 0");
|
|
|
|
return false;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
g_set_error(error_r, recorder_output_quark(), 0,
|
|
|
|
"Failed to write to '%s': %s",
|
2013-04-16 23:55:26 +02:00
|
|
|
path, g_strerror(errno));
|
2009-08-24 18:57:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
inline bool
|
|
|
|
RecorderOutput::EncoderToFile(GError **error_r)
|
2012-10-01 23:59:50 +02:00
|
|
|
{
|
2013-04-16 23:55:26 +02:00
|
|
|
assert(fd >= 0);
|
2012-10-01 23:59:50 +02:00
|
|
|
|
2012-10-01 23:50:50 +02:00
|
|
|
while (true) {
|
|
|
|
/* read from the encoder */
|
2012-10-01 23:59:50 +02:00
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
size_t size = encoder_read(encoder, buffer, sizeof(buffer));
|
2012-10-01 23:50:50 +02:00
|
|
|
if (size == 0)
|
|
|
|
return true;
|
2012-10-01 23:59:50 +02:00
|
|
|
|
2012-10-01 23:50:50 +02:00
|
|
|
/* write everything into the file */
|
2012-10-01 23:59:50 +02:00
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
if (!WriteToFile(buffer, size, error_r))
|
2012-10-01 23:50:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-01 23:59:50 +02:00
|
|
|
}
|
|
|
|
|
2009-08-24 18:57:06 +02:00
|
|
|
static bool
|
2011-09-16 23:31:48 +02:00
|
|
|
recorder_output_open(struct audio_output *ao,
|
|
|
|
struct audio_format *audio_format,
|
2009-08-24 18:57:06 +02:00
|
|
|
GError **error_r)
|
|
|
|
{
|
2013-04-16 23:55:26 +02:00
|
|
|
RecorderOutput *recorder = (RecorderOutput *)ao;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
/* create the output file */
|
|
|
|
|
2010-05-20 06:59:25 +02:00
|
|
|
recorder->fd = open_cloexec(recorder->path,
|
|
|
|
O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,
|
2009-11-10 16:53:24 +01:00
|
|
|
0666);
|
2009-08-24 18:57:06 +02:00
|
|
|
if (recorder->fd < 0) {
|
|
|
|
g_set_error(error_r, recorder_output_quark(), 0,
|
|
|
|
"Failed to create '%s': %s",
|
|
|
|
recorder->path, g_strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the encoder */
|
|
|
|
|
2012-10-01 23:56:10 +02:00
|
|
|
if (!encoder_open(recorder->encoder, audio_format, error_r)) {
|
2009-08-24 18:57:06 +02:00
|
|
|
close(recorder->fd);
|
2012-10-01 23:17:13 +02:00
|
|
|
unlink(recorder->path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
if (!recorder->EncoderToFile(error_r)) {
|
2012-10-01 23:17:13 +02:00
|
|
|
encoder_close(recorder->encoder);
|
|
|
|
close(recorder->fd);
|
2009-08-24 18:57:06 +02:00
|
|
|
unlink(recorder->path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
recorder_output_close(struct audio_output *ao)
|
2009-08-24 18:57:06 +02:00
|
|
|
{
|
2013-04-16 23:55:26 +02:00
|
|
|
RecorderOutput *recorder = (RecorderOutput *)ao;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
/* flush the encoder and write the rest to the file */
|
|
|
|
|
2013-04-16 23:55:26 +02:00
|
|
|
if (encoder_end(recorder->encoder, nullptr))
|
|
|
|
recorder->EncoderToFile(nullptr);
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
/* now really close everything */
|
|
|
|
|
|
|
|
encoder_close(recorder->encoder);
|
|
|
|
|
|
|
|
close(recorder->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2011-09-16 23:31:48 +02:00
|
|
|
recorder_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
2009-08-24 18:57:06 +02:00
|
|
|
GError **error_r)
|
|
|
|
{
|
2013-04-16 23:55:26 +02:00
|
|
|
RecorderOutput *recorder = (RecorderOutput *)ao;
|
2009-08-24 18:57:06 +02:00
|
|
|
|
|
|
|
return encoder_write(recorder->encoder, chunk, size, error_r) &&
|
2013-04-16 23:55:26 +02:00
|
|
|
recorder->EncoderToFile(error_r)
|
2009-08-24 18:57:06 +02:00
|
|
|
? size : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct audio_output_plugin recorder_output_plugin = {
|
2013-04-16 23:55:26 +02:00
|
|
|
"recorder",
|
|
|
|
nullptr,
|
|
|
|
recorder_output_init,
|
|
|
|
recorder_output_finish,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
recorder_output_open,
|
|
|
|
recorder_output_close,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
recorder_output_play,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2009-08-24 18:57:06 +02:00
|
|
|
};
|