| 172 | } |
| 173 | |
| 174 | av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, |
| 175 | unsigned int *sample_rate, |
| 176 | const AVChannelLayout *layout, enum AVCodecID *codec_id) |
| 177 | { |
| 178 | AlsaData *s = ctx->priv_data; |
| 179 | const char *audio_device; |
| 180 | int res, flags = 0; |
| 181 | snd_pcm_format_t format; |
| 182 | snd_pcm_t *h; |
| 183 | snd_pcm_hw_params_t *hw_params; |
| 184 | snd_pcm_uframes_t buffer_size, period_size; |
| 185 | |
| 186 | if (ctx->url[0] == 0) audio_device = "default"; |
| 187 | else audio_device = ctx->url; |
| 188 | |
| 189 | if (*codec_id == AV_CODEC_ID_NONE) |
| 190 | *codec_id = DEFAULT_CODEC_ID; |
| 191 | format = codec_id_to_pcm_format(*codec_id); |
| 192 | if (format == SND_PCM_FORMAT_UNKNOWN) { |
| 193 | av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id); |
| 194 | return AVERROR(ENOSYS); |
| 195 | } |
| 196 | s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * layout->nb_channels; |
| 197 | |
| 198 | if (ctx->flags & AVFMT_FLAG_NONBLOCK) { |
| 199 | flags = SND_PCM_NONBLOCK; |
| 200 | } |
| 201 | res = snd_pcm_open(&h, audio_device, mode, flags); |
| 202 | if (res < 0) { |
| 203 | av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n", |
| 204 | audio_device, snd_strerror(res)); |
| 205 | return AVERROR(EIO); |
| 206 | } |
| 207 | |
| 208 | res = snd_pcm_hw_params_malloc(&hw_params); |
| 209 | if (res < 0) { |
| 210 | av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n", |
| 211 | snd_strerror(res)); |
| 212 | goto fail1; |
| 213 | } |
| 214 | |
| 215 | res = snd_pcm_hw_params_any(h, hw_params); |
| 216 | if (res < 0) { |
| 217 | av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n", |
| 218 | snd_strerror(res)); |
| 219 | goto fail; |
| 220 | } |
| 221 | |
| 222 | res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 223 | if (res < 0) { |
| 224 | av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n", |
| 225 | snd_strerror(res)); |
| 226 | goto fail; |
| 227 | } |
| 228 | |
| 229 | res = snd_pcm_hw_params_set_format(h, hw_params, format); |
| 230 | if (res < 0) { |
| 231 | av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n", |
no test coverage detected