]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_audio_video.c
mediacontrol_audio_video.c: release vout in mediacontrol_snapshot()
[vlc] / src / control / mediacontrol_audio_video.c
1 /*****************************************************************************
2  * audio_video.c: Audio/Video management : volume, snapshot, OSD
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <mediacontrol_internal.h>
25
26 #include <vlc/mediacontrol.h>
27
28 #include <vlc/intf.h>
29 #include <vlc/vout.h>
30 #include <vlc/aout.h>
31 #include <vlc_demux.h>
32
33 #include <vlc_osd.h>
34
35 #include <snapshot.h>
36
37 #include <stdlib.h>                                      /* malloc(), free() */
38 #include <string.h>
39
40 #include <errno.h>                                                 /* ENOMEM */
41 #include <stdio.h>
42 #include <ctype.h>
43
44 #ifdef HAVE_UNISTD_H
45 #    include <unistd.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 #    include <sys/time.h>
49 #endif
50 #ifdef HAVE_SYS_TYPES_H
51 #    include <sys/types.h>
52 #endif
53
54 #define RAISE( c, m )  exception->code = c; \
55                        exception->message = strdup(m);
56
57 mediacontrol_RGBPicture *
58 mediacontrol_snapshot( mediacontrol_Instance *self,
59                        const mediacontrol_Position * a_position,
60                        mediacontrol_Exception *exception )
61 {
62     vlc_object_t* p_cache;
63     vout_thread_t* p_vout;
64     mediacontrol_RGBPicture *p_pic = NULL;
65     char path[256];
66     snapshot_t *p_snapshot;
67
68     exception=mediacontrol_exception_init( exception );
69
70     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
71     if( ! p_vout )
72     {
73         RAISE( mediacontrol_InternalException, "No video output" );
74         return NULL;
75     }
76     p_cache = vlc_object_create( self->p_playlist, VLC_OBJECT_GENERIC );
77     if( p_cache == NULL )
78     {
79         vlc_object_release( p_vout );
80         msg_Err( self->p_playlist, "out of memory" );
81         RAISE( mediacontrol_InternalException, "Out of memory" );
82         return NULL;
83     }
84     snprintf( path, 255, "object:%d", p_cache->i_object_id );
85     var_SetString( p_vout, "snapshot-path", path );
86     var_SetString( p_vout, "snapshot-format", "png" );
87
88     vlc_mutex_lock( &p_cache->object_lock );
89     vout_Control( p_vout, VOUT_SNAPSHOT );
90     vlc_cond_wait( &p_cache->object_wait, &p_cache->object_lock );
91     vlc_object_release( p_vout );
92
93     p_snapshot = ( snapshot_t* ) p_cache->p_private;
94     vlc_object_destroy( p_cache );
95
96     if( p_snapshot )
97     {
98         p_pic = _mediacontrol_createRGBPicture( p_snapshot->i_width,
99                                                 p_snapshot->i_height,
100                                                 VLC_FOURCC( 'p','n','g',' ' ),
101                                                 p_snapshot->date,
102                                                 p_snapshot->p_data,
103                                                 p_snapshot->i_datasize );
104         if( !p_pic )
105             RAISE( mediacontrol_InternalException, "Out of memory" );
106         free( p_snapshot->p_data );
107         free( p_snapshot );
108     }
109     else
110     {
111         RAISE( mediacontrol_InternalException, "Snapshot exception" );
112     }
113     return p_pic;
114 }
115
116 mediacontrol_RGBPicture **
117 mediacontrol_all_snapshots( mediacontrol_Instance *self,
118                             mediacontrol_Exception *exception )
119 {
120     exception=mediacontrol_exception_init( exception );
121
122     RAISE( mediacontrol_InternalException, "Unsupported method" );
123     return NULL;
124 }
125
126 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
127                            char *psz_string, text_style_t *p_style,
128                            int i_flags, int i_hmargin, int i_vmargin,
129                            mtime_t i_start, mtime_t i_stop )
130 {
131     subpicture_t *p_spu;
132     video_format_t fmt;
133
134     if( !psz_string ) return VLC_EGENERIC;
135
136     p_spu = spu_CreateSubpicture( p_vout->p_spu );
137     if( !p_spu ) return VLC_EGENERIC;
138
139     /* Create a new subpicture region */
140     memset( &fmt, 0, sizeof(video_format_t) );
141     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
142     fmt.i_aspect = 0;
143     fmt.i_width = fmt.i_height = 0;
144     fmt.i_x_offset = fmt.i_y_offset = 0;
145     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
146     if( !p_spu->p_region )
147     {
148         msg_Err( p_vout, "cannot allocate SPU region" );
149         spu_DestroySubpicture( p_vout->p_spu, p_spu );
150         return VLC_EGENERIC;
151     }
152
153     p_spu->p_region->psz_text = strdup( psz_string );
154     p_spu->i_start = i_start;
155     p_spu->i_stop = i_stop;
156     p_spu->b_ephemer = VLC_FALSE;
157     p_spu->b_absolute = VLC_FALSE;
158
159     p_spu->i_x = i_hmargin;
160     p_spu->i_y = i_vmargin;
161     p_spu->i_flags = i_flags;
162     p_spu->i_channel = i_channel;
163
164     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
165
166     return VLC_SUCCESS;
167 }
168
169
170 void
171 mediacontrol_display_text( mediacontrol_Instance *self,
172                            const char * message,
173                            const mediacontrol_Position * begin,
174                            const mediacontrol_Position * end,
175                            mediacontrol_Exception *exception )
176 {
177     input_thread_t *p_input = NULL;
178     vout_thread_t *p_vout = NULL;
179     char* psz_message;
180
181     psz_message = strdup( message );
182     if( !psz_message )
183     {
184         RAISE( mediacontrol_InternalException, "No more memory" );
185         return;
186     }
187
188     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
189     if( ! p_vout )
190     {
191         RAISE( mediacontrol_InternalException, "No video output" );
192         return;
193     }
194
195     if( begin->origin == mediacontrol_RelativePosition &&
196         begin->value == 0 &&
197         end->origin == mediacontrol_RelativePosition )
198     {
199         mtime_t i_duration = 0;
200         mtime_t i_now = mdate();
201
202         i_duration = 1000 * mediacontrol_unit_convert( self->p_playlist->p_input,
203                                                        end->key,
204                                                        mediacontrol_MediaTime,
205                                                        end->value );
206
207         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
208                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
209                                i_now, i_now + i_duration );
210     }
211     else
212     {
213         mtime_t i_debut, i_fin, i_now;
214
215         p_input = self->p_playlist->p_input;
216         if( ! p_input )
217         {
218             RAISE( mediacontrol_InternalException, "No input" );
219             vlc_object_release( p_vout );
220             return;
221         }
222
223         /* FIXME */
224         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
225         i_now = mdate();
226
227         i_debut = mediacontrol_position2microsecond( p_input,
228                                                      ( mediacontrol_Position* ) begin );
229         i_debut += i_now;
230
231         i_fin = mediacontrol_position2microsecond( p_input,
232                                                    ( mediacontrol_Position * ) end );
233         i_fin += i_now;
234
235         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
236                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
237                                i_debut, i_fin );
238     }
239
240     vlc_object_release( p_vout );
241 }
242
243 unsigned short
244 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
245                                mediacontrol_Exception *exception )
246 {
247     short retval;
248     audio_volume_t i_volume;
249
250     if( !self->p_intf )
251     {
252         RAISE( mediacontrol_InternalException, "No interface module" );
253         return 0;
254     }
255     aout_VolumeGet( self->p_intf, &i_volume );
256     retval = i_volume;
257     return retval;
258 }
259
260 void
261 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
262                                const unsigned short volume,
263                                mediacontrol_Exception *exception )
264 {
265     if( !self->p_intf )
266     {
267         RAISE( mediacontrol_InternalException, "No interface module" );
268     }
269     else aout_VolumeSet( self->p_intf,( audio_volume_t )volume );
270 }
271
272 vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self,
273                                     WINDOWHANDLE visual_id,
274                                     mediacontrol_Exception *exception )
275 {
276     vlc_value_t value;
277     int ret;
278
279     if( !self->p_vlc )
280     {
281         RAISE( mediacontrol_InternalException, "No vlc reference" );
282         return VLC_FALSE;
283     }
284     value.i_int=visual_id;
285     ret = var_Set(self->p_vlc, "drawable", value);
286
287     return (ret == VLC_SUCCESS);
288 }