/src/vlc/src/misc/picture_fifo.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * picture_fifo.c : picture fifo functions |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2009 VLC authors and VideoLAN |
5 | | * Copyright (C) 2009 Laurent Aimar <fenrir _AT_ videolan _DOT_ org> |
6 | | * |
7 | | * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org> |
8 | | * |
9 | | * This program is free software; you can redistribute it and/or modify it |
10 | | * under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation; either version 2.1 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * This program is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program; if not, write to the Free Software Foundation, |
21 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
22 | | *****************************************************************************/ |
23 | | |
24 | | /***************************************************************************** |
25 | | * Preamble |
26 | | *****************************************************************************/ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | # include "config.h" |
30 | | #endif |
31 | | #include <assert.h> |
32 | | |
33 | | #include <vlc_common.h> |
34 | | #include <vlc_threads.h> |
35 | | #include <vlc_picture_fifo.h> |
36 | | |
37 | | /***************************************************************************** |
38 | | * |
39 | | *****************************************************************************/ |
40 | | struct picture_fifo_t { |
41 | | vlc_mutex_t lock; |
42 | | vlc_picture_chain_t pics; |
43 | | }; |
44 | | |
45 | | static void PictureFifoReset(picture_fifo_t *fifo) |
46 | 1 | { |
47 | 1 | vlc_picture_chain_Init( &fifo->pics ); |
48 | 1 | } |
49 | | static void PictureFifoPush(picture_fifo_t *fifo, picture_t *picture) |
50 | 0 | { |
51 | 0 | assert(!picture_HasChainedPics(picture)); |
52 | 0 | vlc_picture_chain_Append( &fifo->pics, picture ); |
53 | 0 | } |
54 | | static picture_t *PictureFifoPop(picture_fifo_t *fifo) |
55 | 0 | { |
56 | 0 | return vlc_picture_chain_PopFront( &fifo->pics ); |
57 | 0 | } |
58 | | |
59 | | picture_fifo_t *picture_fifo_New(void) |
60 | 1 | { |
61 | 1 | picture_fifo_t *fifo = malloc(sizeof(*fifo)); |
62 | 1 | if (!fifo) |
63 | 0 | return NULL; |
64 | | |
65 | 1 | vlc_mutex_init(&fifo->lock); |
66 | 1 | PictureFifoReset(fifo); |
67 | 1 | return fifo; |
68 | 1 | } |
69 | | |
70 | | void picture_fifo_Push(picture_fifo_t *fifo, picture_t *picture) |
71 | 0 | { |
72 | 0 | vlc_mutex_lock(&fifo->lock); |
73 | 0 | PictureFifoPush(fifo, picture); |
74 | 0 | vlc_mutex_unlock(&fifo->lock); |
75 | 0 | } |
76 | | picture_t *picture_fifo_Pop(picture_fifo_t *fifo) |
77 | 0 | { |
78 | 0 | vlc_mutex_lock(&fifo->lock); |
79 | 0 | picture_t *picture = PictureFifoPop(fifo); |
80 | 0 | vlc_mutex_unlock(&fifo->lock); |
81 | |
|
82 | 0 | return picture; |
83 | 0 | } |
84 | | bool picture_fifo_IsEmpty(picture_fifo_t *fifo) |
85 | 0 | { |
86 | 0 | vlc_mutex_lock(&fifo->lock); |
87 | 0 | bool empty = vlc_picture_chain_IsEmpty( &fifo->pics ); |
88 | 0 | vlc_mutex_unlock(&fifo->lock); |
89 | |
|
90 | 0 | return empty; |
91 | 0 | } |
92 | | void picture_fifo_Flush(picture_fifo_t *fifo, vlc_tick_t date, bool flush_before) |
93 | 1 | { |
94 | 1 | picture_t *picture; |
95 | | |
96 | 1 | vlc_picture_chain_t flush_chain; |
97 | | |
98 | 1 | vlc_picture_chain_Init(&flush_chain); |
99 | | |
100 | 1 | vlc_mutex_lock(&fifo->lock); |
101 | 1 | if (date == VLC_TICK_INVALID) |
102 | 1 | vlc_picture_chain_GetAndClear(&fifo->pics, &flush_chain); |
103 | 0 | else { |
104 | 0 | vlc_picture_chain_t filter_chain; |
105 | 0 | vlc_picture_chain_GetAndClear(&fifo->pics, &filter_chain); |
106 | |
|
107 | 0 | while ( !vlc_picture_chain_IsEmpty( &filter_chain ) ) { |
108 | 0 | picture = vlc_picture_chain_PopFront( &filter_chain ); |
109 | |
|
110 | 0 | if (( flush_before && picture->date <= date) || |
111 | 0 | (!flush_before && picture->date >= date)) |
112 | 0 | vlc_picture_chain_Append( &flush_chain, picture ); |
113 | 0 | else |
114 | 0 | PictureFifoPush(fifo, picture); |
115 | 0 | } |
116 | 0 | } |
117 | 1 | vlc_mutex_unlock(&fifo->lock); |
118 | | |
119 | 1 | while ((picture = vlc_picture_chain_PopFront(&flush_chain)) != NULL) |
120 | 0 | picture_Release(picture); |
121 | 1 | } |
122 | | void picture_fifo_Delete(picture_fifo_t *fifo) |
123 | 1 | { |
124 | 1 | picture_fifo_Flush(fifo, VLC_TICK_INVALID, true); |
125 | 1 | free(fifo); |
126 | 1 | } |