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