<commit_before>/* *
 * This file is part of Feng
 *
 * Copyright (C) 2009 by LScube team <<EMAIL>>
 * See AUTHORS for more details
 *
 * feng is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * feng is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with feng; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * */

#include <glib.h>
#include <stdbool.h>
#include <string.h>

#include "media/media.h"
#include "feng.h"
#include "fnc_log.h"

/**
 * @defgroup resources Media backend resources handling
 *
 * @{
 */

#ifdef LIVE_STREAMING
extern Resource *sd2_open(const char *url);
#else
static Resource *sd2_open(const char *url);
{
    fnc_log(FNC_LOG_ERR,
            "unable to stream resource '%s', live streaming support not built in",
            url);

    return false;
}
#endif

#ifdef HAVE_AVFORMAT
extern Resource *avf_open(const char *url);
#else
static Resource *avf_open(const char *url);
{
    fnc_log(FNC_LOG_ERR,
            "unable to stream resource '%s', libavformat support not built in",
            url);

    return false;
}
#endif

/**
 * @brief Mutex regulating access to virtual resources
 *
 * This mutex should be held when looking up, adding to or removing
 * from @ref virtual_resources, to avoid race conditions when multiple
 * clients request virtual resources.
 *
 * @see r_virtual_lock, r_virtual_unlock
 */
static GStaticMutex virtual_resources_lock = G_STATIC_MUTEX_INIT;

/**
 * @brief Virtual resources table
 *
 * Connect a virtual resource URL with an already open Resource
 * instance.
 *
 * @note To access this table, you need to hold @ref
 *       virtual_resources_lock.
 *
 */
static GHashTable *virtual_resources;

/**
 * @brief Lock the mutex used for virtual resources
 */
static inline void r_virtual_lock()
{
    g_static_mutex_lock(&virtual_resources_lock);
}

static inline void r_virtual_unlock()
{
    g_static_mutex_unlock(&virtual_resources_lock);
}

/**
 * @brief Retrieve or create the resource for a given virtual URL
 *
 * @param url The resolved URL of the resource within the virtual/ path.
 *
 * @return Pointer to the Resource designed by @p url or NULL in case
 *         of error.
 *
 * @TODO The interface should probably be changed to return a proper
 *       error code when the resource is not found, not accessible or
 *       not readable.
 *
 * @see r_open
 */
static Resource *r_open_virtual(const char *url)
{
    Resource *r;

    r_virtual_lock();

    if ( ! virtual_resources )
        virtual_resources = g_hash_table_new(g_str_hash, g_str_equal);

    if ( (r = g_hash_table_lookup(virtual_resources, url)) != NULL )
        g_atomic_int_inc(&r->live.count);
    else
        r = sd2_open(url);<commit_msg>resource: make sure to register virtual resources.<commit_after>/* *
 * This file is part of Feng
 *
 * Copyright (C) 2009 by LScube team <<EMAIL>>
 * See AUTHORS for more details
 *
 * feng is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * feng is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with feng; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * */

#include <glib.h>
#include <stdbool.h>
#include <string.h>

#include "media/media.h"
#include "feng.h"
#include "fnc_log.h"

/**
 * @defgroup resources Media backend resources handling
 *
 * @{
 */

#ifdef LIVE_STREAMING
extern Resource *sd2_open(const char *url);
#else
static Resource *sd2_open(const char *url);
{
    fnc_log(FNC_LOG_ERR,
            "unable to stream resource '%s', live streaming support not built in",
            url);

    return false;
}
#endif

#ifdef HAVE_AVFORMAT
extern Resource *avf_open(const char *url);
#else
static Resource *avf_open(const char *url);
{
    fnc_log(FNC_LOG_ERR,
            "unable to stream resource '%s', libavformat support not built in",
            url);

    return false;
}
#endif

/**
 * @brief Mutex regulating access to virtual resources
 *
 * This mutex should be held when looking up, adding to or removing
 * from @ref virtual_resources, to avoid race conditions when multiple
 * clients request virtual resources.
 *
 * @see r_virtual_lock, r_virtual_unlock
 */
static GStaticMutex virtual_resources_lock = G_STATIC_MUTEX_INIT;

/**
 * @brief Virtual resources table
 *
 * Connect a virtual resource URL with an already open Resource
 * instance.
 *
 * @note To access this table, you need to hold @ref
 *       virtual_resources_lock.
 *
 */
static GHashTable *virtual_resources;

/**
 * @brief Lock the mutex used for virtual resources
 */
static inline void r_virtual_lock()
{
    g_static_mutex_lock(&virtual_resources_lock);
}

static inline void r_virtual_unlock()
{
    g_static_mutex_unlock(&virtual_resources_lock);
}

/**
 * @brief Retrieve or create the resource for a given virtual URL
 *
 * @param url The resolved URL of the resource within the virtual/ path.
 *
 * @return Pointer to the Resource designed by @p url or NULL in case
 *         of error.
 *
 * @TODO The interface should probably be changed to return a proper
 *       error code when the resource is not found, not accessible or
 *       not readable.
 *
 * @see r_open
 */
static Resource *r_open_virtual(const char *url)
{
    Resource *r;

    r_virtual_lock();

    if ( ! virtual_resources )
        virtual_resources = g_hash_table_new(g_str_hash, g_str_equal);

    if ( (r = g_hash_table_lookup(virtual_resources, url)) != NULL )
        g_atomic_int_inc(&r->live.count);
    else {
        r = sd2_open(url);
        g_hash_table_insert(virtual_resources, g_strdup(url), r);
    }
{'fraction_non_alphanumeric': 0.08590542800668997, 'fraction_numerical': 0.006081800212863008, 'mean_word_length': 3.552249134948097, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '1835944', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1926, 'n_words': 766}