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