output_control.c 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: http://www.musicpd.org
 *
 * This program 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.
 *
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "output_control.h"
#include "output_api.h"
21
#include "output_internal.h"
22
#include "output_thread.h"
23
#include "pcm_utils.h"
24

25 26
#include <assert.h>
#include <stdlib.h>
27

28
struct notify audio_output_client_notify;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

static void ao_command_wait(struct audio_output *ao)
{
	while (ao->command != AO_COMMAND_NONE) {
		notify_signal(&ao->notify);
		notify_wait(&audio_output_client_notify);
	}
}

static void ao_command(struct audio_output *ao, enum audio_output_command cmd)
{
	assert(ao->command == AO_COMMAND_NONE);
	ao->command = cmd;
	ao_command_wait(ao);
}
44

45 46 47 48 49 50 51 52
static void ao_command_async(struct audio_output *ao,
			     enum audio_output_command cmd)
{
	assert(ao->command == AO_COMMAND_NONE);
	ao->command = cmd;
	notify_signal(&ao->notify);
}

53 54 55
bool
audio_output_open(struct audio_output *audioOutput,
		  const struct audio_format *audioFormat)
56
{
57 58
	audioOutput->reopen_after = 0;

59
	if (audioOutput->open &&
60
	    audio_format_equals(audioFormat, &audioOutput->inAudioFormat)) {
61
		return true;
62 63
	}

64
	audioOutput->inAudioFormat = *audioFormat;
65

66
	if (audio_format_defined(&audioOutput->reqAudioFormat)) {
67 68 69 70 71 72 73
		/* copy reqAudioFormat to outAudioFormat only if the
		   device is not yet open; if it is already open,
		   plugin->open() may have modified outAudioFormat,
		   and the value is already ok */
		if (!audioOutput->open)
			audioOutput->outAudioFormat =
				audioOutput->reqAudioFormat;
74
	} else {
75
		audioOutput->outAudioFormat = audioOutput->inAudioFormat;
76
		if (audioOutput->open)
77
			audio_output_close(audioOutput);
78 79
	}

80
	if (audioOutput->thread == NULL)
81
		audio_output_thread_start(audioOutput);
82

83
	if (!audioOutput->open)
84
		ao_command(audioOutput, AO_COMMAND_OPEN);
85

86
	return audioOutput->open;
87 88
}

89 90 91 92
void
audio_output_update(struct audio_output *ao,
		    const struct audio_format *audio_format)
{
93 94 95 96
	if (ao->enabled) {
		if (ao->reopen_after == 0 || time(NULL) > ao->reopen_after)
			audio_output_open(ao, audio_format);
	} else if (audio_output_is_open(ao))
97 98 99
		audio_output_close(ao);
}

100 101 102 103 104 105
void
audio_output_signal(struct audio_output *ao)
{
	notify_signal(&ao->notify);
}

106 107
void audio_output_play(struct audio_output *audioOutput,
		       const char *playChunk, size_t size)
108
{
109 110
	assert(size > 0);

111
	if (!audioOutput->open)
112
		return;
113

114 115
	audioOutput->args.play.data = playChunk;
	audioOutput->args.play.size = size;
116
	ao_command_async(audioOutput, AO_COMMAND_PLAY);
117 118
}

119 120 121 122 123
void audio_output_pause(struct audio_output *audioOutput)
{
	ao_command_async(audioOutput, AO_COMMAND_PAUSE);
}

124
void audio_output_cancel(struct audio_output *audioOutput)
125
{
126
	ao_command_async(audioOutput, AO_COMMAND_CANCEL);
127 128
}

129
void audio_output_close(struct audio_output *audioOutput)
130 131
{
	if (audioOutput->open)
132
		ao_command(audioOutput, AO_COMMAND_CLOSE);
133 134
}

135
void audio_output_finish(struct audio_output *audioOutput)
136
{
137
	audio_output_close(audioOutput);
138
	if (audioOutput->thread != NULL)
139
		ao_command(audioOutput, AO_COMMAND_KILL);
140
	if (audioOutput->plugin->finish)
141
		audioOutput->plugin->finish(audioOutput->data);
142 143 144 145
	if (audioOutput->convBuffer)
		free(audioOutput->convBuffer);
}

146 147
void audio_output_send_tag(struct audio_output *audioOutput,
			   const struct tag *tag)
148
{
149 150 151 152
	if (audioOutput->plugin->send_tag == NULL)
		return;

	audioOutput->args.tag = tag;
153
	ao_command_async(audioOutput, AO_COMMAND_SEND_TAG);
154
}