[pjsip] How To Create mem buffer WITH wav header

Thomas Plotkowiak plotti at gmx.net
Wed Nov 28 09:19:41 EST 2007


Create a buffer:

    const int mem_buffer_size = 8000*1024;
    unsigned char *mem_buffer;
    mem_buffer = new unsigned char[mem_buffer_size];

Create a wav-player-port
    status = pjmedia_wav_player_port_create(  pool, file, 20,
PJMEDIA_FILE_NO_LOOP ,0,  &file_port );

Create a mem-record-port
    status = pjmedia_mem_capture_create( pool,mem_buffer+44,mem_buffer_size,
                                                               file_port->
info.clock_rate,file_port->info.channel_count,file_port->
info.samples_per_frame, file_port->info.bits_per_sample,
                                                                0,
                                                                &mem_port
                                                                 );
Create a conference bridge

Add the wav-player-port to the conference bridge
Add the mem-record-port to the conference bridge

Connect the wav-player-port to the mem-record-port
status = pjmedia_conf_connect_port(conf, 2, 1, 0);

Now write a wav-header to the mem-record-port, notice that it was created
after 44 bytes

I wrote a small function that should do that:

void writeWavHeader(unsigned char *mem_buffer, int
mem_buffer_size,pjmedia_port *file_port){

    /* Setup WAV Header */
    unsigned short formatType =        1;
    unsigned short numChannels =    file_port->info.channel_count;
    unsigned long  sampleRate =        file_port->info.clock_rate;
    unsigned short bitsPerChannel = file_port->info.bits_per_sample;
    unsigned short bytesPerSample = (unsigned short) (bitsPerChannel >> 3) *
numChannels;
    unsigned long  bytesPerSecond = sampleRate * numChannels *
bytesPerSample;
    unsigned long  dataLength = mem_buffer_size;

    // constants for the canonical WAVE format
    const int fmtChunkLength = 16;                                // length
of fmt contents
    const int waveHeaderLength = 4 + 8 + fmtChunkLength + 8;    // from
"WAVE" to sample data

    unsigned long wholeLength = waveHeaderLength + dataLength;
    unsigned long chunkLength = fmtChunkLength;
    printf("%ul \n",wholeLength);


    // fill mem buffer
    int offset = 0;
    memcpy (mem_buffer + offset,"RIFF",4);

        printf ("%i %s \n",offset,"RIFF");
    offset += 4;
    memcpy (mem_buffer + offset,&wholeLength, sizeof(wholeLength));
        printf ("%i %ul \n",offset,wholeLength);
offset += sizeof(wholeLength);
    memcpy (mem_buffer + offset,"WAVE",4);

        printf ("%i %s \n",offset,"WAVE");
    offset += 4;
    memcpy (mem_buffer + offset,"fmt ",4);

        printf ("%i %s \n", offset,"fmt");
    offset += 4;
    memcpy (mem_buffer + offset,&chunkLength, sizeof(chunkLength));
        printf ("%i %ld \n", offset,chunkLength);
offset += sizeof(chunkLength);
    memcpy (mem_buffer + offset,&formatType, sizeof(formatType));

        printf("%i %hd \n",offset,formatType);
    offset += sizeof(formatType);
    memcpy (mem_buffer + offset,&numChannels, sizeof(numChannels));
        printf("%i %hd \n",offset,numChannels);
    offset += sizeof(numChannels);
    memcpy (mem_buffer + offset,&sampleRate, sizeof(sampleRate));

        printf("%i %d \n",offset,sampleRate);
offset += sizeof(sampleRate);
    memcpy (mem_buffer + offset,&bytesPerSecond, sizeof(bytesPerSecond));

        printf("%i %ld \n",offset,bytesPerSecond);
    offset += sizeof(bytesPerSecond);
    memcpy (mem_buffer + offset,&bytesPerSample, sizeof(bytesPerSample));

        printf("%i %hd \n",offset,bytesPerSample);
    offset += sizeof(bytesPerSample);
    memcpy (mem_buffer + offset,&bitsPerChannel, sizeof(bitsPerChannel));

        printf("%i %hd \n",offset,bitsPerChannel);
    offset += sizeof(bitsPerChannel);
    memcpy (mem_buffer + offset,"data",4);

        printf("%i %s \n",offset,"data");
offset +=4;
    memcpy (mem_buffer + offset,&dataLength, sizeof(dataLength));

        printf("%i %dl \n",offset,dataLength);
    offset +=sizeof(dataLength);
}

Theoretically I should be able now to play that buffer at the same time with
another soundengine. I am hearing the sound somehow but only if I am lucky
etc...
So where might the problem be, is it because they are both accessing the
same buffer?


Cheers
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/attachments/20071128/74b3008b/attachment.html>


More information about the pjsip mailing list