2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "recplayer.h"
23 RecPlayer::RecPlayer(cRecording* rec)
30 // FIXME find out max file path / name lengths
34 for(int i = 1; i < 1001; i++)
36 snprintf(fileName, 2047, "%s/%03i.vdr", rec->FileName(), i);
37 printf("FILENAME: %s\n", fileName);
38 file = fopen(fileName, "r");
41 segments[i] = new Segment();
42 segments[i]->start = totalLength;
44 fseek(file, 0, SEEK_END);
45 totalLength += ftell(file);
46 printf("File %i found, totalLength now %llu\n", i, totalLength);
47 segments[i]->end = totalLength;
59 RecPlayer::~RecPlayer()
61 printf("RecPlayer destructor\n");
63 while(segments[i++]) delete segments[i];
64 if (file) fclose(file);
67 int RecPlayer::openFile(int index)
69 if (file) fclose(file);
72 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index);
73 printf("openFile called for index %i string:%s\n", index, fileName);
75 file = fopen(fileName, "r");
78 printf("file failed to open\n");
86 ULLONG RecPlayer::getTotalLength()
91 unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsigned long amount)
93 if ((amount > totalLength) || (amount > 100000))
95 printf("Amount %lu requested and rejected\n", amount);
99 if (position >= totalLength)
101 printf("Client asked for data starting past end of recording!\n");
105 if ((position + amount) > totalLength)
107 printf("Client asked for some data past the end of recording, adjusting amount\n");
108 amount = totalLength - position;
111 // work out what block position is in
113 for(segmentNumber = 1; segmentNumber < 1000; segmentNumber++)
115 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
116 // position is in this block
119 // we could be seeking around
120 if (segmentNumber != fileOpen)
122 if (!openFile(segmentNumber)) return 0;
125 ULLONG currentPosition = position;
126 ULONG yetToGet = amount;
128 ULONG getFromThisSegment = 0;
135 // if(got) then we have already got some and we are back around
136 // advance the file pointer to the next file
137 if (!openFile(++segmentNumber)) return 0;
140 // is the request completely in this block?
141 if ((currentPosition + yetToGet) <= segments[segmentNumber]->end)
142 getFromThisSegment = yetToGet;
144 getFromThisSegment = segments[segmentNumber]->end - currentPosition;
146 filePosition = currentPosition - segments[segmentNumber]->start;
147 fseek(file, filePosition, SEEK_SET);
148 if (fread(&buffer[got], getFromThisSegment, 1, file) != 1) return 0; // umm, big problem.
150 got += getFromThisSegment;
151 currentPosition += getFromThisSegment;
152 yetToGet -= getFromThisSegment;
155 lastPosition = position;
159 ULLONG RecPlayer::getLastPosition()
164 cRecording* RecPlayer::getCurrentRecording()