1 /*****************************************************************************
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_block.h>
32 #include <vlc_stream.h>
36 struct vlc_stream_fifo
{
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
);
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
;
60 block
= vlc_fifo_DequeueAllUnlocked(fifo
);
63 vlc_fifo_Unlock(fifo
);
65 block_ChainRelease(block
);
68 /* Destroy shared state if write end is already closed */
69 block_FifoRelease(fifo
);
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
;
81 while (vlc_fifo_IsEmpty(fifo
))
91 block
= vlc_fifo_DequeueUnlocked(fifo
);
92 vlc_fifo_Unlock(fifo
);
96 static int vlc_stream_fifo_Control(stream_t
*s
, int query
, va_list ap
)
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;
109 case STREAM_GET_PTS_DELAY
:
110 *va_arg(ap
, vlc_tick_t
*) = DEFAULT_PTS_DELAY
;
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
))
125 writer
->fifo
= block_FifoNew();
126 if (unlikely(writer
->fifo
== NULL
)) {
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
);
141 sys
= vlc_stream_Private(s
);
142 sys
->writer
= writer
;
143 s
->pf_block
= vlc_stream_fifo_Block
;
145 s
->pf_control
= vlc_stream_fifo_Control
;
150 int vlc_stream_fifo_Queue(vlc_stream_fifo_t
*writer
, block_t
*block
)
152 vlc_fifo_t
*fifo
= writer
->fifo
;
155 if (likely(!writer
->eof
))
157 vlc_fifo_QueueUnlocked(fifo
, block
);
160 vlc_fifo_Unlock(fifo
);
162 if (unlikely(block
!= NULL
))
164 block_Release(block
);
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
))
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
;
188 closed
= writer
->eof
;
190 vlc_fifo_Signal(fifo
);
191 vlc_fifo_Unlock(fifo
);
194 /* Destroy shared state if read end is already closed */
195 block_FifoRelease(fifo
);