Пример #1
0
OwrLocalMediaSource *_owr_local_media_source_new_cached(gint device_index, const gchar *name,
    OwrMediaType media_type, OwrSourceType source_type)
{
    static OwrLocalMediaSource *test_sources[2] = { NULL, };
    static GHashTable *sources[2] = { NULL, };
    G_LOCK_DEFINE_STATIC(source_cache);

    OwrLocalMediaSource *ret = NULL;
    gchar *cached_name;
    int i;

    G_LOCK(source_cache);

    if (G_UNLIKELY(sources[0] == NULL)) {
        sources[0] = g_hash_table_new(NULL, NULL);
        sources[1] = g_hash_table_new(NULL, NULL);
    }

    i = media_type == OWR_MEDIA_TYPE_AUDIO ? 0 : 1;

    if (source_type == OWR_SOURCE_TYPE_TEST) {
        if (test_sources[i] == NULL)
            test_sources[i] = _owr_local_media_source_new(device_index, name, media_type, source_type);

        ret = test_sources[i];

    } else if (source_type == OWR_SOURCE_TYPE_CAPTURE) {
        ret = g_hash_table_lookup(sources[i], GINT_TO_POINTER(device_index));

        if (ret) {
            g_object_get(ret, "name", &cached_name, NULL);

            if (!g_str_equal(name, cached_name)) {
                /* Device at this index seems to have changed, throw the old one away */
                g_object_unref(ret);
                ret = NULL;
            }

            g_free(cached_name);
        }

        if (!ret) {
            ret = _owr_local_media_source_new(device_index, name, media_type, source_type);
            g_hash_table_insert(sources[i], GINT_TO_POINTER(device_index), ret);
        }

    } else
        g_assert_not_reached();

    G_UNLOCK (source_cache);

    return g_object_ref(ret);
}
Пример #2
0
static GList *get_capture_sources(OwrMediaType types)
{
    static GList *cached_sources = NULL;
    OwrLocalMediaSource *source;
    OwrMediaType media_type;
    GList *result_list = NULL;
    GList *elem;

    if (g_once_init_enter(&cached_sources)) {
        GList *sources = NULL;

        /* FIXME: This code makes no sense at all, we shouldn't hardcode
         * capture sources but check what is available. Not everybody has
         * /dev/video0 and /dev/video1, and not always are they camera
         * sources...
         * Use GstDeviceMonitor here! */
        source = _owr_local_media_source_new("Audio capture source", OWR_MEDIA_TYPE_AUDIO,
            OWR_SOURCE_TYPE_CAPTURE);
        sources = g_list_append(sources, OWR_MEDIA_SOURCE(source));
        source = _owr_local_media_source_new("Video capture source", OWR_MEDIA_TYPE_VIDEO,
            OWR_SOURCE_TYPE_CAPTURE);
        _owr_local_media_source_set_capture_device_index(source, PRIMARY_VIDEO_DEVICE_INDEX);
        sources = g_list_append(sources, OWR_MEDIA_SOURCE(source));
        source = _owr_local_media_source_new("Video capture source", OWR_MEDIA_TYPE_VIDEO,
            OWR_SOURCE_TYPE_CAPTURE);
        _owr_local_media_source_set_capture_device_index(source, SECONDARY_VIDEO_DEVICE_INDEX);
        sources = g_list_append(sources, OWR_MEDIA_SOURCE(source));

        if (g_getenv("OWR_USE_TEST_SOURCES")) {
            source = _owr_local_media_source_new("Video test source", OWR_MEDIA_TYPE_VIDEO,
                OWR_SOURCE_TYPE_TEST);
            sources = g_list_append(sources, OWR_MEDIA_SOURCE(source));
            source = _owr_local_media_source_new("Audio test source", OWR_MEDIA_TYPE_AUDIO,
                OWR_SOURCE_TYPE_TEST);
            sources = g_list_append(sources, OWR_MEDIA_SOURCE(source));
        }
        g_once_init_leave(&cached_sources, sources);
    }

    for (elem = cached_sources; elem; elem = elem->next) {
        source = OWR_LOCAL_MEDIA_SOURCE(elem->data);

        g_object_get(source, "media-type", &media_type, NULL);

        if (types & media_type) {
            g_object_ref(source);
            result_list = g_list_append(result_list, source);
        }
    }

    return result_list;
}