| 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 | // Collects a stream of incoming MPEG Transport Stream packets into | 
|---|
| 19 | //  a chunk sufficiently large to send in a single outgoing (RTP or UDP) packet. | 
|---|
| 20 | // Implementation | 
|---|
| 21 |  | 
|---|
| 22 | #include "MPEG2TransportStreamAccumulator.hh" | 
|---|
| 23 |  | 
|---|
| 24 | MPEG2TransportStreamAccumulator* | 
|---|
| 25 | MPEG2TransportStreamAccumulator::createNew(UsageEnvironment& env, | 
|---|
| 26 | FramedSource* inputSource, unsigned maxPacketSize) { | 
|---|
| 27 | return new MPEG2TransportStreamAccumulator(env, inputSource, maxPacketSize); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | #ifndef TRANSPORT_PACKET_SIZE | 
|---|
| 31 | #define TRANSPORT_PACKET_SIZE 188 | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | MPEG2TransportStreamAccumulator | 
|---|
| 35 | ::MPEG2TransportStreamAccumulator(UsageEnvironment& env, | 
|---|
| 36 | FramedSource* inputSource, unsigned maxPacketSize) | 
|---|
| 37 | : FramedFilter(env, inputSource), | 
|---|
| 38 | fDesiredPacketSize(maxPacketSize < TRANSPORT_PACKET_SIZE ? TRANSPORT_PACKET_SIZE : (maxPacketSize/TRANSPORT_PACKET_SIZE)), | 
|---|
| 39 | fNumBytesGathered(0) { | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | MPEG2TransportStreamAccumulator::~MPEG2TransportStreamAccumulator() { | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void MPEG2TransportStreamAccumulator::doGetNextFrame() { | 
|---|
| 46 | if (fNumBytesGathered >= fDesiredPacketSize) { | 
|---|
| 47 | // Complete the delivery to the client: | 
|---|
| 48 | fFrameSize = fNumBytesGathered; | 
|---|
| 49 | fNumBytesGathered = 0; | 
|---|
| 50 | afterGetting(this); | 
|---|
| 51 | } else { | 
|---|
| 52 | // Ask for more data (delivered directly to the client's buffer); | 
|---|
| 53 | fInputSource->getNextFrame(fTo, fMaxSize, afterGettingFrame, this, | 
|---|
| 54 | FramedSource::handleClosure, this); | 
|---|
| 55 | } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void MPEG2TransportStreamAccumulator | 
|---|
| 59 | ::afterGettingFrame(void* clientData, unsigned frameSize, | 
|---|
| 60 | unsigned numTruncatedBytes, | 
|---|
| 61 | struct timeval presentationTime, | 
|---|
| 62 | unsigned durationInMicroseconds) { | 
|---|
| 63 | MPEG2TransportStreamAccumulator* accumulator | 
|---|
| 64 | = (MPEG2TransportStreamAccumulator*)clientData; | 
|---|
| 65 | accumulator->afterGettingFrame1(frameSize, numTruncatedBytes, | 
|---|
| 66 | presentationTime, durationInMicroseconds); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void MPEG2TransportStreamAccumulator | 
|---|
| 70 | ::afterGettingFrame1(unsigned frameSize, | 
|---|
| 71 | unsigned numTruncatedBytes, | 
|---|
| 72 | struct timeval presentationTime, | 
|---|
| 73 | unsigned durationInMicroseconds) { | 
|---|
| 74 | if (fNumBytesGathered == 0) { // this is the first frame of the new chunk | 
|---|
| 75 | fPresentationTime = presentationTime; | 
|---|
| 76 | fDurationInMicroseconds = 0; | 
|---|
| 77 | } | 
|---|
| 78 | fNumBytesGathered += frameSize; | 
|---|
| 79 | fTo += frameSize; | 
|---|
| 80 | fMaxSize -= frameSize; | 
|---|
| 81 | fDurationInMicroseconds += durationInMicroseconds; | 
|---|
| 82 |  | 
|---|
| 83 | // Try again to complete delivery: | 
|---|
| 84 | doGetNextFrame(); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|