Fix non-ASCII comment
[vlc/asuraparaju-public.git] / src / video_output / snapshot.c
blob0f2c93817574b013322e03f6b419fd33d4345590
1 /*****************************************************************************
2 * snapshot.c : vout internal snapshot
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
5 * $Id$
7 * Authors: Gildas Bazin <gbazin _AT_ videolan _DOT_ org>
8 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_fs.h>
31 #include <vlc_strings.h>
32 #include <vlc_block.h>
34 #include "snapshot.h"
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <dirent.h>
39 #include <time.h>
41 /* */
42 void vout_snapshot_Init(vout_snapshot_t *snap)
44 vlc_mutex_init(&snap->lock);
45 vlc_cond_init(&snap->wait);
47 snap->is_available = true;
48 snap->request_count = 0;
49 snap->picture = NULL;
51 void vout_snapshot_Clean(vout_snapshot_t *snap)
53 picture_t *picture = snap->picture;
54 while (picture) {
55 picture_t *next = picture->p_next;
56 picture_Release(picture);
57 picture = next;
60 vlc_cond_destroy(&snap->wait);
61 vlc_mutex_destroy(&snap->lock);
64 void vout_snapshot_End(vout_snapshot_t *snap)
66 vlc_mutex_lock(&snap->lock);
68 snap->is_available = false;
70 vlc_cond_broadcast(&snap->wait);
71 vlc_mutex_unlock(&snap->lock);
74 /* */
75 picture_t *vout_snapshot_Get(vout_snapshot_t *snap, mtime_t timeout)
77 vlc_mutex_lock(&snap->lock);
79 /* */
80 snap->request_count++;
82 /* */
83 const mtime_t deadline = mdate() + timeout;
84 while (snap->is_available && !snap->picture && mdate() < deadline)
85 vlc_cond_timedwait(&snap->wait, &snap->lock, deadline);
87 /* */
88 picture_t *picture = snap->picture;
89 if (picture)
90 snap->picture = picture->p_next;
91 else if (snap->request_count > 0)
92 snap->request_count--;
94 vlc_mutex_unlock(&snap->lock);
96 return picture;
99 /* */
100 bool vout_snapshot_IsRequested(vout_snapshot_t *snap)
102 bool has_request = false;
103 if (!vlc_mutex_trylock(&snap->lock)) {
104 has_request = snap->request_count > 0;
105 vlc_mutex_unlock(&snap->lock);
107 return has_request;
109 void vout_snapshot_Set(vout_snapshot_t *snap,
110 const video_format_t *fmt,
111 const picture_t *picture)
113 if (!fmt)
114 fmt = &picture->format;
116 vlc_mutex_lock(&snap->lock);
117 while (snap->request_count > 0) {
118 picture_t *dup = picture_NewFromFormat(fmt);
119 if (!dup)
120 break;
122 picture_Copy(dup, picture);
124 dup->p_next = snap->picture;
125 snap->picture = dup;
126 snap->request_count--;
128 vlc_cond_broadcast(&snap->wait);
129 vlc_mutex_unlock(&snap->lock);
131 /* */
132 char *vout_snapshot_GetDirectory(void)
134 return config_GetUserDir(VLC_PICTURES_DIR);
136 /* */
137 int vout_snapshot_SaveImage(char **name, int *sequential,
138 const block_t *image,
139 vlc_object_t *object,
140 const vout_snapshot_save_cfg_t *cfg)
142 /* */
143 char *filename;
144 DIR *pathdir = vlc_opendir(cfg->path);
145 if (pathdir != NULL) {
146 /* The use specified a directory path */
147 closedir(pathdir);
149 /* */
150 char *prefix = NULL;
151 if (cfg->prefix_fmt)
152 prefix = str_format(object, cfg->prefix_fmt);
153 if (prefix)
154 filename_sanitize(prefix);
155 else {
156 prefix = strdup("vlcsnap-");
157 if (!prefix)
158 goto error;
161 if (cfg->is_sequential) {
162 for (int num = cfg->sequence; ; num++) {
163 struct stat st;
165 if (asprintf(&filename, "%s" DIR_SEP "%s%05d.%s",
166 cfg->path, prefix, num, cfg->format) < 0) {
167 free(prefix);
168 goto error;
170 if (vlc_stat(filename, &st)) {
171 *sequential = num;
172 break;
174 free(filename);
176 } else {
177 struct tm curtime;
178 time_t lcurtime = time(NULL) ;
180 if (!localtime_r(&lcurtime, &curtime)) {
181 const unsigned int id = (image->i_pts / 100000) & 0xFFFFFF;
183 msg_Warn(object, "failed to get current time. Falling back to legacy snapshot naming");
185 if (asprintf(&filename, "%s" DIR_SEP "%s%u.%s",
186 cfg->path, prefix, id, cfg->format) < 0)
187 filename = NULL;
188 } else {
189 /* suffix with the last decimal digit in 10s of seconds resolution
190 * FIXME gni ? */
191 const int id = (image->i_pts / (100*1000)) & 0xFF;
192 char buffer[128];
194 if (!strftime(buffer, sizeof(buffer), "%Y-%m-%d-%Hh%Mm%Ss", &curtime))
195 strcpy(buffer, "error");
197 if (asprintf(&filename, "%s" DIR_SEP "%s%s%1u.%s",
198 cfg->path, prefix, buffer, id, cfg->format) < 0)
199 filename = NULL;
202 free(prefix);
203 } else {
204 /* The user specified a full path name (including file name) */
205 filename = str_format(object, cfg->path);
206 path_sanitize(filename);
209 if (!filename)
210 goto error;
212 /* Save the snapshot */
213 FILE *file = vlc_fopen(filename, "wb");
214 if (!file) {
215 msg_Err(object, "Failed to open '%s'", filename);
216 free(filename);
217 goto error;
219 if (fwrite(image->p_buffer, image->i_buffer, 1, file) != 1) {
220 msg_Err(object, "Failed to write to '%s'", filename);
221 fclose(file);
222 free(filename);
223 goto error;
225 fclose(file);
227 /* */
228 if (name)
229 *name = filename;
230 else
231 free(filename);
233 return VLC_SUCCESS;
235 error:
236 msg_Err(object, "could not save snapshot");
237 return VLC_EGENERIC;