Libav
oss_audio_enc.c
Go to the documentation of this file.
1 /*
2  * Linux audio 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 #if HAVE_SOUNDCARD_H
25 #include <soundcard.h>
26 #else
27 #include <sys/soundcard.h>
28 #endif
29 
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 
34 #include "libavutil/internal.h"
35 
36 #include "libavcodec/avcodec.h"
37 
38 #include "libavformat/avformat.h"
39 #include "libavformat/internal.h"
40 
41 #include "oss_audio.h"
42 
44 {
45  OSSAudioData *s = s1->priv_data;
46  AVStream *st;
47  int ret;
48 
49  st = s1->streams[0];
50  s->sample_rate = st->codec->sample_rate;
51  s->channels = st->codec->channels;
52  ret = ff_oss_audio_open(s1, 1, s1->filename);
53  if (ret < 0) {
54  return AVERROR(EIO);
55  } else {
56  return 0;
57  }
58 }
59 
61 {
62  OSSAudioData *s = s1->priv_data;
63  int len, ret;
64  int size= pkt->size;
65  uint8_t *buf= pkt->data;
66 
67  while (size > 0) {
68  len = FFMIN(OSS_AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
69  memcpy(s->buffer + s->buffer_ptr, buf, len);
70  s->buffer_ptr += len;
71  if (s->buffer_ptr >= OSS_AUDIO_BLOCK_SIZE) {
72  for(;;) {
73  ret = write(s->fd, s->buffer, OSS_AUDIO_BLOCK_SIZE);
74  if (ret > 0)
75  break;
76  if (ret < 0 && (errno != EAGAIN && errno != EINTR))
77  return AVERROR(EIO);
78  }
79  s->buffer_ptr = 0;
80  }
81  buf += len;
82  size -= len;
83  }
84  return 0;
85 }
86 
88 {
89  OSSAudioData *s = s1->priv_data;
90 
92  return 0;
93 }
94 
96  .name = "oss",
97  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
98  .priv_data_size = sizeof(OSSAudioData),
99  /* XXX: we make the assumption that the soundcard accepts this format */
100  /* XXX: find better solution with "preinit" method, needed also in
101  other formats */
103  .video_codec = AV_CODEC_ID_NONE,
104  .write_header = audio_write_header,
105  .write_packet = audio_write_packet,
106  .write_trailer = audio_write_trailer,
107  .flags = AVFMT_NOFILE,
108 };
int size
#define OSS_AUDIO_BLOCK_SIZE
Definition: oss_audio.h:26
int size
Definition: avcodec.h:974
uint8_t buffer[OSS_AUDIO_BLOCK_SIZE]
Definition: oss_audio.h:36
Format I/O context.
Definition: avformat.h:922
uint8_t
#define AV_NE(be, le)
Definition: common.h:45
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
int channels
Definition: oss_audio.h:32
uint8_t * data
Definition: avcodec.h:973
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
common internal API header
AVOutputFormat ff_oss_muxer
Definition: oss_audio_enc.c:95
char filename[1024]
input or output filename
Definition: avformat.h:998
#define FFMIN(a, b)
Definition: common.h:57
const char * name
Definition: avformat.h:446
static int audio_write_trailer(AVFormatContext *s1)
Definition: oss_audio_enc.c:87
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_audio_enc.c:60
Stream structure.
Definition: avformat.h:699
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:1807
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss_audio.c:44
int buffer_ptr
Definition: oss_audio.h:37
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
int len
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss_audio.c:135
int sample_rate
Definition: oss_audio.h:31
This structure stores compressed data.
Definition: avcodec.h:950
static int audio_write_header(AVFormatContext *s1)
Definition: oss_audio_enc.c:43