summaryrefslogtreecommitdiffstats
path: root/libao2/ao_rsound.c
blob: 8232aad8655c3e9798ee69f4fcddeb7e3c07bc39 (plain)
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
/*
 * RSound audio output driver
 *
 * Copyright (C) 2011 Hans-Kristian Arntzen
 *
 * This file is part of mplayer2.
 *
 * mplayer2 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mplayer2 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 General Public License along
 * with mplayer2; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rsound.h>

#include "talloc.h"

#include "subopt-helper.h"
#include "osdep/timer.h"
#include "libaf/format.h"
#include "audio_out.h"

struct priv {
    rsound_t *rd;
};

static int set_format(struct ao *ao)
{
    int rsd_format;

    switch (ao->format) {
    case AF_FORMAT_U8:
        rsd_format = RSD_U8;
        break;
    case AF_FORMAT_S8:
        rsd_format = RSD_S8;
        break;
    case AF_FORMAT_S16_LE:
        rsd_format = RSD_S16_LE;
        break;
    case AF_FORMAT_S16_BE:
        rsd_format = RSD_S16_BE;
        break;
    case AF_FORMAT_U16_LE:
        rsd_format = RSD_U16_LE;
        break;
    case AF_FORMAT_U16_BE:
        rsd_format = RSD_U16_BE;
        break;
    case AF_FORMAT_S24_LE:
    case AF_FORMAT_S24_BE:
    case AF_FORMAT_U24_LE:
    case AF_FORMAT_U24_BE:
        rsd_format = RSD_S32_LE;
        ao->format = AF_FORMAT_S32_LE;
        break;
    case AF_FORMAT_S32_LE:
        rsd_format = RSD_S32_LE;
        break;
    case AF_FORMAT_S32_BE:
        rsd_format = RSD_S32_BE;
        break;
    case AF_FORMAT_U32_LE:
        rsd_format = RSD_U32_LE;
        break;
    case AF_FORMAT_U32_BE:
        rsd_format = RSD_U32_BE;
        break;
    case AF_FORMAT_A_LAW:
        rsd_format = RSD_ALAW;
        break;
    case AF_FORMAT_MU_LAW:
        rsd_format = RSD_MULAW;
        break;
    default:
        rsd_format = RSD_S16_LE;
        ao->format = AF_FORMAT_S16_LE;
    }

    return rsd_format;
}

static int init(struct ao *ao, char *params)
{
    struct priv *priv = talloc_zero(ao, struct priv);
    ao->priv = priv;

    char *host = NULL;
    char *port = NULL;

    const opt_t subopts[] = {
        {"host", OPT_ARG_MSTRZ, &host, NULL},
        {"port", OPT_ARG_MSTRZ, &port, NULL},
        {NULL}
    };

    if (subopt_parse(params, subopts) != 0)
        return -1;

    if (rsd_init(&priv->rd) < 0) {
        free(host);
        free(port);
        return -1;
    }

    if (host) {
        rsd_set_param(priv->rd, RSD_HOST, host);
        free(host);
    }

    if (port) {
        rsd_set_param(priv->rd, RSD_PORT, port);
        free(port);
    }

    rsd_set_param(priv->rd, RSD_SAMPLERATE, &ao->samplerate);
    rsd_set_param(priv->rd, RSD_CHANNELS, &ao->channels);

    int rsd_format = set_format(ao);
    rsd_set_param(priv->rd, RSD_FORMAT, &rsd_format);

    if (rsd_start(priv->rd) < 0) {
        rsd_free(priv->rd);
        return -1;
    }

    ao->bps = ao->channels * ao->samplerate * af_fmt2bits(ao->format) / 8;

    return 0;
}

static void uninit(struct ao *ao, bool cut_audio)
{
    struct priv *priv = ao->priv;
    /* The API does not provide a direct way to explicitly wait until
     * the last byte has been played server-side as this cannot be
     * guaranteed by backend drivers, so we approximate this behavior.
     */
    if (!cut_audio)
        usec_sleep(rsd_delay_ms(priv->rd) * 1000);

    rsd_stop(priv->rd);
    rsd_free(priv->rd);
}

static void reset(struct ao *ao)
{
    struct priv *priv = ao->priv;
    rsd_stop(priv->rd);
    rsd_start(priv->rd);
}

static void audio_pause(struct ao *ao)
{
    struct priv *priv = ao->priv;
    rsd_pause(priv->rd, 1);
}

static void audio_resume(struct ao *ao)
{
    struct priv *priv = ao->priv;
    rsd_pause(priv->rd, 0);
}

static int get_space(struct ao *ao)
{
    struct priv *priv = ao->priv;
    return rsd_get_avail(priv->rd);
}

static int play(struct ao *ao, void *data, int len, int flags)
{
    struct priv *priv = ao->priv;
    return rsd_write(priv->rd, data, len);
}

static float get_delay(struct ao *ao)
{
    struct priv *priv = ao->priv;
    return rsd_delay_ms(priv->rd) / 1000.0;
}

const struct ao_driver audio_out_rsound = {
    .is_new    = true,
    .info      = &(const struct ao_info) {
        .name       = "RSound output driver",
        .short_name = "rsound",
        .author     = "Hans-Kristian Arntzen",
        .comment    = "",
    },
    .init      = init,
    .uninit    = uninit,
    .reset     = reset,
    .get_space = get_space,
    .play      = play,
    .get_delay = get_delay,
    .pause     = audio_pause,
    .resume    = audio_resume,
};