FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 
38 #include "libavutil/log.h"
39 
40 #include "libavcodec/avcodec.h"
41 #include "avdevice.h"
42 
43 #include "oss_audio.h"
44 
45 int ff_oss_audio_open(AVFormatContext *s1, int is_output,
46  const char *audio_device)
47 {
48  OSSAudioData *s = s1->priv_data;
49  int audio_fd;
50  int tmp, err;
51  char *flip = getenv("AUDIO_FLIP_LEFT");
52  char errbuff[128];
53 
54  if (is_output)
55  audio_fd = avpriv_open(audio_device, O_WRONLY);
56  else
57  audio_fd = avpriv_open(audio_device, O_RDONLY);
58  if (audio_fd < 0) {
59  av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
60  return AVERROR(EIO);
61  }
62 
63  if (flip && *flip == '1') {
64  s->flip_left = 1;
65  }
66 
67  /* non blocking mode */
68  if (!is_output) {
69  if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
70  av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
71  }
72  }
73 
75 
76 #define CHECK_IOCTL_ERROR(event) \
77  if (err < 0) { \
78  av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
79  av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
80  goto fail; \
81  }
82 
83  /* select format : favour native format
84  * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
85  * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
86  * fail anyway. */
87  err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
88  if (err < 0) {
89  av_log(s1, AV_LOG_WARNING, "SNDCTL_DSP_GETFMTS: %s\n", strerror(errno));
90  }
91 
92 #if HAVE_BIGENDIAN
93  if (tmp & AFMT_S16_BE) {
94  tmp = AFMT_S16_BE;
95  } else if (tmp & AFMT_S16_LE) {
96  tmp = AFMT_S16_LE;
97  } else {
98  tmp = 0;
99  }
100 #else
101  if (tmp & AFMT_S16_LE) {
102  tmp = AFMT_S16_LE;
103  } else if (tmp & AFMT_S16_BE) {
104  tmp = AFMT_S16_BE;
105  } else {
106  tmp = 0;
107  }
108 #endif
109 
110  switch(tmp) {
111  case AFMT_S16_LE:
113  break;
114  case AFMT_S16_BE:
116  break;
117  default:
118  av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
119  close(audio_fd);
120  return AVERROR(EIO);
121  }
122  err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
123  CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
124 
125  tmp = (s->channels == 2);
126  err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
127  CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
128 
129  tmp = s->sample_rate;
130  err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
131  CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
132  s->sample_rate = tmp; /* store real sample rate */
133  s->fd = audio_fd;
134 
135  return 0;
136  fail:
137  close(audio_fd);
138  return AVERROR(EIO);
139 #undef CHECK_IOCTL_ERROR
140 }
141 
143 {
144  close(s->fd);
145  return 0;
146 }