FFmpeg
sndio_enc.c
Go to the documentation of this file.
1 /*
2  * sndio play and grab interface
3  * Copyright (c) 2010 Jacob Meuser
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 <stdint.h>
23 #include <sndio.h>
24 
25 #include "libavutil/internal.h"
26 
27 #include "libavformat/mux.h"
28 
29 #include "libavdevice/avdevice.h"
30 #include "libavdevice/sndio.h"
31 
33 {
34  SndioData *s = s1->priv_data;
35  AVStream *st;
36  int ret;
37 
38  st = s1->streams[0];
39  s->sample_rate = st->codecpar->sample_rate;
40  s->channels = st->codecpar->ch_layout.nb_channels;
41 
42  ret = ff_sndio_open(s1, 1, s1->url);
43 
44  return ret;
45 }
46 
48 {
49  SndioData *s = s1->priv_data;
50  const uint8_t *buf = pkt->data;
51  int size = pkt->size;
52  int len, ret;
53 
54  while (size > 0) {
55  len = FFMIN(s->buffer_size - s->buffer_offset, size);
56  memcpy(s->buffer + s->buffer_offset, buf, len);
57  buf += len;
58  size -= len;
59  s->buffer_offset += len;
60  if (s->buffer_offset >= s->buffer_size) {
61  ret = sio_write(s->hdl, s->buffer, s->buffer_size);
62  if (ret == 0 || sio_eof(s->hdl))
63  return AVERROR(EIO);
64  s->softpos += ret;
65  s->buffer_offset = 0;
66  }
67  }
68 
69  return 0;
70 }
71 
73 {
74  SndioData *s = s1->priv_data;
75 
76  sio_write(s->hdl, s->buffer, s->buffer_offset);
77 
79 
80  return 0;
81 }
82 
83 static const AVClass sndio_muxer_class = {
84  .class_name = "sndio outdev",
85  .item_name = av_default_item_name,
86  .version = LIBAVUTIL_VERSION_INT,
88 };
89 
91  .p.name = "sndio",
92  .p.long_name = NULL_IF_CONFIG_SMALL("sndio audio playback"),
93  .priv_data_size = sizeof(SndioData),
94  /* XXX: we make the assumption that the soundcard accepts this format */
95  /* XXX: find better solution with "preinit" method, needed also in
96  other formats */
98  .p.video_codec = AV_CODEC_ID_NONE,
99  .write_header = audio_write_header,
100  .write_packet = audio_write_packet,
101  .write_trailer = audio_write_trailer,
102  .p.flags = AVFMT_NOFILE,
103  .p.priv_class = &sndio_muxer_class,
104 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:334
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
ff_sndio_open
av_cold int ff_sndio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: sndio.c:36
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1368
AVPacket::data
uint8_t * data
Definition: packet.h:539
sndio.h
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
audio_write_packet
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: sndio_enc.c:47
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:335
pkt
AVPacket * pkt
Definition: movenc.c:60
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:61
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
SndioData
Definition: sndio.h:31
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVPacket::size
int size
Definition: packet.h:540
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:94
audio_write_header
static av_cold int audio_write_header(AVFormatContext *s1)
Definition: sndio_enc.c:32
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1416
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:468
sndio_muxer_class
static const AVClass sndio_muxer_class
Definition: sndio_enc.c:83
avdevice.h
ff_sndio_close
int ff_sndio_close(SndioData *s)
Definition: sndio.c:112
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
internal.h
AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
Definition: log.h:42
audio_write_trailer
static int audio_write_trailer(AVFormatContext *s1)
Definition: sndio_enc.c:72
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
len
int len
Definition: vorbis_enc_data.h:426
ff_sndio_muxer
const FFOutputFormat ff_sndio_muxer
Definition: sndio_enc.c:90
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
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:80
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
mux.h