mercredi 28 janvier 2015

C++ - CGI - Audio not working properly



I have a website with an HTML5 audio element whose audio data shall be served via a cgi script.

The markup is rather simple:



<audio controls>
<source type="audio/mpeg" src="audio.cgi?test.mp3">
<em>Me, your browser does not support HTML5 audio</em>
</audio>


The cgi is written in C++ and is pretty simple too, I know there is need of optimizing, e.g. reading the whole file in a buffer is really bad, but that's not the point.

This basic version kinda works, meaning the audio is played, but the player does not display the full length and one can only seek through the track in parts that have already been played.


If the audio file is placed in a location accessible via the web-server everything works fine.

The difference between these two methods seems to be, that the client issues a partial-content request if the latter method is chosen and an ordinary 200 if I try to serve the audio data via the cgi at once.


I wanted to implement partial-content serving into the cgi but I failed to read out the environment variable Request-Range, which is needed to serve the requested part of data.


This leads me to my questions:



  1. Why does the HTML5 player not display the full length of the track if I'm serving the audio data via the cgi script?

  2. Would implementing a partial-content handling solve this issue?

  3. If the partial-content handling is the right approach, how would I access the required environment variables in apache, since I have not found anything about them? Do I need to send a complete HTTP header indicating partial-content is coming, so the client knows he needs to send the required fields?


This is the source of the .cgi:



void serveAudio()
{
//tried these, were not the right ones
//getenv("HTTP_RANGE");
//getenv("HTTP_CONTENT_RANGE");

ifstream in(audioFile, ios::binary | ios::ate);
size_t size = in.tellg();
char *buffer = new char[size];

in.seekg(0, ios::beg);
in.read(buffer, size);

cout<<"Content-Type: audio/mpeg\n\n";
cout.write(buffer, size);
}


Any suggestions and helpful comments are appreciated!

Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire