X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavdevice%2Fpulse_audio_dec.c;h=3777396ef60d2e30922d52d266df5acf5982cef2;hb=1ec87f50f42a16f9228444dc08aa8264879f61e1;hp=5977fb7e9e5f65914605283fb552767ab9fcdf94;hpb=64425e005edf3bdd77c34c078c3e74ab5ecef557;p=ffmpeg diff --git a/libavdevice/pulse_audio_dec.c b/libavdevice/pulse_audio_dec.c index 5977fb7e9e5..3777396ef60 100644 --- a/libavdevice/pulse_audio_dec.c +++ b/libavdevice/pulse_audio_dec.c @@ -48,6 +48,7 @@ typedef struct PulseData { pa_threaded_mainloop *mainloop; pa_context *context; pa_stream *stream; + size_t pa_frame_size; TimeFilter *timefilter; int last_period; @@ -148,6 +149,10 @@ static av_cold int pulse_read_header(AVFormatContext *s) pd->channels }; pa_buffer_attr attr = { -1 }; + pa_channel_map cmap; + const pa_buffer_attr *queried_attr; + + pa_channel_map_init_extend(&cmap, pd->channels, PA_CHANNEL_MAP_WAVEEX); st = avformat_new_stream(s, NULL); @@ -202,7 +207,7 @@ static av_cold int pulse_read_header(AVFormatContext *s) pa_threaded_mainloop_wait(pd->mainloop); } - if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, NULL))) { + if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, &cmap))) { ret = AVERROR(pa_context_errno(pd->context)); goto unlock_and_fail; } @@ -214,7 +219,7 @@ static av_cold int pulse_read_header(AVFormatContext *s) ret = pa_stream_connect_record(pd->stream, device, &attr, PA_STREAM_INTERPOLATE_TIMING - |PA_STREAM_ADJUST_LATENCY + | (pd->fragment_size == -1 ? PA_STREAM_ADJUST_LATENCY : 0) |PA_STREAM_AUTO_TIMING_UPDATE); if (ret < 0) { @@ -239,6 +244,15 @@ static av_cold int pulse_read_header(AVFormatContext *s) pa_threaded_mainloop_wait(pd->mainloop); } + /* Query actual fragment size */ + queried_attr = pa_stream_get_buffer_attr(pd->stream); + if (!queried_attr || queried_attr->fragsize > INT_MAX/100) { + ret = AVERROR_EXTERNAL; + goto unlock_and_fail; + } + pd->fragment_size = queried_attr->fragsize; + pd->pa_frame_size = pa_frame_size(&ss); + pa_threaded_mainloop_unlock(pd->mainloop); /* take real parameters */ @@ -249,7 +263,7 @@ static av_cold int pulse_read_header(AVFormatContext *s) avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate, - 1000, 1.5E-6); + pd->fragment_size / pd->pa_frame_size, 1.5E-6); if (!pd->timefilter) { pulse_close(s); @@ -274,12 +288,13 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt) int64_t dts; pa_usec_t latency; int negative; + ptrdiff_t pos = 0; pa_threaded_mainloop_lock(pd->mainloop); CHECK_DEAD_GOTO(pd, ret, unlock_and_fail); - while (!read_data) { + while (pos < pd->fragment_size) { int r; r = pa_stream_peek(pd->stream, &read_data, &read_length); @@ -293,43 +308,51 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt) * silence, but that wouldn't work for compressed streams. */ r = pa_stream_drop(pd->stream); CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail); + } else { + if (!pos) { + if (av_new_packet(pkt, pd->fragment_size) < 0) { + ret = AVERROR(ENOMEM); + goto unlock_and_fail; + } + + dts = av_gettime(); + pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL)); + + if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) { + if (negative) { + dts += latency; + } else + dts -= latency; + } else { + av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n"); + } + } + if (pkt->size - pos < read_length) { + if (pos) + break; + pa_stream_drop(pd->stream); + /* Oversized fragment??? */ + ret = AVERROR_EXTERNAL; + goto unlock_and_fail; + } + memcpy(pkt->data + pos, read_data, read_length); + pos += read_length; + pa_stream_drop(pd->stream); } } - if (av_new_packet(pkt, read_length) < 0) { - ret = AVERROR(ENOMEM); - goto unlock_and_fail; - } - - dts = av_gettime(); - pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL)); - - if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) { - enum AVCodecID codec_id = - s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id; - int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels); - int frame_duration = read_length / frame_size; - - - if (negative) { - dts += latency; - } else - dts -= latency; - if (pd->wallclock) - pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period); + pa_threaded_mainloop_unlock(pd->mainloop); - pd->last_period = frame_duration; - } else { - av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n"); - } + av_shrink_packet(pkt, pos); - memcpy(pkt->data, read_data, read_length); - pa_stream_drop(pd->stream); + if (pd->wallclock) + pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period); + pd->last_period = pkt->size / pd->pa_frame_size; - pa_threaded_mainloop_unlock(pd->mainloop); return 0; unlock_and_fail: + av_packet_unref(pkt); pa_threaded_mainloop_unlock(pd->mainloop); return ret; } @@ -356,7 +379,7 @@ static const AVOption options[] = { }; static const AVClass pulse_demuxer_class = { - .class_name = "Pulse demuxer", + .class_name = "Pulse indev", .item_name = av_default_item_name, .option = options, .version = LIBAVUTIL_VERSION_INT,