Friday 2 June 2017

Data are not transferred correctly through a FIFO

I am trying to transfer MP3 buffer from a C program to a node.js program using a FIFO. I am doing this through buffering a segment the MP3 in a dynamic memory and then sending it to the node.js when it's ready.

My C code

void send_buffer(buffer* buff)
{
//signal(SIGPIPE,SIG_IGN);
// Attempts to open connection and returns INVALID on failure
int port = open("/tmp/radio", O_WRONLY | O_NONBLOCK);
if(port != -1)
{   
    // if the buffer was not filled
    if(buff == NULL)
    {
        return;
    }
    // Allocates enough memory for the header that will be sent to 
    char* segment = toBYTES(buff);

    // checks if memory allocation failed   
    if(segment == NULL)
    {
        return;
    }

    // saving it to a file to make sure the data in the buffer is correct
    FILE* file = fopen("temp.mp3", "a");
    if(file != NULL)
    {
        fwrite(segment, buff->size, 1, file);
        fclose(file);
    }

    // writes to the node
    write(port, segment, buff->size);       
    free(segment);
}

I am pretty sure the buffer data in the dynamic memory is correct because I tried writing it as shown in the code and the file plays in the media player correctly.

On the other hand my node js

var strict = require('use-strict');
var FIFO = require('fifo-js');
// establishing connection through the FIFO
var fifo = new FIFO("/tmp/radio");
// reading data from the fifo
fifo.setReader(function(data){
var fs = require('fs');
if(data.length != 0)
    fs.appendFile('temp.mp3', data);
});

I tried writing out the data received through the FIFO as well; however, unlike the c program, the media player said the file was corrupted and couldn't play it. I even checked the length of the string coming to the Node.js it was often a string of length zero. What am I missing here ? Is there any better way to transfer relatively big data from one program to another ?



via Abdullah Emad

No comments:

Post a Comment