]> git.sesse.net Git - vlc/blob - modules/access/attachment.c
Added an access for reading input attachments.
[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 *, int64_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_path))
77         a = NULL;
78
79     vlc_object_release(input);
80
81     if (!a) {
82         msg_Err(access, "Failed to find the attachment '%s'", access->psz_path);
83         return VLC_EGENERIC;
84     }
85
86     /* */
87     access->p_sys = sys = malloc(sizeof(*sys));
88     if (!sys) {
89         vlc_input_attachment_Delete(a);
90         return VLC_ENOMEM;
91     }
92     sys->a = a;
93
94     /* */
95     access_InitFields(access);
96     access->pf_read    = Read;
97     access->pf_block   = NULL;
98     access->pf_control = Control;
99     access->pf_seek    = Seek;
100
101     return VLC_SUCCESS;
102 }
103
104 /* */
105 static void Close(vlc_object_t *object)
106 {
107     access_t     *access = (access_t *)object;
108     access_sys_t *sys = access->p_sys;
109
110     vlc_input_attachment_Delete(sys->a);
111     free(sys);
112 }
113
114 /* */
115 static ssize_t Read(access_t *access, uint8_t *buffer, size_t size)
116 {
117     access_sys_t *sys = access->p_sys;
118
119     access->info.b_eof = access->info.i_pos >= sys->a->i_data;
120     if (access->info.b_eof)
121         return 0;
122
123     const size_t copy = __MIN(size, sys->a->i_data - access->info.i_pos);
124     memcpy(buffer, (uint8_t*)sys->a->p_data + access->info.i_pos, copy);
125     access->info.i_pos += copy;
126     return copy;
127 }
128
129 /* */
130 static int Seek(access_t *access, int64_t position)
131 {
132     access->info.i_pos = position;
133     access->info.b_eof = false;
134     return VLC_SUCCESS;
135 }
136
137 /* */
138 static int Control(access_t *access, int query, va_list args)
139 {
140     VLC_UNUSED(access);
141     switch (query)
142     {
143     /* */
144     case ACCESS_CAN_SEEK:
145     case ACCESS_CAN_FASTSEEK:
146     case ACCESS_CAN_PAUSE:
147     case ACCESS_CAN_CONTROL_PACE: {
148         bool *b = va_arg(args, bool*);
149         *b = true;
150         return VLC_SUCCESS;
151     }
152     case ACCESS_GET_PTS_DELAY: {
153         int64_t *d = va_arg(args, int64_t *);
154         *d = DEFAULT_PTS_DELAY;
155         return VLC_SUCCESS;
156     }
157     case ACCESS_SET_PAUSE_STATE:
158         return VLC_SUCCESS;
159
160     default:
161         return VLC_EGENERIC;
162     }
163 }
164