MPEG2TransportStreamIndexFile.hh 3.9 KB

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