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)
28 log = Log::getInstance();
31 // FIXME find out max file path / name lengths
35 for(int i = 1; i < 1001; i++)
37 snprintf(fileName, 2047, "%s/%03i.vdr", rec->FileName(), i);
38 log->log("RecPlayer", Log::DEBUG, "FILENAME: %s", fileName);
39 file = fopen(fileName, "r");
42 segments[i] = new Segment();
43 segments[i]->start = totalLength;
45 fseek(file, 0, SEEK_END);
46 totalLength += ftell(file);
47 log->log("RecPlayer", Log::DEBUG, "File %i found, totalLength now %llu", i, totalLength);
48 segments[i]->end = totalLength;
60 RecPlayer::~RecPlayer()
62 log->log("RecPlayer", Log::DEBUG, "destructor");
64 while(segments[i++]) delete segments[i];
65 if (file) fclose(file);
68 int RecPlayer::openFile(int index)
70 if (file) fclose(file);
73 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index);
74 log->log("RecPlayer", Log::DEBUG, "openFile called for index %i string:%s", index, fileName);
76 file = fopen(fileName, "r");
79 log->log("RecPlayer", Log::DEBUG, "file failed to open");
87 ULLONG RecPlayer::getTotalLength()
92 unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsigned long amount)
94 if ((amount > totalLength) || (amount > 100000))
96 log->log("RecPlayer", Log::DEBUG, "Amount %lu requested and rejected", amount);
100 if (position >= totalLength)
102 log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!");
106 if ((position + amount) > totalLength)
108 log->log("RecPlayer", Log::DEBUG, "Client asked for some data past the end of recording, adjusting amount");
109 amount = totalLength - position;
112 // work out what block position is in
114 for(segmentNumber = 1; segmentNumber < 1000; segmentNumber++)
116 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
117 // position is in this block
120 // we could be seeking around
121 if (segmentNumber != fileOpen)
123 if (!openFile(segmentNumber)) return 0;
126 ULLONG currentPosition = position;
127 ULONG yetToGet = amount;
129 ULONG getFromThisSegment = 0;
136 // if(got) then we have already got some and we are back around
137 // advance the file pointer to the next file
138 if (!openFile(++segmentNumber)) return 0;
141 // is the request completely in this block?
142 if ((currentPosition + yetToGet) <= segments[segmentNumber]->end)
143 getFromThisSegment = yetToGet;
145 getFromThisSegment = segments[segmentNumber]->end - currentPosition;
147 filePosition = currentPosition - segments[segmentNumber]->start;
148 fseek(file, filePosition, SEEK_SET);
149 if (fread(&buffer[got], getFromThisSegment, 1, file) != 1) return 0; // umm, big problem.
151 got += getFromThisSegment;
152 currentPosition += getFromThisSegment;
153 yetToGet -= getFromThisSegment;
156 lastPosition = position;
160 ULLONG RecPlayer::getLastPosition()
165 cRecording* RecPlayer::getCurrentRecording()