Difference between revisions of "Stream to memory (smem) tutorial"

From VideoLAN Wiki
Jump to navigation Jump to search
(Added some info about callbacks)
m (+{{Example code}}, add wikilinks, markup, syntaxhighlight, +{{docmod}})
 
(8 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{Example code}}
 
Here is a small tutorial intended to make a quick tour of the ''stream to memory'' module (smem) , included in VLC 1.1.0.
 
Here is a small tutorial intended to make a quick tour of the ''stream to memory'' module (smem) , included in VLC 1.1.0.
 
This module allows you to write a code which handles the video and audio data read by VLC.
 
This module allows you to write a code which handles the video and audio data read by VLC.
 
This page shows a basic code written in C which uses this module.
 
This page shows a basic code written in C which uses this module.
 +
 +
/!\ Starting from VLC 2.2, smem uses <code>size_t</code> instead of <code>unsigned int</code> for buffer sizes.
  
 
== Including VLC library ==
 
== Including VLC library ==
  
<pre>#include <stdio.h>
+
<syntaxhighlight lang="c">
 +
#include <stdio.h>
 
#include <stdint.h>
 
#include <stdint.h>
  
#include <vlc/vlc.h></pre>
+
#include <vlc/vlc.h>
 +
</syntaxhighlight>
  
 
== Defining callbacks ==
 
== Defining callbacks ==
  
 
VLC sends you the data you need via two functions that you define yourself. They will be run before and after the data is rendered. There are two functions for the audio data, and two for the video one. In this example I will just show how to deal with audio data, the process is the same for video handling.
 
VLC sends you the data you need via two functions that you define yourself. They will be run before and after the data is rendered. There are two functions for the audio data, and two for the video one. In this example I will just show how to deal with audio data, the process is the same for video handling.
<pre>void prepareRender(void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size); // Audio prerender callback
+
<syntaxhighlight lang="c">
void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, unsigned int size, int64_t pts); // Audio postrender callback
+
void prepareRender(void* p_audio_data, uint8_t** pp_pcm_buffer , size_t size); // Audio prerender callback
// Video prerender and postrender callbacks not implemented.</pre>
+
void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, size_t size, int64_t pts); // Audio postrender callback
 +
// Video prerender and postrender callbacks not implemented.
 +
</syntaxhighlight>
  
 
== Creating a VLC instance ==
 
== Creating a VLC instance ==
  
 
As with every application you build with libvlc, you have to initialize the library. But you also have to tell VLC the address of each callback you wrote. This is made by writing the address in the parameters used to launch VLC.
 
As with every application you build with libvlc, you have to initialize the library. But you also have to tell VLC the address of each callback you wrote. This is made by writing the address in the parameters used to launch VLC.
<pre>int main(int argc, char **argv)
+
<syntaxhighlight lang="c">int main(int argc, char **argv)
 
{
 
{
 
         // VLC pointers
 
         // VLC pointers
Line 38: Line 45:
  
 
         const char * const vlc_args[] = {
 
         const char * const vlc_args[] = {
              "-I", "dummy", // Don't use any interface
 
              "--ignore-config", // Don't use VLC's config
 
              "--extraintf=logger", // Log anything
 
 
               "--verbose=2", // Be much more verbose then normal for debugging purpose
 
               "--verbose=2", // Be much more verbose then normal for debugging purpose
 
               "--sout", smem_options // Stream to memory
 
               "--sout", smem_options // Stream to memory
Line 53: Line 57:
  
 
         return 0;
 
         return 0;
}</pre>
+
}
 +
</syntaxhighlight>
  
 
== Implementing callbacks ==
 
== Implementing callbacks ==
{{mmwiki|PCM format|PCM}}
+
{{mmwiki|label1=PCM format|PCM}}
The smem module sends us the data in PCM format.
+
The {{docmod|smem}} module sends us the data in [[PCM]] format.
 +
 
 +
The first callback, called <code>prepareRender</code> in this example, locks the mutex and sets the data pointer (i.e. where the data will be written). All you have to do here is to ensure that <code>*pp_pcm_buffer</code> points now to a valid array where <var>size</var> bytes can be written.
  
The first callback, called ''prepareRender'' in this example, locks the mutex and sets the data pointer (i.e. where the data will be written). All you have to do here is to ensure that ''*pp_pcm_buffer'' points now to a valid array where ''size'' bytes can be written.
+
The second callback, here <code>handleStream</code>, is the place where you can do everything you want with the data you got !
  
The second callback, here ''handleStream'', is the place where you can do everything you want with the data you got !
+
The <code>p_audio_data</code> pointer is linked to the object you set in the command line parameters. It can be useful in order to communicate with the rest of the program.
  
The pointer ''p_audio_data'' points to the object you set in the command line parameters. It can be useful to communicate with the rest of the application.
 
  
<pre>void prepareRender (void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size)
+
<syntaxhighlight lang="c">
 +
void prepareRender (void* p_audio_data, uint8_t** pp_pcm_buffer , size_t size)
 
{
 
{
 
         // TODO: Lock the mutex
 
         // TODO: Lock the mutex
Line 72: Line 79:
 
}
 
}
  
void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, unsigned int size, int64_t pts )
+
void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, size_t size, int64_t pts )
 
{
 
{
 
         // TODO: explain how data should be handled
 
         // TODO: explain how data should be handled
 
         // TODO: Unlock the mutex
 
         // TODO: Unlock the mutex
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
== Handling the data ==
 
== Handling the data ==
 
The PCM format is very raw, but you may still have to do some small transformations in order to be able to get the exact waveform of the audio stream.
 
The PCM format is very raw, but you may still have to do some small transformations in order to be able to get the exact waveform of the audio stream.
  
First, the audio data is sent as an array of bytes, but each sample may be coded on more bytes : if you use the unchanged array, the values won't mean anything. The parameter ''bits_per_sample'' helps you to know how to tackle this problem.
+
First, the audio data is sent as an array of bytes, but each sample may be coded on more bytes : if you use the unchanged array, the values won't mean anything. The parameter <code>bits_per_sample</code> helps you to know how to tackle this problem.
 +
 
 +
Then, the data is still in an unsigned format : the negative values have an offset which makes the data look weird.
  
Then, the data is still in an unsigned format : the negative values have an offset which makes the data look wierd.
+
[[Category:Coding]]
 +
[[Category:libVLC]]

Latest revision as of 09:38, 4 March 2019

This page contains example code.

Here is a small tutorial intended to make a quick tour of the stream to memory module (smem) , included in VLC 1.1.0. This module allows you to write a code which handles the video and audio data read by VLC. This page shows a basic code written in C which uses this module.

/!\ Starting from VLC 2.2, smem uses size_t instead of unsigned int for buffer sizes.

Including VLC library

#include <stdio.h>
#include <stdint.h>

#include <vlc/vlc.h>

Defining callbacks

VLC sends you the data you need via two functions that you define yourself. They will be run before and after the data is rendered. There are two functions for the audio data, and two for the video one. In this example I will just show how to deal with audio data, the process is the same for video handling.

void prepareRender(void* p_audio_data, uint8_t** pp_pcm_buffer , size_t size); // Audio prerender callback
void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, size_t size, int64_t pts); // Audio postrender callback
// Video prerender and postrender callbacks not implemented.

Creating a VLC instance

As with every application you build with libvlc, you have to initialize the library. But you also have to tell VLC the address of each callback you wrote. This is made by writing the address in the parameters used to launch VLC.

int main(int argc, char **argv)
{
        // VLC pointers
        libvlc_instance_t *vlcInstance;
        libvlc_media_player_t *mp;
        libvlc_media_t *media;

        // VLC options
        char smem_options[256];

        sprintf(smem_options, "#transcode{acodec=s16l}:smem{audio-postrender-callback=%lld,audio-prerender-callback=%lld}",
                // We are using transcode because smem only support raw audio and video formats

                 (long long int)(intptr_t)(void*)&handleStream, (long long int)(intptr_t)(void*)&prepareRender);
                // We print (as a decimal value) the addresses. Note that you can also define smem-audio-data : this pointer
                // will be passed to your callbacks (it may be useful to retrive some extra informations) but isn't required at all.

        const char * const vlc_args[] = {
              "--verbose=2", // Be much more verbose then normal for debugging purpose
              "--sout", smem_options // Stream to memory
               };

        // We launch VLC
        vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);

        mp = libvlc_media_player_new(vlcInstance);

        // To be continued.

        return 0;
}

Implementing callbacks

The smem module sends us the data in PCM format.

The first callback, called prepareRender in this example, locks the mutex and sets the data pointer (i.e. where the data will be written). All you have to do here is to ensure that *pp_pcm_buffer points now to a valid array where size bytes can be written.

The second callback, here handleStream, is the place where you can do everything you want with the data you got !

The p_audio_data pointer is linked to the object you set in the command line parameters. It can be useful in order to communicate with the rest of the program.


void prepareRender (void* p_audio_data, uint8_t** pp_pcm_buffer , size_t size)
{
        // TODO: Lock the mutex

        *pp_pcm_buffer = // TODO
}

void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, size_t size, int64_t pts )
{
        // TODO: explain how data should be handled
        // TODO: Unlock the mutex
}

Handling the data

The PCM format is very raw, but you may still have to do some small transformations in order to be able to get the exact waveform of the audio stream.

First, the audio data is sent as an array of bytes, but each sample may be coded on more bytes : if you use the unchanged array, the values won't mean anything. The parameter bits_per_sample helps you to know how to tackle this problem.

Then, the data is still in an unsigned format : the negative values have an offset which makes the data look weird.