multicat_validate.c 6.02 KB
Newer Older
1 2 3
/*****************************************************************************
 * multicat_validate.c: validate position in directory input
 *****************************************************************************
4
 * Copyright (C) 2009, 2011, 2015 VideoLAN
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
 * $Id$
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
34
#include <syslog.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#include "util.h"

#define DEFAULT_TOLERANCE 27000000ULL

/*****************************************************************************
 * Local declarations
 *****************************************************************************/
static uint64_t i_rotate_size = DEFAULT_ROTATE_SIZE;
static int64_t i_tolerance = DEFAULT_TOLERANCE;
static size_t i_asked_payload_size = DEFAULT_PAYLOAD_SIZE;
static int64_t i_delay = 0;
static const char *psz_dir_name;
static uint64_t i_dir_file;
static bool b_status = false;

static void usage(void)
{
53
    msg_Raw( NULL, "Usage: multicat_validate [-l <syslogtag>] [-k <start time>] [-r <file duration>] [-W <tolerance>] [-m <payload size>] <input directory>" );
54 55 56 57 58 59 60 61 62 63 64 65 66
    msg_Raw( NULL, "    -k: start at the given position (in 27 MHz units, negative = from the end)" );
    msg_Raw( NULL, "    -r: in directory mode, rotate file after this duration (default: 97200000000 ticks = 1 hour)" );
    msg_Raw( NULL, "    -W: maximum tolerated wait time before the forthcoming packet (by default: 27000000 ticks = 1 second)" );
    msg_Raw( NULL, "    -m: size of the payload chunk, excluding optional RTP header (default 1316)" );
    exit(EXIT_FAILURE);
}

static void HandleSTC( uint64_t i_stc )
{
    uint64_t i_wall;
    int64_t i_sleep;
retry:
    i_wall = real_Date() - i_delay;
67 68 69 70 71 72 73 74
    i_sleep = (int64_t)i_stc - (int64_t)i_wall;

    if ( i_sleep > (int64_t)i_rotate_size )
    {
        msg_Err( NULL, "invalid aux file %"PRIu64" (stc %"PRIu64")",
                 i_dir_file, i_stc );
        exit(1);
    }
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

    if ( i_sleep > i_tolerance )
    {
        if ( b_status )
        {
            printf( "0\n" );
            b_status = false;
        }

        real_Sleep( i_sleep - i_tolerance );
        goto retry;
    }

    if ( !b_status )
        printf( "1\n" );
    b_status = true;

    if ( i_sleep > 0 )
        real_Sleep( i_sleep );
}

/*****************************************************************************
 * Entry point
 *****************************************************************************/
int main( int i_argc, char **pp_argv )
{
101
    const char *psz_syslog_tag = NULL;
102 103 104 105 106 107 108
    off_t i_nb_skipped_chunks;
    FILE *p_input_aux;
    int c;
    uint64_t i_stc;

    setvbuf(stdout, NULL, _IOLBF, 0);

109
    while ( (c = getopt( i_argc, pp_argv, "l:k:r:W:m:h" )) != -1 )
110 111 112
    {
        switch ( c )
        {
113 114 115 116
        case 'l':
            psz_syslog_tag = optarg;
            break;

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
        case 'k':
            i_delay = strtoull( optarg, NULL, 0 );
            break;

        case 'r':
            i_rotate_size = strtoull( optarg, NULL, 0 );
            break;

        case 'W':
            i_tolerance = strtoull( optarg, NULL, 0 );
            break;

        case 'm':
            i_asked_payload_size = strtol( optarg, NULL, 0 );
            break;

        case 'h':
        default:
            usage();
            break;
        }
    }
    if ( optind >= i_argc )
        usage();
    psz_dir_name = pp_argv[optind];
    printf( "0\n" );

144 145 146
    if ( psz_syslog_tag != NULL )
        msg_Openlog( psz_syslog_tag, LOG_NDELAY, LOG_USER );

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 174 175 176
    if ( i_delay <= 0 )
    {
        i_delay *= -1;
        i_stc = real_Date() - i_delay;
    }
    else
    {
        i_stc = i_delay;
        i_delay = real_Date() - i_stc;
    }

    i_dir_file = GetDirFile( i_rotate_size, i_stc );
    i_nb_skipped_chunks = LookupDirAuxFile( psz_dir_name, i_dir_file, i_stc,
                                            i_asked_payload_size );
    if ( i_nb_skipped_chunks < 0 )
    {
        /* Try at most one more chunk */
        i_dir_file++;
        i_nb_skipped_chunks = LookupDirAuxFile( psz_dir_name, i_dir_file, i_stc,
                                                i_asked_payload_size );
        if ( i_nb_skipped_chunks < 0 )
        {
            msg_Err( NULL, "position not found" );
            exit(1);
        }
    }

    close( OpenDirFile( psz_dir_name, i_dir_file, true, i_asked_payload_size,
                        &p_input_aux ) );

177 178 179 180 181 182
    int ret = fseeko( p_input_aux, 8 * i_nb_skipped_chunks, SEEK_SET );
    if ( ret == -1 )
    {
        msg_Err( NULL, "fseeko failed" );
        exit(1);
    }
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    for ( ; ; )
    {
        uint8_t p_aux[8];

        if ( fread( p_aux, 8, 1, p_input_aux ) != 1 )
        {
            int i_fd;
            fclose( p_input_aux );

            i_dir_file++;

            i_fd = OpenDirFile( psz_dir_name, i_dir_file, true,
                                i_asked_payload_size, &p_input_aux );
            if ( i_fd < 0 )
            {
                msg_Err( NULL, "end of files reached" );
                exit(1);
            }
            close( i_fd );
        }
        i_stc = FromSTC( p_aux );

        HandleSTC( i_stc );
    }
208 209 210

    if ( psz_syslog_tag != NULL )
        msg_Closelog();
211
}