]> git.sesse.net Git - vlc/blob - src/video_output/control.c
Used vout_control for various commands (vout).
[vlc] / src / video_output / control.c
1 /*****************************************************************************
2  * control.c : vout internal control
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include "control.h"
30
31 /* */
32 void vout_control_cmd_Init(vout_control_cmd_t *cmd, int type)
33 {
34     memset(cmd, 0, sizeof(*cmd));
35     cmd->type = type;
36 }
37
38 void vout_control_cmd_Clean(vout_control_cmd_t *cmd)
39 {
40     switch (cmd->type) {
41     //case VOUT_CONTROL_OSD_MESSAGE:
42     case VOUT_CONTROL_OSD_TITLE:
43     case VOUT_CONTROL_CHANGE_FILTERS:
44         free(cmd->u.string);
45         break;
46 #if 0
47     case VOUT_CONTROL_OSD_TEXT:
48         free(cmd->text.string);
49         if (cmd->text.style)
50             text_style_Delete(cmd->text.style);
51         break;
52     case VOUT_CONTROL_OSD_SUBPICTURE:
53         if (cmd->subpicture)
54             subpicture_Delete(cmd->subpicture);
55         break;
56 #endif
57     default:
58         break;
59     }
60 }
61
62 /* */
63 void vout_control_Init(vout_control_t *ctrl)
64 {
65     vlc_mutex_init(&ctrl->lock);
66     vlc_cond_init(&ctrl->wait_request);
67     vlc_cond_init(&ctrl->wait_acknowledge);
68
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);
74 }
75
76 void vout_control_Clean(vout_control_t *ctrl)
77 {
78     /* */
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);
82     }
83     ARRAY_RESET(ctrl->cmd);
84
85     vlc_mutex_destroy(&ctrl->lock);
86     vlc_cond_destroy(&ctrl->wait_request);
87     vlc_cond_destroy(&ctrl->wait_acknowledge);
88 }
89
90 void vout_control_Dead(vout_control_t *ctrl)
91 {
92     vlc_mutex_lock(&ctrl->lock);
93     ctrl->is_dead = true;
94     vlc_cond_broadcast(&ctrl->wait_acknowledge);
95     vlc_mutex_unlock(&ctrl->lock);
96
97 }
98
99 void vout_control_WaitEmpty(vout_control_t *ctrl)
100 {
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);
105 }
106
107 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
108 {
109     vlc_mutex_lock(&ctrl->lock);
110     if (!ctrl->is_dead) {
111         ARRAY_APPEND(ctrl->cmd, *cmd);
112         vlc_cond_signal(&ctrl->wait_request);
113     } else {
114         vout_control_cmd_Clean(cmd);
115     }
116     vlc_mutex_unlock(&ctrl->lock);
117 }
118
119 void vout_control_Wake(vout_control_t *ctrl)
120 {
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);
126 }
127
128 void vout_control_PushVoid(vout_control_t *ctrl, int type)
129 {
130     vout_control_cmd_t cmd;
131
132     vout_control_cmd_Init(&cmd, type);
133     vout_control_Push(ctrl, &cmd);
134 }
135 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
136 {
137     vout_control_cmd_t cmd;
138
139     vout_control_cmd_Init(&cmd, type);
140     cmd.u.boolean = boolean;
141     vout_control_Push(ctrl, &cmd);
142 }
143 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
144 {
145     vout_control_cmd_t cmd;
146
147     vout_control_cmd_Init(&cmd, type);
148     cmd.u.time = time;
149     vout_control_Push(ctrl, &cmd);
150 }
151 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
152 {
153     vout_control_cmd_t cmd;
154
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);
159 }
160 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
161 {
162     vout_control_cmd_t cmd;
163
164     vout_control_cmd_Init(&cmd, type);
165     cmd.u.pair.a = a;
166     cmd.u.pair.b = b;
167     vout_control_Push(ctrl, &cmd);
168 }
169 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
170 {
171     vout_control_cmd_t cmd;
172
173     vout_control_cmd_Init(&cmd, type);
174     cmd.u.string = strdup(string);
175     vout_control_Push(ctrl, &cmd);
176 }
177
178 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
179                      mtime_t deadline, mtime_t timeout)
180 {
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);
185
186         const mtime_t max_deadline = mdate() + timeout;
187
188         /* Supurious wake up are perfectly fine */
189         if (deadline <= VLC_TS_INVALID) {
190             ctrl->is_sleeping = true;
191             if (ctrl->can_sleep)
192                 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, max_deadline);
193             ctrl->is_sleeping = false;
194         } else {
195             vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, __MIN(deadline, max_deadline));
196         }
197     }
198
199     bool has_cmd;
200     if (ctrl->cmd.i_size > 0) {
201         has_cmd = true;
202         *cmd = ARRAY_VAL(ctrl->cmd, 0);
203         ARRAY_REMOVE(ctrl->cmd, 0);
204
205         ctrl->is_processing = true;
206     } else {
207         has_cmd = false;
208         ctrl->can_sleep = true;
209     }
210     vlc_mutex_unlock(&ctrl->lock);
211
212     return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;
213 }
214