]> git.sesse.net Git - vlc/blob - modules/access_filter/bandwidth.c
Remove stdio while we're at it.
[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 vlc_module_end();
51
52 static int Read (access_t *access, uint8_t *buffer, int len);
53 static int Seek (access_t *access, int64_t offset);
54 static int Control (access_t *access, int cmd, va_list ap);
55
56 struct access_sys_t
57 {
58     mtime_t last_time;
59     size_t  last_size;
60     size_t  bandwidth;
61 };
62
63 /**
64  * Open()
65  */
66 static int Open (vlc_object_t *obj)
67 {
68     access_t *access = (access_t*)obj;
69     access_t *src = access->p_source;
70
71     if (src->pf_read != NULL)
72         access->pf_read = Read;
73     else
74     {
75         msg_Err (obj, "block bandwidth limit not implemented");
76         return VLC_EGENERIC;
77     }
78
79     if (src->pf_seek != NULL)
80         access->pf_seek = Seek;
81
82     access->pf_control = Control;
83     access->info = src->info;
84
85     access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
86     if (p_sys == NULL)
87         return VLC_ENOMEM;
88
89     memset (p_sys, 0, sizeof (*p_sys));
90     p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
91     p_sys->last_time = mdate ();
92
93     msg_Dbg (obj, "bandwidth limit: %u bytes/s", p_sys->bandwidth);
94     return VLC_SUCCESS;
95 }
96
97
98 /**
99  * Close()
100  */
101 static void Close (vlc_object_t *obj)
102 {
103     access_t *access = (access_t *)obj;
104     access_sys_t *p_sys = access->p_sys;
105     free (p_sys);
106 }
107
108
109 static int Read (access_t *access, uint8_t *buffer, int len)
110 {
111     access_t *src = access->p_source;
112     access_sys_t *p_sys = access->p_sys;
113     mtime_t now;
114
115     if (len == 0)
116         return 0;
117
118 retry:
119     now = mdate ();
120     if (now <= p_sys->last_time)
121     {
122         msg_Err (access, "*** ALERT *** System clock is going backward! ***");
123         return 0; /* Uho, your clock is broken. */
124     }
125
126     mtime_t delta = now - p_sys->last_time;
127     p_sys->last_time = now;
128
129     delta *= p_sys->bandwidth;
130     delta /= 1000000u;
131
132     if (delta == 0)
133     {
134         now += 1000000u / p_sys->bandwidth;
135         mwait (now);
136         goto retry;
137     }
138
139     if (len > delta)
140     {
141         msg_Dbg (access, "reading %u bytes instead of %u", (unsigned)delta,
142                  len);
143         len = (int)delta;
144     }
145
146
147     src->info.i_update = access->info.i_update;
148     len = src->pf_read (src, buffer, len);
149     access->info = src->info;
150
151     msg_Dbg (access, "read %u bytes", len);
152     return len;
153 }
154
155
156 static int Control (access_t *access, int cmd, va_list ap)
157 {
158     access_t *src = access->p_source;
159     return src->pf_control (src, cmd, ap);
160 }
161
162
163 static int Seek (access_t *access, int64_t offset)
164 {
165     access_t *src = access->p_source;
166
167     src->info.i_update = access->info.i_update;
168     int ret = src->pf_seek (src, offset);
169     access->info = src->info;
170     return ret;
171 }