| 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 Theora video |
| 19 | // Implementation |
| 20 | |
| 21 | #include "TheoraVideoRTPSink.hh" |
| 22 | #include "Base64.hh" |
| 23 | #include "VorbisAudioRTPSource.hh" // for parseVorbisOrTheoraConfigStr() |
| 24 | #include "VorbisAudioRTPSink.hh" // for generateVorbisOrTheoraConfigStr() |
| 25 | |
| 26 | TheoraVideoRTPSink* TheoraVideoRTPSink |
| 27 | ::createNew(UsageEnvironment& env, Groupsock* RTPgs, u_int8_t rtpPayloadFormat, |
| 28 | u_int8_t* , unsigned , |
| 29 | u_int8_t* , unsigned , |
| 30 | u_int8_t* , unsigned , |
| 31 | u_int32_t identField) { |
| 32 | return new TheoraVideoRTPSink(env, RTPgs, |
| 33 | rtpPayloadFormat, |
| 34 | identificationHeader, identificationHeaderSize, |
| 35 | commentHeader, commentHeaderSize, |
| 36 | setupHeader, setupHeaderSize, identField); |
| 37 | } |
| 38 | |
| 39 | TheoraVideoRTPSink* TheoraVideoRTPSink |
| 40 | ::createNew(UsageEnvironment& env, Groupsock* RTPgs, u_int8_t rtpPayloadFormat, |
| 41 | char const* configStr) { |
| 42 | // Begin by decoding and unpacking the configuration string: |
| 43 | u_int8_t* ; unsigned ; |
| 44 | u_int8_t* ; unsigned ; |
| 45 | u_int8_t* ; unsigned ; |
| 46 | u_int32_t identField; |
| 47 | |
| 48 | parseVorbisOrTheoraConfigStr(configStr, |
| 49 | identificationHeader, identificationHeaderSize, |
| 50 | commentHeader, commentHeaderSize, |
| 51 | setupHeader, setupHeaderSize, |
| 52 | identField); |
| 53 | |
| 54 | TheoraVideoRTPSink* resultSink |
| 55 | = new TheoraVideoRTPSink(env, RTPgs, rtpPayloadFormat, |
| 56 | identificationHeader, identificationHeaderSize, |
| 57 | commentHeader, commentHeaderSize, |
| 58 | setupHeader, setupHeaderSize, |
| 59 | identField); |
| 60 | delete[] identificationHeader; delete[] commentHeader; delete[] setupHeader; |
| 61 | |
| 62 | return resultSink; |
| 63 | } |
| 64 | |
| 65 | TheoraVideoRTPSink |
| 66 | ::TheoraVideoRTPSink(UsageEnvironment& env, Groupsock* RTPgs, u_int8_t rtpPayloadFormat, |
| 67 | u_int8_t* , unsigned , |
| 68 | u_int8_t* , unsigned , |
| 69 | u_int8_t* , unsigned , |
| 70 | u_int32_t identField) |
| 71 | : VideoRTPSink(env, RTPgs, rtpPayloadFormat, 90000, "THEORA" ), |
| 72 | fIdent(identField), fFmtpSDPLine(NULL) { |
| 73 | static const char *pf_to_str[] = { |
| 74 | "YCbCr-4:2:0" , |
| 75 | "Reserved" , |
| 76 | "YCbCr-4:2:2" , |
| 77 | "YCbCr-4:4:4" , |
| 78 | }; |
| 79 | |
| 80 | unsigned width = 1280; // default value |
| 81 | unsigned height = 720; // default value |
| 82 | unsigned pf = 0; // default value |
| 83 | if (identificationHeaderSize >= 42) { |
| 84 | // Parse this header to get the "width", "height", "pf" (pixel format), and |
| 85 | // 'nominal bitrate' parameters: |
| 86 | u_int8_t* p = identificationHeader; // alias |
| 87 | width = (p[14]<<16)|(p[15]<<8)|p[16]; |
| 88 | height = (p[17]<<16)|(p[18]<<8)|p[19]; |
| 89 | pf = (p[41]&0x18)>>3; |
| 90 | unsigned nominalBitrate = (p[37]<<16)|(p[38]<<8)|p[39]; |
| 91 | if (nominalBitrate > 0) estimatedBitrate() = nominalBitrate/1000; |
| 92 | } |
| 93 | |
| 94 | // Generate a 'config' string from the supplied configuration headers: |
| 95 | char* |
| 96 | = generateVorbisOrTheoraConfigStr(identificationHeader, identificationHeaderSize, |
| 97 | commentHeader, commentHeaderSize, |
| 98 | setupHeader, setupHeaderSize, |
| 99 | identField); |
| 100 | if (base64PackedHeaders == NULL) return; |
| 101 | |
| 102 | // Then use this 'config' string to construct our "a=fmtp:" SDP line: |
| 103 | unsigned fmtpSDPLineMaxSize = 200 + strlen(base64PackedHeaders);// 200 => more than enough space |
| 104 | fFmtpSDPLine = new char[fmtpSDPLineMaxSize]; |
| 105 | sprintf(fFmtpSDPLine, "a=fmtp:%d sampling=%s;width=%u;height=%u;delivery-method=out_band/rtsp;configuration=%s\r\n" , rtpPayloadType(), pf_to_str[pf], width, height, base64PackedHeaders); |
| 106 | delete[] base64PackedHeaders; |
| 107 | } |
| 108 | |
| 109 | TheoraVideoRTPSink::~TheoraVideoRTPSink() { |
| 110 | delete[] fFmtpSDPLine; |
| 111 | } |
| 112 | |
| 113 | char const* TheoraVideoRTPSink::auxSDPLine() { |
| 114 | return fFmtpSDPLine; |
| 115 | } |
| 116 | |
| 117 | void TheoraVideoRTPSink |
| 118 | ::doSpecialFrameHandling(unsigned fragmentationOffset, |
| 119 | unsigned char* frameStart, |
| 120 | unsigned numBytesInFrame, |
| 121 | struct timeval framePresentationTime, |
| 122 | unsigned numRemainingBytes) { |
| 123 | // Set the 4-byte "payload header", as defined in http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt |
| 124 | u_int8_t [6]; |
| 125 | |
| 126 | // The three bytes of the header are our "Ident": |
| 127 | header[0] = fIdent>>16; header[1] = fIdent>>8; header[2] = fIdent; |
| 128 | |
| 129 | // The final byte contains the "F", "TDT", and "numPkts" fields: |
| 130 | u_int8_t F; // Fragment type |
| 131 | if (numRemainingBytes > 0) { |
| 132 | if (fragmentationOffset > 0) { |
| 133 | F = 2<<6; // continuation fragment |
| 134 | } else { |
| 135 | F = 1<<6; // start fragment |
| 136 | } |
| 137 | } else { |
| 138 | if (fragmentationOffset > 0) { |
| 139 | F = 3<<6; // end fragment |
| 140 | } else { |
| 141 | F = 0<<6; // not fragmented |
| 142 | } |
| 143 | } |
| 144 | u_int8_t const TDT = 0<<4; // Theora Data Type (always a "Raw Theora payload") |
| 145 | u_int8_t numPkts = F == 0 ? (numFramesUsedSoFar() + 1): 0; // set to 0 when we're a fragment |
| 146 | header[3] = F|TDT|numPkts; |
| 147 | |
| 148 | // There's also a 2-byte 'frame-specific' header: The length of the |
| 149 | // Theora data: |
| 150 | header[4] = numBytesInFrame >>8; |
| 151 | header[5] = numBytesInFrame; |
| 152 | setSpecialHeaderBytes(header, sizeof(header)); |
| 153 | |
| 154 | if (numRemainingBytes == 0) { |
| 155 | // This packet contains the last (or only) fragment of the frame. |
| 156 | // Set the RTP 'M' ('marker') bit: |
| 157 | setMarkerBit(); |
| 158 | } |
| 159 | |
| 160 | // Important: Also call our base class's doSpecialFrameHandling(), |
| 161 | // to set the packet's timestamp: |
| 162 | MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset, |
| 163 | frameStart, numBytesInFrame, |
| 164 | framePresentationTime, |
| 165 | numRemainingBytes); |
| 166 | } |
| 167 | |
| 168 | Boolean TheoraVideoRTPSink::frameCanAppearAfterPacketStart(unsigned char const* /*frameStart*/, |
| 169 | unsigned /*numBytesInFrame*/) const { |
| 170 | // Only one frame per packet: |
| 171 | return False; |
| 172 | } |
| 173 | |
| 174 | unsigned TheoraVideoRTPSink::() const { |
| 175 | return 6; |
| 176 | } |
| 177 | |