]> git.sesse.net Git - vlc/blob - modules/access_filter/bandwidth.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / access_filter / bandwidth.c
1 /*****************************************************************************
2  * bandwidth.c
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id: dump.c 19948 2007-04-26 19:53:53Z courmisch $
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #include <vlc/vlc.h>
23
24 #include <assert.h>
25 #include <errno.h>
26
27 #include <vlc_access.h>
28
29 #define BANDWIDTH_TEXT N_("Bandwidth limit (bytes/s)")
30 #define BANDWIDTH_LONGTEXT N_( \
31     "The bandwidth module will drop any data in excess of that many bytes " \
32     "per seconds." )
33
34 static int  Open (vlc_object_t *);
35 static void Close (vlc_object_t *);
36
37 /* TODO: burst support */
38
39 vlc_module_begin ();
40     set_shortname (_("Bandwidth"));
41     set_description (_("Bandwidth limiter"));
42     set_category (CAT_INPUT);
43     set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
44     set_capability ("access_filter", 0);
45     add_shortcut ("bandwidth");
46     set_callbacks (Open, Close);
47
48     add_integer ("access-bandwidth", 65536, NULL, BANDWIDTH_TEXT,
49                  BANDWIDTH_LONGTEXT, VLC_FALSE);
50         change_safe();
51
52 vlc_module_end();
53
54 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
55 static int Seek (access_t *access, int64_t offset);
56 static int Control (access_t *access, int cmd, va_list ap);
57
58 struct access_sys_t
59 {
60     mtime_t last_time;
61     size_t  last_size;
62     size_t  bandwidth;
63 };
64
65 /**
66  * Open()
67  */
68 static int Open (vlc_object_t *obj)
69 {
70     access_t *access = (access_t*)obj;
71     access_t *src = access->p_source;
72
73     if (src->pf_read != NULL)
74         access->pf_read = Read;
75     else
76     {
77         msg_Err (obj, "block bandwidth limit not implemented");
78         return VLC_EGENERIC;
79     }
80
81     if (src->pf_seek != NULL)
82         access->pf_seek = Seek;
83
84     access->pf_control = Control;
85     access->info = src->info;
86
87     access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
88     if (p_sys == NULL)
89         return VLC_ENOMEM;
90
91     memset (p_sys, 0, sizeof (*p_sys));
92     p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
93     p_sys->last_time = mdate ();
94
95     msg_Dbg (obj, "bandwidth limit: %lu bytes/s",
96              (unsigned long)p_sys->bandwidth);
97     return VLC_SUCCESS;
98 }
99
100
101 /**
102  * Close()
103  */
104 static void Close (vlc_object_t *obj)
105 {
106     access_t *access = (access_t *)obj;
107     access_sys_t *p_sys = access->p_sys;
108     free (p_sys);
109 }
110
111
112 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
113 {
114     access_t *src = access->p_source;
115     access_sys_t *p_sys = access->p_sys;
116     mtime_t now;
117
118     if (len == 0)
119         return 0;
120
121 retry:
122     now = mdate ();
123     if (now <= p_sys->last_time)
124     {
125         msg_Err (access, "*** ALERT *** System clock is going backward! ***");
126         return 0; /* Uho, your clock is broken. */
127     }
128
129     mtime_t delta = now - p_sys->last_time;
130     p_sys->last_time = now;
131
132     delta *= p_sys->bandwidth;
133     delta /= 1000000u;
134
135     if (delta == 0)
136     {
137         now += 1000000u / p_sys->bandwidth;
138         mwait (now);
139         goto retry;
140     }
141
142     if (len > delta)
143     {
144         msg_Dbg (access, "reading %u bytes instead of %u", (unsigned)delta,
145                  len);
146         len = (int)delta;
147     }
148
149
150     src->info.i_update = access->info.i_update;
151     len = src->pf_read (src, buffer, len);
152     access->info = src->info;
153
154     msg_Dbg (access, "read %u bytes", len);
155     return len;
156 }
157
158
159 static int Control (access_t *access, int cmd, va_list ap)
160 {
161     access_t *src = access->p_source;
162     return src->pf_control (src, cmd, ap);
163 }
164
165
166 static int Seek (access_t *access, int64_t offset)
167 {
168     access_t *src = access->p_source;
169
170     src->info.i_update = access->info.i_update;
171     int ret = src->pf_seek (src, offset);
172     access->info = src->info;
173     return ret;
174 }