codec/esout: add support for CEA708
[vlc.git] / src / input / stream_fifo.c
blobeb7b0ba8d6c85256b2b449767fd1736ecc275b85
1 /*****************************************************************************
2 * stream_fifo.c
3 *****************************************************************************
4 * Copyright (C) 2016 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
30 #include <vlc_common.h>
31 #include <vlc_block.h>
32 #include <vlc_stream.h>
34 #include "stream.h"
36 struct stream_sys_t
38 vlc_fifo_t *fifo;
39 bool eof;
42 static void vlc_stream_fifo_Destroy(stream_t *s)
44 stream_sys_t *sys = s->p_sys;
45 vlc_fifo_t *fifo = sys->fifo;
46 block_t *block;
47 bool closed;
49 vlc_fifo_Lock(fifo);
50 block = vlc_fifo_DequeueAllUnlocked(fifo);
51 closed = sys->eof;
52 sys->eof = true;
53 vlc_fifo_Unlock(fifo);
55 block_ChainRelease(block);
57 if (closed)
58 { /* Destroy shared state if write end is already closed */
59 block_FifoRelease(fifo);
60 free(sys);
64 static block_t *vlc_stream_fifo_Block(stream_t *s, bool *restrict eof)
66 stream_sys_t *sys = s->p_sys;
67 vlc_fifo_t *fifo = sys->fifo;
68 block_t *block;
70 vlc_fifo_Lock(fifo);
71 while (vlc_fifo_IsEmpty(fifo))
73 if (sys->eof)
75 *eof = true;
76 break;
78 vlc_fifo_Wait(fifo);
81 block = vlc_fifo_DequeueUnlocked(fifo);
82 vlc_fifo_Unlock(fifo);
83 return block;
86 static int vlc_stream_fifo_Control(stream_t *s, int query, va_list ap)
88 (void) s;
90 switch (query)
92 case STREAM_CAN_SEEK:
93 case STREAM_CAN_FASTSEEK:
94 case STREAM_CAN_PAUSE:
95 case STREAM_CAN_CONTROL_PACE:
96 *va_arg(ap, bool *) = false;
97 break;
99 case STREAM_GET_PTS_DELAY:
100 *va_arg(ap, int64_t *) = DEFAULT_PTS_DELAY;
101 break;
103 default:
104 return VLC_EGENERIC;
106 return VLC_SUCCESS;
109 stream_t *vlc_stream_fifo_New(vlc_object_t *parent)
111 stream_sys_t *sys = malloc(sizeof (*sys));
112 if (unlikely(sys == NULL))
113 return NULL;
115 sys->fifo = block_FifoNew();
116 if (unlikely(sys->fifo == NULL))
118 free(sys);
119 return NULL;
122 sys->eof = false;
124 stream_t *s = vlc_stream_CommonNew(parent, vlc_stream_fifo_Destroy);
125 if (unlikely(s == NULL))
127 block_FifoRelease(sys->fifo);
128 free(sys);
129 return NULL;
132 s->pf_block = vlc_stream_fifo_Block;
133 s->pf_seek = NULL;
134 s->pf_control = vlc_stream_fifo_Control;
135 s->p_sys = sys;
136 return vlc_object_hold(s);
139 int vlc_stream_fifo_Queue(stream_t *s, block_t *block)
141 stream_sys_t *sys = s->p_sys;
142 vlc_fifo_t *fifo = sys->fifo;
144 vlc_fifo_Lock(fifo);
145 if (likely(!sys->eof))
147 vlc_fifo_QueueUnlocked(fifo, block);
148 block = NULL;
150 vlc_fifo_Unlock(fifo);
152 if (unlikely(block != NULL))
154 block_Release(block);
155 errno = EPIPE;
156 return -1;
158 return 0;
161 ssize_t vlc_stream_fifo_Write(stream_t *s, const void *buf, size_t len)
163 block_t *block = block_Alloc(len);
164 if (unlikely(block == NULL))
165 return -1;
167 memcpy(block->p_buffer, buf, len);
168 return vlc_stream_fifo_Queue(s, block) ? -1 : (ssize_t)len;
171 void vlc_stream_fifo_Close(stream_t *s)
173 stream_sys_t *sys = s->p_sys;
174 vlc_fifo_t *fifo = sys->fifo;
175 bool closed;
177 vlc_fifo_Lock(fifo);
178 closed = sys->eof;
179 sys->eof = true;
180 vlc_fifo_Signal(fifo);
181 vlc_fifo_Unlock(fifo);
183 if (closed)
184 { /* Destroy shared state if read end is already closed */
185 block_FifoRelease(fifo);
186 free(sys);
189 vlc_object_release(s);