]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : OSD text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999-2010 the VideoLAN team
5  * $Id$
6  *
7  * Author: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *         Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_vout.h>
32 #include <vlc_vout_osd.h>
33
34 struct subpicture_updater_sys_t {
35     int  position;
36     char *text;
37 };
38
39 static int OSDTextValidate(subpicture_t *subpic,
40                            bool has_src_changed, const video_format_t *fmt_src,
41                            bool has_dst_changed, const video_format_t *fmt_dst,
42                            mtime_t ts)
43 {
44     VLC_UNUSED(subpic); VLC_UNUSED(ts); VLC_UNUSED(fmt_src);
45     VLC_UNUSED(has_dst_changed); VLC_UNUSED(fmt_dst);
46
47     if( !has_src_changed && !has_dst_changed)
48         return VLC_SUCCESS;
49     return VLC_EGENERIC;
50 }
51
52 static void OSDTextUpdate(subpicture_t *subpic,
53                           const video_format_t *fmt_src,
54                           const video_format_t *fmt_dst,
55                           mtime_t ts)
56 {
57     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
58     VLC_UNUSED(fmt_dst); VLC_UNUSED(ts);
59
60     subpic->i_original_picture_width  = fmt_src->i_width;
61     subpic->i_original_picture_height = fmt_src->i_height;
62
63     video_format_t fmt;
64     video_format_Init( &fmt, VLC_CODEC_TEXT);
65     fmt.i_sar_num = 0;
66     fmt.i_sar_den = 1;
67
68     subpicture_region_t *r = subpic->p_region = subpicture_region_New(&fmt);
69     if (!r)
70         return;
71
72     r->psz_text = strdup(sys->text);
73     r->i_align  = sys->position;
74     r->i_x      = 30 + fmt_src->i_width
75                      - fmt_src->i_visible_width
76                      - fmt_src->i_x_offset;
77     r->i_y      = 20 + fmt_src->i_y_offset;
78 }
79
80 static void OSDTextDestroy(subpicture_t *subpic)
81 {
82     subpicture_updater_sys_t *sys = subpic->updater.p_sys;
83
84     free(sys->text);
85     free(sys);
86 }
87
88 void vout_OSDText(vout_thread_t *vout, int channel,
89                    int position, mtime_t duration, const char *text)
90 {
91     assert( (position & ~SUBPICTURE_ALIGN_MASK) == 0);
92     if (!var_InheritBool(vout, "osd") || duration <= 0)
93         return;
94
95     subpicture_updater_sys_t *sys = malloc(sizeof(*sys));
96     if (!sys)
97         return;
98     sys->position = position;
99     sys->text     = strdup(text);
100
101     subpicture_updater_t updater = {
102         .pf_validate = OSDTextValidate,
103         .pf_update   = OSDTextUpdate,
104         .pf_destroy  = OSDTextDestroy,
105         .p_sys       = sys,
106     };
107     subpicture_t *subpic = subpicture_New(&updater);
108     if (!subpic) {
109         free(sys->text);
110         free(sys);
111         return;
112     }
113
114     subpic->i_channel  = channel;
115     subpic->i_start    = mdate();
116     subpic->i_stop     = subpic->i_start + duration;
117     subpic->b_ephemer  = true;
118     subpic->b_absolute = false;
119     subpic->b_fade     = true;
120
121     vout_PutSubpicture(vout, subpic);
122 }
123
124 void vout_OSDMessage(vout_thread_t *vout, int channel, const char *format, ...)
125 {
126     va_list args;
127     va_start(args, format);
128
129     char *string;
130     if (vasprintf(&string, format, args) != -1) {
131         vout_OSDText(vout, channel,
132                      SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_RIGHT, 1000000,
133                      string);
134         free(string);
135     }
136     va_end(args);
137 }
138