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 | // MPEG4-GENERIC ("audio", "video", or "application") RTP stream sinks |
19 | // Implementation |
20 | |
21 | #include "MPEG4GenericRTPSink.hh" |
22 | #include "Locale.hh" |
23 | #include <ctype.h> // needed on some systems to define "tolower()" |
24 | |
25 | MPEG4GenericRTPSink |
26 | ::MPEG4GenericRTPSink(UsageEnvironment& env, Groupsock* RTPgs, |
27 | u_int8_t rtpPayloadFormat, |
28 | u_int32_t rtpTimestampFrequency, |
29 | char const* sdpMediaTypeString, |
30 | char const* mpeg4Mode, char const* configString, |
31 | unsigned numChannels) |
32 | : MultiFramedRTPSink(env, RTPgs, rtpPayloadFormat, |
33 | rtpTimestampFrequency, "MPEG4-GENERIC" , numChannels), |
34 | fSDPMediaTypeString(strDup(sdpMediaTypeString)), |
35 | fMPEG4Mode(strDup(mpeg4Mode)), fConfigString(strDup(configString)) { |
36 | // Check whether "mpeg4Mode" is one that we handle: |
37 | if (mpeg4Mode == NULL) { |
38 | env << "MPEG4GenericRTPSink error: NULL \"mpeg4Mode\" parameter\n" ; |
39 | } else { |
40 | // To ease comparison, convert "mpeg4Mode" to lower case: |
41 | size_t const len = strlen(mpeg4Mode) + 1; |
42 | char* m = new char[len]; |
43 | |
44 | Locale l("POSIX" ); |
45 | for (size_t i = 0; i < len; ++i) m[i] = tolower(mpeg4Mode[i]); |
46 | |
47 | if (strcmp(m, "aac-hbr" ) != 0) { |
48 | env << "MPEG4GenericRTPSink error: Unknown \"mpeg4Mode\" parameter: \"" << mpeg4Mode << "\"\n" ; |
49 | } |
50 | delete[] m; |
51 | } |
52 | |
53 | // Set up the "a=fmtp:" SDP line for this stream: |
54 | char const* fmtpFmt = |
55 | "a=fmtp:%d " |
56 | "streamtype=%d;profile-level-id=1;" |
57 | "mode=%s;sizelength=13;indexlength=3;indexdeltalength=3;" |
58 | "config=%s\r\n" ; |
59 | unsigned fmtpFmtSize = strlen(fmtpFmt) |
60 | + 3 /* max char len */ |
61 | + 3 /* max char len */ |
62 | + strlen(fMPEG4Mode) |
63 | + strlen(fConfigString); |
64 | char* fmtp = new char[fmtpFmtSize]; |
65 | sprintf(fmtp, fmtpFmt, |
66 | rtpPayloadType(), |
67 | strcmp(fSDPMediaTypeString, "video" ) == 0 ? 4 : 5, |
68 | fMPEG4Mode, |
69 | fConfigString); |
70 | fFmtpSDPLine = strDup(fmtp); |
71 | delete[] fmtp; |
72 | } |
73 | |
74 | MPEG4GenericRTPSink::~MPEG4GenericRTPSink() { |
75 | delete[] fFmtpSDPLine; |
76 | delete[] (char*)fConfigString; |
77 | delete[] (char*)fMPEG4Mode; |
78 | delete[] (char*)fSDPMediaTypeString; |
79 | } |
80 | |
81 | MPEG4GenericRTPSink* |
82 | MPEG4GenericRTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs, |
83 | u_int8_t rtpPayloadFormat, |
84 | u_int32_t rtpTimestampFrequency, |
85 | char const* sdpMediaTypeString, |
86 | char const* mpeg4Mode, |
87 | char const* configString, unsigned numChannels) { |
88 | return new MPEG4GenericRTPSink(env, RTPgs, rtpPayloadFormat, |
89 | rtpTimestampFrequency, |
90 | sdpMediaTypeString, mpeg4Mode, |
91 | configString, numChannels); |
92 | } |
93 | |
94 | Boolean MPEG4GenericRTPSink |
95 | ::frameCanAppearAfterPacketStart(unsigned char const* /*frameStart*/, |
96 | unsigned /*numBytesInFrame*/) const { |
97 | // (For now) allow at most 1 frame in a single packet: |
98 | return False; |
99 | } |
100 | |
101 | void MPEG4GenericRTPSink |
102 | ::doSpecialFrameHandling(unsigned fragmentationOffset, |
103 | unsigned char* frameStart, |
104 | unsigned numBytesInFrame, |
105 | struct timeval framePresentationTime, |
106 | unsigned numRemainingBytes) { |
107 | // Set the "AU Header Section". This is 4 bytes: 2 bytes for the |
108 | // initial "AU-headers-length" field, and 2 bytes for the first |
109 | // (and only) "AU Header": |
110 | unsigned fullFrameSize |
111 | = fragmentationOffset + numBytesInFrame + numRemainingBytes; |
112 | unsigned char [4]; |
113 | headers[0] = 0; headers[1] = 16 /* bits */; // AU-headers-length |
114 | headers[2] = fullFrameSize >> 5; headers[3] = (fullFrameSize&0x1F)<<3; |
115 | |
116 | setSpecialHeaderBytes(headers, sizeof headers); |
117 | |
118 | if (numRemainingBytes == 0) { |
119 | // This packet contains the last (or only) fragment of the frame. |
120 | // Set the RTP 'M' ('marker') bit: |
121 | setMarkerBit(); |
122 | } |
123 | |
124 | // Important: Also call our base class's doSpecialFrameHandling(), |
125 | // to set the packet's timestamp: |
126 | MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset, |
127 | frameStart, numBytesInFrame, |
128 | framePresentationTime, |
129 | numRemainingBytes); |
130 | } |
131 | |
132 | unsigned MPEG4GenericRTPSink::() const { |
133 | return 2 + 2; |
134 | } |
135 | |
136 | char const* MPEG4GenericRTPSink::sdpMediaType() const { |
137 | return fSDPMediaTypeString; |
138 | } |
139 | |
140 | char const* MPEG4GenericRTPSink::auxSDPLine() { |
141 | return fFmtpSDPLine; |
142 | } |
143 | |