[FFmpeg-devel] [PATCH 2/2] lavd/alsa: add stream validation
Lukasz Marek
lukasz.m.luki at gmail.com
Sat Oct 26 01:40:08 CEST 2013
Don't trust provided streams. Find first audio stream and use it.
Make a warning if more than one.
Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
---
libavdevice/alsa-audio-enc.c | 20 +++++++++++++++++---
libavdevice/alsa-audio.h | 1 +
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c
index 4d0e17b..1d57462 100644
--- a/libavdevice/alsa-audio-enc.c
+++ b/libavdevice/alsa-audio-enc.c
@@ -51,8 +51,22 @@ static av_cold int audio_write_header(AVFormatContext *s1)
unsigned int sample_rate;
enum AVCodecID codec_id;
int res;
+ unsigned int i;
+
+ for (i = 0; i < s1->nb_streams; i++) {
+ if (s1->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+ st = s1->streams[i];
+ s->index = i;
+ } else {
+ av_log(s1, AV_LOG_WARNING, "More than one audio stream found. First one is used.\n");
+ break;
+ }
+ }
+ if (!st) {
+ av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
+ return AVERROR(EINVAL);
+ }
- st = s1->streams[0];
sample_rate = st->codec->sample_rate;
codec_id = st->codec->codec_id;
res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
@@ -80,7 +94,7 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
uint8_t *buf = pkt->data;
if (!(s->timestamp_diff = pkt->duration)) {
- AVStream *st = s1->streams[0];
+ AVStream *st = s1->streams[s->index];
AVCodecContext *codec_ctx = st->codec;
/*XXX: no need to recalculate: 1/sample_rate == avprinv_set_pts_info() */
s->timestamp_diff = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
@@ -118,7 +132,7 @@ static void audio_get_output_timestamp(AVFormatContext *s1, int stream,
snd_pcm_sframes_t delay = 0;
*wall = av_gettime();
snd_pcm_delay(s->h, &delay);
- *dts = s1->streams[0]->cur_dts + s->timestamp_diff - delay;
+ *dts = s1->streams[s->index]->cur_dts + s->timestamp_diff - delay;
}
AVOutputFormat ff_alsa_muxer = {
diff --git a/libavdevice/alsa-audio.h b/libavdevice/alsa-audio.h
index b9670b9..1173d2b 100644
--- a/libavdevice/alsa-audio.h
+++ b/libavdevice/alsa-audio.h
@@ -58,6 +58,7 @@ typedef struct AlsaData {
void *reorder_buf;
int reorder_buf_size; ///< in frames
int64_t timestamp_diff; ///< duration of last packet, need to calculate timestamp
+ unsigned int index; ///< index of the firstfound audio
} AlsaData;
/**
--
1.7.10.4
More information about the ffmpeg-devel
mailing list