Merge remote-tracking branch 'qatar/master'
[ffmpeg.git] / libavdevice / alsa-audio-enc.c
index 901af8b..0f4e4a2 100644 (file)
@@ -21,7 +21,7 @@
  */
 
 /**
- * @file libavdevice/alsa-audio-enc.c
+ * @file
  * ALSA input and output: output
  * @author Luca Abeni ( lucabe72 email it )
  * @author Benoit Fouet ( benoit fouet free fr )
  * which gives a low latency suitable for real-time playback.
  */
 
-#include "libavformat/avformat.h"
 #include <alsa/asoundlib.h>
 
+#include "libavutil/time.h"
+#include "libavformat/internal.h"
+#include "avdevice.h"
 #include "alsa-audio.h"
 
-av_cold static int audio_write_header(AVFormatContext *s1)
+static av_cold int audio_write_header(AVFormatContext *s1)
 {
     AlsaData *s = s1->priv_data;
     AVStream *st;
     unsigned int sample_rate;
-    enum CodecID codec_id;
+    enum AVCodecID codec_id;
     int res;
 
     st = s1->streams[0];
@@ -61,6 +63,7 @@ av_cold static int audio_write_header(AVFormatContext *s1)
                st->codec->sample_rate, sample_rate);
         goto fail;
     }
+    avpriv_set_pts_info(st, 64, 1, sample_rate);
 
     return res;
 
@@ -76,7 +79,15 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
     int size     = pkt->size;
     uint8_t *buf = pkt->data;
 
-    while((res = snd_pcm_writei(s->h, buf, size / s->frame_size)) < 0) {
+    size /= s->frame_size;
+    if (s->reorder_func) {
+        if (size > s->reorder_buf_size)
+            if (ff_alsa_extend_reorder_buf(s, size))
+                return AVERROR(ENOMEM);
+        s->reorder_func(buf, s->reorder_buf, size);
+        buf = s->reorder_buf;
+    }
+    while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
         if (res == -EAGAIN) {
 
             return AVERROR(EAGAIN);
@@ -93,16 +104,26 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
     return 0;
 }
 
-AVOutputFormat alsa_muxer = {
-    "alsa",
-    NULL_IF_CONFIG_SMALL("ALSA audio output"),
-    "",
-    "",
-    sizeof(AlsaData),
-    DEFAULT_CODEC_ID,
-    CODEC_ID_NONE,
-    audio_write_header,
-    audio_write_packet,
-    ff_alsa_close,
-    .flags = AVFMT_NOFILE,
+static void
+audio_get_output_timestamp(AVFormatContext *s1, int stream,
+    int64_t *dts, int64_t *wall)
+{
+    AlsaData *s  = s1->priv_data;
+    snd_pcm_sframes_t delay = 0;
+    *wall = av_gettime();
+    snd_pcm_delay(s->h, &delay);
+    *dts = s1->streams[0]->cur_dts - delay;
+}
+
+AVOutputFormat ff_alsa_muxer = {
+    .name           = "alsa",
+    .long_name      = NULL_IF_CONFIG_SMALL("ALSA audio output"),
+    .priv_data_size = sizeof(AlsaData),
+    .audio_codec    = DEFAULT_CODEC_ID,
+    .video_codec    = AV_CODEC_ID_NONE,
+    .write_header   = audio_write_header,
+    .write_packet   = audio_write_packet,
+    .write_trailer  = ff_alsa_close,
+    .get_output_timestamp = audio_get_output_timestamp,
+    .flags          = AVFMT_NOFILE,
 };