bandwidth.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*****************************************************************************
 * bandwidth.c
 *****************************************************************************
 * Copyright © 2007 Rémi Denis-Courmont
 * $Id: dump.c 19948 2007-04-26 19:53:53Z courmisch $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <vlc/vlc.h>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

#include <vlc_access.h>

#define BANDWIDTH_TEXT N_("Bandwidth limit (bytes/s)")
#define BANDWIDTH_LONGTEXT N_( \
    "The bandwidth module will drop any data in excess of that many bytes " \
    "per seconds." )

static int  Open (vlc_object_t *);
static void Close (vlc_object_t *);

/* TODO: burst support */

vlc_module_begin ();
    set_shortname (_("Bandwidth"));
    set_description (_("Bandwidth limiter"));
    set_category (CAT_INPUT);
    set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
    set_capability ("access_filter", 0);
    add_shortcut ("bandwidth");
    set_callbacks (Open, Close);

    add_integer ("access-bandwidth", 65536, NULL, BANDWIDTH_TEXT,
                 BANDWIDTH_LONGTEXT, VLC_FALSE);
vlc_module_end();

static int Read (access_t *access, uint8_t *buffer, int len);
static int Seek (access_t *access, int64_t offset);
static int Control (access_t *access, int cmd, va_list ap);

struct access_sys_t
{
    mtime_t last_time;
    size_t  last_size;
    size_t  bandwidth;
};

/**
 * Open()
 */
static int Open (vlc_object_t *obj)
{
    access_t *access = (access_t*)obj;
    access_t *src = access->p_source;

    if (src->pf_read != NULL)
        access->pf_read = Read;
    else
    {
        msg_Err (obj, "block bandwidth limit not implemented");
        return VLC_EGENERIC;
    }

    if (src->pf_seek != NULL)
        access->pf_seek = Seek;

    access->pf_control = Control;
    access->info = src->info;

    access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
    if (p_sys == NULL)
        return VLC_ENOMEM;

    memset (p_sys, 0, sizeof (*p_sys));
    p_sys->bandwidth = var_CreateGetInteger (access, "access-bandwidth");
    p_sys->last_time = mdate ();

    msg_Dbg (obj, "bandwidth limit: %u bytes/s", p_sys->bandwidth);
    return VLC_SUCCESS;
}


/**
 * Close()
 */
static void Close (vlc_object_t *obj)
{
    access_t *access = (access_t *)obj;
    access_sys_t *p_sys = access->p_sys;
    free (p_sys);
}


static int Read (access_t *access, uint8_t *buffer, int len)
{
    access_t *src = access->p_source;
    access_sys_t *p_sys = access->p_sys;
    mtime_t now;

    if (len == 0)
        return 0;

retry:
    now = mdate ();
    if (now <= p_sys->last_time)
    {
        msg_Err (access, "*** ALERT *** System clock is going backward! ***");
        return 0; /* Uho, your clock is broken. */
    }

    mtime_t delta = now - p_sys->last_time;
    p_sys->last_time = now;

    delta *= p_sys->bandwidth;
    delta /= 1000000u;

    if (delta == 0)
    {
        now += 1000000u / p_sys->bandwidth;
        mwait (now);
        goto retry;
    }

    if (len > delta)
    {
        msg_Dbg (access, "reading %u bytes instead of %u", (unsigned)delta,
                 len);
        len = (int)delta;
    }


    src->info.i_update = access->info.i_update;
    len = src->pf_read (src, buffer, len);
    access->info = src->info;

    msg_Dbg (access, "read %u bytes", len);
    return len;
}


static int Control (access_t *access, int cmd, va_list ap)
{
    access_t *src = access->p_source;
    return src->pf_control (src, cmd, ap);
}


static int Seek (access_t *access, int64_t offset)
{
    access_t *src = access->p_source;

    src->info.i_update = access->info.i_update;
    int ret = src->pf_seek (src, offset);
    access->info = src->info;
    return ret;
}