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)
25 log = Log::getInstance();
30 for(int i = 1; i < 1000; i++) segments[i] = NULL;
32 // FIXME find out max file path / name lengths
37 void RecPlayer::scan()
39 if (file) fclose(file);
44 while(segments[i++]) delete segments[i];
47 for(i = 1; i < 1000; i++)
49 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), i);
50 log->log("RecPlayer", Log::DEBUG, "FILENAME: %s", fileName);
51 file = fopen(fileName, "r");
54 segments[i] = new Segment();
55 segments[i]->start = totalLength;
56 fseek(file, 0, SEEK_END);
57 totalLength += ftell(file);
58 log->log("RecPlayer", Log::DEBUG, "File %i found, totalLength now %llu", i, totalLength);
59 segments[i]->end = totalLength;
66 RecPlayer::~RecPlayer()
68 log->log("RecPlayer", Log::DEBUG, "destructor");
70 while(segments[i++]) delete segments[i];
71 if (file) fclose(file);
74 int RecPlayer::openFile(int index)
76 if (file) fclose(file);
79 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index);
80 log->log("RecPlayer", Log::DEBUG, "openFile called for index %i string:%s", index, fileName);
82 file = fopen(fileName, "r");
85 log->log("RecPlayer", Log::DEBUG, "file failed to open");
93 ULLONG RecPlayer::getTotalLength()
98 unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsigned long amount)
100 if ((amount > totalLength) || (amount > 500000))
102 log->log("RecPlayer", Log::DEBUG, "Amount %lu requested and rejected", amount);
106 if (position >= totalLength)
108 log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!");
112 if ((position + amount) > totalLength)
114 log->log("RecPlayer", Log::DEBUG, "Client asked for some data past the end of recording, adjusting amount");
115 amount = totalLength - position;
118 // work out what block position is in
120 for(segmentNumber = 1; segmentNumber < 1000; segmentNumber++)
122 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
123 // position is in this block
126 // we could be seeking around
127 if (segmentNumber != fileOpen)
129 if (!openFile(segmentNumber)) return 0;
132 ULLONG currentPosition = position;
133 ULONG yetToGet = amount;
135 ULONG getFromThisSegment = 0;
142 // if(got) then we have already got some and we are back around
143 // advance the file pointer to the next file
144 if (!openFile(++segmentNumber)) return 0;
147 // is the request completely in this block?
148 if ((currentPosition + yetToGet) <= segments[segmentNumber]->end)
149 getFromThisSegment = yetToGet;
151 getFromThisSegment = segments[segmentNumber]->end - currentPosition;
153 filePosition = currentPosition - segments[segmentNumber]->start;
154 fseek(file, filePosition, SEEK_SET);
155 if (fread(&buffer[got], getFromThisSegment, 1, file) != 1) return 0; // umm, big problem.
157 got += getFromThisSegment;
158 currentPosition += getFromThisSegment;
159 yetToGet -= getFromThisSegment;
162 lastPosition = position;
166 ULLONG RecPlayer::getLastPosition()
171 cRecording* RecPlayer::getCurrentRecording()