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
34 indexFile = new cIndexFile(recording->FileName(), false);
35 if (!indexFile) log->log("RecPlayer", Log::ERR, "Failed to create indexfile!");
40 void RecPlayer::scan()
42 if (file) fclose(file);
47 while(segments[i++]) delete segments[i];
50 for(i = 1; i < 1000; i++)
52 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), i);
53 log->log("RecPlayer", Log::DEBUG, "FILENAME: %s", fileName);
54 file = fopen(fileName, "r");
57 segments[i] = new Segment();
58 segments[i]->start = totalLength;
59 fseek(file, 0, SEEK_END);
60 totalLength += ftell(file);
61 log->log("RecPlayer", Log::DEBUG, "File %i found, totalLength now %llu", i, totalLength);
62 segments[i]->end = totalLength;
69 RecPlayer::~RecPlayer()
71 log->log("RecPlayer", Log::DEBUG, "destructor");
73 while(segments[i++]) delete segments[i];
74 if (file) fclose(file);
77 int RecPlayer::openFile(int index)
79 if (file) fclose(file);
82 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index);
83 log->log("RecPlayer", Log::DEBUG, "openFile called for index %i string:%s", index, fileName);
85 file = fopen(fileName, "r");
88 log->log("RecPlayer", Log::DEBUG, "file failed to open");
96 ULLONG RecPlayer::getTotalLength()
101 unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsigned long amount)
103 if ((amount > totalLength) || (amount > 500000))
105 log->log("RecPlayer", Log::DEBUG, "Amount %lu requested and rejected", amount);
109 if (position >= totalLength)
111 log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!");
115 if ((position + amount) > totalLength)
117 log->log("RecPlayer", Log::DEBUG, "Client asked for some data past the end of recording, adjusting amount");
118 amount = totalLength - position;
121 // work out what block position is in
123 for(segmentNumber = 1; segmentNumber < 1000; segmentNumber++)
125 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
126 // position is in this block
129 // we could be seeking around
130 if (segmentNumber != fileOpen)
132 if (!openFile(segmentNumber)) return 0;
135 ULLONG currentPosition = position;
136 ULONG yetToGet = amount;
138 ULONG getFromThisSegment = 0;
145 // if(got) then we have already got some and we are back around
146 // advance the file pointer to the next file
147 if (!openFile(++segmentNumber)) return 0;
150 // is the request completely in this block?
151 if ((currentPosition + yetToGet) <= segments[segmentNumber]->end)
152 getFromThisSegment = yetToGet;
154 getFromThisSegment = segments[segmentNumber]->end - currentPosition;
156 filePosition = currentPosition - segments[segmentNumber]->start;
157 fseek(file, filePosition, SEEK_SET);
158 if (fread(&buffer[got], getFromThisSegment, 1, file) != 1) return 0; // umm, big problem.
160 got += getFromThisSegment;
161 currentPosition += getFromThisSegment;
162 yetToGet -= getFromThisSegment;
165 lastPosition = position;
169 ULLONG RecPlayer::getLastPosition()
174 cRecording* RecPlayer::getCurrentRecording()
179 ULLONG RecPlayer::positionFromFrameNumber(ULONG frameNumber)
181 if (!indexFile) return 0;
188 if (!indexFile->Get((int)frameNumber, &retFileNumber, &retFileOffset, &retPicType, &retLength))
193 // log->log("RecPlayer", Log::DEBUG, "FN: %u FO: %i", retFileNumber, retFileOffset);
194 if (!segments[retFileNumber]) return 0;
195 ULLONG position = segments[retFileNumber]->start + retFileOffset;
196 // log->log("RecPlayer", Log::DEBUG, "Pos: %llu", position);
201 ULONG RecPlayer::frameNumberFromPosition(ULLONG position)
203 if (!indexFile) return 0;
206 for(segmentNumber = 1; segmentNumber < 255; segmentNumber++)
208 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
209 // position is in this block
211 ULONG askposition = position - segments[segmentNumber]->start;
212 return indexFile->Get((int)segmentNumber, askposition);