Libav
oss_audio.c
Go to the documentation of this file.
1 /*
2  * Linux audio play and grab interface
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 
24 #include <string.h>
25 
26 #if HAVE_SOUNDCARD_H
27 #include <soundcard.h>
28 #else
29 #include <sys/soundcard.h>
30 #endif
31 
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 
36 #include "libavutil/log.h"
37 
38 #include "libavcodec/avcodec.h"
39 
40 #include "libavformat/avformat.h"
41 
42 #include "oss_audio.h"
43 
44 int ff_oss_audio_open(AVFormatContext *s1, int is_output,
45  const char *audio_device)
46 {
47  OSSAudioData *s = s1->priv_data;
48  int audio_fd;
49  int tmp, err;
50  char *flip = getenv("AUDIO_FLIP_LEFT");
51 
52  if (is_output)
53  audio_fd = avpriv_open(audio_device, O_WRONLY);
54  else
55  audio_fd = avpriv_open(audio_device, O_RDONLY);
56  if (audio_fd < 0) {
57  av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
58  return AVERROR(EIO);
59  }
60 
61  if (flip && *flip == '1') {
62  s->flip_left = 1;
63  }
64 
65  /* non blocking mode */
66  if (!is_output)
67  fcntl(audio_fd, F_SETFL, O_NONBLOCK);
68 
70 
71  /* select format : favour native format */
72  err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
73 
74 #if HAVE_BIGENDIAN
75  if (tmp & AFMT_S16_BE) {
76  tmp = AFMT_S16_BE;
77  } else if (tmp & AFMT_S16_LE) {
78  tmp = AFMT_S16_LE;
79  } else {
80  tmp = 0;
81  }
82 #else
83  if (tmp & AFMT_S16_LE) {
84  tmp = AFMT_S16_LE;
85  } else if (tmp & AFMT_S16_BE) {
86  tmp = AFMT_S16_BE;
87  } else {
88  tmp = 0;
89  }
90 #endif
91 
92  switch(tmp) {
93  case AFMT_S16_LE:
95  break;
96  case AFMT_S16_BE:
98  break;
99  default:
100  av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
101  close(audio_fd);
102  return AVERROR(EIO);
103  }
104  err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
105  if (err < 0) {
106  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
107  goto fail;
108  }
109 
110  tmp = (s->channels == 2);
111  err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
112  if (err < 0) {
113  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
114  goto fail;
115  }
116 
117  tmp = s->sample_rate;
118  err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
119  if (err < 0) {
120  av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
121  goto fail;
122  }
123  s->sample_rate = tmp; /* store real sample rate */
124  s->fd = audio_fd;
125 
126  return 0;
127  fail:
128  close(audio_fd);
129  return AVERROR(EIO);
130 }
131 
133 {
134  close(s->fd);
135  return 0;
136 }
#define OSS_AUDIO_BLOCK_SIZE
Definition: oss_audio.h:26
Format I/O context.
Definition: avformat.h:922
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:71
int channels
Definition: oss_audio.h:32
enum AVCodecID codec_id
Definition: oss_audio.h:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
Libavcodec external API header.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss_audio.c:44
int frame_size
Definition: oss_audio.h:33
Main libavformat public API header.
static void flip(AVCodecContext *avctx, AVPicture *picture)
Definition: rawdec.c:132
unsigned int flip_left
Definition: oss_audio.h:35
void * priv_data
Format private data.
Definition: avformat.h:950
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss_audio.c:132
int sample_rate
Definition: oss_audio.h:31