13

How do I send binary data, e.g. mp3/mp4 data back to the frontend ?

I know there are two ways of doing it: utilizing the sandbox filesystem provided by NACL and get the url at the frontend; passing the data through the PostMessage() using VarArrayBuffer. It would be great if someone could give me a simple example of how to pass the binary data through PostMessage(). There is a Pong example for NACl FileSystem API but I 'm kind of confused on how to retrieve the file location as url so that the frontend JS could get it through the message.

Here is what I have done so far, using the second method of passing data through PostMessage() and VarArrayBuffer:

  • I successfully retrieved data from online mp4 file and stored it in a char vector vector<char> outputBuffer.

  • Dumped the data into a new char buffer and create VarArrayBuffer to hold the data and pass it to the JS side

    char* binaryBuffer = new char[outputBuffer.size()];
    int increment = 0;
    for (vector<char>::iterator it = outputBuffer.begin(); 
         it != outputBuffer.end(); it++) {
         binaryBuffer[increment] = *it;
    }
    
    pp::VarArrayBuffer outBuffer(binaryBuffer);
    instance_->PostMessage(outBuffer);//instance_ is a NACL module instance
    

So, how should the JS side catch the array buffer? Is it through message.data or something else?

Towkir
  • 3,889
  • 2
  • 22
  • 41
JJin
  • 327
  • 4
  • 14
  • I don't see how the constructor of VarArrayBuffer should know about the size of binaryBuffer. I guess that your code compiles because the pp::Var::Var(const char* utf8_str) constructor that expects a null terminated UTF-8 string is called implicitly (https://developers.google.com/native-client/dev/peppercpp/classpp_1_1_var?hl=de#a24ae309e6e0335d2b16aab6039c231fa). – h0b0 Feb 07 '13 at 10:41

0 Answers0