You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 lines
2.8 KiB

/*
MediaMtx (C++) - H264 feeder
Turns the H264 an application already receives into units of a path (MediaPath):
- NAL units one at a time, as an RTP depacketizer (live555) gives them: grouped into access units by
the RTP marker bit / a new presentation time;
- whole access units in Annex-B (libav RTSP demuxer, H264 encoders) or length prefixed (MP4 / avcC).
The pts is made continuous (90 kHz, starting at 0, always increasing): a reader (recorder, WebRTC) sees
no jumps when the presentation times move onto the RTCP clock or a source restarts.
The path becomes ready when SPS and PPS are known (out of band from the SDP / extradata, or in-band),
and not ready on reset() (the source stopped).
Thread safe: the source thread pushes, any thread may reset() / setParent().
*/
#pragma once
#include <cstdint>
#include <mutex>
#include <vector>
#include "../core/mediaPath.h"
#include "../unit/unit.h"
namespace mtx {
class H264Feeder
{
public:
explicit H264Feeder(SourceParent * parent = nullptr) : m_pParent(parent) {}
~H264Feeder() { reset(); }
// The path to feed (nullptr: none). The previous path is set not ready.
void setParent(SourceParent * parent);
// Out of band parameter sets (sprop-parameter-sets of the SDP, libav extradata)
void setParameterSets(const std::vector<Bytes> & nalus);
void setExtradata(const uint8_t * data, size_t size);
// One NAL unit (no start code). ptUs: presentation time (us); marker: last NAL unit of the access
// unit (RTP marker bit); synced: the presentation times are RTCP synchronised (a change = a jump)
void pushNalu(const uint8_t * data, size_t size, int64_t ptUs, bool marker, bool synced = false);
// One access unit. ptsUs: presentation time (us, any origin); < 0: use the wall clock
void pushAnnexB(const uint8_t * data, size_t size, int64_t ptsUs);
void pushAvcc(const uint8_t * data, size_t size, int lengthSize, int64_t ptsUs);
// length prefix size of the avcC extradata (0: the packets are Annex-B)
int avccLengthSize() const { return m_nAvccLengthSize; }
// The source stopped (or reconnects): the path is set not ready, the parameter sets are kept
void reset();
bool ready() const;
uint64_t units() const { return m_nUnits; }
private:
void flushLocked();
void unitLocked(std::vector<Bytes> && nalus, int64_t ptUs, int64_t wallUs);
mutable std::mutex m_mutex;
SourceParent * m_pParent = nullptr;
Bytes m_sps;
Bytes m_pps;
bool m_bReady = false;
int m_nAvccLengthSize = 0;
uint64_t m_nUnits = 0;
// NAL unit assembly
std::vector<Bytes> m_vNalus;
int64_t m_nPtUs = 0;
bool m_bSynced = false;
// pts continuity
bool m_bHavePts = false;
bool m_bLastSynced = false;
int64_t m_nOffset = 0;
int64_t m_nLastPts = 0;
int64_t m_nLastDelta = 3000;
};
} // namespace mtx