]> git.sesse.net Git - vlc/blob - src/video_output/control.c
9540ebd10d40e6014139a5ca5d65e505240ad281
[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_TITLE:
42     case VOUT_CONTROL_CHANGE_FILTERS:
43     case VOUT_CONTROL_CHANGE_SUB_FILTERS:
44         free(cmd->u.string);
45         break;
46     default:
47         break;
48     }
49 }
50
51 /* */
52 void vout_control_Init(vout_control_t *ctrl)
53 {
54     vlc_mutex_init(&ctrl->lock);
55     vlc_cond_init(&ctrl->wait_request);
56     vlc_cond_init(&ctrl->wait_acknowledge);
57
58     ctrl->is_dead = false;
59     ctrl->is_sleeping = false;
60     ctrl->can_sleep = true;
61     ctrl->is_processing = false;
62     ARRAY_INIT(ctrl->cmd);
63 }
64
65 void vout_control_Clean(vout_control_t *ctrl)
66 {
67     /* */
68     for (int i = 0; i < ctrl->cmd.i_size; i++) {
69         vout_control_cmd_t cmd = ARRAY_VAL(ctrl->cmd, i);
70         vout_control_cmd_Clean(&cmd);
71     }
72     ARRAY_RESET(ctrl->cmd);
73
74     vlc_mutex_destroy(&ctrl->lock);
75     vlc_cond_destroy(&ctrl->wait_request);
76     vlc_cond_destroy(&ctrl->wait_acknowledge);
77 }
78
79 void vout_control_Dead(vout_control_t *ctrl)
80 {
81     vlc_mutex_lock(&ctrl->lock);
82     ctrl->is_dead = true;
83     vlc_cond_broadcast(&ctrl->wait_acknowledge);
84     vlc_mutex_unlock(&ctrl->lock);
85
86 }
87
88 void vout_control_WaitEmpty(vout_control_t *ctrl)
89 {
90     vlc_mutex_lock(&ctrl->lock);
91     while ((ctrl->cmd.i_size > 0 || ctrl->is_processing) && !ctrl->is_dead)
92         vlc_cond_wait(&ctrl->wait_acknowledge, &ctrl->lock);
93     vlc_mutex_unlock(&ctrl->lock);
94 }
95
96 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
97 {
98     vlc_mutex_lock(&ctrl->lock);
99     if (!ctrl->is_dead) {
100         ARRAY_APPEND(ctrl->cmd, *cmd);
101         vlc_cond_signal(&ctrl->wait_request);
102     } else {
103         vout_control_cmd_Clean(cmd);
104     }
105     vlc_mutex_unlock(&ctrl->lock);
106 }
107
108 void vout_control_Wake(vout_control_t *ctrl)
109 {
110     vlc_mutex_lock(&ctrl->lock);
111     ctrl->can_sleep = false;
112     if (ctrl->is_sleeping)
113         vlc_cond_signal(&ctrl->wait_request);
114     vlc_mutex_unlock(&ctrl->lock);
115 }
116
117 void vout_control_PushVoid(vout_control_t *ctrl, int type)
118 {
119     vout_control_cmd_t cmd;
120
121     vout_control_cmd_Init(&cmd, type);
122     vout_control_Push(ctrl, &cmd);
123 }
124 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
125 {
126     vout_control_cmd_t cmd;
127
128     vout_control_cmd_Init(&cmd, type);
129     cmd.u.boolean = boolean;
130     vout_control_Push(ctrl, &cmd);
131 }
132 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
133 {
134     vout_control_cmd_t cmd;
135
136     vout_control_cmd_Init(&cmd, type);
137     cmd.u.time = time;
138     vout_control_Push(ctrl, &cmd);
139 }
140 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
141 {
142     vout_control_cmd_t cmd;
143
144     vout_control_cmd_Init(&cmd, type);
145     cmd.u.message.channel = channel;
146     cmd.u.message.string = strdup(string);
147     vout_control_Push(ctrl, &cmd);
148 }
149 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
150 {
151     vout_control_cmd_t cmd;
152
153     vout_control_cmd_Init(&cmd, type);
154     cmd.u.pair.a = a;
155     cmd.u.pair.b = b;
156     vout_control_Push(ctrl, &cmd);
157 }
158 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
159 {
160     vout_control_cmd_t cmd;
161
162     vout_control_cmd_Init(&cmd, type);
163     cmd.u.string = strdup(string);
164     vout_control_Push(ctrl, &cmd);
165 }
166
167 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
168                      mtime_t deadline, mtime_t timeout)
169 {
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);
174
175         const mtime_t max_deadline = mdate() + timeout;
176
177         /* Supurious wake up are perfectly fine */
178         if (deadline <= VLC_TS_INVALID) {
179             ctrl->is_sleeping = true;
180             if (ctrl->can_sleep)
181                 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, max_deadline);
182             ctrl->is_sleeping = false;
183         } else {
184             vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, __MIN(deadline, max_deadline));
185         }
186     }
187
188     bool has_cmd;
189     if (ctrl->cmd.i_size > 0) {
190         has_cmd = true;
191         *cmd = ARRAY_VAL(ctrl->cmd, 0);
192         ARRAY_REMOVE(ctrl->cmd, 0);
193
194         ctrl->is_processing = true;
195     } else {
196         has_cmd = false;
197         ctrl->can_sleep = true;
198     }
199     vlc_mutex_unlock(&ctrl->lock);
200
201     return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;
202 }
203