2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 // sanitizes ai structure before calling other functions
32 int audio_in_init(audio_in_t
*ai
, int type
)
40 ai
->bytes_per_sample
= -1;
46 ai
->alsa
.handle
= NULL
;
48 ai
->alsa
.device
= strdup("default");
51 #ifdef CONFIG_OSS_AUDIO
53 ai
->oss
.audio_fd
= -1;
54 ai
->oss
.device
= strdup("/dev/dsp");
62 int audio_in_setup(audio_in_t
*ai
)
68 if (ai_alsa_init(ai
) < 0) return -1;
72 #ifdef CONFIG_OSS_AUDIO
74 if (ai_oss_init(ai
) < 0) return -1;
83 int audio_in_set_samplerate(audio_in_t
*ai
, int rate
)
88 ai
->req_samplerate
= rate
;
89 if (!ai
->setup
) return 0;
90 if (ai_alsa_setup(ai
) < 0) return -1;
91 return ai
->samplerate
;
93 #ifdef CONFIG_OSS_AUDIO
95 ai
->req_samplerate
= rate
;
96 if (!ai
->setup
) return 0;
97 if (ai_oss_set_samplerate(ai
) < 0) return -1;
98 return ai
->samplerate
;
105 int audio_in_set_channels(audio_in_t
*ai
, int channels
)
110 ai
->req_channels
= channels
;
111 if (!ai
->setup
) return 0;
112 if (ai_alsa_setup(ai
) < 0) return -1;
115 #ifdef CONFIG_OSS_AUDIO
117 ai
->req_channels
= channels
;
118 if (!ai
->setup
) return 0;
119 if (ai_oss_set_channels(ai
) < 0) return -1;
127 int audio_in_set_device(audio_in_t
*ai
, char *device
)
132 if (ai
->setup
) return -1;
136 if (ai
->alsa
.device
) free(ai
->alsa
.device
);
137 ai
->alsa
.device
= strdup(device
);
138 /* mplayer cannot handle colons in arguments */
139 for (i
= 0; i
< (int)strlen(ai
->alsa
.device
); i
++) {
140 if (ai
->alsa
.device
[i
] == '.') ai
->alsa
.device
[i
] = ':';
144 #ifdef CONFIG_OSS_AUDIO
146 if (ai
->oss
.device
) free(ai
->oss
.device
);
147 ai
->oss
.device
= strdup(device
);
155 int audio_in_uninit(audio_in_t
*ai
)
162 snd_output_close(ai
->alsa
.log
);
163 if (ai
->alsa
.handle
) {
164 snd_pcm_close(ai
->alsa
.handle
);
169 #ifdef CONFIG_OSS_AUDIO
171 close(ai
->oss
.audio_fd
);
180 int audio_in_start_capture(audio_in_t
*ai
)
185 return snd_pcm_start(ai
->alsa
.handle
);
187 #ifdef CONFIG_OSS_AUDIO
196 int audio_in_read_chunk(audio_in_t
*ai
, unsigned char *buffer
)
203 ret
= snd_pcm_readi(ai
->alsa
.handle
, buffer
, ai
->alsa
.chunk_size
);
204 if (ret
!= ai
->alsa
.chunk_size
) {
206 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrReadingAudio
, snd_strerror(ret
));
208 if (ai_alsa_xrun(ai
) == 0) {
209 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_XRUNSomeFramesMayBeLeftOut
);
211 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrFatalCannotRecover
);
215 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_NotEnoughSamples
);
221 #ifdef CONFIG_OSS_AUDIO
223 ret
= read(ai
->oss
.audio_fd
, buffer
, ai
->blocksize
);
224 if (ret
!= ai
->blocksize
) {
226 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrReadingAudio
, strerror(errno
));
228 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_NotEnoughSamples
);