1 /*****************************************************************************
2 * control.c : vout internal control
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
30 #include "vout_internal.h"
33 void vout_control_cmd_Init(vout_control_cmd_t
*cmd
, int type
)
35 memset(cmd
, 0, sizeof(*cmd
));
39 void vout_control_cmd_Clean(vout_control_cmd_t
*cmd
)
42 case VOUT_CONTROL_SUBPICTURE
:
44 subpicture_Delete(cmd
->subpicture
);
46 case VOUT_CONTROL_CHANGE_FILTERS
:
55 void vout_control_Init(vout_control_t
*ctrl
)
57 vlc_mutex_init(&ctrl
->lock
);
58 vlc_cond_init(&ctrl
->wait_request
);
59 vlc_cond_init(&ctrl
->wait_acknowledge
);
61 ctrl
->is_dead
= false;
62 ctrl
->can_sleep
= true;
63 ctrl
->is_processing
= true;
64 ARRAY_INIT(ctrl
->cmd
);
67 void vout_control_Clean(vout_control_t
*ctrl
)
70 for (int i
= 0; i
< ctrl
->cmd
.i_size
; i
++) {
71 vout_control_cmd_t cmd
= ARRAY_VAL(ctrl
->cmd
, i
);
72 vout_control_cmd_Clean(&cmd
);
74 ARRAY_RESET(ctrl
->cmd
);
76 vlc_mutex_destroy(&ctrl
->lock
);
77 vlc_cond_destroy(&ctrl
->wait_request
);
78 vlc_cond_destroy(&ctrl
->wait_acknowledge
);
81 void vout_control_Dead(vout_control_t
*ctrl
)
83 vlc_mutex_lock(&ctrl
->lock
);
85 vlc_cond_broadcast(&ctrl
->wait_acknowledge
);
86 vlc_mutex_unlock(&ctrl
->lock
);
90 void vout_control_WaitEmpty(vout_control_t
*ctrl
)
92 vlc_mutex_lock(&ctrl
->lock
);
93 while ((ctrl
->cmd
.i_size
> 0 || ctrl
->is_processing
) && !ctrl
->is_dead
)
94 vlc_cond_wait(&ctrl
->wait_acknowledge
, &ctrl
->lock
);
95 vlc_mutex_unlock(&ctrl
->lock
);
98 void vout_control_Push(vout_control_t
*ctrl
, vout_control_cmd_t
*cmd
)
100 vlc_mutex_lock(&ctrl
->lock
);
101 if (!ctrl
->is_dead
) {
102 ARRAY_APPEND(ctrl
->cmd
, *cmd
);
103 vlc_cond_signal(&ctrl
->wait_request
);
105 vout_control_cmd_Clean(cmd
);
107 vlc_mutex_unlock(&ctrl
->lock
);
110 void vout_control_Wake(vout_control_t
*ctrl
)
112 vlc_mutex_lock(&ctrl
->lock
);
113 ctrl
->can_sleep
= false;
114 vlc_cond_signal(&ctrl
->wait_request
);
115 vlc_mutex_unlock(&ctrl
->lock
);
118 void vout_control_PushVoid(vout_control_t
*ctrl
, int type
)
120 vout_control_cmd_t cmd
;
122 vout_control_cmd_Init(&cmd
, type
);
123 vout_control_Push(ctrl
, &cmd
);
125 void vout_control_PushBool(vout_control_t
*ctrl
, int type
, bool boolean
)
127 vout_control_cmd_t cmd
;
129 vout_control_cmd_Init(&cmd
, type
);
130 cmd
.boolean
= boolean
;
131 vout_control_Push(ctrl
, &cmd
);
133 void vout_control_PushInteger(vout_control_t
*ctrl
, int type
, int integer
)
135 vout_control_cmd_t cmd
;
137 vout_control_cmd_Init(&cmd
, type
);
138 cmd
.integer
= integer
;
139 vout_control_Push(ctrl
, &cmd
);
141 void vout_control_PushTime(vout_control_t
*ctrl
, int type
, vlc_tick_t time
)
143 vout_control_cmd_t cmd
;
145 vout_control_cmd_Init(&cmd
, type
);
147 vout_control_Push(ctrl
, &cmd
);
149 void vout_control_PushPair(vout_control_t
*ctrl
, int type
, int a
, int b
)
151 vout_control_cmd_t cmd
;
153 vout_control_cmd_Init(&cmd
, type
);
156 vout_control_Push(ctrl
, &cmd
);
158 void vout_control_PushString(vout_control_t
*ctrl
, int type
, const char *string
)
160 vout_control_cmd_t cmd
;
162 vout_control_cmd_Init(&cmd
, type
);
163 cmd
.string
= string
? strdup(string
) : NULL
;
164 vout_control_Push(ctrl
, &cmd
);
167 int vout_control_Pop(vout_control_t
*ctrl
, vout_control_cmd_t
*cmd
,
170 vlc_mutex_lock(&ctrl
->lock
);
171 if (ctrl
->cmd
.i_size
<= 0) {
172 ctrl
->is_processing
= false;
173 vlc_cond_broadcast(&ctrl
->wait_acknowledge
);
175 /* Spurious wakeups are perfectly fine */
176 if (deadline
!= VLC_TICK_INVALID
&& ctrl
->can_sleep
)
177 vlc_cond_timedwait(&ctrl
->wait_request
, &ctrl
->lock
, deadline
);
181 if (ctrl
->cmd
.i_size
> 0) {
183 *cmd
= ARRAY_VAL(ctrl
->cmd
, 0);
184 ARRAY_REMOVE(ctrl
->cmd
, 0);
186 ctrl
->is_processing
= true;
189 ctrl
->can_sleep
= true;
191 vlc_mutex_unlock(&ctrl
->lock
);
193 return has_cmd
? VLC_SUCCESS
: VLC_EGENERIC
;