FFmpeg
oss_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 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 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/soundcard.h>
30 
31 #include "libavutil/internal.h"
32 
33 #include "avdevice.h"
34 #include "libavformat/internal.h"
35 
36 #include "oss.h"
37 
39 {
40  OSSAudioData *s = s1->priv_data;
41  AVStream *st;
42  int ret;
43 
44  st = s1->streams[0];
45  s->sample_rate = st->codecpar->sample_rate;
46  s->channels = st->codecpar->ch_layout.nb_channels;
47  ret = ff_oss_audio_open(s1, 1, s1->url);
48  if (ret < 0) {
49  return AVERROR(EIO);
50  } else {
51  return 0;
52  }
53 }
54 
56 {
57  OSSAudioData *s = s1->priv_data;
58  const uint8_t *buf = pkt->data;
59  int len, ret;
60  int size= pkt->size;
61 
62  while (size > 0) {
63  len = FFMIN(OSS_AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
64  memcpy(s->buffer + s->buffer_ptr, buf, len);
65  s->buffer_ptr += len;
66  if (s->buffer_ptr >= OSS_AUDIO_BLOCK_SIZE) {
67  for(;;) {
68  ret = write(s->fd, s->buffer, OSS_AUDIO_BLOCK_SIZE);
69  if (ret > 0)
70  break;
71  if (ret < 0 && (errno != EAGAIN && errno != EINTR))
72  return AVERROR(EIO);
73  }
74  s->buffer_ptr = 0;
75  }
76  buf += len;
77  size -= len;
78  }
79  return 0;
80 }
81 
83 {
84  OSSAudioData *s = s1->priv_data;
85 
87  return 0;
88 }
89 
90 static const AVClass oss_muxer_class = {
91  .class_name = "OSS outdev",
92  .item_name = av_default_item_name,
93  .version = LIBAVUTIL_VERSION_INT,
95 };
96 
98  .name = "oss",
99  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
100  .priv_data_size = sizeof(OSSAudioData),
101  /* XXX: we make the assumption that the soundcard accepts this format */
102  /* XXX: find better solution with "preinit" method, needed also in
103  other formats */
105  .video_codec = AV_CODEC_ID_NONE,
106  .write_header = audio_write_header,
107  .write_packet = audio_write_packet,
108  .write_trailer = audio_write_trailer,
109  .flags = AVFMT_NOFILE,
110  .priv_class = &oss_muxer_class,
111 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:318
ff_oss_audio_open
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss.c:39
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVPacket::data
uint8_t * data
Definition: packet.h:374
ff_oss_muxer
const AVOutputFormat ff_oss_muxer
Definition: oss_enc.c:97
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
ff_oss_audio_close
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss.c:136
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:319
pkt
AVPacket * pkt
Definition: movenc.c:59
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
s1
#define s1
Definition: regdef.h:38
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
OSS_AUDIO_BLOCK_SIZE
#define OSS_AUDIO_BLOCK_SIZE
Definition: oss.h:27
OSSAudioData
Definition: oss.h:29
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:212
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
avdevice.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:509
internal.h
AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
Definition: log.h:42
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
audio_write_packet
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_enc.c:55
audio_write_header
static int audio_write_header(AVFormatContext *s1)
Definition: oss_enc.c:38
oss.h
AVPacket
This structure stores compressed data.
Definition: packet.h:351
oss_muxer_class
static const AVClass oss_muxer_class
Definition: oss_enc.c:90
audio_write_trailer
static int audio_write_trailer(AVFormatContext *s1)
Definition: oss_enc.c:82