test/run_filter: use class FileDescriptor

This commit is contained in:
Max Kellermann 2019-03-25 08:53:58 +01:00
parent ca4e53859d
commit 3231706628

View File

@ -27,6 +27,7 @@
#include "pcm/Volume.hxx" #include "pcm/Volume.hxx"
#include "mixer/MixerControl.hxx" #include "mixer/MixerControl.hxx"
#include "system/Error.hxx" #include "system/Error.hxx"
#include "system/FileDescriptor.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/StringBuffer.hxx" #include "util/StringBuffer.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
@ -39,8 +40,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <unistd.h>
void void
mixer_set_volume(gcc_unused Mixer *mixer, mixer_set_volume(gcc_unused Mixer *mixer,
@ -61,9 +60,9 @@ LoadFilter(const ConfigData &config, const char *name)
} }
static size_t static size_t
ReadOrThrow(int fd, void *buffer, size_t size) ReadOrThrow(FileDescriptor fd, void *buffer, size_t size)
{ {
auto nbytes = read(fd, buffer, size); auto nbytes = fd.Read(buffer, size);
if (nbytes < 0) if (nbytes < 0)
throw MakeErrno("Read failed"); throw MakeErrno("Read failed");
@ -71,7 +70,7 @@ ReadOrThrow(int fd, void *buffer, size_t size)
} }
static void static void
FullRead(int fd, void *_buffer, size_t size) FullRead(FileDescriptor fd, void *_buffer, size_t size)
{ {
auto buffer = (uint8_t *)_buffer; auto buffer = (uint8_t *)_buffer;
@ -86,7 +85,7 @@ FullRead(int fd, void *_buffer, size_t size)
} }
static size_t static size_t
ReadFrames(int fd, void *_buffer, size_t size, size_t frame_size) ReadFrames(FileDescriptor fd, void *_buffer, size_t size, size_t frame_size)
{ {
auto buffer = (uint8_t *)_buffer; auto buffer = (uint8_t *)_buffer;
@ -141,17 +140,20 @@ try {
/* play */ /* play */
FileDescriptor input_fd(STDIN_FILENO);
FileDescriptor output_fd(STDOUT_FILENO);
while (true) { while (true) {
char buffer[4096]; char buffer[4096];
ssize_t nbytes = ReadFrames(0, buffer, sizeof(buffer), ssize_t nbytes = ReadFrames(input_fd, buffer, sizeof(buffer),
in_frame_size); in_frame_size);
if (nbytes == 0) if (nbytes == 0)
break; break;
auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes}); auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
nbytes = write(1, dest.data, dest.size); nbytes = output_fd.Write(dest.data, dest.size);
if (nbytes < 0) { if (nbytes < 0) {
fprintf(stderr, "Failed to write: %s\n", fprintf(stderr, "Failed to write: %s\n",
strerror(errno)); strerror(errno));