]> git.sesse.net Git - vlc/blob - src/spu_decoder/spu_decoder.c
. le d�codeur de sous-titres s'appelle maintenant spu_decoder
[vlc] / src / spu_decoder / spu_decoder.c
1 /*******************************************************************************
2  * spu_decoder.c : spu decoder thread
3  * (c)2000 VideoLAN
4  *******************************************************************************/
5
6 /* repompé sur video_decoder.c
7  * ?? passer en terminate/destroy avec les signaux supplémentaires */
8
9 /*******************************************************************************
10  * Preamble
11  *******************************************************************************/
12 //#include "vlc.h"
13
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/uio.h>
20
21 #include "config.h"
22 #include "common.h"
23 #include "mtime.h"
24 #include "vlc_thread.h"
25
26 #include "intf_msg.h"
27 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
28
29 #include "input.h"
30 #include "input_netlist.h"
31 #include "decoder_fifo.h"
32
33 #include "spu_decoder.h"
34
35 /*
36  * Local prototypes
37  */
38 static int      InitThread          ( spudec_thread_t *p_spudec );
39 static void     RunThread           ( spudec_thread_t *p_spudec );
40 static void     ErrorThread         ( spudec_thread_t *p_spudec );
41 static void     EndThread           ( spudec_thread_t *p_spudec );
42
43 /******************************************************************************
44  * spudec_CreateThread: create a spu decoder thread
45  ******************************************************************************/
46 spudec_thread_t * spudec_CreateThread( input_thread_t * p_input )
47 {
48     spudec_thread_t *     p_spudec;
49
50     intf_DbgMsg("spudec debug: creating spu decoder thread\n");
51     fprintf(stderr, "spudec debug: creating spu decoder thread\n");
52
53     /* Allocate the memory needed to store the thread's structure */
54     if ( (p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) )) == NULL )
55     {
56         intf_ErrMsg("spudec error: not enough memory for spudec_CreateThread() to create the new thread\n");
57         return( NULL );
58     }
59
60     /*
61      * Initialize the thread properties
62      */
63     p_spudec->b_die = 0;
64     p_spudec->b_error = 0;
65
66     /* Spawn the spu decoder thread */
67     if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
68          (vlc_thread_func_t)RunThread, (void *)p_spudec) )
69     {
70         intf_ErrMsg("spudec error: can't spawn spu decoder thread\n");
71         free( p_spudec );
72         return( NULL );
73     }
74
75     intf_DbgMsg("spudec debug: spu decoder thread (%p) created\n", p_spudec);
76     return( p_spudec );
77 }
78
79 /*******************************************************************************
80  * spudec_DestroyThread: destroy a spu decoder thread
81  *******************************************************************************
82  * Destroy and terminate thread. This function will return 0 if the thread could
83  * be destroyed, and non 0 else. The last case probably means that the thread
84  * was still active, and another try may succeed.
85  *******************************************************************************/
86 void spudec_DestroyThread( spudec_thread_t *p_spudec )
87 {
88     intf_DbgMsg("spudec debug: requesting termination of spu decoder thread %p\n", p_spudec);
89     fprintf(stderr, "spudec debug: requesting termination of spu decoder thread %p\n", p_spudec);
90
91     /* Ask thread to kill itself */
92     p_spudec->b_die = 1;
93
94     /* Waiting for the decoder thread to exit */
95     /* Remove this as soon as the "status" flag is implemented */
96     vlc_thread_join( p_spudec->thread_id );
97 }
98
99 /* following functions are local */
100
101 /*******************************************************************************
102  * InitThread: initialize spu decoder thread
103  *******************************************************************************
104  * This function is called from RunThread and performs the second step of the
105  * initialization. It returns 0 on success. Note that the thread's flag are not
106  * modified inside this function.
107  *******************************************************************************/
108 static int InitThread( spudec_thread_t *p_spudec )
109 {
110
111     intf_DbgMsg("spudec debug: initializing spu decoder thread %p\n", p_spudec);
112
113     /* Mark thread as running and return */
114     intf_DbgMsg("spudec debug: InitThread(%p) succeeded\n", p_spudec);    
115     return( 0 );    
116 }
117
118 /*******************************************************************************
119  * RunThread: spu decoder thread
120  *******************************************************************************
121  * spu decoder thread. This function does only return when the thread is
122  * terminated. 
123  *******************************************************************************/
124 static void RunThread( spudec_thread_t *p_spudec )
125 {
126     intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)\n",
127                 p_spudec, getpid());
128
129     /* 
130      * Initialize thread and free configuration 
131      */
132     p_spudec->b_error = InitThread( p_spudec );
133     if( p_spudec->b_error )
134     {
135         return;
136     }
137     p_spudec->b_run = 1;
138
139     /*
140      * Main loop - it is not executed if an error occured during
141      * initialization
142      */
143     while( (!p_spudec->b_die) && (!p_spudec->b_error) )
144     {
145         fprintf(stderr, "I'm a spu decoder !\n");
146         sleep(1);
147     } 
148
149     /*
150      * Error loop
151      */
152     if( p_spudec->b_error )
153     {
154         ErrorThread( p_spudec );        
155     }
156
157     /* End of thread */
158     EndThread( p_spudec );
159     p_spudec->b_run = 0;
160 }
161
162 /*******************************************************************************
163  * ErrorThread: RunThread() error loop
164  *******************************************************************************
165  * This function is called when an error occured during thread main's loop. The
166  * thread can still receive feed, but must be ready to terminate as soon as
167  * possible.
168  *******************************************************************************/
169 static void ErrorThread( spudec_thread_t *p_spudec )
170 {
171     /* Wait until a `die' order */
172     while( !p_spudec->b_die )
173     {
174         // foo();
175     }
176 }
177
178 /*******************************************************************************
179  * EndThread: thread destruction
180  *******************************************************************************
181  * This function is called when the thread ends after a sucessfull 
182  * initialization.
183  *******************************************************************************/
184 static void EndThread( spudec_thread_t *p_spudec )
185 {
186     intf_DbgMsg("spudec debug: EndThread(%p)\n", p_spudec);
187 }
188