11
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
13
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
#include "output_control.h"
20
21
#include "output_api.h"
21
22
#include "output_internal.h"
22
23
#include "output_thread.h"
23
#include "pcm_utils.h"
24
#include "mixer_control.h"
25
#include "mixer_plugin.h"
26
27
#include <assert.h>
27
28
#include <stdlib.h>
29
struct notify audio_output_client_notify = NOTIFY_INITIALIZER;
31
/** after a failure, wait this number of seconds before
32
automatically reopening the device */
36
struct notify audio_output_client_notify;
31
38
static void ao_command_wait(struct audio_output *ao)
51
58
notify_signal(&ao->notify);
62
audio_output_open(struct audio_output *ao,
63
const struct audio_format *audio_format,
64
const struct music_pipe *mp)
70
if (ao->fail_timer != NULL) {
71
g_timer_destroy(ao->fail_timer);
72
ao->fail_timer = NULL;
76
audio_format_equals(audio_format, &ao->in_audio_format)) {
77
assert(ao->pipe == mp);
80
/* unpause with the CANCEL command; this is a
81
hack, but suits well for forcing the thread
82
to leave the ao_pause() thread, and we need
83
to flush the device buffer anyway */
85
/* we're not using audio_output_cancel() here,
86
because that function is asynchronous */
87
ao_command(ao, AO_COMMAND_CANCEL);
93
ao->in_audio_format = *audio_format;
96
if (!ao->config_audio_format) {
98
audio_output_close(ao);
100
/* no audio format is configured: copy in->out, let
101
the output's open() method determine the effective
103
ao->out_audio_format = ao->in_audio_format;
108
if (ao->thread == NULL)
109
audio_output_thread_start(ao);
113
ao_command(ao, AO_COMMAND_OPEN);
117
if (open && ao->mixer != NULL)
118
mixer_open(ao->mixer);
55
audio_output_open(struct audio_output *audioOutput,
56
const struct audio_format *audioFormat)
58
audioOutput->reopen_after = 0;
60
if (audioOutput->open &&
61
audio_format_equals(audioFormat, &audioOutput->inAudioFormat)) {
65
audioOutput->inAudioFormat = *audioFormat;
67
if (audio_format_defined(&audioOutput->reqAudioFormat)) {
68
/* copy reqAudioFormat to outAudioFormat only if the
69
device is not yet open; if it is already open,
70
plugin->open() may have modified outAudioFormat,
71
and the value is already ok */
72
if (!audioOutput->open)
73
audioOutput->outAudioFormat =
74
audioOutput->reqAudioFormat;
76
audioOutput->outAudioFormat = audioOutput->inAudioFormat;
77
if (audioOutput->open)
78
audio_output_close(audioOutput);
81
if (audioOutput->thread == 0)
82
audio_output_thread_start(audioOutput);
84
if (!audioOutput->open)
85
ao_command(audioOutput, AO_COMMAND_OPEN);
87
return audioOutput->open;
91
124
audio_output_update(struct audio_output *ao,
92
const struct audio_format *audio_format)
125
const struct audio_format *audio_format,
126
const struct music_pipe *mp)
94
130
if (ao->enabled) {
95
if (ao->reopen_after == 0 || time(NULL) > ao->reopen_after)
96
audio_output_open(ao, audio_format);
131
if (ao->fail_timer == NULL ||
132
g_timer_elapsed(ao->fail_timer, NULL) > REOPEN_AFTER)
133
return audio_output_open(ao, audio_format, mp);
97
134
} else if (audio_output_is_open(ao))
98
135
audio_output_close(ao);
102
audio_output_signal(struct audio_output *ao)
141
audio_output_play(struct audio_output *ao)
104
146
notify_signal(&ao->notify);
107
void audio_output_play(struct audio_output *audioOutput,
108
const char *playChunk, size_t size)
112
if (!audioOutput->open)
115
audioOutput->args.play.data = playChunk;
116
audioOutput->args.play.size = size;
117
ao_command_async(audioOutput, AO_COMMAND_PLAY);
120
void audio_output_pause(struct audio_output *audioOutput)
122
ao_command_async(audioOutput, AO_COMMAND_PAUSE);
125
void audio_output_cancel(struct audio_output *audioOutput)
127
ao_command_async(audioOutput, AO_COMMAND_CANCEL);
130
void audio_output_close(struct audio_output *audioOutput)
132
if (audioOutput->open)
133
ao_command(audioOutput, AO_COMMAND_CLOSE);
136
void audio_output_finish(struct audio_output *audioOutput)
138
audio_output_close(audioOutput);
139
if (audioOutput->thread != 0)
140
ao_command(audioOutput, AO_COMMAND_KILL);
141
if (audioOutput->plugin->finish)
142
audioOutput->plugin->finish(audioOutput->data);
143
if (audioOutput->convBuffer)
144
free(audioOutput->convBuffer);
147
void audio_output_send_tag(struct audio_output *audioOutput,
148
const struct tag *tag)
150
if (audioOutput->plugin->send_tag == NULL)
153
audioOutput->args.tag = tag;
154
ao_command_async(audioOutput, AO_COMMAND_SEND_TAG);
149
void audio_output_pause(struct audio_output *ao)
151
if (ao->mixer != NULL && ao->plugin->pause == NULL)
152
/* the device has no pause mode: close the mixer,
153
unless its "global" flag is set (checked by
154
mixer_auto_close()) */
155
mixer_auto_close(ao->mixer);
157
ao_command_async(ao, AO_COMMAND_PAUSE);
160
void audio_output_cancel(struct audio_output *ao)
162
ao_command_async(ao, AO_COMMAND_CANCEL);
165
void audio_output_close(struct audio_output *ao)
167
assert(!ao->open || ao->fail_timer == NULL);
169
if (ao->mixer != NULL)
170
mixer_auto_close(ao->mixer);
173
ao_command(ao, AO_COMMAND_CLOSE);
174
else if (ao->fail_timer != NULL) {
175
g_timer_destroy(ao->fail_timer);
176
ao->fail_timer = NULL;
180
void audio_output_finish(struct audio_output *ao)
182
audio_output_close(ao);
184
assert(ao->fail_timer == NULL);
186
if (ao->thread != NULL) {
187
ao_command(ao, AO_COMMAND_KILL);
188
g_thread_join(ao->thread);
191
if (ao->mixer != NULL)
192
mixer_free(ao->mixer);
194
ao_plugin_finish(ao->plugin, ao->data);
196
notify_deinit(&ao->notify);
197
g_mutex_free(ao->mutex);