Libav
oss.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.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  char errbuff[128];
52 
53  if (is_output)
54  audio_fd = avpriv_open(audio_device, O_WRONLY);
55  else
56  audio_fd = avpriv_open(audio_device, O_RDONLY);
57  if (audio_fd < 0) {
58  av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
59  return AVERROR(EIO);
60  }
61 
62  if (flip && *flip == '1') {
63  s->flip_left = 1;
64  }
65 
66  /* non blocking mode */
67  if (!is_output)
68  fcntl(audio_fd, F_SETFL, O_NONBLOCK);
69 
71 
72 #define CHECK_IOCTL_ERROR(event) \
73  if (err < 0) { \
74  av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
75  av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
76  goto fail; \
77  }
78 
79  /* select format : favour native format
80  * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
81  * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
82  * fail anyway. */
83  (void) ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
84 
85 #if HAVE_BIGENDIAN
86  if (tmp & AFMT_S16_BE) {
87  tmp = AFMT_S16_BE;
88  } else if (tmp & AFMT_S16_LE) {
89  tmp = AFMT_S16_LE;
90  } else {
91  tmp = 0;
92  }
93 #else
94  if (tmp & AFMT_S16_LE) {
95  tmp = AFMT_S16_LE;
96  } else if (tmp & AFMT_S16_BE) {
97  tmp = AFMT_S16_BE;
98  } else {
99  tmp = 0;
100  }
101 #endif
102 
103  switch(tmp) {
104  case AFMT_S16_LE:
106  break;
107  case AFMT_S16_BE:
109  break;
110  default:
111  av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
112  close(audio_fd);
113  return AVERROR(EIO);
114  }
115  err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
116  CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
117 
118  tmp = (s->channels == 2);
119  err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
120  CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
121 
122  tmp = s->sample_rate;
123  err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
124  CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
125  s->sample_rate = tmp; /* store real sample rate */
126  s->fd = audio_fd;
127 
128  return 0;
129  fail:
130  close(audio_fd);
131  return AVERROR(EIO);
132 #undef CHECK_IOCTL_ERROR
133 }
134 
136 {
137  close(s->fd);
138  return 0;
139 }
#define CHECK_IOCTL_ERROR(event)
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
Format I/O context.
Definition: avformat.h:940
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:66
int channels
Definition: oss.h:32
#define OSS_AUDIO_BLOCK_SIZE
Definition: oss.h:26
enum AVCodecID codec_id
Definition: oss.h:34
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss.c:44
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:134
#define AVERROR(e)
Definition: error.h:43
#define fail()
Definition: checkasm.h:80
Libavcodec external API header.
int fd
Definition: oss.h:30
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
int frame_size
Definition: oss.h:33
Main libavformat public API header.
unsigned int flip_left
Definition: oss.h:35
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss.c:135
static uint8_t tmp[8]
Definition: des.c:38
void * priv_data
Format private data.
Definition: avformat.h:968
int sample_rate
Definition: oss.h:31