00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef HAVE_LIBDVDNAV
00037
00038 #include <stdint.h>
00039
00040 #include <sys/types.h>
00041 #include <dvdnav/dvdnav.h>
00042 #include <sys/stat.h>
00043 #include <unistd.h>
00044 #include <string.h>
00045 #include <stdio.h>
00046 #include "server.h"
00047 #include "common.h"
00048 #include "dvd_io_handler.h"
00049
00050 using namespace zmm;
00051 using namespace mxml;
00052
00053 DVDIOHandler::DVDIOHandler(String dvdname, int track, int chapter,
00054 int audio_stream_id) : IOHandler()
00055 {
00056 this->dvdname = dvdname;
00057 small_buffer = (unsigned char *)MALLOC(DVD_VIDEO_LB_LEN);
00058 if (small_buffer == NULL)
00059 throw _Exception(_("Could not allocate memory for DVD small databuffer!"));
00060 small_buffer[0] = '\0';
00061 small_buffer_pos = NULL;
00062 last_read = false;
00063 dvd = Ref<DVDNavReader>(new DVDNavReader(dvdname));
00064 dvd->selectPGC(track, chapter);
00065 }
00066
00067 void DVDIOHandler::open(IN enum UpnpOpenFileMode mode)
00068 {
00069 if (mode != UPNP_READ)
00070 throw _Exception(_("DVDIOHandler::open: write not supported!"));
00071 }
00072
00073 int DVDIOHandler::read(OUT char *buf, IN size_t length)
00074 {
00075
00076 char *pbuf = buf;
00077 size_t count = 0;
00078
00079 if (last_read)
00080 return 0;
00081
00082
00083
00084
00085
00086 if (small_buffer_pos != NULL)
00087 {
00088 size_t rest = (small_buffer + DVD_VIDEO_LB_LEN) - small_buffer_pos;
00089 if (rest >= length)
00090 {
00091 memcpy(pbuf, small_buffer_pos, length);
00092 small_buffer_pos = small_buffer_pos + length;
00093
00094 if (rest == length)
00095 small_buffer_pos = NULL;
00096
00097 return (int)length;
00098 }
00099 else if (rest < length)
00100 {
00101 memcpy(pbuf, small_buffer_pos, rest);
00102 pbuf = pbuf + rest;
00103 count = count + rest;
00104 length = length - rest;
00105 small_buffer_pos = NULL;
00106 }
00107 }
00108
00109 if ((length) < DVD_VIDEO_LB_LEN)
00110 {
00111 size_t bytes = dvd->readSector((unsigned char *)small_buffer, DVD_VIDEO_LB_LEN);
00112 if (bytes == 0)
00113 {
00114 last_read = true;
00115 return count;
00116 }
00117 else if (bytes < 0)
00118 return bytes;
00119 else
00120 {
00121 small_buffer_pos = small_buffer;
00122
00123 memcpy(pbuf, small_buffer_pos, length);
00124 count = count + length;
00125 small_buffer_pos = small_buffer_pos + length;
00126 return (int)count;
00127 }
00128 }
00129 else
00130 {
00131 int ret = dvd->readSector((unsigned char *)pbuf, length);
00132 if (ret == 0)
00133 {
00134 last_read = true;
00135 }
00136
00137 if (ret >= 0)
00138 ret = count + ret;
00139
00140 return ret;
00141 }
00142
00143 return -1;
00144 }
00145
00146 int DVDIOHandler::write(IN char *buf, IN size_t length)
00147 {
00148 throw _Exception(_("DVD write is not possible"));
00149 }
00150
00151 void DVDIOHandler::seek(IN off_t offset, IN int whence)
00152 {
00153 }
00154
00155 void DVDIOHandler::close()
00156 {
00157 dvd = nil;
00158 small_buffer_pos = NULL;
00159 }
00160
00161 off_t DVDIOHandler::length()
00162 {
00163 return -1;
00164 }
00165
00166 DVDIOHandler::~DVDIOHandler()
00167 {
00168 if (small_buffer)
00169 FREE(small_buffer);
00170 }
00171
00172 #endif//HAVE_LIBDVDNAV