13 // sanitizes ai structure before calling other functions
14 int audio_in_init(audio_in_t
*ai
, int type
)
22 ai
->bytes_per_sample
= -1;
26 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
28 ai
->alsa
.handle
= NULL
;
30 ai
->alsa
.device
= strdup("default");
35 ai
->oss
.audio_fd
= -1;
36 ai
->oss
.device
= strdup("/dev/dsp");
44 int audio_in_setup(audio_in_t
*ai
)
48 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
50 if (ai_alsa_init(ai
) < 0) return -1;
56 if (ai_oss_init(ai
) < 0) return -1;
65 int audio_in_set_samplerate(audio_in_t
*ai
, int rate
)
68 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
70 ai
->req_samplerate
= rate
;
71 if (!ai
->setup
) return 0;
72 if (ai_alsa_setup(ai
) < 0) return -1;
73 return ai
->samplerate
;
77 ai
->req_samplerate
= rate
;
78 if (!ai
->setup
) return 0;
79 if (ai_oss_set_samplerate(ai
) < 0) return -1;
80 return ai
->samplerate
;
87 int audio_in_set_channels(audio_in_t
*ai
, int channels
)
90 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
92 ai
->req_channels
= channels
;
93 if (!ai
->setup
) return 0;
94 if (ai_alsa_setup(ai
) < 0) return -1;
99 ai
->req_channels
= channels
;
100 if (!ai
->setup
) return 0;
101 if (ai_oss_set_channels(ai
) < 0) return -1;
109 int audio_in_set_device(audio_in_t
*ai
, char *device
)
111 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
114 if (ai
->setup
) return -1;
116 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
118 if (ai
->alsa
.device
) free(ai
->alsa
.device
);
119 ai
->alsa
.device
= strdup(device
);
120 /* mplayer cannot handle colons in arguments */
121 for (i
= 0; i
< (int)strlen(ai
->alsa
.device
); i
++) {
122 if (ai
->alsa
.device
[i
] == '.') ai
->alsa
.device
[i
] = ':';
128 if (ai
->oss
.device
) free(ai
->oss
.device
);
129 ai
->oss
.device
= strdup(device
);
137 int audio_in_uninit(audio_in_t
*ai
)
141 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
144 snd_output_close(ai
->alsa
.log
);
145 if (ai
->alsa
.handle
) {
146 snd_pcm_close(ai
->alsa
.handle
);
153 close(ai
->oss
.audio_fd
);
162 int audio_in_start_capture(audio_in_t
*ai
)
165 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
167 return snd_pcm_start(ai
->alsa
.handle
);
178 int audio_in_read_chunk(audio_in_t
*ai
, unsigned char *buffer
)
183 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
185 ret
= snd_pcm_readi(ai
->alsa
.handle
, buffer
, ai
->alsa
.chunk_size
);
186 if (ret
!= ai
->alsa
.chunk_size
) {
188 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrReadingAudio
, snd_strerror(ret
));
190 if (ai_alsa_xrun(ai
) == 0) {
191 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_XRUNSomeFramesMayBeLeftOut
);
193 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrFatalCannotRecover
);
197 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_NotEnoughSamples
);
205 ret
= read(ai
->oss
.audio_fd
, buffer
, ai
->blocksize
);
206 if (ret
!= ai
->blocksize
) {
208 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_ErrReadingAudio
, strerror(errno
));
210 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AUDIOIN_NotEnoughSamples
);