]> git.sesse.net Git - vlc/blob - modules/access/sdp.c
mux: avi: fix leak on format failure
[vlc] / modules / access / sdp.c
1 /*****************************************************************************
2  * sdp.c: Fake input for sdp:// scheme
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_access.h>
28
29 static int Open (vlc_object_t *);
30 static void Close (vlc_object_t *);
31
32 vlc_module_begin ()
33     set_shortname (N_("SDP"))
34     set_description (N_("Session Description Protocol"))
35     set_category (CAT_INPUT)
36     set_subcategory (SUBCAT_INPUT_ACCESS)
37
38     set_capability ("access", 0)
39     set_callbacks (Open, Close)
40     add_shortcut ("sdp")
41 vlc_module_end()
42
43 static ssize_t Read (access_t *, uint8_t *, size_t);
44 static int Seek (access_t *, uint64_t);
45 static int Control (access_t *, int, va_list);
46
47 struct access_sys_t
48 {
49     size_t length;
50     char   data[];
51 };
52
53 static int Open (vlc_object_t *obj)
54 {
55     access_t *access = (access_t *)obj;
56     size_t len = strlen (access->psz_location);
57
58     access_sys_t *sys = malloc (sizeof(*sys) + len);
59     if (unlikely(sys == NULL))
60         return VLC_ENOMEM;
61
62     /* NOTE: This copy is not really needed. Better safe than sorry. */
63     sys->length = len;
64     memcpy (sys->data, access->psz_location, len);
65
66     access_InitFields (access);
67     access->pf_read = Read;
68     access->pf_block = NULL;
69     access->pf_seek = Seek;
70     access->pf_control = Control;
71     access->p_sys = sys;
72
73     return VLC_SUCCESS;
74 }
75
76 static void Close (vlc_object_t *obj)
77 {
78     access_t *access = (access_t *)obj;
79     access_sys_t *sys = access->p_sys;
80
81     free (sys);
82 }
83
84 static ssize_t Read (access_t *access, uint8_t *buf, size_t len)
85 {
86     access_sys_t *sys = access->p_sys;
87
88     if (access->info.i_pos >= sys->length)
89     {
90         access->info.b_eof = true;
91         return 0;
92     }
93
94     if (len > sys->length)
95         len = sys->length;
96     memcpy (buf, sys->data + access->info.i_pos, len);
97     access->info.i_pos += len;
98     return len;
99 }
100
101 static int Seek (access_t *access, uint64_t position)
102 {
103     access->info.i_pos = position;
104     access->info.b_eof = false;
105     return VLC_SUCCESS;
106 }
107
108 static int Control (access_t *access, int query, va_list args)
109 {
110     switch (query)
111     {
112         case ACCESS_CAN_SEEK:
113         case ACCESS_CAN_FASTSEEK:
114         case ACCESS_CAN_PAUSE:
115         case ACCESS_CAN_CONTROL_PACE:
116         {
117             bool *b = va_arg(args, bool*);
118             *b = true;
119             return VLC_SUCCESS;
120         }
121
122         case ACCESS_GET_PTS_DELAY:
123         {
124             int64_t *dp = va_arg(args, int64_t *);
125             *dp = DEFAULT_PTS_DELAY;
126             return VLC_SUCCESS;
127         }
128     
129         case ACCESS_SET_PAUSE_STATE:
130             return VLC_SUCCESS;
131     }
132     (void) access;
133     return VLC_EGENERIC;
134 }