1 /*****************************************************************************
2  * control.c : vout internal control
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id: e4cca29d2a6ade21f4536d3e82c99d133f5fb607 $
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 /* */
vout_control_cmd_Init(vout_control_cmd_t * cmd,int type)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 
vout_control_cmd_Clean(vout_control_cmd_t * cmd)39 void vout_control_cmd_Clean(vout_control_cmd_t *cmd)
40 {
41     switch (cmd->type) {
42     case VOUT_CONTROL_SUBPICTURE:
43         if (cmd->u.subpicture)
44             subpicture_Delete(cmd->u.subpicture);
45         break;
46     case VOUT_CONTROL_OSD_TITLE:
47     case VOUT_CONTROL_CHANGE_FILTERS:
48     case VOUT_CONTROL_CHANGE_SUB_SOURCES:
49     case VOUT_CONTROL_CHANGE_SUB_FILTERS:
50         free(cmd->u.string);
51         break;
52     default:
53         break;
54     }
55 }
56 
57 /* */
vout_control_Init(vout_control_t * ctrl)58 void vout_control_Init(vout_control_t *ctrl)
59 {
60     vlc_mutex_init(&ctrl->lock);
61     vlc_cond_init(&ctrl->wait_request);
62     vlc_cond_init(&ctrl->wait_acknowledge);
63 
64     ctrl->is_dead = false;
65     ctrl->can_sleep = true;
66     ctrl->is_processing = false;
67     ARRAY_INIT(ctrl->cmd);
68 }
69 
vout_control_Clean(vout_control_t * ctrl)70 void vout_control_Clean(vout_control_t *ctrl)
71 {
72     /* */
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);
76     }
77     ARRAY_RESET(ctrl->cmd);
78 
79     vlc_mutex_destroy(&ctrl->lock);
80     vlc_cond_destroy(&ctrl->wait_request);
81     vlc_cond_destroy(&ctrl->wait_acknowledge);
82 }
83 
vout_control_Dead(vout_control_t * ctrl)84 void vout_control_Dead(vout_control_t *ctrl)
85 {
86     vlc_mutex_lock(&ctrl->lock);
87     ctrl->is_dead = true;
88     vlc_cond_broadcast(&ctrl->wait_acknowledge);
89     vlc_mutex_unlock(&ctrl->lock);
90 
91 }
92 
vout_control_WaitEmpty(vout_control_t * ctrl)93 void vout_control_WaitEmpty(vout_control_t *ctrl)
94 {
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);
99 }
100 
vout_control_Push(vout_control_t * ctrl,vout_control_cmd_t * cmd)101 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
102 {
103     vlc_mutex_lock(&ctrl->lock);
104     if (!ctrl->is_dead) {
105         ARRAY_APPEND(ctrl->cmd, *cmd);
106         vlc_cond_signal(&ctrl->wait_request);
107     } else {
108         vout_control_cmd_Clean(cmd);
109     }
110     vlc_mutex_unlock(&ctrl->lock);
111 }
112 
vout_control_Wake(vout_control_t * ctrl)113 void vout_control_Wake(vout_control_t *ctrl)
114 {
115     vlc_mutex_lock(&ctrl->lock);
116     ctrl->can_sleep = false;
117     vlc_cond_signal(&ctrl->wait_request);
118     vlc_mutex_unlock(&ctrl->lock);
119 }
120 
vout_control_PushVoid(vout_control_t * ctrl,int type)121 void vout_control_PushVoid(vout_control_t *ctrl, int type)
122 {
123     vout_control_cmd_t cmd;
124 
125     vout_control_cmd_Init(&cmd, type);
126     vout_control_Push(ctrl, &cmd);
127 }
vout_control_PushBool(vout_control_t * ctrl,int type,bool boolean)128 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
129 {
130     vout_control_cmd_t cmd;
131 
132     vout_control_cmd_Init(&cmd, type);
133     cmd.u.boolean = boolean;
134     vout_control_Push(ctrl, &cmd);
135 }
vout_control_PushInteger(vout_control_t * ctrl,int type,int integer)136 void vout_control_PushInteger(vout_control_t *ctrl, int type, int integer)
137 {
138     vout_control_cmd_t cmd;
139 
140     vout_control_cmd_Init(&cmd, type);
141     cmd.u.integer = integer;
142     vout_control_Push(ctrl, &cmd);
143 }
vout_control_PushTime(vout_control_t * ctrl,int type,mtime_t time)144 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
145 {
146     vout_control_cmd_t cmd;
147 
148     vout_control_cmd_Init(&cmd, type);
149     cmd.u.time = time;
150     vout_control_Push(ctrl, &cmd);
151 }
vout_control_PushMessage(vout_control_t * ctrl,int type,int channel,const char * string)152 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
153 {
154     vout_control_cmd_t cmd;
155 
156     vout_control_cmd_Init(&cmd, type);
157     cmd.u.message.channel = channel;
158     cmd.u.message.string = strdup(string);
159     vout_control_Push(ctrl, &cmd);
160 }
vout_control_PushPair(vout_control_t * ctrl,int type,int a,int b)161 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
162 {
163     vout_control_cmd_t cmd;
164 
165     vout_control_cmd_Init(&cmd, type);
166     cmd.u.pair.a = a;
167     cmd.u.pair.b = b;
168     vout_control_Push(ctrl, &cmd);
169 }
vout_control_PushString(vout_control_t * ctrl,int type,const char * string)170 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
171 {
172     vout_control_cmd_t cmd;
173 
174     vout_control_cmd_Init(&cmd, type);
175     cmd.u.string = string ? strdup(string) : NULL;
176     vout_control_Push(ctrl, &cmd);
177 }
178 
vout_control_Pop(vout_control_t * ctrl,vout_control_cmd_t * cmd,mtime_t deadline)179 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
180                      mtime_t deadline)
181 {
182     vlc_mutex_lock(&ctrl->lock);
183     if (ctrl->cmd.i_size <= 0) {
184         ctrl->is_processing = false;
185         vlc_cond_broadcast(&ctrl->wait_acknowledge);
186 
187         /* Spurious wakeups are perfectly fine */
188         if (deadline > VLC_TS_INVALID && ctrl->can_sleep)
189             vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, deadline);
190     }
191 
192     bool has_cmd;
193     if (ctrl->cmd.i_size > 0) {
194         has_cmd = true;
195         *cmd = ARRAY_VAL(ctrl->cmd, 0);
196         ARRAY_REMOVE(ctrl->cmd, 0);
197 
198         ctrl->is_processing = true;
199     } else {
200         has_cmd = false;
201         ctrl->can_sleep = true;
202     }
203     vlc_mutex_unlock(&ctrl->lock);
204 
205     return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;
206 }
207 
208