1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
32 #include <vlc_access.h>
34 #define BANDWIDTH_TEXT N_("Bandwidth limit (bytes/s)")
35 #define BANDWIDTH_LONGTEXT N_( \
36 "The bandwidth module will drop any data in excess of that many bytes " \
39 static int Open (vlc_object_t *);
40 static void Close (vlc_object_t *);
42 /* TODO: burst support */
45 set_shortname (N_("Bandwidth"))
46 set_description (N_("Bandwidth limiter"))
47 set_category (CAT_INPUT)
48 set_subcategory (SUBCAT_INPUT_ACCESS_FILTER)
49 set_capability ("access_filter", 0)
50 add_shortcut ("bandwidth")
51 set_callbacks (Open, Close)
53 add_integer ("access-bandwidth", 65536, NULL, BANDWIDTH_TEXT,
54 BANDWIDTH_LONGTEXT, false);
57 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
58 static int Seek (access_t *access, int64_t offset);
59 static int Control (access_t *access, int cmd, va_list ap);
71 static int Open (vlc_object_t *obj)
73 access_t *access = (access_t*)obj;
74 access_t *src = access->p_source;
76 if (src->pf_read != NULL)
77 access->pf_read = Read;
80 msg_Err (obj, "block bandwidth limit not implemented");
84 if (src->pf_seek != NULL)
85 access->pf_seek = Seek;
87 access->pf_control = Control;
88 access->info = src->info;
90 access_sys_t *p_sys = access->p_sys = calloc( 1, sizeof (*p_sys) );
94 p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
95 p_sys->last_time = mdate ();
97 msg_Dbg (obj, "bandwidth limit: %lu bytes/s",
98 (unsigned long)p_sys->bandwidth);
106 static void Close (vlc_object_t *obj)
108 access_t *access = (access_t *)obj;
109 access_sys_t *p_sys = access->p_sys;
114 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
116 access_t *src = access->p_source;
117 access_sys_t *p_sys = access->p_sys;
125 if (now <= p_sys->last_time)
127 msg_Err (access, "*** ALERT *** System clock is going backward! ***");
128 return 0; /* Uho, your clock is broken. */
131 mtime_t delta = now - p_sys->last_time;
132 p_sys->last_time = now;
134 delta *= p_sys->bandwidth;
139 now += 1000000u / p_sys->bandwidth;
146 msg_Dbg (access, "reading %"PRIu64" bytes instead of %zu", delta,
152 src->info.i_update = access->info.i_update;
153 len = src->pf_read (src, buffer, len);
154 access->info = src->info;
156 msg_Dbg (access, "read %zu bytes", len);
161 static int Control (access_t *access, int cmd, va_list ap)
163 access_t *src = access->p_source;
164 return src->pf_control (src, cmd, ap);
168 static int Seek (access_t *access, int64_t offset)
170 access_t *src = access->p_source;
172 src->info.i_update = access->info.i_update;
173 int ret = src->pf_seek (src, offset);
174 access->info = src->info;