Transcoding: Connect player to recorder

Goal

This very first transcoding tutorial focuses on connecting the player to a recorder without changing any encoding parameters. The output media will have exactly the same video and audio parameters as the input media. Output media video is encoded in H.264 and audio in AAC. After the tutorial you will know how to:

  • Create a player and a recorder, creating connections of the streams of the same kind

  • Configure player buffers notification

  • Copy raw buffers from the player’s streams to the recorder ones, effectively transcoding the video and audio

Introduction

Transcoding is used to convert from one encoding to another. Fluendo SDK player can output raw buffers from decoded media streams and, as you have learned in Webcam Recorder, Fluendo SDK recorder can encode raw streams and save them into a muxed file. Essentially, transcoding is the same as recording from a webcam but instead of using a device, we use the output from a player.

Walkthrough

Fluendo SDK API initialization and cleanup, so as main loop management, are similar as in all previous tutorials. Those application calls have been isolated into _init_app() and _clean_app() functions in order to simplify progression through upcoming transcoding tutorials.

Apart from setting up the usual listeners to detect errors, state changes or when EOS is reached, we subscribe to FLU_PLAYER_EVENT_REQUEST_RENDER_MODE and FLU_PLAYER_EVENT_RENDER.

Player creation/destruction
FluPlayer *_player_create(App *app)
{
    FluPlayer *player = flu_player_new();

    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_STATE, _player_on_state_changed, app);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_ERROR, _player_on_error, app->main_loop);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_EOS, _player_on_eos, app->main_loop);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_REQUEST_RENDER_MODE, _player_on_request_render_mode, NULL);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_RENDER, _player_on_render, app);

    return player;
}

Player default behavior is to quit the application on error or when EOS event is received. Recorder creation, destruction and events management are the same as in Webcam Recorder. Its default behavior is to quit the application on error. Output muxed file name is set through FLU_RECORDER_EVENT_REQUEST_SAVE_MODE event just like in Webcam Recorder.

In order to perform transcoding we need to:

  1. Request an input stream from the recorder for each available stream coming out of the player

  2. Configure the player to notify us each time a raw buffer is available from any stream

  3. Intercept each raw buffer coming out of the player’s streams and copy them to corresponding input streams in the recorder

All this can be done through listening to the right events on the player.

Request recorder’s streams

First, let’s begin by requesting streams from the recorder. We are going to use a convenient struct to keep relationships between player and recorder’s streams.

typedef struct
{
    FluStream *player_stream;
    FluStream *recorder_stream;
} Stream;

This struct is used in App struct in order to store a stream link for each player’s stream that we are going to transcode.


typedef struct
{
    gchar *input_uri;

    GMainLoop *main_loop;
    guint keyboard_source_id;

    FluPlayer *player;
    FluRecorder *recorder;

    /* Unlike previous fields which are initialized BEFORE threads creation
     * (see _init_app) and cleaned up AFTER joining all threads (see _clean_app),
     * nb_streams and streams pointers are initialized from the Fluendo-SDK events
     * thread (see _player_on_state_changed) and then read from potentially
     * different streaming threads (see _player_on_render). They are cleaned up
     * AFTER joining all threads like the rest of fields.
     * As nb_streams and streams pointers are only written once and then constant
     * until joining all threads, we only need a memory barrier (no need for locks).
     * We obtain it by using atomic for the streams pointer taking care of writing
     * nb_streams BEFORE setting the atomic pointer. */
    guint nb_streams;
    volatile Stream *streams;
} App;

As you can see in the previous code comment, we have to take extra care of variables access as all event listeners are not called from the same thread. In this particular case, we are going to write the streams’ links array when requesting streams from the recorder (which is done from the Fluendo SDK main event thread), but we are going to read this array when copying raw buffers from the player’s streams to the recorder’s streams (which is done from different streaming threads).

Recorder’s streams requests are done when the player is switching to playing state for the very first time. At this moment we are sure that all player active streams are available. This listener is exclusively called from the SDK’s main event thread, so we can avoid race conditions in the stream links array using memory fences instead of blocking synchronization.

static gboolean _player_on_state_changed(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    gboolean ret = TRUE;

    FluPlayerEventState *ev = (FluPlayerEventState *)event;
    g_print("Player state changed to %s\n", flu_player_state_name_get(ev->state));

    if (ev->state == FLU_PLAYER_STATE_PLAYING)
    {
        App *app = (App *)data;

        /* First time player goes to PLAYING state, we initialize a new recorder output stream
         * for each player stream, and we keep the relationships between corresponding streams.
         * This callback is called from the Fluendo-SDK event thread. As app->streams pointer
         * is only written once here and then read from streaming threads later, we use an atomic
         * in order to ensure a memory fence. */
        Stream *streams = g_atomic_pointer_get(&app->streams);
        if (!streams)
        {
            /* For the purpose of the tutorial, we just take into account video and audio streams,
             * but the same may be achieved with text and data streams too. */
            GList *player_streams = flu_player_video_active_streams_get(player);
            player_streams = g_list_concat(player_streams, flu_player_audio_active_streams_get(player));

            app->nb_streams = g_list_length(player_streams);
            streams = g_new0(Stream, app->nb_streams);

            Stream *stream_link = streams;
            for (GList *l = player_streams; l; l = l->next)
            {
                /* We don't need to keep any extra references to player or recorder streams. Weak
                 * references are enough as all links are only used within callbacks during lifetimes
                 * of the player and recorder (which already keep hard references to those streams). */
                stream_link->player_stream = (FluStream *)l->data;

                FluStreamType stream_type = flu_stream_type_get(stream_link->player_stream);
                stream_link->recorder_stream = _recorder_request_stream(app, stream_type);
                if (stream_link->recorder_stream)
                {
                    flu_stream_unref(stream_link->recorder_stream);
                }

                ++stream_link;
            }

            flu_stream_list_free(player_streams);
            g_atomic_pointer_set(&app->streams, streams);

            /* We are now ready for transcoding: let's start the recorder. */
            if (!_recorder_start(app))
            {
                ret = FALSE;
                g_main_loop_quit(app->main_loop);
            }
        }
    }

    return ret;
}

We are using flu_player_video_active_streams_get and flu_player_audio_active_streams_get functions to list all active video and audio streams coming out of the player. Then for each individual stream, we request a stream of the same type from the recorder. This way, we can store the link between the player’s streams and the recorder’s streams into the links array.

How a stream is concretely requested from the recorder is isolated in a separated function:

FluStream *_recorder_request_stream(const App *app, FluStreamType stream_type)
{
    FluStreamInfo info = {0};
    info.type = stream_type;

    if (stream_type == FLU_STREAM_TYPE_VIDEO)
    {
        info.data.video.vcodec.type = FLU_STREAM_VIDEO_CODEC_H264;
    }
    else if (stream_type == FLU_STREAM_TYPE_AUDIO)
    {
        info.data.audio.acodec = FLU_STREAM_AUDIO_CODEC_AAC;
    }
    else
    {
        return NULL;
    }

    GError *error = NULL;
    FluStream *stream = flu_recorder_request_stream(app->recorder, &info, &error);
    if (!stream)
    {
        g_print("Error: cannot create %s stream for recorder (%s)\n",
                (stream_type == FLU_STREAM_TYPE_VIDEO) ? "video" : "audio",
                error ? error->message : "undefined error");
        if (error)
        {
            g_error_free(error);
        }
    }

    return stream;
}

Requesting a recorder’s stream is as a matter of calling flu_recorder_request_stream function. As seen in Webcam Recorder when connecting a device, we need to pass a FluStreamInfo struct in order to configure the recorded stream. Here, we are just configuring the encoding codecs we are going to use (H.264 for video and AAC for audio). All other parameters (video dimensions, framerate, audio channels, etc…) are automatically copied from the player’s input streams. In the next tutorials, you will learn how to configure audio and video advanced parameters.

Once all the recorder’s streams are requested and bound to the player’s streams through the streams’ links array, the only thing left is to start the recorder.

gboolean _recorder_start(const App *app)
{
    GError *error = NULL;
    flu_recorder_record(app->recorder, &error);
    if (error)
    {
        g_print("Error: cannot start recorder (%s)\n", error->message);
        g_error_free(error);
        return FALSE;
    }

    return TRUE;
}

Configure player buffers notification

Now that we have streams coming out of the player and bound to recorder’s streams configured for encoding, we need to be notified each time a raw buffer is available from any player’s stream.

This is done by listening to the FLU_PLAYER_EVENT_REQUEST_RENDER_MODE event.

static gboolean _player_on_request_render_mode(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    FluPlayerEventRequestRenderMode *ev = (FluPlayerEventRequestRenderMode *)event;

    /* As we are just transcoding, we request:
     * - no visual rendering,
     * - no synchronization (buffers are treated as fast as they arrive),
     * - notification (to know when buffers are ready to be copied). */
    ev->render = FALSE;
    ev->synchronize = FALSE;
    ev->notify = TRUE;

    return TRUE;
}

FluPlayerEventRequestRenderMode event is a synchronous event. Listener callback is waiting for specific configuration. It is important not to perform any more processing here than just updating the requested configuration to avoid blocking the system.

Configuration is done using 3 booleans:

  • render: here we set it to FALSE as we don’t want to display videos or play sounds

  • synchronize: we also set it to FALSE as we want to process all buffers as fast as they arrive (and not wait for presentation time synchronization)

  • notify: we set it to TRUE in order to receive notification events each time a buffer is available

Setting the notify flag to TRUE is what enables FLU_PLAYER_EVENT_RENDER events that we are going to use to copy raw buffers from player to recorder.

Copy raw buffers

Now that we’ve activated FLU_PLAYER_EVENT_RENDER events, we just need to implement its listener in order to copy raw buffers from the player to the recorder.

static gboolean _player_on_render(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    FluPlayerEventRender *ev = (FluPlayerEventRender *)event;
    FluStreamType stream_type = flu_stream_type_get(ev->stream);

    /* For the purpose of the tutorial, we just take into account video and audio streams,
     * but the same may be achieved with text and data streams too. */
    if ((stream_type == FLU_STREAM_TYPE_VIDEO) || (stream_type == FLU_STREAM_TYPE_AUDIO))
    {
        const App *app = (const App *)data;

        /* This callback is called from different streaming threads (for audio and
         * video streams). As app->streams pointer is initialized from another thread
         * (the event thread in _player_on_state_changed), we use an atomic to ensure
         * a memory fence. */
        Stream *streams = g_atomic_pointer_get(&app->streams);
        if (streams)
        {
            for (guint i = 0; i < app->nb_streams; ++i)
            {
                Stream *stream = streams + i;
                if (stream->player_stream == ev->stream)
                {
                    /* Write last read buffer from selected player stream to corresponding recorder stream. */
                    if (stream->recorder_stream)
                    {
                        /* Copy decoded buffer from player stream to corresponding recorder
                         * stream for further encoding. */
                        if (!flu_stream_last_sample_copy(ev->stream, stream->recorder_stream))
                        {
                            g_print("Warning: cannot copy %s buffer from player to recorder\n",
                                    (stream_type == FLU_STREAM_TYPE_VIDEO) ? "video" : "audio");
                        }
                    }
                    break;
                }
            }
        }
    }

    return TRUE;
}

The listener callback is called by different streaming threads each time a raw buffer is available from a particular player’s stream. FluPlayerEventRender event contains the information about which player’s stream is currently emitting a new raw buffer. We use this stream to obtain the linked recorder’s stream from the array.

Once we’ve found the corresponding recorded stream, we only need to copy the raw buffer from the player’s stream to the recorder’s stream. To do so, we call flu_stream_last_sample_copy function passing both streams as parameters.

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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <fluendo-sdk.h>
#include <stdio.h>

/*****************************************************************************
 * Application structures
 *****************************************************************************/

typedef struct
{
    FluStream *player_stream;
    FluStream *recorder_stream;
} Stream;

typedef struct
{
    gchar *input_uri;

    GMainLoop *main_loop;
    guint keyboard_source_id;

    FluPlayer *player;
    FluRecorder *recorder;

    /* Unlike previous fields which are initialized BEFORE threads creation
     * (see _init_app) and cleaned up AFTER joining all threads (see _clean_app),
     * nb_streams and streams pointers are initialized from the Fluendo-SDK events
     * thread (see _player_on_state_changed) and then read from potentially
     * different streaming threads (see _player_on_render). They are cleaned up
     * AFTER joining all threads like the rest of fields.
     * As nb_streams and streams pointers are only written once and then constant
     * until joining all threads, we only need a memory barrier (no need for locks).
     * We obtain it by using atomic for the streams pointer taking care of writing
     * nb_streams BEFORE setting the atomic pointer. */
    guint nb_streams;
    volatile Stream *streams;
} App;

/*****************************************************************************
 * Recorder events management
 *****************************************************************************/

static gboolean _recorder_on_request_save_mode(FluRecorder *recorder, FluRecorderEvent *event, gpointer data)
{
    FluRecorderEventRequestSaveMode *ev = (FluRecorderEventRequestSaveMode *)event;

    ev->mode = FLU_RECORDER_SAVE_MODE_FILE;
    ev->data.file.filename = "./output.mp4";

    return TRUE;
}

static gboolean _recorder_on_state_changed(FluRecorder *recorder, FluRecorderEvent *event, gpointer data)
{
    FluRecorderEventState *ev = (FluRecorderEventState *)event;

    g_print("Recorder state changed to %s\n", flu_recorder_state_name_get(ev->state));

    return TRUE;
}

static gboolean _recorder_on_error(FluRecorder *recorder, FluRecorderEvent *event, gpointer data)
{
    FluRecorderEventError *ev = (FluRecorderEventError *)event;

    g_print("Error (recorder): %s %s\n", ev->error.error->message, ev->error.dbg);
    g_main_loop_quit((GMainLoop *)data);

    return FALSE;
}

/*****************************************************************************
 * Recorder creation/destruction
 *****************************************************************************/

FluRecorder *_recorder_create(GMainLoop *main_loop)
{
    FluMediaInfo media_info = {0};
    media_info.format = FLU_MEDIA_INFO_FORMAT_MP4;

    GError *error = NULL;
    FluRecorder *recorder = flu_recorder_new(&media_info, &error);
    if (recorder)
    {
        flu_recorder_event_listener_add(recorder, FLU_RECORDER_EVENT_REQUEST_SAVE_MODE, _recorder_on_request_save_mode, NULL);
        flu_recorder_event_listener_add(recorder, FLU_RECORDER_EVENT_STATE, _recorder_on_state_changed, NULL);
        flu_recorder_event_listener_add(recorder, FLU_RECORDER_EVENT_ERROR, _recorder_on_error, main_loop);
    }
    else
    {
        g_print("Error: cannot create recorder (%s)\n",
                error ? error->message : "undefined error");
        if (error)
        {
            g_error_free(error);
        }
    }

    return recorder;
}

void _recorder_destroy(FluRecorder *recorder)
{
    if (recorder)
    {
        flu_recorder_stop(recorder);
        flu_recorder_unref(recorder);
    }
}

/*****************************************************************************
 * Recorder functions
 *****************************************************************************/

FluStream *_recorder_request_stream(const App *app, FluStreamType stream_type)
{
    FluStreamInfo info = {0};
    info.type = stream_type;

    if (stream_type == FLU_STREAM_TYPE_VIDEO)
    {
        info.data.video.vcodec.type = FLU_STREAM_VIDEO_CODEC_H264;
    }
    else if (stream_type == FLU_STREAM_TYPE_AUDIO)
    {
        info.data.audio.acodec = FLU_STREAM_AUDIO_CODEC_AAC;
    }
    else
    {
        return NULL;
    }

    GError *error = NULL;
    FluStream *stream = flu_recorder_request_stream(app->recorder, &info, &error);
    if (!stream)
    {
        g_print("Error: cannot create %s stream for recorder (%s)\n",
                (stream_type == FLU_STREAM_TYPE_VIDEO) ? "video" : "audio",
                error ? error->message : "undefined error");
        if (error)
        {
            g_error_free(error);
        }
    }

    return stream;
}

gboolean _recorder_start(const App *app)
{
    GError *error = NULL;
    flu_recorder_record(app->recorder, &error);
    if (error)
    {
        g_print("Error: cannot start recorder (%s)\n", error->message);
        g_error_free(error);
        return FALSE;
    }

    return TRUE;
}

/*****************************************************************************
 * Player events management
 *****************************************************************************/

static gboolean _player_on_state_changed(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    gboolean ret = TRUE;

    FluPlayerEventState *ev = (FluPlayerEventState *)event;
    g_print("Player state changed to %s\n", flu_player_state_name_get(ev->state));

    if (ev->state == FLU_PLAYER_STATE_PLAYING)
    {
        App *app = (App *)data;

        /* First time player goes to PLAYING state, we initialize a new recorder output stream
         * for each player stream, and we keep the relationships between corresponding streams.
         * This callback is called from the Fluendo-SDK event thread. As app->streams pointer
         * is only written once here and then read from streaming threads later, we use an atomic
         * in order to ensure a memory fence. */
        Stream *streams = g_atomic_pointer_get(&app->streams);
        if (!streams)
        {
            /* For the purpose of the tutorial, we just take into account video and audio streams,
             * but the same may be achieved with text and data streams too. */
            GList *player_streams = flu_player_video_active_streams_get(player);
            player_streams = g_list_concat(player_streams, flu_player_audio_active_streams_get(player));

            app->nb_streams = g_list_length(player_streams);
            streams = g_new0(Stream, app->nb_streams);

            Stream *stream_link = streams;
            for (GList *l = player_streams; l; l = l->next)
            {
                /* We don't need to keep any extra references to player or recorder streams. Weak
                 * references are enough as all links are only used within callbacks during lifetimes
                 * of the player and recorder (which already keep hard references to those streams). */
                stream_link->player_stream = (FluStream *)l->data;

                FluStreamType stream_type = flu_stream_type_get(stream_link->player_stream);
                stream_link->recorder_stream = _recorder_request_stream(app, stream_type);
                if (stream_link->recorder_stream)
                {
                    flu_stream_unref(stream_link->recorder_stream);
                }

                ++stream_link;
            }

            flu_stream_list_free(player_streams);
            g_atomic_pointer_set(&app->streams, streams);

            /* We are now ready for transcoding: let's start the recorder. */
            if (!_recorder_start(app))
            {
                ret = FALSE;
                g_main_loop_quit(app->main_loop);
            }
        }
    }

    return ret;
}

static gboolean _player_on_error(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    FluPlayerEventError *ev = (FluPlayerEventError *)event;

    g_print("Error (player): %s %s\n", ev->error->message, ev->dbg);
    g_main_loop_quit((GMainLoop *)data);

    return FALSE;
}

static gboolean _player_on_eos(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    g_print("End of source media reached => transcoding stopped automatically.\n");
    g_main_loop_quit((GMainLoop *)data);

    return TRUE;
}

static gboolean _player_on_request_render_mode(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    FluPlayerEventRequestRenderMode *ev = (FluPlayerEventRequestRenderMode *)event;

    /* As we are just transcoding, we request:
     * - no visual rendering,
     * - no synchronization (buffers are treated as fast as they arrive),
     * - notification (to know when buffers are ready to be copied). */
    ev->render = FALSE;
    ev->synchronize = FALSE;
    ev->notify = TRUE;

    return TRUE;
}

static gboolean _player_on_render(FluPlayer *player, FluPlayerEvent *event, gpointer data)
{
    FluPlayerEventRender *ev = (FluPlayerEventRender *)event;
    FluStreamType stream_type = flu_stream_type_get(ev->stream);

    /* For the purpose of the tutorial, we just take into account video and audio streams,
     * but the same may be achieved with text and data streams too. */
    if ((stream_type == FLU_STREAM_TYPE_VIDEO) || (stream_type == FLU_STREAM_TYPE_AUDIO))
    {
        const App *app = (const App *)data;

        /* This callback is called from different streaming threads (for audio and
         * video streams). As app->streams pointer is initialized from another thread
         * (the event thread in _player_on_state_changed), we use an atomic to ensure
         * a memory fence. */
        Stream *streams = g_atomic_pointer_get(&app->streams);
        if (streams)
        {
            for (guint i = 0; i < app->nb_streams; ++i)
            {
                Stream *stream = streams + i;
                if (stream->player_stream == ev->stream)
                {
                    /* Write last read buffer from selected player stream to corresponding recorder stream. */
                    if (stream->recorder_stream)
                    {
                        /* Copy decoded buffer from player stream to corresponding recorder
                         * stream for further encoding. */
                        if (!flu_stream_last_sample_copy(ev->stream, stream->recorder_stream))
                        {
                            g_print("Warning: cannot copy %s buffer from player to recorder\n",
                                    (stream_type == FLU_STREAM_TYPE_VIDEO) ? "video" : "audio");
                        }
                    }
                    break;
                }
            }
        }
    }

    return TRUE;
}

/*****************************************************************************
 * Player creation/destruction
 *****************************************************************************/

FluPlayer *_player_create(App *app)
{
    FluPlayer *player = flu_player_new();

    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_STATE, _player_on_state_changed, app);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_ERROR, _player_on_error, app->main_loop);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_EOS, _player_on_eos, app->main_loop);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_REQUEST_RENDER_MODE, _player_on_request_render_mode, NULL);
    flu_player_event_listener_add(player, FLU_PLAYER_EVENT_RENDER, _player_on_render, app);

    return player;
}

void _player_destroy(FluPlayer *player)
{
    if (player)
    {
        flu_player_close(player);
        flu_player_unref(player);
    }
}

/*****************************************************************************
 * Application management functions
 *****************************************************************************/

static gboolean _handle_keyboard(GIOChannel *source, GIOCondition cond, gpointer data)
{
    static gboolean exiting = FALSE;

    gchar *str = NULL;
    if (g_io_channel_read_line(source, &str, NULL, NULL, NULL) == G_IO_STATUS_NORMAL)
    {
        if ((str[0] == 'q') && !exiting)
        {
            exiting = TRUE;
            g_print("Exiting...\n");
            g_main_loop_quit((GMainLoop *)data);
        }

        g_free(str);
    }

    return TRUE;
}

static void _clean_app(App *app)
{
    if (app->keyboard_source_id)
    {
        g_source_remove(app->keyboard_source_id);
        app->keyboard_source_id = 0;
    }

    _player_destroy(app->player);
    app->player = NULL;

    _recorder_destroy(app->recorder);
    app->recorder = NULL;

    /* We don't need memory fence anymore here as all threads have been
     * joined at destroying the player and the recorder. */
    g_free((Stream *)app->streams);
    app->streams = NULL;
    app->nb_streams = 0;

    g_main_loop_unref(app->main_loop);
    app->main_loop = NULL;

    flu_shutdown();

    g_free(app->input_uri);
    app->input_uri = NULL;
}

static gboolean _init_app(App *app, int argc, const char **argv)
{
    if (argc >= 2)
    {
        gchar *absolute_path = g_canonicalize_filename(argv[1], NULL);
        app->input_uri = g_filename_to_uri(absolute_path, NULL, NULL);
        g_free(absolute_path);
    }

    if (!app->input_uri)
    {
        g_print("Usage: %s [INPUT_FILE_PATH]\n", argv[0]);
        return FALSE;
    }

    flu_initialize();
    app->main_loop = g_main_loop_new(NULL, FALSE);

    app->recorder = _recorder_create(app->main_loop);
    if (!app->recorder)
    {
        _clean_app(app);
        return FALSE;
    }

    app->player = _player_create(app);
    if (!app->player)
    {
        _clean_app(app);
        return FALSE;
    }

    return TRUE;
}

/*****************************************************************************
 * Application main entry point
 *****************************************************************************/

int main(int argc, const char **argv)
{
    App app = {0};

    /* Initialize application. */
    if (!_init_app(&app, argc, argv))
    {
        return -1;
    }

    /* Start player. */
    flu_player_uri_open(app.player, app.input_uri);
    flu_player_play(app.player);

    /* Run main loop. */
    GIOChannel *io_stdin = g_io_channel_unix_new(fileno(stdin));
    app.keyboard_source_id = g_io_add_watch(io_stdin, G_IO_IN, (GIOFunc)_handle_keyboard, app.main_loop);
    g_io_channel_unref(io_stdin);

    g_print("Transcoding started. Press q<Enter> to stop.\n");
    g_main_loop_run(app.main_loop);

    /* Clean application. */
    _clean_app(&app);

    return 0;
}

You can also download it here.

This source code can be compiled using the following command line:

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.

Conclusions

As a summary of everything that this tutorial covered, you have learned a handful of useful knowledge:

Continue with the next tutorial to learn how to set audio and video advanced parameters to transform streams during transcoding.