| 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 | // RTP sink for AC3 audio | 
|---|
| 19 | // Implementation | 
|---|
| 20 |  | 
|---|
| 21 | #include "AC3AudioRTPSink.hh" | 
|---|
| 22 |  | 
|---|
| 23 | AC3AudioRTPSink::AC3AudioRTPSink(UsageEnvironment& env, Groupsock* RTPgs, | 
|---|
| 24 | u_int8_t rtpPayloadFormat, | 
|---|
| 25 | u_int32_t rtpTimestampFrequency) | 
|---|
| 26 | : AudioRTPSink(env, RTPgs, rtpPayloadFormat, | 
|---|
| 27 | rtpTimestampFrequency, "AC3"), | 
|---|
| 28 | fTotNumFragmentsUsed(0) { | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | AC3AudioRTPSink::~AC3AudioRTPSink() { | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | AC3AudioRTPSink* | 
|---|
| 35 | AC3AudioRTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs, | 
|---|
| 36 | u_int8_t rtpPayloadFormat, | 
|---|
| 37 | u_int32_t rtpTimestampFrequency) { | 
|---|
| 38 | return new AC3AudioRTPSink(env, RTPgs, | 
|---|
| 39 | rtpPayloadFormat, rtpTimestampFrequency); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | Boolean AC3AudioRTPSink | 
|---|
| 43 | ::frameCanAppearAfterPacketStart(unsigned char const* /*frameStart*/, | 
|---|
| 44 | unsigned /*numBytesInFrame*/) const { | 
|---|
| 45 | // (For now) allow at most 1 frame in a single packet: | 
|---|
| 46 | return False; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void AC3AudioRTPSink | 
|---|
| 50 | ::doSpecialFrameHandling(unsigned fragmentationOffset, | 
|---|
| 51 | unsigned char* frameStart, | 
|---|
| 52 | unsigned numBytesInFrame, | 
|---|
| 53 | struct timeval framePresentationTime, | 
|---|
| 54 | unsigned numRemainingBytes) { | 
|---|
| 55 | // Set the 2-byte "payload header", as defined in RFC 4184. | 
|---|
| 56 | unsigned char [2]; | 
|---|
| 57 |  | 
|---|
| 58 | Boolean isFragment = numRemainingBytes > 0 || fragmentationOffset > 0; | 
|---|
| 59 | if (!isFragment) { | 
|---|
| 60 | headers[0] = 0; // One or more complete frames | 
|---|
| 61 | headers[1] = 1; // because we (for now) allow at most 1 frame per packet | 
|---|
| 62 | } else { | 
|---|
| 63 | if (fragmentationOffset > 0) { | 
|---|
| 64 | headers[0] = 3; // Fragment of frame other than initial fragment | 
|---|
| 65 | } else { | 
|---|
| 66 | // An initial fragment of the frame | 
|---|
| 67 | unsigned const totalFrameSize = fragmentationOffset + numBytesInFrame + numRemainingBytes; | 
|---|
| 68 | unsigned const fiveEighthsPoint = totalFrameSize/2 + totalFrameSize/8; | 
|---|
| 69 | headers[0] = numBytesInFrame >= fiveEighthsPoint ? 1 : 2; | 
|---|
| 70 |  | 
|---|
| 71 | // Because this outgoing packet will be full (because it's an initial fragment), we can compute how many total | 
|---|
| 72 | // fragments (and thus packets) will make up the complete AC-3 frame: | 
|---|
| 73 | fTotNumFragmentsUsed = (totalFrameSize + (numBytesInFrame-1))/numBytesInFrame; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | headers[1] = fTotNumFragmentsUsed; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | setSpecialHeaderBytes(headers, sizeof headers); | 
|---|
| 80 |  | 
|---|
| 81 | if (numRemainingBytes == 0) { | 
|---|
| 82 | // This packet contains the last (or only) fragment of the frame. | 
|---|
| 83 | // Set the RTP 'M' ('marker') bit: | 
|---|
| 84 | setMarkerBit(); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | // Important: Also call our base class's doSpecialFrameHandling(), | 
|---|
| 88 | // to set the packet's timestamp: | 
|---|
| 89 | MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset, | 
|---|
| 90 | frameStart, numBytesInFrame, | 
|---|
| 91 | framePresentationTime, | 
|---|
| 92 | numRemainingBytes); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | unsigned AC3AudioRTPSink::() const { | 
|---|
| 96 | return 2; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|