I want to send binary audio data to IBM watson STT service trhough websocket connection. I have successfully made the connection and now trying to send the data in following format:
{
"action":"start",
"content-type": "audio/l16;rate=44100"
}
<binary audio data>
{
"action":"stop"
}
for this: I am reading a raw audio file (extn .pcm) as below
#include <string>
#include <fstream>
ifstream fin;
string binarydata, bdataline;
fin.open("/home/rohan/TestFile.pcm", ios::binary | ios::in);
while (fin) {
// Read a Line from File
getline(fin, bdataline);
binarydata = binarydata + bdataline;
}
Question 1: I am not sure if I am reading the binary data correctly. Should the datatype of the binarydata
be string
?
Next to send the data on boost websocket (after handshake) I have followed this routine
void on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
// Send the Start message
ws_.async_write(net::buffer("{\"action\":\"start\",\"content-type\": \"audio/l16;rate=44100\"}"), bind(&session::on_start, shared_from_this(), placeholders::_1));
}
void on_start(beast::error_code ec)
{
if(ec)
return fail(ec, "write:start");
ws_.async_write(net::buffer(binarydata), bind(&session::on_binarysent, shared_from_this(), placeholders::_1));
}
void on_binarysent(beast::error_code ec)
{
if(ec)
return fail(ec, "write:Msg");
ws_.async_write(net::buffer("{\"action\":\"stop\"}"), bind(&session::on_write, shared_from_this(), placeholders::_1));
}
void on_write( beast::error_code ec) //,
{
if(ec)
return fail(ec, "write:end");
ws_.async_read(buffer_, bind(&session::on_start, shared_from_this(), placeholders::_1));
}
The program does not show any output and exits with
write:start: The WebSocket stream was gracefully closed at both endpoints
Question 2: Whether the data is going correctly as expected? How to check that? (expected : See this link)
How is the websocket getting closed without sending close command?
UPDATED:
void on_start(beast::error_code ec)
{
if(ec)
return fail(ec, "write:start");
ifstream infile("/home/rohan/TestFile.pcm", ios::in | ios::binary);
streampos FileSize;
if (infile) {
// Get the size of the file
infile.seekg(0, ios::end);
FileSize = infile.tellg();
infile.seekg(0, ios::beg);
}
char binarydata[(size_t)FileSize];
ws_.binary(true);
// Send binary data
ws_.async_write(net::buffer(binarydata, sizeof(binarydata)), bind(&session::on_binarysent, shared_from_this(), placeholders::_1));
}