demux.c 5.92 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * demux.c: demux functions for dvdplay.
 *****************************************************************************
 * Copyright (C) 1998-2001 VideoLAN
5
 * $Id: demux.c,v 1.3 2002/08/12 09:34:15 sam Exp $
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
 *
 * Author: Stphane Borel <stef@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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

34 35
#include "../../demux/mpeg/system.h"

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
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>

#ifdef STRNCASECMP_IN_STRINGS_H
#   include <strings.h>
#endif

#include "interface.h"
#include "dvd.h"
#include "intf.h"
#include "es.h"

/* how many packets dvdplay_Demux will read in each loop */
#define dvdplay_READ_ONCE 64

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Demux         ( input_thread_t * );

63 64 65 66 67 68 69 70 71 72 73
/*****************************************************************************
 * Private structure
 *****************************************************************************/
struct demux_sys_t
{
    dvd_data_t * p_dvd;

    module_t *   p_module;
    mpeg_demux_t mpeg;
};

74 75 76 77 78 79
/*****************************************************************************
 * InitDVD: initializes dvdplay structures
 *****************************************************************************/
int E_(InitDVD) ( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
80 81
    dvd_data_t *    p_dvd = (dvd_data_t *)p_input->p_access_data;
    demux_sys_t *   p_demux;
82 83 84 85 86 87 88
    char *          psz_intf = NULL;

    if( p_input->stream.i_method != INPUT_METHOD_DVD )
    {
        return -1;
    }

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
    if( p_demux == NULL )
    {
        return -1;
    }

    p_input->p_private = (void*)&p_demux->mpeg;
    p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
    if( p_demux->p_module == NULL )
    {
        free( p_input->p_demux_data );
        return -1;
    }

    p_input->p_demux_data->p_dvd = p_dvd;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    p_input->pf_demux = Demux;
    p_input->pf_rewind = NULL;

    psz_intf = config_GetPsz( p_input, "intf" );
    config_PutPsz( p_input, "intf", "dvdplay" );
    p_dvd->p_intf = intf_Create( p_input );
    p_dvd->p_intf->b_block = VLC_FALSE;
    intf_RunThread( p_dvd->p_intf );
    
    if( psz_intf != NULL )
    {
        config_PutPsz( p_input, "intf", psz_intf );
    }

    return 0;
}

/*****************************************************************************
 * EndDVD: frees unused data
 *****************************************************************************/
void E_(EndDVD) ( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
128
    dvd_data_t *    p_dvd = p_input->p_demux_data->p_dvd;
129 130 131 132 133 134
    intf_thread_t * p_intf = NULL;

    p_intf = vlc_object_find( p_input, VLC_OBJECT_INTF, FIND_CHILD );
    if( p_intf != NULL )
    {
        intf_StopThread( p_intf );
135
        vlc_object_detach( p_intf );
136 137 138 139 140
        vlc_object_release( p_intf );
        intf_Destroy( p_intf );
    }

    p_dvd->p_intf = NULL;
141 142 143

    module_Unneed( p_input, p_input->p_demux_data->p_module );
    free( p_input->p_demux_data );
144 145 146 147 148 149 150 151 152 153 154 155 156
}

/*****************************************************************************
 * Demux
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
    dvd_data_t *            p_dvd;
    data_packet_t *         p_data;
    ssize_t                 i_result;
    ptrdiff_t               i_remains;
    int                     i_data_nb = 0;

157
    p_dvd = p_input->p_demux_data->p_dvd;
158 159 160 161
   
    /* Read headers to compute payload length */
    do
    {
162 163 164
        i_result = p_input->p_demux_data->mpeg.pf_read_ps( p_input, &p_data );

        if( i_result <= 0 )
165 166 167 168 169 170
        {
            return i_result;
        }

        i_remains = p_input->p_last_data - p_input->p_current_data;

171
        p_input->p_demux_data->mpeg.pf_demux_ps( p_input, p_data );
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        

        ++i_data_nb;
    }
    while( i_remains );
    

    
//    if( p_dvd->b_still && p_dvd->b_end_of_cell && p_dvd->p_intf != NULL )
    if( p_dvd->i_still_time && p_dvd->b_end_of_cell && p_dvd->p_intf != NULL )
    {
        pgrm_descriptor_t * p_pgrm;

        /* when we receive still_time flag, we have to pause immediately */
        input_SetStatus( p_input, INPUT_STATUS_PAUSE );

        dvdIntfStillTime( p_dvd->p_intf, p_dvd->i_still_time );
        p_dvd->i_still_time = 0;
        
        vlc_mutex_lock( &p_input->stream.stream_lock );
        
        p_pgrm = p_input->stream.p_selected_program;
        p_pgrm->i_synchro_state = SYNCHRO_REINIT;
        
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        
        input_ClockManageControl( p_input, p_pgrm, 0 );
    }

    return i_data_nb;
}