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 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/* Glue to play a stream from a remote TiVo
*
* tivo@wingert.org, February 2003
*
* Copyright (C) 2003 Christopher R. Wingert
*
* 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-1307, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mfs.h"
#include "vstream-client.h"
#define CHUNKSIZE ( 128 * 1024 )
struct sNowShowing
{
char dirName[ 80 ];
char blankTitle[ 80 ];
};
struct sNowShowing dirList[] =
{
{ "/Recording/LiveCache", "Live TV Buffer" },
{ "/Recording/NowShowingByTitle", "Now Showing Title" },
{ "/Recording/NowShowing", "Now Showing Title" },
{ "/Recording/Complete", "Now Showing Title" },
{ "/Recording/NowShowingByBucketTitle", "Now Showing Title" },
{ "", "" }
};
struct sfileParts
{
int fileNo;
int64_t fileSize;
int chunks;
};
char lastFsid[ 80 ] = "";
int filePart = -1;
struct sfileParts fileParts[ 255 ];
int filePartsTotal = 0;
static int64_t vstream_totalSize = 0;
int vstream_startstream( char *fsid )
{
struct mfs_obj_attr *obj;
int pcount;
int index;
int ifsid;
char calcFsid[ 80 ];
unsigned char *buffer;
int pesFileId;
int size;
int count;
char myfsid[ 80 ];
char *mypart;
if ( strcmp( fsid, lastFsid ) != 0 )
{
strcpy( myfsid, fsid );
mypart = strchr( myfsid, '/' );
if ( mypart != 0 )
{
*mypart = 0;
mypart++;
filePart = atoi( mypart );
}
ifsid = vstream_mfs_resolve( myfsid );
if ( ifsid > 0 )
{
vstream_totalSize = 0;
obj = query_object( ifsid, "Part", &pcount );
for( index = 0 ; index < pcount ; index ++ )
{
sprintf( calcFsid, "Part/%d/File", obj[ index ].subobj );
fileParts[ index ].fileNo = vstream_query_int( ifsid, calcFsid );
// HACK - Ignore last chunk of a Part File
// Why? I have no idea.
fileParts[ index ].fileSize =
vstream_mfs_fsid_size( fileParts[ index ].fileNo ) - 0x20000;
fileParts[ index ].chunks =
( fileParts[ index ].fileSize / CHUNKSIZE );
}
free( obj );
filePartsTotal = pcount;
if ( filePart != -1 )
{
if ( filePart > filePartsTotal )
{
vstream_error("vstream_fsidtoparts(): Invalid Part File %d\n", filePart);
return( 1 );
}
else
{
fileParts[ 0 ].fileNo = fileParts[ filePart ].fileNo;
fileParts[ 0 ].fileSize = fileParts[ filePart ].fileSize;
fileParts[ 0 ].chunks = fileParts[ filePart ].chunks;
filePartsTotal = 1;
}
}
for( index = 0 ; index < filePartsTotal ; index++ )
{
vstream_totalSize += fileParts[ index ].fileSize;
}
// We have to go into the last chunk to figure its exact size
buffer = malloc( CHUNKSIZE );
count = vstream_mfs_fsid_pread( fileParts[ filePartsTotal - 1 ].fileNo,
buffer, 0, CHUNKSIZE );
if ( count == CHUNKSIZE )
{
pesFileId = buffer[ 0x00 ] << 24 | buffer[ 0x01 ] << 16 |
buffer[ 0x02 ] << 8 | buffer[ 0x03 ];
if ( pesFileId == 0xf5467abd )
{
vstream_totalSize -= fileParts[ filePartsTotal - 1 ].fileSize;
size = buffer[ 0x0c ] << 24 | buffer[ 0x0d ] << 16 |
buffer[ 0x0e ] << 8 | buffer[ 0x03 ];
size /= 256;
size -= 4;
size *= CHUNKSIZE;
fileParts[ filePartsTotal - 1 ].fileSize = size;
fileParts[ filePartsTotal - 1 ].chunks = ( size / CHUNKSIZE );
vstream_totalSize += size;
}
}
free( buffer );
strcpy( lastFsid, fsid );
}
else
{
filePartsTotal = 0;
}
}
return( 0 );
}
int64_t vstream_streamsize( )
{
return( vstream_totalSize );
}
void vstream_fsidtooffset( int chunk, int *fileNo, int64_t *fileChunk )
{
int index;
*fileNo = 0;
*fileChunk = 0;
for( index = 0 ; index < filePartsTotal ; index++ )
{
if ( chunk >= fileParts[ index ].chunks )
{
chunk -= fileParts[ index ].chunks;
}
else
{
break;
}
}
if ( chunk < fileParts[ index ].chunks )
{
*fileNo = fileParts[ index ].fileNo;
*fileChunk = chunk;
}
}
int vstream_load_chunk( char *fsid, unsigned char *buff, int size, int64_t foffset )
{
int count;
int64_t chunk;
int64_t fileoffset;
int fileNo;
int64_t fileNoChunkOffset;
int64_t offset;
vstream_mfs_readahead( 1 );
if ( vstream_startstream( fsid ) == 1 )
{
return( 0 );
}
if ( filePartsTotal <= 0 )
{
return( 0 );
}
if ( foffset >= vstream_streamsize() )
{
return( 0 );
}
chunk = foffset / CHUNKSIZE;
offset = foffset - ( chunk * CHUNKSIZE );
vstream_fsidtooffset( chunk, &fileNo, &fileNoChunkOffset );
fileoffset = ( fileNoChunkOffset * CHUNKSIZE ) + offset;
count = vstream_mfs_fsid_pread( fileNo, buff, fileoffset, size );
return( count );
}
void vstream_list_streams( int longList )
{
struct mfs_dirent *dir;
struct mfs_obj_attr *obj;
u32 count;
u32 h;
u32 i;
int pcount;
time_t recTime;
struct tm *recTimetm;
if (vstream_start()) return;
h = 0;
while( strlen( dirList[ h ].dirName ) > 0 )
{
dir = mfs_dir( vstream_mfs_resolve( dirList[ h ].dirName ), &count );
for ( i = 0 ; i < count ; i++ )
{
obj = query_object( dir[ i ].fsid, "Part", &pcount );
free( obj );
recTime = ( vstream_query_int( dir[ i ].fsid, "StartDate" ) * 86400 );
recTime += vstream_query_int( dir[ i ].fsid, "StartTime" ) + 60;
recTimetm = localtime( &recTime );
recTimetm->tm_year %= 100;
vstream_error( "%d", vstream_query_int( dir[ i ].fsid, "Part" ) );
vstream_error( " -- " );
vstream_error( "%2.2d/%2.2d/%2.2d", recTimetm->tm_mon + 1, recTimetm->tm_mday, recTimetm->tm_year );
vstream_error( " " );
vstream_error( "%2.2d:%2.2d", recTimetm->tm_hour, recTimetm->tm_min );
vstream_error( " -- " );
vstream_error( "%02d", pcount );
vstream_error( " -- " );
vstream_error( "%s", vstream_query_string( dir[ i ].fsid, "Showing/Station/CallSign" ) );
vstream_error( "\n" );
if (longList) {
vstream_error("\t%s -- ", vstream_query_string( dir[ i ].fsid, "Showing/Program/Title" ));
vstream_error("%s\n", vstream_query_string( dir[ i ].fsid, "Showing/Program/EpisodeTitle" ));
}
}
if ( dir ) vstream_mfs_dir_free( dir );
h++;
}
}
int vstream_start()
{
if (load_super()) return 1;
if (load_zones()) return 1;
return 0;
}
|