1 /**********
2 This library is free software; you can redistribute it and/or modify it under
3 the terms of the GNU Lesser General Public License as published by the
4 Free Software Foundation; either version 3 of the License, or (at your
5 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
15 **********/
16 // "liveMedia"
17 // Copyright (c) 1996-2020 Live Networks, Inc.  All rights reserved.
18 // A class that encapsulates MPEG-2 Transport Stream 'index files'/
19 // These index files are used to implement 'trick play' operations
20 // (seek-by-time, fast forward, reverse play) on Transport Stream files.
21 //
22 // C++ header
23 
24 #ifndef _MPEG2_TRANSPORT_STREAM_INDEX_FILE_HH
25 #define _MPEG2_TRANSPORT_STREAM_INDEX_FILE_HH
26 
27 #ifndef _MEDIA_HH
28 #include "Media.hh"
29 #endif
30 
31 #define INDEX_RECORD_SIZE 11
32 
33 class MPEG2TransportStreamIndexFile: public Medium {
34 public:
35   static MPEG2TransportStreamIndexFile* createNew(UsageEnvironment& env,
36 						  char const* indexFileName);
37 
38   virtual ~MPEG2TransportStreamIndexFile();
39 
40   // Functions that map between a playing time and a Transport packet number
41   // in the original Transport Stream file:
42 
43   void lookupTSPacketNumFromNPT(float& npt, unsigned long& tsPacketNumber,
44 			    unsigned long& indexRecordNumber);
45     // Looks up the Transport Stream Packet number corresponding to "npt".
46 	// (This may modify "npt" to a more exact value.)
47         // (We also return the index record number that we looked up.)
48 
49   void lookupPCRFromTSPacketNum(unsigned long& tsPacketNumber, Boolean reverseToPreviousCleanPoint,
50 				float& pcr, unsigned long& indexRecordNumber);
51     // Looks up the PCR timestamp for the transport packet "tsPacketNumber".
52 	// (Adjust "tsPacketNumber" only if "reverseToPreviousCleanPoint" is True.)
53         // (We also return the index record number that we looked up.)
54 
55   // Miscellaneous functions used to implement 'trick play':
56   Boolean readIndexRecordValues(unsigned long indexRecordNum,
57 				unsigned long& transportPacketNum, u_int8_t& offset,
58 				u_int8_t& size, float& pcr, u_int8_t& recordType);
59   float getPlayingDuration();
stopReading()60   void stopReading() { closeFid(); }
61 
62   int mpegVersion();
63       // returns the best guess for the version of MPEG being used for data within the underlying Transport Stream file.
64       // (1,2,4, or 5 (representing H.264).  0 means 'don't know' (usually because the index file is empty))
65 
66 private:
67   MPEG2TransportStreamIndexFile(UsageEnvironment& env, char const* indexFileName);
68 
69   Boolean openFid();
70   Boolean seekToIndexRecord(unsigned long indexRecordNumber);
71   Boolean readIndexRecord(unsigned long indexRecordNum); // into "fBuf"
72   Boolean readOneIndexRecord(unsigned long indexRecordNum); // closes "fFid" at end
73   void closeFid();
74 
recordTypeFromBuf()75   u_int8_t recordTypeFromBuf() { return fBuf[0]; }
offsetFromBuf()76   u_int8_t offsetFromBuf() { return fBuf[1]; }
sizeFromBuf()77   u_int8_t sizeFromBuf() { return fBuf[2]; }
78   float pcrFromBuf(); // after "fBuf" has been read
79   unsigned long tsPacketNumFromBuf();
80   void setMPEGVersionFromRecordType(u_int8_t recordType);
81 
82   Boolean rewindToCleanPoint(unsigned long&ixFound);
83       // used to implement "lookupTSPacketNumber()"
84 
85 private:
86   char* fFileName;
87   FILE* fFid; // used internally when reading from the file
88   int fMPEGVersion;
89   unsigned long fCurrentIndexRecordNum; // within "fFid"
90   float fCachedPCR;
91   unsigned long fCachedTSPacketNumber, fCachedIndexRecordNumber;
92   unsigned long fNumIndexRecords;
93   unsigned char fBuf[INDEX_RECORD_SIZE]; // used for reading index records from file
94 };
95 
96 #endif
97