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.
30 // sanitizes ai structure before calling other functions
31 int audio_in_init(audio_in_t
*ai
, int type
)
39 ai
->bytes_per_sample
= -1;
45 ai
->alsa
.handle
= NULL
;
47 ai
->alsa
.device
= strdup("default");
50 #ifdef CONFIG_OSS_AUDIO
52 ai
->oss
.audio_fd
= -1;
53 ai
->oss
.device
= strdup("/dev/dsp");
61 int audio_in_setup(audio_in_t
*ai
)
67 if (ai_alsa_init(ai
) < 0) return -1;
71 #ifdef CONFIG_OSS_AUDIO
73 if (ai_oss_init(ai
) < 0) return -1;
82 int audio_in_set_samplerate(audio_in_t
*ai
, int rate
)
87 ai
->req_samplerate
= rate
;
88 if (!ai
->setup
) return 0;
89 if (ai_alsa_setup(ai
) < 0) return -1;
90 return ai
->samplerate
;
92 #ifdef CONFIG_OSS_AUDIO
94 ai
->req_samplerate
= rate
;
95 if (!ai
->setup
) return 0;
96 if (ai_oss_set_samplerate(ai
) < 0) return -1;
97 return ai
->samplerate
;
104 int audio_in_set_channels(audio_in_t
*ai
, int channels
)
109 ai
->req_channels
= channels
;
110 if (!ai
->setup
) return 0;
111 if (ai_alsa_setup(ai
) < 0) return -1;
114 #ifdef CONFIG_OSS_AUDIO
116 ai
->req_channels
= channels
;
117 if (!ai
->setup
) return 0;
118 if (ai_oss_set_channels(ai
) < 0) return -1;
126 int audio_in_set_device(audio_in_t
*ai
, char *device
)
131 if (ai
->setup
) return -1;
135 free(ai
->alsa
.device
);
136 ai
->alsa
.device
= strdup(device
);
137 /* mplayer cannot handle colons in arguments */
138 for (i
= 0; i
< (int)strlen(ai
->alsa
.device
); i
++) {
139 if (ai
->alsa
.device
[i
] == '.') ai
->alsa
.device
[i
] = ':';
143 #ifdef CONFIG_OSS_AUDIO
145 free(ai
->oss
.device
);
146 ai
->oss
.device
= strdup(device
);
154 int audio_in_uninit(audio_in_t
*ai
)
161 snd_output_close(ai
->alsa
.log
);
162 if (ai
->alsa
.handle
) {
163 snd_pcm_close(ai
->alsa
.handle
);
168 #ifdef CONFIG_OSS_AUDIO
170 close(ai
->oss
.audio_fd
);
179 int audio_in_start_capture(audio_in_t
*ai
)
184 return snd_pcm_start(ai
->alsa
.handle
);
186 #ifdef CONFIG_OSS_AUDIO
195 int audio_in_read_chunk(audio_in_t
*ai
, unsigned char *buffer
)
202 ret
= snd_pcm_readi(ai
->alsa
.handle
, buffer
, ai
->alsa
.chunk_size
);
203 if (ret
!= ai
->alsa
.chunk_size
) {
205 mp_tmsg(MSGT_TV
, MSGL_ERR
, "\nError reading audio: %s\n", snd_strerror(ret
));
207 if (ai_alsa_xrun(ai
) == 0) {
208 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Recovered from cross-run, some frames may be left out!\n");
210 mp_tmsg(MSGT_TV
, MSGL_ERR
, "Fatal error, cannot recover!\n");
214 mp_tmsg(MSGT_TV
, MSGL_ERR
, "\nNot enough audio samples!\n");
220 #ifdef CONFIG_OSS_AUDIO
222 ret
= read(ai
->oss
.audio_fd
, buffer
, ai
->blocksize
);
223 if (ret
!= ai
->blocksize
) {
225 mp_tmsg(MSGT_TV
, MSGL_ERR
, "\nError reading audio: %s\n", strerror(errno
));
227 mp_tmsg(MSGT_TV
, MSGL_ERR
, "\nNot enough audio samples!\n");