Update of /cvsroot/freevo/freevo/WIP/Thomas/pyshift
In directory sc8-pr-cvs1:/tmp/cvs-serv28281

Added Files:
        pyshift.c pyshift_test.py setup.py ts.h tserrors.h 
Log Message:
New Timeshifting. Use mplayer_timeshift2.patch for mplayer.


--- NEW FILE: pyshift.c ---
#include <Python.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h> 
#include <string.h>
#include <stdio.h>
#include "ts.h"
#include <signal.h>
#include <errno.h>


static PyObject* pyshift_init(PyObject* self, PyObject* args)
{
  int fd ;
  HEADERINFO header ;
  caddr_t pointer ;
  char * filename;
  int filesize=65536*1024;
  if ( !PyArg_ParseTuple(args, "s|i", &filename, &filesize) )
  {
    return NULL;
  }
  fd = open( filename, O_RDWR | O_CREAT, 00600 ) ;
  if ( fd == -1 ) 
  {
    // TODO: Handle Error 
    return NULL;
  }
  memset( &header, 0, sizeof( header ) ) ;
  strcpy( header.freevots, "freevots" ) ;
  header.header_size = sizeof( header ) ;
  header.file_size = filesize + sizeof( header ) ;
  header.data_size = filesize ;
  header.writepointer = 0 ;
  // Write the header info 
  write( fd, &header, sizeof( header ) ) ;
  ftruncate( fd, header.file_size ) ;
  pointer = mmap( 0, header.file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ) ;
  close( fd ) ;
  return Py_BuildValue("i", pointer);
}

static int pyshift_max_position(PyObject* self, PyObject* args)
{
  int ts;
  HEADERINFO * pHeader ;
  int iSize;
  if ( !PyArg_ParseTuple(args, "i", &ts) )
  {
    return -1;
  }
  pHeader = (HEADERINFO*)(ts);
  return pHeader->writepointer;
}

static int pyshift_min_position(PyObject* self, PyObject* args)
{
  int ts;
  HEADERINFO * pHeader ;
  int iSize;
  if ( !PyArg_ParseTuple(args, "i", &ts) )
  {
    return -1;
  }
  pHeader = (HEADERINFO*)(ts);
  return pHeader->writepointer - ( pHeader->writepointer % pHeader->data_size );
}


static PyObject* pyshift_close(PyObject* self, PyObject* args)
{
  int ts;
  HEADERINFO * pHeader ;
  int iSize;
  if ( !PyArg_ParseTuple(args, "i", &ts) )
  {
    return NULL;
  }
  pHeader = (HEADERINFO*)(ts);
  iSize = pHeader->file_size;
  if ( munmap((void*)ts, iSize) != 0 )
  {
    return NULL;
  }
  return Py_BuildValue("");
}


static PyObject* pyshift_write(PyObject* self, PyObject* args)
{
  int ts;
  int size;
  char * data;

  int remainingsize ;
  int writtensize ;
  int writepos;
  HEADERINFO * pHeader ;

  if ( !PyArg_ParseTuple(args, "is#", &ts, &data, &size) )
  {
    return NULL;
  }
  pHeader = (HEADERINFO*) ts ;
  remainingsize = size ;
  writtensize = 0 ;
  writepos = pHeader->writepointer % pHeader->data_size ;
  while ( remainingsize > ( pHeader->data_size - writepos ) )
  { 
    int writesize ;
    writesize = pHeader->data_size - writepos ;
    memcpy( (void*)ts + sizeof(HEADERINFO) + writepos, data+writtensize, writesize ) ;
    remainingsize -= writesize ;
    writtensize += writesize ;
    pHeader->writepointer += writesize ;
    // printf( "pyshift: Writepointer wrapped around at size: %d\n", 
pHeader->writepointer ) ;
    writepos = 0 ;
  }
  /* Now perform a final clean write of all data */
  memcpy( (void*)ts+sizeof(HEADERINFO)+writepos, data+writtensize, remainingsize ) ;
  pHeader->writepointer += remainingsize ;
  if ( msync( (void*)ts, pHeader->file_size, MS_SYNC ) == -1 )
  { 
    fprintf( stderr, "pyshift: Error in msync.\n" ) ;
  }
  return Py_BuildValue( "i", size ) ;
}

static char pyshift_init_docs[] = 
  "pyshift_init - intializes a timeshifting buffer\n";
static char pyshift_write_docs[] =
  "pyshift_write - writes the given bytes into the timeshifting buffer\n";
static char pyshift_close_docs[] =
  "pyshift_close - closes the timeshifting buffer\n";
static char pyshift_max_position_docs[] =
  "pyshift_max_position - returns the maximum position in the current buffer\n";
static char pyshift_min_position_docs[] =
  "pyshift_min_position - returns the minimum position in the current buffer\n";

static PyMethodDef pyshift_funcs[] = {
  { "pyshift_init", (PyCFunction)pyshift_init, METH_VARARGS, pyshift_init_docs },
  { "pyshift_write", (PyCFunction)pyshift_write, METH_VARARGS, pyshift_write_docs },
  { "pyshift_close", (PyCFunction)pyshift_close, METH_VARARGS, pyshift_close_docs },
  { "pyshift_min_position", (PyCFunction)pyshift_min_position, METH_VARARGS, 
pyshift_min_position_docs },
  { "pyshift_max_position", (PyCFunction)pyshift_max_position, METH_VARARGS, 
pyshift_max_position_docs },
  { NULL }
};

void initpyshift(void)
{
  Py_InitModule3("pyshift", pyshift_funcs, "Toy Level");
}

--- NEW FILE: pyshift_test.py ---
import pyshift
import os

TIMESHIFT_FILESIZE = 1024*1024*60 # 60MB
TIMESHIFT_CHUNKSIZE = 1024*1024   # 1 MB
TIMESHIFT_FILE = '/tmp/pyshift.test'

print "Initializing Timeshift at '%s'" % TIMESHIFT_FILE
timeshift = pyshift.pyshift_init(TIMESHIFT_FILE, TIMESHIFT_FILESIZE)
f = open('/dev/video0', 'rb')
#f = os.popen('mp1e')
while 1:
    pyshift.pyshift_write(timeshift,f.read(TIMESHIFT_CHUNKSIZE))
    print "%d Bytes transferred to Timeshift-Buffer" % TIMESHIFT_CHUNKSIZE
pyshift.pyshift_close(timeshift)
f.close()

--- NEW FILE: setup.py ---
from distutils.core import setup, Extension
setup(name='pyshift', ext_modules=[ Extension('pyshift', sources=['pyshift.c']) ])

--- NEW FILE: ts.h ---
#ifndef __ts__h
#define __ts__h

typedef struct tagHeaderInfo
{
  char freevots[12] ;
  int file_size ;
  int file_size2 ;
  int header_size ;
  int header_size2 ;
  int data_size ;
  int data_size2 ;
  int writepointer ;
  int writepointer2 ;
  char reserved[20] ;
} HEADERINFO ;

#define OVERRUN_DELTA 4*1024*1024


#endif

--- NEW FILE: tserrors.h ---
#ifndef __tserrors__h
#define __tserrors__h

const char * ERRORTEXT[] = { "OK", 
                             "ALREADY RUNNING",
                 "SYSTEM ERROR",
                             "NOT IMPLEMENTED",
                             "UNKNOWN COMMAND", 
                             "EOF FROM CLIENT",
                                 "OUT OF MEMORY",
                                 "SYNTAX ERROR"
                              };


const enum TSERROR {
        TS_OK, 
        TS_ALREADY_RUNNING, 
        TS_SYSTEM_ERROR,
        TS_NOT_IMPLEMENTED,
        TS_UNKNOWN_COMMAND,
        TS_CLIENT_EOF,
        TS_OUTOFMEM,
        TS_SYNTAX_ERROR
        } ERROR ;


#define TS_ERROR(x)                                     \
        x>=sizeof(ERRORTEXT)/sizeof(ERRORTEXT[0])?      \
        "Unknown Error":ERRORTEXT[x]


#endif




-------------------------------------------------------
This SF.net email is sponsored by:  Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to