| 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 filter for converting a stream of MPEG PES packets to a MPEG-2 Transport Stream |
| 19 | // Implementation |
| 20 | |
| 21 | #include "MPEG2TransportStreamFromPESSource.hh" |
| 22 | |
| 23 | #define MAX_PES_PACKET_SIZE (6+65535) |
| 24 | |
| 25 | MPEG2TransportStreamFromPESSource* MPEG2TransportStreamFromPESSource |
| 26 | ::createNew(UsageEnvironment& env, MPEG1or2DemuxedElementaryStream* inputSource) { |
| 27 | return new MPEG2TransportStreamFromPESSource(env, inputSource); |
| 28 | } |
| 29 | |
| 30 | MPEG2TransportStreamFromPESSource |
| 31 | ::MPEG2TransportStreamFromPESSource(UsageEnvironment& env, |
| 32 | MPEG1or2DemuxedElementaryStream* inputSource) |
| 33 | : MPEG2TransportStreamMultiplexor(env), |
| 34 | fInputSource(inputSource) { |
| 35 | fInputBuffer = new unsigned char[MAX_PES_PACKET_SIZE]; |
| 36 | } |
| 37 | |
| 38 | MPEG2TransportStreamFromPESSource::~MPEG2TransportStreamFromPESSource() { |
| 39 | Medium::close(fInputSource); |
| 40 | delete[] fInputBuffer; |
| 41 | } |
| 42 | |
| 43 | void MPEG2TransportStreamFromPESSource::doStopGettingFrames() { |
| 44 | fInputSource->stopGettingFrames(); |
| 45 | } |
| 46 | |
| 47 | void MPEG2TransportStreamFromPESSource |
| 48 | ::awaitNewBuffer(unsigned char* /*oldBuffer*/) { |
| 49 | fInputSource->getNextFrame(fInputBuffer, MAX_PES_PACKET_SIZE, |
| 50 | afterGettingFrame, this, |
| 51 | FramedSource::handleClosure, this); |
| 52 | } |
| 53 | |
| 54 | void MPEG2TransportStreamFromPESSource |
| 55 | ::afterGettingFrame(void* clientData, unsigned frameSize, |
| 56 | unsigned numTruncatedBytes, |
| 57 | struct timeval presentationTime, |
| 58 | unsigned durationInMicroseconds) { |
| 59 | MPEG2TransportStreamFromPESSource* source |
| 60 | = (MPEG2TransportStreamFromPESSource*)clientData; |
| 61 | source->afterGettingFrame1(frameSize, numTruncatedBytes, |
| 62 | presentationTime, durationInMicroseconds); |
| 63 | } |
| 64 | |
| 65 | void MPEG2TransportStreamFromPESSource |
| 66 | ::afterGettingFrame1(unsigned frameSize, |
| 67 | unsigned /*numTruncatedBytes*/, |
| 68 | struct timeval /*presentationTime*/, |
| 69 | unsigned /*durationInMicroseconds*/) { |
| 70 | if (frameSize < 4) return; |
| 71 | |
| 72 | handleNewBuffer(fInputBuffer, frameSize, |
| 73 | fInputSource->mpegVersion(), fInputSource->lastSeenSCR()); |
| 74 | } |
| 75 | |