JPEG RTP Encoder - Fragments JPEG frames per RFC 2435 for RTSP video streaming.
More...
|
| | JPEGRtpEncoder ()=default |
| |
| | JPEGRtpEncoder (size_t maxFragmentSize) |
| | Constructor with maximum fragment size.
|
| |
| AudioInfo | audioInfo () override |
| | provides the actual input AudioInfo
|
| |
| virtual AudioInfo | audioInfoOut () |
| |
| bool | begin () override |
| |
| virtual bool | begin (AudioInfo info) |
| |
| void | end () override |
| |
| virtual uint32_t | frameDurationUs () |
| | Optional rtsp function: provide the frame duration in microseconds.
|
| |
| virtual int | frameSize () |
| |
| RTSPFormat & | getFormat () override |
| | Get the media format configuration.
|
| |
| const char * | mime () override |
| | Provides the mime type of the encoded result.
|
| |
| | operator bool () override |
| |
| int | packetSize () override |
| | Size of the next queued, ready-to-send fragment, for sources that provide already RTP-payload-ready, self-delimited fragments (e.g. RFC 2435 JPEG) instead of a continuous byte stream. For these sources, readBytes() must always be called with exactly packetSize() as maxBytes, so each call retrieves exactly one complete fragment instead of the transport having to guess where one RTP payload ends and the next begins.
|
| |
| int | readBytes (void *dest, int maxBytes) override |
| |
| virtual uint16_t | samplesPerFrame () |
| | Optional rtsp function: provide samples per the frame.
|
| |
| void | setAudioInfo (AudioInfo info) override |
| | Defines the sample rate, number of channels and bits per sample.
|
| |
| void | setFormat (RTSPFormat &format) override |
| | Associates the RTSPFormat (e.g. RTSPFormatMJPEG) that supplies width/height.
|
| |
| void | setMaxFragmentSize (size_t size) |
| | Set the maximum RTP payload size per packet (header + data)
|
| |
| void | setMaxQueuedFrames (size_t frames) |
| | Maximum number of complete frames buffered when the consumer falls behind.
|
| |
| virtual void | setOutput (Print &out_stream) override |
| | Default output assignment (encoders may override to store Print reference)
|
| |
| void | start () override |
| | Initialize media source for streaming.
|
| |
| void | stop () override |
| | Cleanup media source after streaming.
|
| |
| size_t | write (const uint8_t *data, size_t len) override |
| | Process JPEG frame data and queue the resulting RTP fragments.
|
| |
|
| void | appendFragment (const uint8_t *payload, size_t len, bool last) |
| |
| void | buildJpegRtpHeader (uint8_t *buffer, size_t fragmentOffset, int width, int height, uint8_t type, uint8_t qValue) |
| |
| void | clearQueue () |
| |
| uint8_t | detectJpegType (const uint8_t *data, size_t headerLen) |
| | Determine the RFC 2435 Type (0 = 4:2:2, 1 = 4:2:0) from the JPEG's SOF0/SOF1 marker so the receiver reconstructs the correct chroma subsampling. Defaults to 0 (4:2:2) if it cannot be determined.
|
| |
| void | dropOldestFrame () |
| |
| int | extractQuantTables (const uint8_t *data, size_t headerLen, uint8_t *out) |
| | Extract the (8-bit precision) quantization tables 0 and 1 from the JPEG DQT segments found in [data, data+headerLen). RFC 2435 expects table 0 (luma) followed by table 1 (chroma), each 64 bytes in zigzag order exactly as stored in the JPEG - no reordering needed.
|
| |
| size_t | findJpegDataStart () |
| | Find the start of the entropy-coded scan data, i.e. the first byte after the SOS (Start Of Scan) marker segment. RFC 2435 requires everything before this point (SOI/APPn/DQT/SOF/SOS) to be stripped from the RTP payload.
|
| |
| bool | isCompleteJpegFrame () |
| | Check if current buffer contains a complete JPEG frame.
|
| |
| void | processCompleteFrame () |
| | Strip the JPEG headers, fragment the scan data and queue one RFC 2435 RTP payload per fragment.
|
| |
| int | queuePacketSize () |
| |
| int | queueReadBytes (void *dest, int maxBytes) |
| |
| uint32_t | readQueueU32 (size_t pos) |
| |
| void | writeBlocking (Print *out, uint8_t *data, size_t len) |
| |
| void | writeQuantTableHeader (uint8_t *buffer, const uint8_t *qTable, int qTableLen) |
| |
JPEG RTP Encoder - Fragments JPEG frames per RFC 2435 for RTSP video streaming.
This class plays both roles needed to get a JPEG frame onto the wire:
- As an AudioEncoder, write() accepts complete JPEG frames (e.g. straight from a camera).
- As an IMediaSource, packetSize()/readBytes() hand out the resulting RTP payloads one fragment at a time, each one already carrying its RFC 2435 header (and, on the first fragment of a frame, an embedded quantization-table header).
Keeping fragmentation and header construction in one place means the RTP transport never has to re-derive fragment boundaries or rebuild a JPEG header of its own - it only wraps each queued fragment in the generic RTP header (sequence number, shared timestamp, marker bit set once packetSize() reports the queue empty).
Per RFC 2435, the payload must be the raw entropy-coded scan data only; SOI/APPn/DQT/SOF/SOS markers are stripped and reconstructed by the receiver from the RTP header fields, so header stripping is not optional.
- Note
- Restart intervals (DRI) are not supported.
Usage with RTSPOutput:
- Author
- Phil Schatzmann
| int extractQuantTables |
( |
const uint8_t * |
data, |
|
|
size_t |
headerLen, |
|
|
uint8_t * |
out |
|
) |
| |
|
inlineprotected |
Extract the (8-bit precision) quantization tables 0 and 1 from the JPEG DQT segments found in [data, data+headerLen). RFC 2435 expects table 0 (luma) followed by table 1 (chroma), each 64 bytes in zigzag order exactly as stored in the JPEG - no reordering needed.
Some encoders emit a single quantization table shared by all components (SOF Tq=0 for luma and chroma alike) instead of two - a valid standalone JPEG, but not expressible in RFC 2435's wire format, whose receiver-side reconstruction (Appendix A) always assumes two distinct tables. Sending only the one table found would leave the reconstructed chroma table uninitialized on the receiver, corrupting decode; duplicating table 0 into table 1 is faithful to the source (both components really do use that table) and satisfies the two-table format.
- Returns
- Number of bytes written to out (0 or 128), 0 if no usable 8-bit-precision table was found
| virtual int frameSize |
( |
| ) |
|
|
inlinevirtualinherited |
Optional rtsp function: provide the encoded size (in bytes) of one frame, once known (e.g. after the encoder has seen real data). Lets an RTSPFormat size its RTP fragments to match one frame instead of a fixed guess, so throughput actually tracks frameDurationUs() - a fragment size covering several frames' worth of bytes sent at one-frame timing overruns real-time bandwidth and, over UDP (no flow control), causes packet loss. Returns 0 if not yet known/not applicable (fixed-frame-size codecs have no need to override this).
Reimplemented in ADPCMEncoder, EncoderALAC, AMRNBEncoder, AMRWBEncoder, and MP3ParserEncoder.
Size of the next queued, ready-to-send fragment, for sources that provide already RTP-payload-ready, self-delimited fragments (e.g. RFC 2435 JPEG) instead of a continuous byte stream. For these sources, readBytes() must always be called with exactly packetSize() as maxBytes, so each call retrieves exactly one complete fragment instead of the transport having to guess where one RTP payload ends and the next begins.
Call it once before each readBytes() to size that call, and again right after to tell whether the fragment just read was the last one currently available - a 0 result means the caller should set the RTP marker bit on the fragment it just sent (and may advance the timestamp for the next frame).
- Returns
- -1 if this source is not packetized (use the plain readBytes() stream instead), 0 if packetized but nothing is queued right now, otherwise the size in bytes of the next fragment
Reimplemented from IMediaSource.