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