rendered paste body/*
HOW TO BUILD:
Source neuros-env then run:
arm-linux-gcc -o srvlink srvlink.c -lncooler-core -lncooler-media -L$PRJROOT/toolchain/arm-linux/lib -DBUILD_TARGET_ARM -I$PRJROOT/toolchain/arm-linux/include
- Other TODOs in the file
*/
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "cooler-core.h"
#include "cooler-media.h"
#define SAMPLERATE 44100
#define CHANNELS 2
#define READ_SIZE 4096 /* (1024 * 4) */
#define SLEEP_NO_BUFFERS 100
//TODO: most of this stuff shall be moved as into the nmsplugin.h public interface of cooler-media
typedef void * (*plugin_interface_get) (void);
#define NEUROS_OUTPUT_PLUGIN_INTERFACE "GetNmsOutputPlugin"
//END TODO
#define NEUROS_OUTPUT_PLUGIN_PATH NMS_PLUGIN_DIR "libdm320nmso.so"
static media_desc_t output_mode_info = {
.adesc = {
.audio_type = NMS_AC_PCM,
.num_channels = CHANNELS,
.sample_rate = SAMPLERATE,
.bitrate = CHANNELS * SAMPLERATE * 16,
},
.vdesc = {
.video_type = NMS_VC_NO_VIDEO,
.width = 0,
.height = 0,
.frame_rate = 0,
},
.sdesc = {
.subtitle_type = NMS_SC_NO_SUBTITLE,
},
};
static media_buf_t buf;
static media_output_plugin_t * plugin;
static int plugin_inited = 0;
#define cleanup() cleanup_(1)
static void cleanup_(int startmonitor)
{
if (plugin_inited) plugin->finish(1);
if (startmonitor) NmsStartMonitor();
}
static void signal_handler(int signum)
{
printf("------- Exiting on signal [%d] --------\n", signum);
cleanup();
exit(0);
}
static void signal_init(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
if (sigaction(SIGHUP, &sa, NULL)){}
if (sigaction(SIGINT, &sa, NULL)){}
if (sigaction(SIGABRT, &sa, NULL)){}
if (sigaction(SIGQUIT, &sa, NULL)){}
if (sigaction(SIGTERM, &sa, NULL)){}
}
static media_output_plugin_t * LoadNMSPlugin()
{
void * plugin_lib;
void * interface_get;
plugin_lib = dlopen(NEUROS_OUTPUT_PLUGIN_PATH, RTLD_LAZY);
if (plugin_lib == NULL) {
printf ("Neuros Output Plugin is not in %s\n", NEUROS_OUTPUT_PLUGIN_PATH);
return NULL;
}
interface_get = dlsym(plugin_lib, NEUROS_OUTPUT_PLUGIN_INTERFACE);
if (interface_get == NULL) {
printf ("Neuros Output Plugin has interface symbol %d\n", NEUROS_OUTPUT_PLUGIN_INTERFACE);
return NULL;
}
plugin = (media_output_plugin_t *) (((plugin_interface_get)interface_get)());
if (plugin == NULL) {
printf ("Neuros Output Plugin did not return plugin interface\n");
return NULL;
}
else {
printf("Neuros Ouput Plugin loaded: \"%s\" ver \"%s\" descr: \"%d\"\n",
plugin->brief, plugin->ver, plugin->description );
}
return plugin;
}
static int reads = 0;
int main()
{
media_output_plugin_t * plugin;
int ret;
plugin = LoadNMSPlugin();
if (plugin == NULL) return -1;
//I stop the monitor since this is holding the dm320 I/O locked.
if (NmsConnect() == 0) { printf("Stopping monitor.."); NmsStopMonitor(); printf(".stopped\n");}
else printf("No connection to nmsd\n");
//initialize plugin for doing only direct PCM-audio-only output
//second param (mode) doesn't really matter as it's NTSC/PAL for video
printf("calling plugin->init\n");
ret = plugin->init(&output_mode_info, 0);
if (ret == -1) {
printf(".failed\n");
cleanup();
return -1;
} else if (ret == 1) {
printf(".output locked\n");
cleanup_(0);
return -2;
} else {
plugin_inited = 1;
printf(".success\n");
}
FILE* f = fopen("/media/USB/test.raw", "r");
int bytes = -1;
memset(&buf, sizeof(buf), 0);
if (f == NULL) {
printf("Test file failed to open\n");
cleanup();
return -1;
}
printf("-------- calling plugin->start ----------\n");
ret = plugin->start();
if (ret == -1) {
printf("plugin->start failed\n");
cleanup();
return -1;
}
while ( reads++ < 1000 )
{
//Get output buffers (2st param = timeout, 3rd param = "we're not preloading")
ret = plugin->getBuffer(&buf, 0, 0);
//printf("Output buffer returned\tvbuf-size = %u \tabuf-size = %u \tsbuf-size = %u \n", buf.vbuf.size, buf.abuf.size, buf.sbuf.size);
if (ret == 1)
{
printf("No empty buffers available right now, sleeping %d\n", SLEEP_NO_BUFFERS);
continue;
} else if (ret < 0) {
printf("ERROR: plugin->getBuffer failed\n");
cleanup();
return -1;
}
if (buf.abuf.size > 0) {
bytes = fread(buf.abuf.data, 1, READ_SIZE, f);
buf.curbuf = &buf.abuf;
buf.abuf.size = bytes;
if (bytes <= 0) break;
} else {
cleanup();
printf("ERROR: plugin->getBuffer returned zero-length audio buffer\n");
return -1;
}
plugin->write(&buf);
}
fclose(f);
cleanup();
printf("------- Exiting normally --------\n");
return 0;
}