<commit_before>/*
 * ngfd - Non-graphical feedback daemon
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This software, including documentation, is protected by copyright
 * controlled by Nokia Corporation. All rights are reserved.
 * Copying, including reproducing, storing, adapting or translating,
 * any or all of this material requires the prior written consent of
 * Nokia Corporation. This material also contains confidential
 * information which may not be disclosed to others without the prior
 * written consent of Nokia.
 */

#include <glib.h>
#include <gst/gst.h>
#include <gio/gio.h>

#include "ngf-log.h"
#include "ngf-audio-gstreamer.h"

static gboolean _gst_initialize (NgfAudioInterface *iface, NgfPulseContext *context);
static void     _gst_shutdown   (NgfAudioInterface *iface);
static gboolean _gst_prepare    (NgfAudioInterface *iface, NgfAudioStream *stream);
static gboolean _gst_play       (NgfAudioInterface *iface, NgfAudioStream *stream);
static void     _gst_stop       (NgfAudioInterface *iface, NgfAudioStream *stream);

static gboolean
_bus_cb (GstBus     *bus,
         GstMessage *msg,
         gpointer    userdata)
{
    NgfAudioStream *stream     = (NgfAudioStream*) userdata;
    GstElement     *element    = (GstElement*) stream->data;

    switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR: {
            gst_element_set_state (element, GST_STATE_NULL);

            if (stream->callback)
                stream->callback (stream, NGF_AUDIO_STREAM_STATE_FAILED, stream->userdata);

            _gst_stop (stream->iface, stream);
            return FALSE;
        }

        case GST_MESSAGE_STATE_CHANGED: {

            if (GST_ELEMENT (GST_MESSAGE_SRC (msg)) != element)
                break;

            GstState old_state, new_state, pending_state;
            gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);

            if (old_state == GST_STATE_PAUSED && new_state == GST_STATE_PLAYING) {
                if (stream->callback)
                    stream->callback (stream, NGF_AUDIO_STREAM_STATE_STARTED, stream->userdata);
            }
            break;
        }

        case GST_MESSAGE_EOS: {
            if (GST_ELEMENT (GST_MESSAGE_SRC (msg)) != element)
                break;

            if (stream->callback)
                stream->callback (stream, NGF_AUDIO_STREAM_STATE_COMPLETED, stream->userdata);

            _gst_stop (stream->iface, stream);
            return FALSE;
        }

        default:
            break;
    }

    return TRUE;
}

static void
_new_decoded_pad_cb (GstElement *element,
                     GstPad     *pad,
                     gboolean    is_last,
                     gpointer    userdata)
{
    GstElement   *sink_element = (GstElement*) userdata;
    GstStructure *structure    = NULL;
    GstCaps      *caps         = NULL;
    GstPad       *sink_pad     = NULL;

    caps = gst_pad_get_caps (pad);
    if (gst_caps_is_empty (caps) || gst_caps_is_any (caps))
        return;

    structure = gst_caps_get_structure (caps, 0);
    if (g_str_has_prefix (gst_structure_get_name (structure), "audio")) {
        sink_pad = gst_element_get_pad (sink_element, "sink");
        if (!gst_pad_is_linked (sink_pad))
            gst_pad_link (pad, sink_pad);
        gst_object_unref (sink_pad);
    }
<commit_msg>audio-gstreamer: Free caps if caps is empty or any when linking the audio pad to the sink.<commit_after>/*
 * ngfd - Non-graphical feedback daemon
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This software, including documentation, is protected by copyright
 * controlled by Nokia Corporation. All rights are reserved.
 * Copying, including reproducing, storing, adapting or translating,
 * any or all of this material requires the prior written consent of
 * Nokia Corporation. This material also contains confidential
 * information which may not be disclosed to others without the prior
 * written consent of Nokia.
 */

#include <glib.h>
#include <gst/gst.h>
#include <gio/gio.h>

#include "ngf-log.h"
#include "ngf-audio-gstreamer.h"

static gboolean _gst_initialize (NgfAudioInterface *iface, NgfPulseContext *context);
static void     _gst_shutdown   (NgfAudioInterface *iface);
static gboolean _gst_prepare    (NgfAudioInterface *iface, NgfAudioStream *stream);
static gboolean _gst_play       (NgfAudioInterface *iface, NgfAudioStream *stream);
static void     _gst_stop       (NgfAudioInterface *iface, NgfAudioStream *stream);

static gboolean
_bus_cb (GstBus     *bus,
         GstMessage *msg,
         gpointer    userdata)
{
    NgfAudioStream *stream     = (NgfAudioStream*) userdata;
    GstElement     *element    = (GstElement*) stream->data;

    switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR: {
            gst_element_set_state (element, GST_STATE_NULL);

            if (stream->callback)
                stream->callback (stream, NGF_AUDIO_STREAM_STATE_FAILED, stream->userdata);

            _gst_stop (stream->iface, stream);
            return FALSE;
        }

        case GST_MESSAGE_STATE_CHANGED: {

            if (GST_ELEMENT (GST_MESSAGE_SRC (msg)) != element)
                break;

            GstState old_state, new_state, pending_state;
            gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);

            if (old_state == GST_STATE_PAUSED && new_state == GST_STATE_PLAYING) {
                if (stream->callback)
                    stream->callback (stream, NGF_AUDIO_STREAM_STATE_STARTED, stream->userdata);
            }
            break;
        }

        case GST_MESSAGE_EOS: {
            if (GST_ELEMENT (GST_MESSAGE_SRC (msg)) != element)
                break;

            if (stream->callback)
                stream->callback (stream, NGF_AUDIO_STREAM_STATE_COMPLETED, stream->userdata);

            _gst_stop (stream->iface, stream);
            return FALSE;
        }

        default:
            break;
    }

    return TRUE;
}

static void
_new_decoded_pad_cb (GstElement *element,
                     GstPad     *pad,
                     gboolean    is_last,
                     gpointer    userdata)
{
    GstElement   *sink_element = (GstElement*) userdata;
    GstStructure *structure    = NULL;
    GstCaps      *caps         = NULL;
    GstPad       *sink_pad     = NULL;

    caps = gst_pad_get_caps (pad);
    if (gst_caps_is_empty (caps) || gst_caps_is_any (caps)) {
        gst_caps_unref (caps);
        return;
    }

    structure = gst_caps_get_structure (caps, 0);
    if (g_str_has_prefix (gst_structure_get_name (structure), "audio")) {
        sink_pad = gst_element_get_pad (sink_element, "sink");
        if (!gst_pad_is_linked (sink_pad))
            gst_pad_link (pad, sink_pad);
        gst_object_unref (sink_pad);
    }

{'fraction_non_alphanumeric': 0.08910034602076125, 'fraction_numerical': 0.0014417531718569781, 'mean_word_length': 2.526690391459075, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5868855', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2119, 'n_words': 534}