Hello World

Goal

This is the first tutorial. It shows you how to create a simple but illustrative video player. Get ready, because in this first experience you will learn to:

  • Initialize the Fluendo SDK

  • Play a video stream in a new window

  • Get user feedback to close the player

  • Compile an application that depends on the Fluendo SDK

Some extra code, such as error handling or stopping the playback when the video finishes, has been omitted on purpose to keep this tutorial as short and straightforward as possible.

Prerequisites

Walkthrough

As usual, the first thing we need to do is including the proper header files. fluendo-sdk.h is the Fluendo SDK header file, which provides the API that we are going to use. stdio.h is included to get keyboard strokes.

Include headers
#include <fluendo-sdk.h>
#include <stdio.h>

flu_initialize call is needed to initialize the Fluendo SDK. This function must be called before any other function of the library. Immediately, we use flu_player_new to create a new FluPlayer instance which we use to load and play a video sample.

Initialize the Fluendo SDK and player
    flu_initialize();
    FluPlayer *player = flu_player_new();

Through the FluPlayer we can load a media source using flu_player_uri_open and start playing the video stream with flu_player_play. Pay attention that flu_player_uri_open works with URIs. In this case we load an HTTP resource, but in order to open a local file you will need to use something as file:///absolute/path/to/my/video.mp4. We leave the possibility to set any URI by passing it as the first argument of the application, but have the Tears of Steel set by default.

Open the video and start playing
    /* Set the URI to the first argument if given */
    const char *uri = "http://ftp.halifax.rwth-aachen.de/blender/demo/movies/ToS/tears_of_steel_720p.mov";
    if (argc > 1)
    {
        uri = argv[1];
        g_print("Setting video stream URI: %s\n", uri);
    }

    /* Ask the player to open the video and start playing it */
    flu_player_uri_open(player, uri);
    flu_player_play(player);

The flu_player_play function creates its own window where the video is playing on its own thread, so we need some mechanism that allows us to control the lifetime of the player. We use getchar as a simple way to wait until the user presses the q letter along with Enter. Note that this is a blocking call, so there is no CPU consumption while we are waiting on this loop.

Play the video until ‘q’ is pressed
    while (getchar() != 'q')
    {
        /* Empty loop, waiting for user input... */
    }

Finally, we need to cope with the shutdown of the player. We use flu_player_close to stop the playback and close the player window. flu_player_unref is needed to release a reference to the player, so that when there are none left its resources can be given back. The closing statement flu_shutdown deallocates all Fluendo SDK resources.

Close the player and shutdown
    flu_player_close(player);
    flu_player_unref(player);
    flu_shutdown();

Full source code

Full source code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <fluendo-sdk.h>
#include <stdio.h>

int main(int argc, const char **argv)
{
    /* Initialize the Fluendo SDK and create the player */
    flu_initialize();
    FluPlayer *player = flu_player_new();

    /* Set the URI to the first argument if given */
    const char *uri = "http://ftp.halifax.rwth-aachen.de/blender/demo/movies/ToS/tears_of_steel_720p.mov";
    if (argc > 1)
    {
        uri = argv[1];
        g_print("Setting video stream URI: %s\n", uri);
    }

    /* Ask the player to open the video and start playing it */
    flu_player_uri_open(player, uri);
    flu_player_play(player);

    /* Wait for key strokes on a blocking loop until key `q` is pressed */
    while (getchar() != 'q')
    {
        /* Empty loop, waiting for user input... */
    }

    /* Close the player, unref it and shutdown the Fluendo SDK */
    flu_player_close(player);
    flu_player_unref(player);
    flu_shutdown();

    return 0;
}

You can also download it here.

Building

This source code along with the rest of tutorials can be compiled using the following commands.

On Linux:

mkdir fluendo-sdk-tutorials && cd fluendo-sdk-tutorials
meson /opt/fluendo-sdk/share/doc/fluendo-sdk/tutorials/src
ninja

On Windows:

mkdir fluendo-sdk-tutorials
cd fluendo-sdk-tutorials
meson C:\fluendo-sdk\<version>\<x86/x86_64>\share\doc\fluendo-sdk\tutorials\src
ninja

To generate a Visual Studio project, you can pass the --backend=vs option to meson.

Conclusion

Let’s recap all that this tutorial covered:

Continue to the next tutorial to learn how subscribing to events can help us to create a more powerful and user-friendly player.