]> git.sesse.net Git - vlc/blob - src/video_output/osd_text.c
osd: privatize osd_ShowText(Absolute|Relative)
[vlc] / src / video_output / osd_text.c
1 /*****************************************************************************
2  * osd_text.c : text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Author: Sigmund Augdal Helberg <dnumgis@videolan.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 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_vout.h>
29 #include <vlc_block.h>
30 #include <vlc_filter.h>
31 #include <vlc_osd.h>
32
33 /**
34  * \brief Show text on the video from a given start date to a given end date
35  * \param p_spu pointer to the subpicture queue the text is to be showed on
36  * \param i_channel Subpicture channel
37  * \param psz_string The text to be shown
38  * \param p_style Pointer to a struct with text style info (it is duplicated)
39  * \param i_flags flags for alignment and such
40  * \param i_hmargin horizontal margin in pixels
41  * \param i_vmargin vertical margin in pixels
42  * \param i_start the time when this string is to appear on the video
43  * \param i_stop the time when this string should stop to be displayed
44  *               if this is 0 the string will be shown untill the next string
45  *               is about to be shown
46  */
47 static
48 int osd_ShowTextAbsolute( spu_t *p_spu_channel, int i_channel,
49                            const char *psz_string, const text_style_t *p_style,
50                            int i_flags, int i_hmargin, int i_vmargin,
51                            mtime_t i_start, mtime_t i_stop )
52 {
53     subpicture_t *p_spu;
54     video_format_t fmt;
55     (void)p_style;
56
57     if( !psz_string ) return VLC_EGENERIC;
58
59     p_spu = subpicture_New( NULL );
60     if( !p_spu )
61         return VLC_EGENERIC;
62
63     p_spu->i_channel = i_channel;
64     p_spu->i_start = i_start;
65     p_spu->i_stop = i_stop;
66     p_spu->b_ephemer = true;
67     p_spu->b_absolute = false;
68
69     /* Create a new subpicture region */
70     memset( &fmt, 0, sizeof(video_format_t) );
71     fmt.i_chroma = VLC_CODEC_TEXT;
72     fmt.i_width = fmt.i_height = 0;
73     fmt.i_x_offset = fmt.i_y_offset = 0;
74     p_spu->p_region = subpicture_region_New( &fmt );
75     if( !p_spu->p_region )
76     {
77         msg_Err( p_spu_channel, "cannot allocate SPU region" );
78         subpicture_Delete( p_spu );
79         return VLC_EGENERIC;
80     }
81
82     p_spu->p_region->psz_text = strdup( psz_string );
83     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
84     p_spu->p_region->i_x = i_hmargin;
85     p_spu->p_region->i_y = i_vmargin;
86
87     spu_PutSubpicture( p_spu_channel, p_spu );
88
89     return VLC_SUCCESS;
90 }
91
92 /**
93  * \brief Show text on the video for some time
94  * \param p_spu pointer to the subpicture queue the text is to be showed on
95  * \param i_channel Subpicture channel
96  * \param psz_string The text to be shown
97  * \param p_style Pointer to a struct with text style info (it is duplicated)
98  * \param i_flags flags for alignment and such
99  * \param i_hmargin horizontal margin in pixels
100  * \param i_vmargin vertical margin in pixels
101  * \param i_duration Amount of time the text is to be shown.
102  */
103 static
104 int osd_ShowTextRelative( spu_t *p_spu, int i_channel,
105                            const char *psz_string, const text_style_t *p_style,
106                            int i_flags, int i_hmargin, int i_vmargin,
107                            mtime_t i_duration )
108 {
109     mtime_t i_now = mdate();
110
111     return osd_ShowTextAbsolute( p_spu, i_channel, psz_string,
112                                   p_style, i_flags, i_hmargin, i_vmargin,
113                                   i_now, i_now + i_duration );
114 }
115
116 /**
117  * \brief Write an informative message at the default location,
118  *        for the default duration and only if the OSD option is enabled.
119  * \param p_caller The object that called the function.
120  * \param i_channel Subpicture channel
121  * \param psz_format printf style formatting
122  **/
123 void osd_Message( spu_t *p_spu, int i_channel,
124                         char *psz_format, ... )
125 {
126     va_list args;
127
128     if( p_spu )
129     {
130         char *psz_string;
131         va_start( args, psz_format );
132         if( vasprintf( &psz_string, psz_format, args ) != -1 )
133         {
134             osd_ShowTextRelative( p_spu, i_channel, psz_string, NULL,
135                     SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_RIGHT, 30,20,1000000 );
136
137             free( psz_string );
138         }
139         va_end( args );
140     }
141 }