FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
28 #include "libavdevice/avdevice.h"
29 #include "libavdevice/sndio.h"
30 
32 {
33  SndioData *s = s1->priv_data;
34  AVStream *st;
35  int ret;
36 
37  st = s1->streams[0];
39  s->channels = st->codecpar->channels;
40 
41  ret = ff_sndio_open(s1, 1, s1->url);
42 
43  return ret;
44 }
45 
47 {
48  SndioData *s = s1->priv_data;
49  uint8_t *buf= pkt->data;
50  int size = pkt->size;
51  int len, ret;
52 
53  while (size > 0) {
54  len = FFMIN(s->buffer_size - s->buffer_offset, size);
55  memcpy(s->buffer + s->buffer_offset, buf, len);
56  buf += len;
57  size -= len;
58  s->buffer_offset += len;
59  if (s->buffer_offset >= s->buffer_size) {
60  ret = sio_write(s->hdl, s->buffer, s->buffer_size);
61  if (ret == 0 || sio_eof(s->hdl))
62  return AVERROR(EIO);
63  s->softpos += ret;
64  s->buffer_offset = 0;
65  }
66  }
67 
68  return 0;
69 }
70 
72 {
73  SndioData *s = s1->priv_data;
74 
75  sio_write(s->hdl, s->buffer, s->buffer_offset);
76 
77  ff_sndio_close(s);
78 
79  return 0;
80 }
81 
82 static const AVClass sndio_muxer_class = {
83  .class_name = "sndio outdev",
84  .item_name = av_default_item_name,
85  .version = LIBAVUTIL_VERSION_INT,
87 };
88 
90  .name = "sndio",
91  .long_name = NULL_IF_CONFIG_SMALL("sndio audio playback"),
92  .priv_data_size = sizeof(SndioData),
93  /* XXX: we make the assumption that the soundcard accepts this format */
94  /* XXX: find better solution with "preinit" method, needed also in
95  other formats */
97  .video_codec = AV_CODEC_ID_NONE,
98  .write_header = audio_write_header,
99  .write_packet = audio_write_packet,
100  .write_trailer = audio_write_trailer,
101  .flags = AVFMT_NOFILE,
102  .priv_class = &sndio_muxer_class,
103 };
int channels
Definition: sndio.h:41
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int size
Definition: avcodec.h:1446
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1351
static int audio_write_trailer(AVFormatContext *s1)
Definition: sndio_enc.c:71
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:72
uint8_t
#define av_cold
Definition: attributes.h:82
int sample_rate
Definition: sndio.h:42
#define AV_NE(be, le)
Definition: common.h:50
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
uint8_t * data
Definition: avcodec.h:1445
ptrdiff_t size
Definition: opengl_enc.c:101
Main libavdevice API header.
#define AVERROR(e)
Definition: error.h:43
uint8_t * buffer
Definition: sndio.h:37
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
char * url
input or output URL.
Definition: avformat.h:1447
common internal API header
#define FFMIN(a, b)
Definition: common.h:96
const char * name
Definition: avformat.h:507
#define s(width, name)
Definition: cbs_vp9.c:257
int ff_sndio_close(SndioData *s)
Definition: sndio.c:112
Stream structure.
Definition: avformat.h:874
int buffer_size
Definition: sndio.h:39
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
int buffer_offset
Definition: sndio.h:40
av_cold int ff_sndio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: sndio.c:36
#define s1
Definition: regdef.h:38
int64_t softpos
Definition: sndio.h:36
struct sio_hdl * hdl
Definition: sndio.h:33
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
static av_cold int audio_write_header(AVFormatContext *s1)
Definition: sndio_enc.c:31
int len
AVOutputFormat ff_sndio_muxer
Definition: sndio_enc.c:89
void * priv_data
Format private data.
Definition: avformat.h:1379
int channels
Audio only.
Definition: avcodec.h:4006
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: sndio_enc.c:46
static const AVClass sndio_muxer_class
Definition: sndio_enc.c:82
This structure stores compressed data.
Definition: avcodec.h:1422