source: titan/exteplayer3/manager/subtitle.c @ 39831

Last change on this file since 39831 was 39831, checked in by obi, 7 years ago

move extplayer3

File size: 9.2 KB
Line 
1/*
2 * subtitle manager handling.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20/* ***************************** */
21/* Includes                      */
22/* ***************************** */
23
24#include <stdlib.h>
25#include <string.h>
26
27#include "manager.h"
28#include "common.h"
29
30/* ***************************** */
31/* Makros/Constants              */
32/* ***************************** */
33#define TRACKWRAP 20
34
35#define SAM_WITH_DEBUG
36#ifdef SAM_WITH_DEBUG
37#define SUBTITLE_MGR_DEBUG
38#else
39#define SUBTITLE_MGR_SILENT
40#endif
41
42
43#ifdef SUBTITLE_MGR_DEBUG
44
45static short debug_level = 20;
46
47#define subtitle_mgr_printf(level, x...) do { \
48if (debug_level >= level) printf(x); } while (0)
49#else
50#define subtitle_mgr_printf(level, x...)
51#endif
52
53#ifndef SUBTITLE_MGR_SILENT
54#define subtitle_mgr_err(x...) do { printf(x); } while (0)
55#else
56#define subtitle_mgr_err(x...)
57#endif
58
59/* Error Constants */
60#define cERR_SUBTITLE_MGR_NO_ERROR        0
61#define cERR_SUBTITLE_MGR_ERROR          -1
62
63static const char FILENAME[] = __FILE__;
64
65/* ***************************** */
66/* Types                         */
67/* ***************************** */
68
69/* ***************************** */
70/* Varaibles                     */
71/* ***************************** */
72
73static Track_t * Tracks = NULL;
74static int TrackCount = 0;
75static int CurrentTrack = -1; //no as default.
76
77/* ***************************** */
78/* Prototypes                    */
79/* ***************************** */
80
81/* ***************************** */
82/* Functions                     */
83/* ***************************** */
84
85static int ManagerAdd(Context_t *context, Track_t track)
86{
87    uint32_t i = 0;
88    subtitle_mgr_printf(10, "%s::%s %s %s %d\n", FILENAME, __FUNCTION__, track.Name, track.Encoding, track.Id);
89
90    if (Tracks == NULL)
91    {
92        Tracks = malloc(sizeof(Track_t) * TRACKWRAP);
93        for (i = 0; i < TRACKWRAP; ++i)
94        {
95            Tracks[i].Id = -1;
96        }
97    }
98
99    if (Tracks == NULL)
100    {
101        subtitle_mgr_err("%s:%s malloc failed\n", FILENAME, __FUNCTION__);
102        return cERR_SUBTITLE_MGR_ERROR;
103    }
104
105   
106    for (i = 0; i < TRACKWRAP; ++i)
107    {
108        if (Tracks[i].Id == track.Id)
109        {
110            Tracks[i].pending = 0;
111            return cERR_SUBTITLE_MGR_NO_ERROR;
112        }
113    }
114
115    if (TrackCount < TRACKWRAP)
116    {
117        copyTrack(&Tracks[TrackCount], &track);
118        TrackCount++;
119    }
120    else
121    {
122        subtitle_mgr_err("%s:%s TrackCount out if range %d - %d\n", FILENAME, __FUNCTION__, TrackCount, TRACKWRAP);
123        return cERR_SUBTITLE_MGR_ERROR;
124    }
125
126    if (TrackCount > 0)
127    {
128        context->playback->isSubtitle = 1;
129    }
130
131    subtitle_mgr_printf(10, "%s::%s\n", FILENAME, __FUNCTION__);
132
133    return cERR_SUBTITLE_MGR_NO_ERROR;
134}
135
136static TrackDescription_t* ManagerList(Context_t  *context __attribute__((unused)))
137{
138    int i = 0;
139    TrackDescription_t *tracklist = NULL;
140
141    subtitle_mgr_printf(10, "%s::%s\n", FILENAME, __FUNCTION__);
142    if (Tracks != NULL)
143    {
144        tracklist = malloc(sizeof(TrackDescription_t) *((TrackCount) + 1));
145        if (tracklist == NULL)
146        {
147            subtitle_mgr_err("%s:%s malloc failed\n", FILENAME, __FUNCTION__);
148            return NULL;
149        }
150       
151        int j = 0;
152        for (i = 0; i < TrackCount; ++i)
153        {
154            if (Tracks[i].pending || Tracks[i].Id < 0)
155            {
156                continue;
157            }
158           
159            tracklist[j].Id = Tracks[i].Id;
160            tracklist[j].Name = strdup(Tracks[i].Name);
161            tracklist[j].Encoding = strdup(Tracks[i].Encoding);
162            ++j;
163        }
164        tracklist[j].Id = -1;
165    }
166
167    return tracklist;
168}
169
170static int32_t ManagerDel(Context_t * context, int32_t onlycurrent)
171{
172    uint32_t i = 0;
173    subtitle_mgr_printf(10, "%s::%s\n", FILENAME, __FUNCTION__);
174    if(onlycurrent == 0)
175    {
176        if(Tracks != NULL)
177        {
178            for (i = 0; i < TrackCount; i++)
179            {
180                freeTrack(&Tracks[i]);
181            }
182            free(Tracks);
183            Tracks = NULL;
184        }
185        else
186        {
187            subtitle_mgr_err("%s::%s nothing to delete!\n", FILENAME, __FUNCTION__);
188            return cERR_SUBTITLE_MGR_ERROR;
189        }
190        TrackCount = 0;
191        context->playback->isSubtitle = 0;
192    }
193   
194    CurrentTrack = -1;
195    subtitle_mgr_printf(10, "%s::%s return no error\n", FILENAME, __FUNCTION__);
196    return cERR_SUBTITLE_MGR_NO_ERROR;
197}
198
199static int32_t Command(void  *_context, ManagerCmd_t command, void *argument)
200{
201    Context_t  *context = (Context_t*) _context;
202    int32_t ret = cERR_SUBTITLE_MGR_NO_ERROR;
203
204    subtitle_mgr_printf(50, "%s::%s %d\n", FILENAME, __FUNCTION__, command);
205
206    switch(command)
207    {
208    case MANAGER_ADD:
209    {
210        Track_t * track = argument;
211        ret = ManagerAdd(context, *track);
212        break;
213    }
214    case MANAGER_LIST:
215    {
216        container_ffmpeg_update_tracks(context, context->playback->uri, 0);
217        *((char***)argument) = (char **)ManagerList(context);
218        break;
219    }
220    case MANAGER_GET:
221    {
222        if (TrackCount > 0 && CurrentTrack >= 0)
223        {
224            *((int*)argument) = (int)Tracks[CurrentTrack].Id;
225        }
226        else
227        {
228            *((int*)argument) = (int)-1;
229        }
230        break;
231    }
232    case MANAGER_GET_TRACK_DESC:
233    {
234        if ((TrackCount > 0) && (CurrentTrack >=0))
235        {
236            TrackDescription_t *track =  malloc(sizeof(TrackDescription_t));
237            *((TrackDescription_t**)argument) = track;
238            if (track)
239            {
240                memset(track, 0, sizeof(TrackDescription_t));
241                track->Id       = Tracks[CurrentTrack].Id;
242                track->Name     = strdup(Tracks[CurrentTrack].Name);
243                track->Encoding = strdup(Tracks[CurrentTrack].Encoding);
244            }
245        }
246        else
247        {
248            *((TrackDescription_t**)argument) = NULL;
249        }
250    break;
251    }
252    case MANAGER_GET_TRACK:
253    {
254        if ((TrackCount > 0) && (CurrentTrack >=0))
255        {
256            *((Track_t**)argument) = (Track_t*) &Tracks[CurrentTrack];
257        }
258        else
259        {
260            *((Track_t**)argument) = NULL;
261        }
262        break;
263    }
264    case MANAGER_GETENCODING:
265    {
266        if (TrackCount > 0 && CurrentTrack >= 0)
267        {
268            *((char**)argument) = (char *)strdup(Tracks[CurrentTrack].Encoding);
269        }
270        else
271        {
272            *((char**)argument) = (char *)strdup("");
273        }
274        break;
275    }
276    case MANAGER_GETNAME:
277    {
278        if (TrackCount > 0 && CurrentTrack >= 0)
279        {
280            *((char**)argument) = (char *)strdup(Tracks[CurrentTrack].Name);
281        }
282        else
283        {
284            *((char**)argument) = (char *)strdup("");
285        }
286        break;
287    }
288    case MANAGER_SET:
289    {
290        uint32_t i = 0;
291        int32_t requestedTrackId = *((int*)argument);
292       
293        subtitle_mgr_printf(20, "%s::%s MANAGER_SET id=%d\n", FILENAME, __FUNCTION__, *((int*)argument));
294        if (requestedTrackId == -1)
295        {
296            // track id -1 mean disable subtitle
297            CurrentTrack = -1;
298            break;
299        }
300
301        for (i = 0; i < TrackCount; ++i)
302        {
303            if (Tracks[i].Id == requestedTrackId)
304            {
305                CurrentTrack = i;
306                break;
307            }
308        }
309       
310        if (i == TrackCount)
311        {
312            subtitle_mgr_err("%s::%s track id %d unknown\n", FILENAME, __FUNCTION__, *((int*)argument));
313            ret = cERR_SUBTITLE_MGR_ERROR;
314        }
315        break;
316    }
317    case MANAGER_DEL:
318    {
319        if(argument == NULL)
320        {
321            ret = ManagerDel(context, 0);
322        }
323        else
324        {
325            ret = ManagerDel(context, *((int*)argument));
326        }
327        break;
328    }
329    case MANAGER_INIT_UPDATE:
330    {
331        uint32_t i;
332        for (i = 0; i < TrackCount; i++)
333        {
334            Tracks[i].pending = 1;
335        }
336        break;
337    }
338    default:
339        subtitle_mgr_err("%s:%s: ConatinerCmd not supported!", FILENAME, __FUNCTION__);
340        ret = cERR_SUBTITLE_MGR_ERROR;
341        break;
342    }
343
344    subtitle_mgr_printf(50, "%s:%s: returning %d\n", FILENAME, __FUNCTION__,ret);
345    return ret;
346}
347
348
349Manager_t SubtitleManager = {
350    "Subtitle",
351    &Command,
352    NULL
353};
Note: See TracBrowser for help on using the repository browser.