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 JPEG video (RFC 2435) |
19 | // Implementation |
20 | |
21 | #include "JPEGVideoRTPSink.hh" |
22 | #include "JPEGVideoSource.hh" |
23 | |
24 | JPEGVideoRTPSink |
25 | ::JPEGVideoRTPSink(UsageEnvironment& env, Groupsock* RTPgs) |
26 | : VideoRTPSink(env, RTPgs, 26, 90000, "JPEG" ) { |
27 | } |
28 | |
29 | JPEGVideoRTPSink::~JPEGVideoRTPSink() { |
30 | } |
31 | |
32 | JPEGVideoRTPSink* |
33 | JPEGVideoRTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs) { |
34 | return new JPEGVideoRTPSink(env, RTPgs); |
35 | } |
36 | |
37 | Boolean JPEGVideoRTPSink::sourceIsCompatibleWithUs(MediaSource& source) { |
38 | return source.isJPEGVideoSource(); |
39 | } |
40 | |
41 | Boolean JPEGVideoRTPSink |
42 | ::frameCanAppearAfterPacketStart(unsigned char const* /*frameStart*/, |
43 | unsigned /*numBytesInFrame*/) const { |
44 | // A packet can contain only one frame |
45 | return False; |
46 | } |
47 | |
48 | void JPEGVideoRTPSink |
49 | ::doSpecialFrameHandling(unsigned fragmentationOffset, |
50 | unsigned char* /*frameStart*/, |
51 | unsigned /*numBytesInFrame*/, |
52 | struct timeval framePresentationTime, |
53 | unsigned numRemainingBytes) { |
54 | // Our source is known to be a JPEGVideoSource |
55 | JPEGVideoSource* source = (JPEGVideoSource*)fSource; |
56 | if (source == NULL) return; // sanity check |
57 | |
58 | u_int8_t mainJPEGHeader[8]; // the special header |
59 | u_int8_t const type = source->type(); |
60 | |
61 | mainJPEGHeader[0] = 0; // Type-specific |
62 | mainJPEGHeader[1] = fragmentationOffset >> 16; |
63 | mainJPEGHeader[2] = fragmentationOffset >> 8; |
64 | mainJPEGHeader[3] = fragmentationOffset; |
65 | mainJPEGHeader[4] = type; |
66 | mainJPEGHeader[5] = source->qFactor(); |
67 | mainJPEGHeader[6] = source->width(); |
68 | mainJPEGHeader[7] = source->height(); |
69 | setSpecialHeaderBytes(mainJPEGHeader, sizeof mainJPEGHeader); |
70 | |
71 | unsigned = 0; // by default |
72 | if (type >= 64 && type <= 127) { |
73 | // There is also a Restart Marker Header: |
74 | restartMarkerHeaderSize = 4; |
75 | u_int16_t const restartInterval = source->restartInterval(); // should be non-zero |
76 | |
77 | u_int8_t [4]; |
78 | restartMarkerHeader[0] = restartInterval>>8; |
79 | restartMarkerHeader[1] = restartInterval&0xFF; |
80 | restartMarkerHeader[2] = restartMarkerHeader[3] = 0xFF; // F=L=1; Restart Count = 0x3FFF |
81 | |
82 | setSpecialHeaderBytes(restartMarkerHeader, restartMarkerHeaderSize, |
83 | sizeof mainJPEGHeader/* start position */); |
84 | } |
85 | |
86 | if (fragmentationOffset == 0 && source->qFactor() >= 128) { |
87 | // There is also a Quantization Header: |
88 | u_int8_t precision; |
89 | u_int16_t length; |
90 | u_int8_t const* quantizationTables |
91 | = source->quantizationTables(precision, length); |
92 | |
93 | unsigned const = 4 + length; |
94 | u_int8_t* = new u_int8_t[quantizationHeaderSize]; |
95 | |
96 | quantizationHeader[0] = 0; // MBZ |
97 | quantizationHeader[1] = precision; |
98 | quantizationHeader[2] = length >> 8; |
99 | quantizationHeader[3] = length&0xFF; |
100 | if (quantizationTables != NULL) { // sanity check |
101 | for (u_int16_t i = 0; i < length; ++i) { |
102 | quantizationHeader[4+i] = quantizationTables[i]; |
103 | } |
104 | } |
105 | |
106 | setSpecialHeaderBytes(quantizationHeader, quantizationHeaderSize, |
107 | sizeof mainJPEGHeader + restartMarkerHeaderSize/* start position */); |
108 | delete[] quantizationHeader; |
109 | } |
110 | |
111 | if (numRemainingBytes == 0) { |
112 | // This packet contains the last (or only) fragment of the frame. |
113 | // Set the RTP 'M' ('marker') bit: |
114 | setMarkerBit(); |
115 | } |
116 | |
117 | // Also set the RTP timestamp: |
118 | setTimestamp(framePresentationTime); |
119 | } |
120 | |
121 | |
122 | unsigned JPEGVideoRTPSink::() const { |
123 | // Our source is known to be a JPEGVideoSource |
124 | JPEGVideoSource* source = (JPEGVideoSource*)fSource; |
125 | if (source == NULL) return 0; // sanity check |
126 | |
127 | unsigned = 8; // by default |
128 | |
129 | u_int8_t const type = source->type(); |
130 | if (type >= 64 && type <= 127) { |
131 | // There is also a Restart Marker Header: |
132 | headerSize += 4; |
133 | } |
134 | |
135 | if (curFragmentationOffset() == 0 && source->qFactor() >= 128) { |
136 | // There is also a Quantization Header: |
137 | u_int8_t dummy; |
138 | u_int16_t quantizationTablesSize; |
139 | (void)(source->quantizationTables(dummy, quantizationTablesSize)); |
140 | |
141 | headerSize += 4 + quantizationTablesSize; |
142 | } |
143 | |
144 | return headerSize; |
145 | } |
146 | |