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>
32 void vout_control_cmd_Init(vout_control_cmd_t *cmd, int type)
34 memset(cmd, 0, sizeof(*cmd));
38 void vout_control_cmd_Clean(vout_control_cmd_t *cmd)
41 //case VOUT_CONTROL_OSD_MESSAGE:
42 case VOUT_CONTROL_OSD_TITLE:
43 case VOUT_CONTROL_CHANGE_FILTERS:
47 case VOUT_CONTROL_OSD_TEXT:
48 free(cmd->text.string);
50 text_style_Delete(cmd->text.style);
52 case VOUT_CONTROL_OSD_SUBPICTURE:
54 subpicture_Delete(cmd->subpicture);
63 void vout_control_Init(vout_control_t *ctrl)
65 vlc_mutex_init(&ctrl->lock);
66 vlc_cond_init(&ctrl->wait_request);
67 vlc_cond_init(&ctrl->wait_acknowledge);
69 ctrl->is_dead = false;
70 ctrl->is_sleeping = false;
71 ctrl->can_sleep = true;
72 ctrl->is_processing = false;
73 ARRAY_INIT(ctrl->cmd);
76 void vout_control_Clean(vout_control_t *ctrl)
79 for (int i = 0; i < ctrl->cmd.i_size; i++) {
80 vout_control_cmd_t cmd = ARRAY_VAL(ctrl->cmd, i);
81 vout_control_cmd_Clean(&cmd);
83 ARRAY_RESET(ctrl->cmd);
85 vlc_mutex_destroy(&ctrl->lock);
86 vlc_cond_destroy(&ctrl->wait_request);
87 vlc_cond_destroy(&ctrl->wait_acknowledge);
90 void vout_control_Dead(vout_control_t *ctrl)
92 vlc_mutex_lock(&ctrl->lock);
94 vlc_cond_broadcast(&ctrl->wait_acknowledge);
95 vlc_mutex_unlock(&ctrl->lock);
99 void vout_control_WaitEmpty(vout_control_t *ctrl)
101 vlc_mutex_lock(&ctrl->lock);
102 while ((ctrl->cmd.i_size > 0 || ctrl->is_processing) && !ctrl->is_dead)
103 vlc_cond_wait(&ctrl->wait_acknowledge, &ctrl->lock);
104 vlc_mutex_unlock(&ctrl->lock);
107 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
109 vlc_mutex_lock(&ctrl->lock);
110 if (!ctrl->is_dead) {
111 ARRAY_APPEND(ctrl->cmd, *cmd);
112 vlc_cond_signal(&ctrl->wait_request);
114 vout_control_cmd_Clean(cmd);
116 vlc_mutex_unlock(&ctrl->lock);
119 void vout_control_Wake(vout_control_t *ctrl)
121 vlc_mutex_lock(&ctrl->lock);
122 ctrl->can_sleep = false;
123 if (ctrl->is_sleeping)
124 vlc_cond_signal(&ctrl->wait_request);
125 vlc_mutex_unlock(&ctrl->lock);
128 void vout_control_PushVoid(vout_control_t *ctrl, int type)
130 vout_control_cmd_t cmd;
132 vout_control_cmd_Init(&cmd, type);
133 vout_control_Push(ctrl, &cmd);
135 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
137 vout_control_cmd_t cmd;
139 vout_control_cmd_Init(&cmd, type);
140 cmd.u.boolean = boolean;
141 vout_control_Push(ctrl, &cmd);
143 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
145 vout_control_cmd_t cmd;
147 vout_control_cmd_Init(&cmd, type);
149 vout_control_Push(ctrl, &cmd);
151 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
153 vout_control_cmd_t cmd;
155 vout_control_cmd_Init(&cmd, type);
156 cmd.u.message.channel = channel;
157 cmd.u.message.string = strdup(string);
158 vout_control_Push(ctrl, &cmd);
160 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
162 vout_control_cmd_t cmd;
164 vout_control_cmd_Init(&cmd, type);
167 vout_control_Push(ctrl, &cmd);
169 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
171 vout_control_cmd_t cmd;
173 vout_control_cmd_Init(&cmd, type);
174 cmd.u.string = strdup(string);
175 vout_control_Push(ctrl, &cmd);
178 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
179 mtime_t deadline, mtime_t timeout)
181 vlc_mutex_lock(&ctrl->lock);
182 if (ctrl->cmd.i_size <= 0) {
183 ctrl->is_processing = false;
184 vlc_cond_broadcast(&ctrl->wait_acknowledge);
186 const mtime_t max_deadline = mdate() + timeout;
188 /* Supurious wake up are perfectly fine */
189 if (deadline <= VLC_TS_INVALID) {
190 ctrl->is_sleeping = true;
192 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, max_deadline);
193 ctrl->is_sleeping = false;
195 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, __MIN(deadline, max_deadline));
200 if (ctrl->cmd.i_size > 0) {
202 *cmd = ARRAY_VAL(ctrl->cmd, 0);
203 ARRAY_REMOVE(ctrl->cmd, 0);
205 ctrl->is_processing = true;
208 ctrl->can_sleep = true;
210 vlc_mutex_unlock(&ctrl->lock);
212 return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;