]> git.sesse.net Git - vlc/blob - modules/access/attachment.c
Update LGPL license blurb, choosing v2.1+.
[vlc] / modules / access / attachment.c
1 /*****************************************************************************
2  * attachment.c: Input reading an attachment.
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_input.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open (vlc_object_t *);
41 static void Close(vlc_object_t *);
42
43 vlc_module_begin()
44     set_shortname(N_("Attachment"))
45     set_description(N_("Attachment input"))
46     set_category(CAT_INPUT)
47     set_subcategory(SUBCAT_INPUT_ACCESS)
48
49     set_capability("access", 0)
50     add_shortcut("attachment")
51     set_callbacks(Open, Close)
52 vlc_module_end()
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 struct access_sys_t {
58     input_attachment_t *a;
59 };
60
61 static ssize_t Read(access_t *, uint8_t *, size_t);
62 static int     Seek(access_t *, uint64_t);
63 static int     Control(access_t *, int, va_list);
64
65 /* */
66 static int Open(vlc_object_t *object)
67 {
68     access_t     *access = (access_t *)object;
69     access_sys_t *sys;
70
71     input_thread_t *input = access_GetParentInput(access);
72     if (!input)
73         return VLC_EGENERIC;
74
75     input_attachment_t *a;
76     if (input_Control(input, INPUT_GET_ATTACHMENT, &a, access->psz_location))
77         a = NULL;
78
79     vlc_object_release(input);
80
81     if (!a) {
82         msg_Err(access, "Failed to find the attachment '%s'",
83                 access->psz_location);
84         return VLC_EGENERIC;
85     }
86
87     /* */
88     access->p_sys = sys = malloc(sizeof(*sys));
89     if (!sys) {
90         vlc_input_attachment_Delete(a);
91         return VLC_ENOMEM;
92     }
93     sys->a = a;
94
95     /* */
96     access_InitFields(access);
97     access->pf_read    = Read;
98     access->pf_block   = NULL;
99     access->pf_control = Control;
100     access->pf_seek    = Seek;
101
102     return VLC_SUCCESS;
103 }
104
105 /* */
106 static void Close(vlc_object_t *object)
107 {
108     access_t     *access = (access_t *)object;
109     access_sys_t *sys = access->p_sys;
110
111     vlc_input_attachment_Delete(sys->a);
112     free(sys);
113 }
114
115 /* */
116 static ssize_t Read(access_t *access, uint8_t *buffer, size_t size)
117 {
118     access_sys_t *sys = access->p_sys;
119
120     access->info.b_eof = access->info.i_pos >= sys->a->i_data;
121     if (access->info.b_eof)
122         return 0;
123
124     const size_t copy = __MIN(size, sys->a->i_data - access->info.i_pos);
125     memcpy(buffer, (uint8_t*)sys->a->p_data + access->info.i_pos, copy);
126     access->info.i_pos += copy;
127     return copy;
128 }
129
130 /* */
131 static int Seek(access_t *access, uint64_t position)
132 {
133     access->info.i_pos = position;
134     access->info.b_eof = false;
135     return VLC_SUCCESS;
136 }
137
138 /* */
139 static int Control(access_t *access, int query, va_list args)
140 {
141     VLC_UNUSED(access);
142     switch (query)
143     {
144     /* */
145     case ACCESS_CAN_SEEK:
146     case ACCESS_CAN_FASTSEEK:
147     case ACCESS_CAN_PAUSE:
148     case ACCESS_CAN_CONTROL_PACE: {
149         bool *b = va_arg(args, bool*);
150         *b = true;
151         return VLC_SUCCESS;
152     }
153     case ACCESS_GET_PTS_DELAY: {
154         int64_t *d = va_arg(args, int64_t *);
155         *d = DEFAULT_PTS_DELAY;
156         return VLC_SUCCESS;
157     }
158     case ACCESS_SET_PAUSE_STATE:
159         return VLC_SUCCESS;
160
161     default:
162         return VLC_EGENERIC;
163     }
164 }
165