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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.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:
43 if (cmd->u.subpicture)
44 subpicture_Delete(cmd->u.subpicture);
46 case VOUT_CONTROL_OSD_TITLE:
47 case VOUT_CONTROL_CHANGE_FILTERS:
48 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
57 void vout_control_Init(vout_control_t *ctrl)
59 vlc_mutex_init(&ctrl->lock);
60 vlc_cond_init(&ctrl->wait_request);
61 vlc_cond_init(&ctrl->wait_acknowledge);
63 ctrl->is_dead = false;
64 ctrl->is_sleeping = false;
65 ctrl->can_sleep = true;
66 ctrl->is_processing = false;
67 ARRAY_INIT(ctrl->cmd);
70 void vout_control_Clean(vout_control_t *ctrl)
73 for (int i = 0; i < ctrl->cmd.i_size; i++) {
74 vout_control_cmd_t cmd = ARRAY_VAL(ctrl->cmd, i);
75 vout_control_cmd_Clean(&cmd);
77 ARRAY_RESET(ctrl->cmd);
79 vlc_mutex_destroy(&ctrl->lock);
80 vlc_cond_destroy(&ctrl->wait_request);
81 vlc_cond_destroy(&ctrl->wait_acknowledge);
84 void vout_control_Dead(vout_control_t *ctrl)
86 vlc_mutex_lock(&ctrl->lock);
88 vlc_cond_broadcast(&ctrl->wait_acknowledge);
89 vlc_mutex_unlock(&ctrl->lock);
93 void vout_control_WaitEmpty(vout_control_t *ctrl)
95 vlc_mutex_lock(&ctrl->lock);
96 while ((ctrl->cmd.i_size > 0 || ctrl->is_processing) && !ctrl->is_dead)
97 vlc_cond_wait(&ctrl->wait_acknowledge, &ctrl->lock);
98 vlc_mutex_unlock(&ctrl->lock);
101 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
103 vlc_mutex_lock(&ctrl->lock);
104 if (!ctrl->is_dead) {
105 ARRAY_APPEND(ctrl->cmd, *cmd);
106 vlc_cond_signal(&ctrl->wait_request);
108 vout_control_cmd_Clean(cmd);
110 vlc_mutex_unlock(&ctrl->lock);
113 void vout_control_Wake(vout_control_t *ctrl)
115 vlc_mutex_lock(&ctrl->lock);
116 ctrl->can_sleep = false;
117 if (ctrl->is_sleeping)
118 vlc_cond_signal(&ctrl->wait_request);
119 vlc_mutex_unlock(&ctrl->lock);
122 void vout_control_PushVoid(vout_control_t *ctrl, int type)
124 vout_control_cmd_t cmd;
126 vout_control_cmd_Init(&cmd, type);
127 vout_control_Push(ctrl, &cmd);
129 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
131 vout_control_cmd_t cmd;
133 vout_control_cmd_Init(&cmd, type);
134 cmd.u.boolean = boolean;
135 vout_control_Push(ctrl, &cmd);
137 void vout_control_PushInteger(vout_control_t *ctrl, int type, int integer)
139 vout_control_cmd_t cmd;
141 vout_control_cmd_Init(&cmd, type);
142 cmd.u.integer = integer;
143 vout_control_Push(ctrl, &cmd);
145 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
147 vout_control_cmd_t cmd;
149 vout_control_cmd_Init(&cmd, type);
151 vout_control_Push(ctrl, &cmd);
153 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
155 vout_control_cmd_t cmd;
157 vout_control_cmd_Init(&cmd, type);
158 cmd.u.message.channel = channel;
159 cmd.u.message.string = strdup(string);
160 vout_control_Push(ctrl, &cmd);
162 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
164 vout_control_cmd_t cmd;
166 vout_control_cmd_Init(&cmd, type);
169 vout_control_Push(ctrl, &cmd);
171 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
173 vout_control_cmd_t cmd;
175 vout_control_cmd_Init(&cmd, type);
176 cmd.u.string = strdup(string);
177 vout_control_Push(ctrl, &cmd);
180 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
181 mtime_t deadline, mtime_t timeout)
183 vlc_mutex_lock(&ctrl->lock);
184 if (ctrl->cmd.i_size <= 0) {
185 ctrl->is_processing = false;
186 vlc_cond_broadcast(&ctrl->wait_acknowledge);
188 const mtime_t max_deadline = mdate() + timeout;
190 /* Supurious wake up are perfectly fine */
191 if (deadline <= VLC_TS_INVALID) {
192 ctrl->is_sleeping = true;
194 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, max_deadline);
195 ctrl->is_sleeping = false;
197 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, __MIN(deadline, max_deadline));
202 if (ctrl->cmd.i_size > 0) {
204 *cmd = ARRAY_VAL(ctrl->cmd, 0);
205 ARRAY_REMOVE(ctrl->cmd, 0);
207 ctrl->is_processing = true;
210 ctrl->can_sleep = true;
212 vlc_mutex_unlock(&ctrl->lock);
214 return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;