]> git.sesse.net Git - vlc/blob - modules/access_filter/bandwidth.c
Remove stdlib.h
[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 <stdio.h>
25 #include <assert.h>
26 #include <errno.h>
27
28 #include <vlc_access.h>
29
30 #define BANDWIDTH_TEXT N_("Bandwidth limit (bytes/s)")
31 #define BANDWIDTH_LONGTEXT N_( \
32     "The bandwidth module will drop any data in excess of that many bytes " \
33     "per seconds." )
34
35 static int  Open (vlc_object_t *);
36 static void Close (vlc_object_t *);
37
38 /* TODO: burst support */
39
40 vlc_module_begin ();
41     set_shortname (_("Bandwidth"));
42     set_description (_("Bandwidth limiter"));
43     set_category (CAT_INPUT);
44     set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
45     set_capability ("access_filter", 0);
46     add_shortcut ("bandwidth");
47     set_callbacks (Open, Close);
48
49     add_integer ("access-bandwidth", 65536, NULL, BANDWIDTH_TEXT,
50                  BANDWIDTH_LONGTEXT, VLC_FALSE);
51 vlc_module_end();
52
53 static int Read (access_t *access, uint8_t *buffer, int len);
54 static int Seek (access_t *access, int64_t offset);
55 static int Control (access_t *access, int cmd, va_list ap);
56
57 struct access_sys_t
58 {
59     mtime_t last_time;
60     size_t  last_size;
61     size_t  bandwidth;
62 };
63
64 /**
65  * Open()
66  */
67 static int Open (vlc_object_t *obj)
68 {
69     access_t *access = (access_t*)obj;
70     access_t *src = access->p_source;
71
72     if (src->pf_read != NULL)
73         access->pf_read = Read;
74     else
75     {
76         msg_Err (obj, "block bandwidth limit not implemented");
77         return VLC_EGENERIC;
78     }
79
80     if (src->pf_seek != NULL)
81         access->pf_seek = Seek;
82
83     access->pf_control = Control;
84     access->info = src->info;
85
86     access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
87     if (p_sys == NULL)
88         return VLC_ENOMEM;
89
90     memset (p_sys, 0, sizeof (*p_sys));
91     p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
92     p_sys->last_time = mdate ();
93
94     msg_Dbg (obj, "bandwidth limit: %u bytes/s", p_sys->bandwidth);
95     return VLC_SUCCESS;
96 }
97
98
99 /**
100  * Close()
101  */
102 static void Close (vlc_object_t *obj)
103 {
104     access_t *access = (access_t *)obj;
105     access_sys_t *p_sys = access->p_sys;
106     free (p_sys);
107 }
108
109
110 static int Read (access_t *access, uint8_t *buffer, int len)
111 {
112     access_t *src = access->p_source;
113     access_sys_t *p_sys = access->p_sys;
114     mtime_t now;
115
116     if (len == 0)
117         return 0;
118
119 retry:
120     now = mdate ();
121     if (now <= p_sys->last_time)
122     {
123         msg_Err (access, "*** ALERT *** System clock is going backward! ***");
124         return 0; /* Uho, your clock is broken. */
125     }
126
127     mtime_t delta = now - p_sys->last_time;
128     p_sys->last_time = now;
129
130     delta *= p_sys->bandwidth;
131     delta /= 1000000u;
132
133     if (delta == 0)
134     {
135         now += 1000000u / p_sys->bandwidth;
136         mwait (now);
137         goto retry;
138     }
139
140     if (len > delta)
141     {
142         msg_Dbg (access, "reading %u bytes instead of %u", (unsigned)delta,
143                  len);
144         len = (int)delta;
145     }
146
147
148     src->info.i_update = access->info.i_update;
149     len = src->pf_read (src, buffer, len);
150     access->info = src->info;
151
152     msg_Dbg (access, "read %u bytes", len);
153     return len;
154 }
155
156
157 static int Control (access_t *access, int cmd, va_list ap)
158 {
159     access_t *src = access->p_source;
160     return src->pf_control (src, cmd, ap);
161 }
162
163
164 static int Seek (access_t *access, int64_t offset)
165 {
166     access_t *src = access->p_source;
167
168     src->info.i_update = access->info.i_update;
169     int ret = src->pf_seek (src, offset);
170     access->info = src->info;
171     return ret;
172 }