1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
5 * $Id: dump.c 19948 2007-04-26 19:53:53Z courmisch $
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 *****************************************************************************/
29 #include <vlc_access.h>
31 #define BANDWIDTH_TEXT N_("Bandwidth limit (bytes/s)")
32 #define BANDWIDTH_LONGTEXT N_( \
33 "The bandwidth module will drop any data in excess of that many bytes " \
36 static int Open (vlc_object_t *);
37 static void Close (vlc_object_t *);
39 /* TODO: burst support */
42 set_shortname (_("Bandwidth"));
43 set_description (_("Bandwidth limiter"));
44 set_category (CAT_INPUT);
45 set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
46 set_capability ("access_filter", 0);
47 add_shortcut ("bandwidth");
48 set_callbacks (Open, Close);
50 add_integer ("access-bandwidth", 65536, NULL, BANDWIDTH_TEXT,
51 BANDWIDTH_LONGTEXT, VLC_FALSE);
54 static int Read (access_t *access, uint8_t *buffer, int len);
55 static int Seek (access_t *access, int64_t offset);
56 static int Control (access_t *access, int cmd, va_list ap);
68 static int Open (vlc_object_t *obj)
70 access_t *access = (access_t*)obj;
71 access_t *src = access->p_source;
73 if (src->pf_read != NULL)
74 access->pf_read = Read;
77 msg_Err (obj, "block bandwidth limit not implemented");
81 if (src->pf_seek != NULL)
82 access->pf_seek = Seek;
84 access->pf_control = Control;
85 access->info = src->info;
87 access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
91 memset (p_sys, 0, sizeof (*p_sys));
92 p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
93 p_sys->last_time = mdate ();
95 msg_Dbg (obj, "bandwidth limit: %u bytes/s", p_sys->bandwidth);
103 static void Close (vlc_object_t *obj)
105 access_t *access = (access_t *)obj;
106 access_sys_t *p_sys = access->p_sys;
111 static int Read (access_t *access, uint8_t *buffer, int len)
113 access_t *src = access->p_source;
114 access_sys_t *p_sys = access->p_sys;
122 if (now <= p_sys->last_time)
124 msg_Err (access, "*** ALERT *** System clock is going backward! ***");
125 return 0; /* Uho, your clock is broken. */
128 mtime_t delta = now - p_sys->last_time;
129 p_sys->last_time = now;
131 delta *= p_sys->bandwidth;
136 now += 1000000u / p_sys->bandwidth;
143 msg_Dbg (access, "reading %u bytes instead of %u", (unsigned)delta,
149 src->info.i_update = access->info.i_update;
150 len = src->pf_read (src, buffer, len);
151 access->info = src->info;
153 msg_Dbg (access, "read %u bytes", len);
158 static int Control (access_t *access, int cmd, va_list ap)
160 access_t *src = access->p_source;
161 return src->pf_control (src, cmd, ap);
165 static int Seek (access_t *access, int64_t offset)
167 access_t *src = access->p_source;
169 src->info.i_update = access->info.i_update;
170 int ret = src->pf_seek (src, offset);
171 access->info = src->info;