amem.c 4.62 KB
Newer Older
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
/*****************************************************************************
 * amem.c : virtual LibVLC audio output plugin
 *****************************************************************************
 * Copyright (C) 2011 Rémi Denis-Courmont
 *
 * This program 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>

static int Open (vlc_object_t *);
static void Close (vlc_object_t *);

vlc_module_begin ()
    set_shortname (N_("Audio memory"))
    set_description (N_("Audio memory output"))
    set_capability ("audio output", 0)
    set_category (CAT_AUDIO)
    set_subcategory (SUBCAT_AUDIO_AOUT)
    set_callbacks (Open, Close)

    add_string ("amem-format", "S16N",
                N_("Sample format"), N_("Sample format"), false)
        change_private()
    add_integer ("amem-rate", 44100,
                 N_("Sample rate"), N_("Sample rate"), false)
        change_integer_range (1, 192000)
        change_private()
    add_integer ("amem-channels", 2,
                 N_("Channels count"), N_("Channels count"), false)
        change_integer_range (1, AOUT_CHAN_MAX)
        change_private()

vlc_module_end ()

struct aout_sys_t
{
    void *opaque;
57
    void (*play) (void *opaque, const void *data, unsigned count, int64_t pts);
58 59 60 61
    int (*set_volume) (void *opaque, float vol, bool mute);
    void (*cleanup) (void *opaque);
};

62
static void Play (audio_output_t *aout)
63
{
64
    aout_sys_t *sys = aout->sys;
65 66
    block_t *block;

67
    while ((block = aout_FifoPop(&aout->fifo)) != NULL)
68 69 70 71 72 73 74
    {
        sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
                   block->i_pts);
        block_Release (block);
    }
}

75
static int VolumeSet (audio_output_t *aout, float vol, bool mute)
76
{
77
    aout_sys_t *sys = aout->sys;
78

79
    return sys->set_volume (sys->opaque, vol, mute) ? -1 : 0;
80 81 82 83 84 85
}

typedef int (*vlc_audio_format_cb) (void **, char *, unsigned *, unsigned *);

static int Open (vlc_object_t *obj)
{
86
    audio_output_t *aout = (audio_output_t *)obj;
87 88 89 90
    aout_sys_t *sys = malloc (sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

91
    aout->sys = sys;
92 93 94 95 96 97 98 99 100 101 102 103 104
    sys->opaque = var_InheritAddress (obj, "amem-data");
    sys->play = var_InheritAddress (obj, "amem-play");
    sys->set_volume = var_InheritAddress (obj, "amem-set-volume");
    sys->cleanup = NULL; /* defer */
    if (sys->play == NULL)
        goto error;

    vlc_audio_format_cb setup = var_InheritAddress (obj, "amem-setup");
    char format[5] = "S16N";
    unsigned rate, channels;

    if (setup != NULL)
    {
105 106
        rate = aout->format.i_rate;
        channels = aout_FormatNbChannels(&aout->format);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

        if (setup (&sys->opaque, format, &rate, &channels))
            goto error;
        /* Only call this callback if setup succeeded */ 
        sys->cleanup = var_InheritAddress (obj, "amem-cleanup");
    }
    else
    {
        rate = var_InheritInteger (obj, "amem-rate");
        channels = var_InheritInteger (obj, "amem-channels");
    }

    if (rate == 0 || rate > 192000
     || channels == 0 || channels > AOUT_CHAN_MAX)
        goto error;

    /* TODO: amem-format */
    /* FIXME/TODO channel mapping */
125
    if (strcmp(format, "S16N") || aout->format.i_channels != channels)
126 127 128 129
    {
        msg_Err (aout, "format not supported");
        goto error;
    }
130 131
    aout->format.i_format = VLC_CODEC_S16N;
    aout->format.i_rate = rate;
132

133 134
    aout->pf_play = Play;
    aout->pf_pause = NULL;
135
    aout->pf_flush = NULL;
136
    if (sys->set_volume != NULL)
137
        aout->pf_volume_set = VolumeSet;
138 139 140 141 142 143 144 145 146 147 148
    else
        aout_VolumeSoftInit (aout);
    return VLC_SUCCESS;

error:
    Close (obj);
    return VLC_EGENERIC;
}

static void Close (vlc_object_t *obj)
{
149
    audio_output_t *aout = (audio_output_t *)obj;
150
    aout_sys_t *sys = aout->sys;
151 152 153 154 155

    if (sys->cleanup != NULL)
        sys->cleanup (sys->opaque);
    free (sys);
}