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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "recplayer.h"
23 #define _XOPEN_SOURCE 600
26 RecPlayer::RecPlayer(cRecording* rec)
28 log = Log::getInstance();
33 for(int i = 1; i < 1000; i++) segments[i] = NULL;
35 // FIXME find out max file path / name lengths
37 indexFile = new cIndexFile(recording->FileName(), false);
38 if (!indexFile) log->log("RecPlayer", Log::ERR, "Failed to create indexfile!");
43 void RecPlayer::scan()
45 if (file) fclose(file);
51 while(segments[i++]) delete segments[i];
54 for(i = 1; i < 1000; i++)
56 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), i);
57 log->log("RecPlayer", Log::DEBUG, "FILENAME: %s", fileName);
58 file = fopen(fileName, "r");
61 segments[i] = new Segment();
62 segments[i]->start = totalLength;
63 fseek(file, 0, SEEK_END);
64 totalLength += ftell(file);
65 totalFrames = indexFile->Last();
66 log->log("RecPlayer", Log::DEBUG, "File %i found, totalLength now %llu, numFrames = %lu", i, totalLength, totalFrames);
67 segments[i]->end = totalLength;
74 RecPlayer::~RecPlayer()
76 log->log("RecPlayer", Log::DEBUG, "destructor");
78 while(segments[i++]) delete segments[i];
79 if (file) fclose(file);
82 int RecPlayer::openFile(int index)
84 if (file) fclose(file);
87 snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index);
88 log->log("RecPlayer", Log::DEBUG, "openFile called for index %i string:%s", index, fileName);
90 file = fopen(fileName, "r");
93 log->log("RecPlayer", Log::DEBUG, "file failed to open");
101 ULLONG RecPlayer::getLengthBytes()
106 ULONG RecPlayer::getLengthFrames()
111 unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsigned long amount)
113 if ((amount > totalLength) || (amount > 500000))
115 log->log("RecPlayer", Log::DEBUG, "Amount %lu requested and rejected", amount);
119 if (position >= totalLength)
121 log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!");
125 if ((position + amount) > totalLength)
127 log->log("RecPlayer", Log::DEBUG, "Client asked for some data past the end of recording, adjusting amount");
128 amount = totalLength - position;
131 // work out what block position is in
133 for(segmentNumber = 1; segmentNumber < 1000; segmentNumber++)
135 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
136 // position is in this block
139 // we could be seeking around
140 if (segmentNumber != fileOpen)
142 if (!openFile(segmentNumber)) return 0;
145 ULLONG currentPosition = position;
146 ULONG yetToGet = amount;
148 ULONG getFromThisSegment = 0;
155 // if(got) then we have already got some and we are back around
156 // advance the file pointer to the next file
157 if (!openFile(++segmentNumber)) return 0;
160 // is the request completely in this block?
161 if ((currentPosition + yetToGet) <= segments[segmentNumber]->end)
162 getFromThisSegment = yetToGet;
164 getFromThisSegment = segments[segmentNumber]->end - currentPosition;
166 filePosition = currentPosition - segments[segmentNumber]->start;
167 fseek(file, filePosition, SEEK_SET);
168 if (fread(&buffer[got], getFromThisSegment, 1, file) != 1) return 0; // umm, big problem.
170 // Tell linux not to bother keeping the data in the FS cache
171 posix_fadvise(file->_fileno, filePosition, getFromThisSegment, POSIX_FADV_DONTNEED);
173 got += getFromThisSegment;
174 currentPosition += getFromThisSegment;
175 yetToGet -= getFromThisSegment;
178 lastPosition = position;
182 ULLONG RecPlayer::getLastPosition()
187 cRecording* RecPlayer::getCurrentRecording()
192 ULLONG RecPlayer::positionFromFrameNumber(ULONG frameNumber)
194 if (!indexFile) return 0;
201 if (!indexFile->Get((int)frameNumber, &retFileNumber, &retFileOffset, &retPicType, &retLength))
206 // log->log("RecPlayer", Log::DEBUG, "FN: %u FO: %i", retFileNumber, retFileOffset);
207 if (!segments[retFileNumber]) return 0;
208 ULLONG position = segments[retFileNumber]->start + retFileOffset;
209 // log->log("RecPlayer", Log::DEBUG, "Pos: %llu", position);
214 ULONG RecPlayer::frameNumberFromPosition(ULLONG position)
216 if (!indexFile) return 0;
218 if (position >= totalLength)
220 log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!");
225 for(segmentNumber = 1; segmentNumber < 255; segmentNumber++)
227 if ((position >= segments[segmentNumber]->start) && (position < segments[segmentNumber]->end)) break;
228 // position is in this block
230 ULONG askposition = position - segments[segmentNumber]->start;
231 return indexFile->Get((int)segmentNumber, askposition);
236 bool RecPlayer::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
241 if (!indexFile) return false;
247 int indexReturnFrameNumber;
249 indexReturnFrameNumber = (ULONG)indexFile->GetNextIFrame(frameNumber, (direction==1 ? true : false), &waste1, &waste2, &iframeLength);
250 log->log("RecPlayer", Log::DEBUG, "GNIF input framenumber:%lu, direction=%lu, output:framenumber=%i, framelength=%i", frameNumber, direction, indexReturnFrameNumber, iframeLength);
252 if (indexReturnFrameNumber == -1) return false;
254 *rfilePosition = positionFromFrameNumber(indexReturnFrameNumber);
255 *rframeNumber = (ULONG)indexReturnFrameNumber;
256 *rframeLength = (ULONG)iframeLength;