Contribs, Fribidi: allow to not build bins nor tests
[vlc.git] / src / input / stream_fifo.c
blob4cdd54801655ec1ee4909c9f658f64d27a4fc134
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 vlc_stream_fifo {
37 vlc_fifo_t *fifo;
38 bool eof;
41 struct vlc_stream_fifo_private {
42 vlc_stream_fifo_t *writer;
45 static vlc_stream_fifo_t *vlc_stream_fifo_Writer(stream_t *s)
47 struct vlc_stream_fifo_private *sys = vlc_stream_Private(s);
49 return sys->writer;
52 static void vlc_stream_fifo_Destroy(stream_t *s)
54 struct vlc_stream_fifo *writer = vlc_stream_fifo_Writer(s);
55 vlc_fifo_t *fifo = writer->fifo;
56 block_t *block;
57 bool closed;
59 vlc_fifo_Lock(fifo);
60 block = vlc_fifo_DequeueAllUnlocked(fifo);
61 closed = writer->eof;
62 writer->eof = true;
63 vlc_fifo_Unlock(fifo);
65 block_ChainRelease(block);
67 if (closed) {
68 /* Destroy shared state if write end is already closed */
69 block_FifoRelease(fifo);
70 free(writer);
74 static block_t *vlc_stream_fifo_Block(stream_t *s, bool *restrict eof)
76 struct vlc_stream_fifo *sys = vlc_stream_fifo_Writer(s);
77 vlc_fifo_t *fifo = sys->fifo;
78 block_t *block;
80 vlc_fifo_Lock(fifo);
81 while (vlc_fifo_IsEmpty(fifo))
83 if (sys->eof)
85 *eof = true;
86 break;
88 vlc_fifo_Wait(fifo);
91 block = vlc_fifo_DequeueUnlocked(fifo);
92 vlc_fifo_Unlock(fifo);
93 return block;
96 static int vlc_stream_fifo_Control(stream_t *s, int query, va_list ap)
98 (void) s;
100 switch (query)
102 case STREAM_CAN_SEEK:
103 case STREAM_CAN_FASTSEEK:
104 case STREAM_CAN_PAUSE:
105 case STREAM_CAN_CONTROL_PACE:
106 *va_arg(ap, bool *) = false;
107 break;
109 case STREAM_GET_PTS_DELAY:
110 *va_arg(ap, vlc_tick_t *) = DEFAULT_PTS_DELAY;
111 break;
113 default:
114 return VLC_EGENERIC;
116 return VLC_SUCCESS;
119 vlc_stream_fifo_t *vlc_stream_fifo_New(vlc_object_t *parent, stream_t **reader)
121 struct vlc_stream_fifo *writer = malloc(sizeof (*writer));
122 if (unlikely(writer == NULL))
123 return NULL;
125 writer->fifo = block_FifoNew();
126 if (unlikely(writer->fifo == NULL)) {
127 free(writer);
128 return NULL;
130 writer->eof = false;
132 struct vlc_stream_fifo_private *sys;
133 stream_t *s = vlc_stream_CustomNew(parent, vlc_stream_fifo_Destroy,
134 sizeof (*sys), "stream");
135 if (unlikely(s == NULL)) {
136 block_FifoRelease(writer->fifo);
137 free(writer);
138 return NULL;
141 sys = vlc_stream_Private(s);
142 sys->writer = writer;
143 s->pf_block = vlc_stream_fifo_Block;
144 s->pf_seek = NULL;
145 s->pf_control = vlc_stream_fifo_Control;
146 *reader = s;
147 return writer;
150 int vlc_stream_fifo_Queue(vlc_stream_fifo_t *writer, block_t *block)
152 vlc_fifo_t *fifo = writer->fifo;
154 vlc_fifo_Lock(fifo);
155 if (likely(!writer->eof))
157 vlc_fifo_QueueUnlocked(fifo, block);
158 block = NULL;
160 vlc_fifo_Unlock(fifo);
162 if (unlikely(block != NULL))
164 block_Release(block);
165 errno = EPIPE;
166 return -1;
168 return 0;
171 ssize_t vlc_stream_fifo_Write(vlc_stream_fifo_t *writer,
172 const void *buf, size_t len)
174 block_t *block = block_Alloc(len);
175 if (unlikely(block == NULL))
176 return -1;
178 memcpy(block->p_buffer, buf, len);
179 return vlc_stream_fifo_Queue(writer, block) ? -1 : (ssize_t)len;
182 void vlc_stream_fifo_Close(vlc_stream_fifo_t *writer)
184 vlc_fifo_t *fifo = writer->fifo;
185 bool closed;
187 vlc_fifo_Lock(fifo);
188 closed = writer->eof;
189 writer->eof = true;
190 vlc_fifo_Signal(fifo);
191 vlc_fifo_Unlock(fifo);
193 if (closed) {
194 /* Destroy shared state if read end is already closed */
195 block_FifoRelease(fifo);
196 free(writer);