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>
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
;
50 block
= vlc_fifo_DequeueAllUnlocked(fifo
);
53 vlc_fifo_Unlock(fifo
);
55 block_ChainRelease(block
);
58 { /* Destroy shared state if write end is already closed */
59 block_FifoRelease(fifo
);
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
;
71 while (vlc_fifo_IsEmpty(fifo
))
81 block
= vlc_fifo_DequeueUnlocked(fifo
);
82 vlc_fifo_Unlock(fifo
);
86 static int vlc_stream_fifo_Control(stream_t
*s
, int query
, va_list ap
)
93 case STREAM_CAN_FASTSEEK
:
94 case STREAM_CAN_PAUSE
:
95 case STREAM_CAN_CONTROL_PACE
:
96 *va_arg(ap
, bool *) = false;
99 case STREAM_GET_PTS_DELAY
:
100 *va_arg(ap
, int64_t *) = DEFAULT_PTS_DELAY
;
109 stream_t
*vlc_stream_fifo_New(vlc_object_t
*parent
)
111 stream_sys_t
*sys
= malloc(sizeof (*sys
));
112 if (unlikely(sys
== NULL
))
115 sys
->fifo
= block_FifoNew();
116 if (unlikely(sys
->fifo
== NULL
))
124 stream_t
*s
= vlc_stream_CommonNew(parent
, vlc_stream_fifo_Destroy
);
125 if (unlikely(s
== NULL
))
127 block_FifoRelease(sys
->fifo
);
132 s
->pf_block
= vlc_stream_fifo_Block
;
134 s
->pf_control
= vlc_stream_fifo_Control
;
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
;
145 if (likely(!sys
->eof
))
147 vlc_fifo_QueueUnlocked(fifo
, block
);
150 vlc_fifo_Unlock(fifo
);
152 if (unlikely(block
!= NULL
))
154 block_Release(block
);
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
))
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
;
180 vlc_fifo_Signal(fifo
);
181 vlc_fifo_Unlock(fifo
);
184 { /* Destroy shared state if read end is already closed */
185 block_FifoRelease(fifo
);
189 vlc_object_release(s
);