]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/audio.c
remove non portable get/put_be64_double()
[ffmpeg] / libavformat / audio.c
index 05055a17818f1a3391c5f7fda95f7cc312a9c22b..054ced2266e7d960cb87b96b8dd4df3b745b0f4b 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#ifdef __OpenBSD__
+#include <soundcard.h>
+#else
 #include <sys/soundcard.h>
+#endif
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -37,7 +41,7 @@ typedef struct {
     int frame_size; /* in bytes ! */
     int codec_id;
     int flip_left : 1;
-    UINT8 buffer[AUDIO_BLOCK_SIZE];
+    uint8_t buffer[AUDIO_BLOCK_SIZE];
     int buffer_ptr;
 } AudioData;
 
@@ -49,7 +53,11 @@ static int audio_open(AudioData *s, int is_output, const char *audio_device)
 
     /* open linux audio device */
     if (!audio_device)
+#ifdef __OpenBSD__
+       audio_device = "/dev/sound";
+#else
         audio_device = "/dev/dsp";
+#endif
 
     if (is_output)
         audio_fd = open(audio_device, O_WRONLY);
@@ -57,7 +65,7 @@ static int audio_open(AudioData *s, int is_output, const char *audio_device)
         audio_fd = open(audio_device, O_RDONLY);
     if (audio_fd < 0) {
         perror(audio_device);
-        return -EIO;
+        return AVERROR_IO;
     }
 
     if (flip && *flip == '1') {
@@ -106,9 +114,9 @@ static int audio_open(AudioData *s, int is_output, const char *audio_device)
         s->codec_id = CODEC_ID_PCM_S16BE;
         break;
     default:
-        fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
+        av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
         close(audio_fd);
-        return -EIO;
+        return AVERROR_IO;
     }
     err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
     if (err < 0) {
@@ -137,7 +145,7 @@ static int audio_open(AudioData *s, int is_output, const char *audio_device)
     return 0;
  fail:
     close(audio_fd);
-    return -EIO;
+    return AVERROR_IO;
 }
 
 static int audio_close(AudioData *s)
@@ -154,21 +162,22 @@ static int audio_write_header(AVFormatContext *s1)
     int ret;
 
     st = s1->streams[0];
-    s->sample_rate = st->codec.sample_rate;
-    s->channels = st->codec.channels;
+    s->sample_rate = st->codec->sample_rate;
+    s->channels = st->codec->channels;
     ret = audio_open(s, 1, NULL);
     if (ret < 0) {
-        return -EIO;
+        return AVERROR_IO;
     } else {
         return 0;
     }
 }
 
-static int audio_write_packet(AVFormatContext *s1, int stream_index,
-                              UINT8 *buf, int size, int force_pts)
+static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
 {
     AudioData *s = s1->priv_data;
     int len, ret;
+    int size= pkt->size;
+    uint8_t *buf= pkt->data;
 
     while (size > 0) {
         len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
@@ -182,7 +191,7 @@ static int audio_write_packet(AVFormatContext *s1, int stream_index,
                 if (ret > 0)
                     break;
                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
-                    return -EIO;
+                    return AVERROR_IO;
             }
             s->buffer_ptr = 0;
         }
@@ -221,16 +230,16 @@ static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     ret = audio_open(s, 0, ap->device);
     if (ret < 0) {
         av_free(st);
-        return -EIO;
+        return AVERROR_IO;
     }
 
     /* take real parameters */
-    st->codec.codec_type = CODEC_TYPE_AUDIO;
-    st->codec.codec_id = s->codec_id;
-    st->codec.sample_rate = s->sample_rate;
-    st->codec.channels = s->channels;
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
+    st->codec->codec_id = s->codec_id;
+    st->codec->sample_rate = s->sample_rate;
+    st->codec->channels = s->channels;
 
-    av_set_pts_info(s1, 48, 1, 1000000);  /* 48 bits pts in us */
+    av_set_pts_info(st, 48, 1, 1000000);  /* 48 bits pts in us */
     return 0;
 }
 
@@ -242,19 +251,32 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
     struct audio_buf_info abufi;
     
     if (av_new_packet(pkt, s->frame_size) < 0)
-        return -EIO;
+        return AVERROR_IO;
     for(;;) {
+        struct timeval tv;
+        fd_set fds;
+
+        tv.tv_sec = 0;
+        tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
+
+        FD_ZERO(&fds);
+        FD_SET(s->fd, &fds);
+
+        /* This will block until data is available or we get a timeout */
+        (void) select(s->fd + 1, &fds, 0, 0, &tv);
+
         ret = read(s->fd, pkt->data, pkt->size);
         if (ret > 0)
             break;
         if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
             av_free_packet(pkt);
             pkt->size = 0;
+            pkt->pts = av_gettime() & ((1LL << 48) - 1);
             return 0;
         }
         if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
             av_free_packet(pkt);
-            return -EIO;
+            return AVERROR_IO;
         }
     }
     pkt->size = ret;