static av_cold int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap) { AlsaData *s = s1->priv_data; AVStream *st; int ret; enum CodecID codec_id; snd_pcm_sw_params_t *sw_params; st = av_new_stream(s1, 0); if (!st) { av_log(s1, AV_LOG_ERROR, "Cannot add stream\n"); return AVERROR(ENOMEM); } codec_id = s1->audio_codec_id; ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels, &codec_id); if (ret < 0) { return AVERROR(EIO); } if (snd_pcm_type(s->h) != SND_PCM_TYPE_HW) av_log(s1, AV_LOG_WARNING, "capture with some ALSA plugins, especially dsnoop, " "may hang.\n"); ret = snd_pcm_sw_params_malloc(&sw_params); if (ret < 0) { av_log(s1, AV_LOG_ERROR, "cannot allocate software parameters structure (%s)\n", snd_strerror(ret)); goto fail; } snd_pcm_sw_params_current(s->h, sw_params); snd_pcm_sw_params_set_tstamp_mode(s->h, sw_params, SND_PCM_TSTAMP_ENABLE); ret = snd_pcm_sw_params(s->h, sw_params); snd_pcm_sw_params_free(sw_params); if (ret < 0) { av_log(s1, AV_LOG_ERROR, "cannot install ALSA software parameters (%s)\n", snd_strerror(ret)); goto fail; } /* take real parameters */ st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->codec_id = codec_id; st->codec->sample_rate = s->sample_rate; st->codec->channels = s->channels; av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ return 0; fail: snd_pcm_close(s->h); return AVERROR(EIO); }
bool ALSAWriter::processParams(bool *paramsCorrected) { const unsigned chn = getParam("chn").toUInt(); const unsigned rate = getParam("rate").toUInt(); const bool resetAudio = channels != chn || sample_rate != rate; channels = chn; sample_rate = rate; if (resetAudio || err) { snd_pcm_hw_params_t *params; snd_pcm_hw_params_alloca(¶ms); close(); QString chosenDevName = devName; if (autoFindMultichannelDevice && channels > 2) { bool mustAutoFind = true, forceStereo = false; if (!snd_pcm_open(&snd, chosenDevName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0)) { if (snd_pcm_type(snd) == SND_PCM_TYPE_HW) { unsigned max_chn = 0; snd_pcm_hw_params_any(snd, params); mustAutoFind = snd_pcm_hw_params_get_channels_max(params, &max_chn) || max_chn < channels; } #ifdef HAVE_CHMAP else if (paramsCorrected) { snd_pcm_chmap_query_t **chmaps = snd_pcm_query_chmaps(snd); if (chmaps) snd_pcm_free_chmaps(chmaps); else forceStereo = true; } #endif snd_pcm_close(snd); snd = NULL; } if (mustAutoFind) { QString newDevName; if (channels <= 4) newDevName = "surround40"; else if (channels <= 6) newDevName = "surround51"; else newDevName = "surround71"; if (!newDevName.isEmpty() && newDevName != chosenDevName) { if (ALSACommon::getDevices().first.contains(newDevName)) chosenDevName = newDevName; else if (forceStereo) { channels = 2; *paramsCorrected = true; } } } } if (!chosenDevName.isEmpty()) { bool sndOpen = !snd_pcm_open(&snd, chosenDevName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0); if (devName != chosenDevName) { if (sndOpen) QMPlay2Core.logInfo("ALSA :: " + devName + "\" -> \"" + chosenDevName + "\""); else { sndOpen = !snd_pcm_open(&snd, devName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0); QMPlay2Core.logInfo("ALSA :: " + tr("Cannot open") + " \"" + chosenDevName + "\", " + tr("back to") + " \"" + devName + "\""); } } if (sndOpen) { snd_pcm_hw_params_any(snd, params); snd_pcm_format_t fmt = SND_PCM_FORMAT_UNKNOWN; if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S32)) { fmt = SND_PCM_FORMAT_S32; sample_size = 4; } else if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S16)) { fmt = SND_PCM_FORMAT_S16; sample_size = 2; } else if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S8)) { fmt = SND_PCM_FORMAT_S8; sample_size = 1; } unsigned delay_us = round(delay * 1000000.0); if (fmt != SND_PCM_FORMAT_UNKNOWN && set_snd_pcm_hw_params(snd, params, fmt, channels, sample_rate, delay_us)) { bool err2 = false; if (channels != chn || sample_rate != rate) { if (paramsCorrected) *paramsCorrected = true; else err2 = true; } if (!err2) { err2 = snd_pcm_hw_params(snd, params); if (err2 && paramsCorrected) //jakiś błąd, próba zmiany sample_rate { snd_pcm_hw_params_any(snd, params); err2 = snd_pcm_hw_params_set_rate_resample(snd, params, false) || !set_snd_pcm_hw_params(snd, params, fmt, channels, sample_rate, delay_us) || snd_pcm_hw_params(snd, params); if (!err2) *paramsCorrected = true; } if (!err2) { modParam("delay", delay_us / 1000000.0); if (paramsCorrected && *paramsCorrected) { modParam("chn", channels); modParam("rate", sample_rate); } canPause = snd_pcm_hw_params_can_pause(params) && snd_pcm_hw_params_can_resume(params); mustSwapChn = channels == 6 || channels == 8; #ifdef HAVE_CHMAP if (mustSwapChn) { snd_pcm_chmap_query_t **chmaps = snd_pcm_query_chmaps(snd); if (chmaps) { for (int i = 0; chmaps[i]; ++i) { if (chmaps[i]->map.channels >= channels) { for (uint j = 0; j < channels; ++j) { mustSwapChn &= chmaps[i]->map.pos[j] == j + SND_CHMAP_FL; if (!mustSwapChn) break; } break; } } snd_pcm_free_chmaps(chmaps); } } #endif return true; } } } } } err = true; QMPlay2Core.logError("ALSA :: " + tr("Cannot open audio output stream")); } return readyWrite(); }
/** * Allocate the memory-mapped buffer for direct sound, and set up the * callback. */ static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi) { snd_pcm_t *pcm = pdbi->pcm; snd_pcm_format_t format; snd_pcm_uframes_t frames, ofs, avail, psize, boundary; unsigned int channels, bits_per_sample, bits_per_frame; int err, mmap_mode; const snd_pcm_channel_area_t *areas; snd_pcm_hw_params_t *hw_params = pdbi->hw_params; snd_pcm_sw_params_t *sw_params = pdbi->sw_params; void *buf; mmap_mode = snd_pcm_type(pcm); if (mmap_mode == SND_PCM_TYPE_HW) TRACE("mmap'd buffer is a direct hardware buffer.\n"); else if (mmap_mode == SND_PCM_TYPE_DMIX) TRACE("mmap'd buffer is an ALSA dmix buffer\n"); else TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode); err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL); err = snd_pcm_hw_params_get_format(hw_params, &format); err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames); err = snd_pcm_hw_params_get_channels(hw_params, &channels); bits_per_sample = snd_pcm_format_physical_width(format); bits_per_frame = bits_per_sample * channels; if (TRACE_ON(dsalsa)) ALSA_TraceParameters(hw_params, NULL, FALSE); TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n", snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame); pdbi->mmap_buflen_frames = frames; pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames ); snd_pcm_sw_params_current(pcm, sw_params); snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0); snd_pcm_sw_params_get_boundary(sw_params, &boundary); snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary); snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, boundary); snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0); snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0); err = snd_pcm_sw_params(pcm, sw_params); avail = snd_pcm_avail_update(pcm); if ((snd_pcm_sframes_t)avail < 0) { ERR("No buffer is available: %s.\n", snd_strerror(avail)); return DSERR_GENERIC; } if (!pdbi->mmap) { buf = pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(), 0, pdbi->mmap_buflen_bytes); if (!buf) return DSERR_OUTOFMEMORY; snd_pcm_format_set_silence(format, buf, pdbi->mmap_buflen_frames); pdbi->mmap_pos = 0; } else { err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail); if ( err < 0 ) { ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err); return DSERR_GENERIC; } snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames); pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0); pdbi->mmap_buffer = areas->addr; } TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n", frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer); return DS_OK; }
int main(int argc, char *argv[]) { const char *device_name = "hw"; snd_pcm_t *pcm; snd_pcm_hw_params_t *hw_params; unsigned int i; unsigned int min, max; int any_rate; int err; if (argc > 1) device_name = argv[1]; err = snd_pcm_open(&pcm, device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK); if (err < 0) { fprintf(stderr, "cannot open device '%s': %s\n", device_name, snd_strerror(err)); return 1; } snd_pcm_hw_params_alloca(&hw_params); err = snd_pcm_hw_params_any(pcm, hw_params); if (err < 0) { fprintf(stderr, "cannot get hardware parameters: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } printf("Device: %s (type: %s)\n", device_name, snd_pcm_type_name(snd_pcm_type(pcm))); printf("Access types:"); for (i = 0; i < ARRAY_SIZE(accesses); ++i) { if (!snd_pcm_hw_params_test_access(pcm, hw_params, accesses[i])) printf(" %s", snd_pcm_access_name(accesses[i])); } putchar('\n'); printf("Formats:"); for (i = 0; i < ARRAY_SIZE(formats); ++i) { if (!snd_pcm_hw_params_test_format(pcm, hw_params, formats[i])) printf(" %s", snd_pcm_format_name(formats[i])); } putchar('\n'); err = snd_pcm_hw_params_get_channels_min(hw_params, &min); if (err < 0) { fprintf(stderr, "cannot get minimum channels count: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } err = snd_pcm_hw_params_get_channels_max(hw_params, &max); if (err < 0) { fprintf(stderr, "cannot get maximum channels count: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } printf("Channels:"); for (i = min; i <= max; ++i) { if (!snd_pcm_hw_params_test_channels(pcm, hw_params, i)) printf(" %u", i); } putchar('\n'); err = snd_pcm_hw_params_get_rate_min(hw_params, &min, NULL); if (err < 0) { fprintf(stderr, "cannot get minimum rate: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } err = snd_pcm_hw_params_get_rate_max(hw_params, &max, NULL); if (err < 0) { fprintf(stderr, "cannot get maximum rate: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } printf("Sample rates:"); if (min == max) printf(" %u", min); else if (!snd_pcm_hw_params_test_rate(pcm, hw_params, min + 1, 0)) printf(" %u-%u", min, max); else { any_rate = 0; for (i = 0; i < ARRAY_SIZE(rates); ++i) { if (!snd_pcm_hw_params_test_rate(pcm, hw_params, rates[i], 0)) { any_rate = 1; printf(" %u", rates[i]); } } if (!any_rate) printf(" %u-%u", min, max); } putchar('\n'); err = snd_pcm_hw_params_get_period_time_min(hw_params, &min, NULL); if (err < 0) { fprintf(stderr, "cannot get minimum period time: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } err = snd_pcm_hw_params_get_period_time_max(hw_params, &max, NULL); if (err < 0) { fprintf(stderr, "cannot get maximum period time: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } printf("Interrupt interval: %u-%u us\n", min, max); err = snd_pcm_hw_params_get_buffer_time_min(hw_params, &min, NULL); if (err < 0) { fprintf(stderr, "cannot get minimum buffer time: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } err = snd_pcm_hw_params_get_buffer_time_max(hw_params, &max, NULL); if (err < 0) { fprintf(stderr, "cannot get maximum buffer time: %s\n", snd_strerror(err)); snd_pcm_close(pcm); return 1; } printf("Buffer size: %u-%u us\n", min, max); snd_pcm_close(pcm); return 0; }