1 /*****************************************************************************
2 * video_epg.c : EPG manipulation functions
3 *****************************************************************************
4 * Copyright (C) 2010 Adrien Maglo
6 * Author: Adrien Maglo <magsoft@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_block.h>
31 #include <vlc_filter.h>
33 #include <vlc_events.h>
34 #include <vlc_input_item.h>
37 /* Layout percentage defines */
40 #define EPG_NAME_SIZE 0.05
41 #define EPG_PROGRAM_SIZE 0.03
43 static subpicture_region_t * vout_OSDEpgSlider( int i_x, int i_y,
44 int i_width, int i_height,
48 subpicture_region_t *p_region;
50 /* Create a new subpicture region */
51 video_format_Init( &fmt, VLC_CODEC_YUVA );
52 fmt.i_width = fmt.i_visible_width = i_width;
53 fmt.i_height = fmt.i_visible_height = i_height;
57 p_region = subpicture_region_New( &fmt );
64 picture_t *p_picture = p_region->p_picture;
66 f_ratio = __MIN( __MAX( f_ratio, 0 ), 1 );
67 int i_filled_part_width = f_ratio * i_width;
69 for( int j = 0; j < i_height; j++ )
71 for( int i = 0; i < i_width; i++ )
73 #define WRITE_COMP( plane, value ) \
74 p_picture->p[plane].p_pixels[p_picture->p[plane].i_pitch * j + i] = value
76 /* Draw the slider. */
77 bool is_outline = j == 0 || j == i_height - 1
78 || i == 0 || i == i_width - 1;
79 WRITE_COMP( 0, is_outline ? 0x00 : 0xff );
80 WRITE_COMP( 1, 0x80 );
81 WRITE_COMP( 2, 0x80 );
83 /* We can see the video through the part of the slider
84 which corresponds to the leaving time. */
85 bool is_border = j < 3 || j > i_height - 4
86 || i < 3 || i > i_width - 4
87 || i < i_filled_part_width;
88 WRITE_COMP( 3, is_border ? 0xff : 0x00 );
98 static subpicture_region_t * vout_OSDEpgText( const char *psz_string,
100 int i_size, uint32_t i_color )
103 subpicture_region_t *p_region;
108 /* Create a new subpicture region */
109 video_format_Init( &fmt, VLC_CODEC_TEXT );
113 p_region = subpicture_region_New( &fmt );
117 /* Set subpicture parameters */
118 p_region->psz_text = strdup( psz_string );
119 p_region->i_align = 0;
124 p_region->p_style = text_style_New();
125 if( p_region->p_style )
127 p_region->p_style->i_font_size = i_size;
128 p_region->p_style->i_font_color = i_color;
129 p_region->p_style->i_font_alpha = 0;
136 static subpicture_region_t * vout_BuildOSDEpg( vlc_epg_t *p_epg,
139 int i_visible_height )
141 subpicture_region_t *p_region_ret;
142 subpicture_region_t **pp_region = &p_region_ret;
144 time_t i_test = time( NULL );
146 /* Display the name of the channel. */
147 *pp_region = vout_OSDEpgText( p_epg->psz_name,
148 i_x + i_visible_width * EPG_LEFT,
149 i_y + i_visible_height * EPG_TOP,
150 i_visible_height * EPG_NAME_SIZE,
156 /* Display the name of the current program. */
157 pp_region = &(* pp_region)->p_next;
158 *pp_region = vout_OSDEpgText( p_epg->p_current->psz_name,
159 i_x + i_visible_width * ( EPG_LEFT + 0.025 ),
160 i_y + i_visible_height * ( EPG_TOP + 0.05 ),
161 i_visible_height * EPG_PROGRAM_SIZE,
167 /* Display the current program time slider. */
168 pp_region = &(* pp_region)->p_next;
169 *pp_region = vout_OSDEpgSlider( i_x + i_visible_width * EPG_LEFT,
170 i_y + i_visible_height * ( EPG_TOP + 0.1 ),
171 i_visible_width * ( 1 - 2 * EPG_LEFT ),
172 i_visible_height * 0.05,
173 ( i_test - p_epg->p_current->i_start )
174 / (float)p_epg->p_current->i_duration );
179 /* Format the hours of the beginning and the end of the current program. */
180 struct tm tm_start, tm_end;
181 time_t t_start = p_epg->p_current->i_start;
182 time_t t_end = p_epg->p_current->i_start + p_epg->p_current->i_duration;
183 localtime_r( &t_start, &tm_start );
184 localtime_r( &t_end, &tm_end );
187 snprintf( psz_start, sizeof(psz_start), "%2.2d:%2.2d",
188 tm_start.tm_hour, tm_start.tm_min );
189 snprintf( psz_end, sizeof(psz_end), "%2.2d:%2.2d",
190 tm_end.tm_hour, tm_end.tm_min );
192 /* Display those hours. */
193 pp_region = &(* pp_region)->p_next;
194 *pp_region = vout_OSDEpgText( psz_start,
195 i_x + i_visible_width * ( EPG_LEFT + 0.02 ),
196 i_y + i_visible_height * ( EPG_TOP + 0.15 ),
197 i_visible_height * EPG_PROGRAM_SIZE,
203 pp_region = &(* pp_region)->p_next;
204 *pp_region = vout_OSDEpgText( psz_end,
205 i_x + i_visible_width * ( 1 - EPG_LEFT - 0.085 ),
206 i_y + i_visible_height * ( EPG_TOP + 0.15 ),
207 i_visible_height * EPG_PROGRAM_SIZE,
213 struct subpicture_updater_sys_t
218 static int OSDEpgValidate( subpicture_t *p_subpic,
219 bool has_src_changed, const video_format_t *p_fmt_src,
220 bool has_dst_changed, const video_format_t *p_fmt_dst,
223 VLC_UNUSED(p_subpic); VLC_UNUSED(i_ts); VLC_UNUSED(p_fmt_src);
224 VLC_UNUSED(has_dst_changed); VLC_UNUSED(p_fmt_dst);
226 if( !has_src_changed && !has_dst_changed)
231 static void OSDEpgUpdate( subpicture_t *p_subpic,
232 const video_format_t *p_fmt_src,
233 const video_format_t *p_fmt_dst,
236 subpicture_updater_sys_t *p_sys = p_subpic->updater.p_sys;
237 VLC_UNUSED(p_fmt_dst); VLC_UNUSED(i_ts);
239 p_subpic->i_original_picture_width = p_fmt_src->i_width;
240 p_subpic->i_original_picture_height = p_fmt_src->i_height;
241 p_subpic->p_region = vout_BuildOSDEpg( p_sys->p_epg,
242 p_fmt_src->i_x_offset,
243 p_fmt_src->i_y_offset,
244 p_fmt_src->i_visible_width,
245 p_fmt_src->i_visible_height );
248 static void OSDEpgDestroy( subpicture_t *p_subpic )
250 subpicture_updater_sys_t *p_sys = p_subpic->updater.p_sys;
252 vlc_epg_Delete( p_sys->p_epg );
257 * \brief Show EPG information about the current program of an input item
258 * \param p_vout pointer to the vout the information is to be showed on
259 * \param p_input pointer to the input item the information is to be showed
261 int vout_OSDEpg( vout_thread_t *p_vout, input_item_t *p_input )
264 mtime_t i_now = mdate();
266 char *psz_now_playing = input_item_GetNowPlaying( p_input );
267 vlc_epg_t *p_epg = NULL;
269 vlc_mutex_lock( &p_input->lock );
271 /* Look for the current program EPG event */
272 for( int i = 0; i < p_input->i_epg; i++ )
274 vlc_epg_t *p_tmp = p_input->pp_epg[i];
276 if( p_tmp->p_current && p_tmp->p_current->psz_name
277 && psz_now_playing != NULL
278 && !strcmp( p_tmp->p_current->psz_name, psz_now_playing ) )
280 p_epg = vlc_epg_New( p_tmp->psz_name );
281 vlc_epg_Merge( p_epg, p_tmp );
286 vlc_mutex_unlock( &p_input->lock );
288 /* If no EPG event has been found. */
292 subpicture_updater_sys_t *p_sys = malloc( sizeof( *p_sys ) );
295 vlc_epg_Delete( p_epg );
298 p_sys->p_epg = p_epg;
299 subpicture_updater_t updater = {
300 .pf_validate = OSDEpgValidate,
301 .pf_update = OSDEpgUpdate,
302 .pf_destroy = OSDEpgDestroy,
306 p_spu = subpicture_New( &updater );
309 vlc_epg_Delete( p_sys->p_epg );
314 p_spu->i_channel = SPU_DEFAULT_CHANNEL;
315 p_spu->i_start = i_now;
316 p_spu->i_stop = i_now + 3000 * INT64_C(1000);
317 p_spu->b_ephemer = true;
318 p_spu->b_absolute = true;
319 p_spu->b_fade = true;
321 spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_spu );