 videoLIVE555Source: fix the crash in the destructor
videoLIVE555Source:
- _stop() tore the stream down on the caller's thread while the live555
thread was still inside doEventLoop(), and deleted the RTSP client, the
environment and the scheduler under the running loop. Now it ends the
loop, joins the thread, then closes what is left on its own thread.
- shutdownStream() no longer deletes the scheduler/environment (a second
shutdown, e.g. _close() then the destructor or a live555 callback then
the destructor, used them after they were freed). Only the destructor
frees them, once. A second shutdown is a no-op.
- m_pRtspClient and the other pointers are initialised: a source that was
never opened, or failed to open, shut down a random pointer.
- The event-loop flag is reset in _open(), not at thread start, so a stop
issued before the thread ran is not lost.
- shutdownStream closes the subsession sinks and sends TEARDOWN, as
live555 testRTSPClient does. The sinks were never closed and leaked.
- continueAfterPLAY frees resultString.
DummySink:
- Every pointer is NULL-initialised before the constructor can return
early. The destructor freed an uninitialised m_pFrame when the codec
was not found or in stream-proxy mode.
- Decode resources are created whenever DECODE_FRAMES is set, also
together with STREAM_PROXY (deviceNodeCamera uses both). They were
used uninitialised. Decoding is skipped if any of them is missing.
- m_sAvPkt is zeroed (av_packet_unref() frees opaque_ref/side_data).
- The destructor frees the frames, the RGB buffer, the scaler, the codec
context, the output format context and dictionary, and m_pH264. The
trailer is written only after a written header.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago  videoLIVE555Source: fix the crash in the destructor
videoLIVE555Source:
- _stop() tore the stream down on the caller's thread while the live555
thread was still inside doEventLoop(), and deleted the RTSP client, the
environment and the scheduler under the running loop. Now it ends the
loop, joins the thread, then closes what is left on its own thread.
- shutdownStream() no longer deletes the scheduler/environment (a second
shutdown, e.g. _close() then the destructor or a live555 callback then
the destructor, used them after they were freed). Only the destructor
frees them, once. A second shutdown is a no-op.
- m_pRtspClient and the other pointers are initialised: a source that was
never opened, or failed to open, shut down a random pointer.
- The event-loop flag is reset in _open(), not at thread start, so a stop
issued before the thread ran is not lost.
- shutdownStream closes the subsession sinks and sends TEARDOWN, as
live555 testRTSPClient does. The sinks were never closed and leaked.
- continueAfterPLAY frees resultString.
DummySink:
- Every pointer is NULL-initialised before the constructor can return
early. The destructor freed an uninitialised m_pFrame when the codec
was not found or in stream-proxy mode.
- Decode resources are created whenever DECODE_FRAMES is set, also
together with STREAM_PROXY (deviceNodeCamera uses both). They were
used uninitialised. Decoding is skipped if any of them is missing.
- m_sAvPkt is zeroed (av_packet_unref() frees opaque_ref/side_data).
- The destructor frees the frames, the RGB buffer, the scaler, the codec
context, the output format context and dictionary, and m_pH264. The
trailer is written only after a written header.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago |
|
#ifndef __VIDEOLIVE555AVLIBSOURCE_H_INCL__
#define __VIDEOLIVE555AVLIBSOURCE_H_INCL__
#ifndef LRF_PROXY_ONLY
#include "../../../alg/generic/md5.h"
#include "../../../components/base/basicLIBAV.h"
#include "../../../components/base/basicLogCollection.h"
#include "../../../components/base/basicMediaSource.h"
#include "../../../components/base/basicSettings.h"
#include "../../../components/base/basicThread.h"
#include <BasicUsageEnvironment.hh>
#include <liveMedia/liveMedia.hh>
#define RTSP_CLIENT_VERBOSITY_LEVEL 1
#define INBUF_SIZE 40960
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
#define DUMMY_SINK_RECEIVE_BUFFER_SIZE 400000
#define REQUEST_STREAMING_OVER_TCP True
class videoLIVE555Source;
class StreamClientState{ public:
StreamClientState(); virtual ~StreamClientState();
public:
MediaSubsessionIterator * iter; MediaSession * session; MediaSubsession * subsession; TaskToken streamTimerTask; double duration;};
class CustomRTSPClient : public RTSPClient{ private:
videoLIVE555Source * session; StreamClientState streamClientState;
public:
static CustomRTSPClient * createNew(videoLIVE555Source * session, UsageEnvironment & env, const char * rtspURL, int verbosityLevel = 0, const char * applicationName = NULL, portNumBits tunnelOverHTTPPortNum = 0);
StreamClientState & getStreamClientState() { return this->streamClientState; }
videoLIVE555Source * getSession() { return this->session; }
virtual ~CustomRTSPClient();
private:
CustomRTSPClient(videoLIVE555Source * session, UsageEnvironment & env, const char * rtspURL, int verbosityLevel, const char * applicationName, portNumBits tunnelOverHTTPPortNum);};
class DummySink : public MediaSink{ public:
static DummySink * createNew(videoLIVE555Source * session, UsageEnvironment & env, MediaSubsession & subsession, // identifies the kind of data that's being received
const char * streamId = NULL, int w = -1, int h = -1, int ww = -1, int hh = -1); // identifies the stream itself (optional)
virtual ~DummySink();
private:
basicLogCollection * m_pLog; AVCodecID m_eCodecProbe; cv::Mat * m_pRGB;
int m_nInWidth; int m_nInHeight; int m_nOutWidth; int m_nOutHeight;
DummySink(videoLIVE555Source * session, UsageEnvironment & env, MediaSubsession & subsession, const char * streamId, int w, int h, int ww, int hh); // called only by "createNew()"
static void afterGettingFrame(void * clientData, unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds); void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds); // void saveFrameToFile()
private:
// redefined virtual functions:
virtual Boolean continuePlaying();
public:
void setSprop(const u_int8_t * prop, unsigned size);
private:
videoLIVE555Source * m_pSession; uint8_t * m_pReceiveBuffer; uint8_t * m_pReceiveBufferAV; MediaSubsession & m_sSubSession; char * m_pStreamId; H264or5VideoFileSink * qtOut; FILE * m_pOutFile[2]; uint8_t m_nCurrentOutFile; // const avlib v6
const AVCodec * m_pCodec; AVCodecContext * m_pCodecContext;
AVCodec * m_pOutCodec; AVCodecContext * m_pOutCodecContext; AVFormatContext * m_pOutFormatCtx; AVDictionary * m_pOutFormatOpts; AVStream * m_pOutStream; uint8_t * m_pH264;
uint64_t m_iFrameIndex; AVFrame * m_pFrame; AVFrame * m_pRGBFrame; SwsContext * m_pSwsContext;
bool m_bOutHeaderWritten;
bool canDecode(void) const; // decoder, frames and scaler were all created
uint8_t m_aInBuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; // char buf[1024];
AVPacket m_sAvPkt;};
class videoLIVE555Source : public basicMediaSource, public basicThread{ private:
basicLogCollection * m_pLog; void * m_pOpaque; bool m_bIsParamsSet;
int m_nInWidth; int m_nInHeight; int m_nOutWidth; int m_nOutHeight; MS_WORK_MODE m_eWM;
StreamClientState streamClientState;
virtual Boolean continuePlaying(); uint8_t * fReceiveBuffer; TaskScheduler * m_pScheduler;
UsageEnvironment * m_pUsageEnvironment; MediaSession * m_pMediaSession;
volatile char _eventLoopWatchVariable;
CustomRTSPClient * m_pRtspClient; DummySink * m_pSink; MediaSubsession * fSubsession; char * fStreamId;
static void continueAfterDESCRIBE(RTSPClient * rtspClient, int resultCode, char * resultString); static void continueAfterSETUP(RTSPClient * rtspClient, int resultCode, char * resultString); static void continueAfterPLAY(RTSPClient * rtspClient, int resultCode, char * resultString); // Other event handler functions:
static void subsessionAfterPlaying(void * clientData);
// called when a stream's subsession (e.g., audio or video substream) ends
static void subsessionByeHandler(void * clientData); //, char const* reason );
// called when a RTCP "BYE" is received for a subsession
static void streamTimerHandler(void * clientData); // The main streaming routine (for each "rtsp://" URL):
void openURL(UsageEnvironment & env, const char * progName, const char * rtspURL); // Used to iterate through each stream's 'subsessions', setting up each one:
void setupNextSubsession(RTSPClient * rtspClient);
// Used to shut down and close a stream (including its "RTSPClient" object):
void shutdownStream(RTSPClient * rtspClient, int exitCode = 1);
pthread_t m_tEventLoopThread; void initMembers(void); bool isEventLoopThread(void); // called from a live555 callback
protected:
void m_fThreadProc(void);
public:
static void sessionAfterPlaying(void * clientData);
virtual bool _open(CSTR URI, bool ShowDeviceMenu = false, float framerate = -1.0f, int inputPin = -1, int width = -1, int height = -1, int s_width = -1, int s_height = -1); virtual bool _close(void); virtual bool _stop(void); bool isFinished(void);
MS_WORK_MODE getWM(void) const { return m_eWM; }
void setWM(int M) { m_eWM = (MS_WORK_MODE) M; }
MediaStateStatus getStatus(void);
explicit videoLIVE555Source(void * opaque = NULL); explicit videoLIVE555Source(CSTR URI, void * opaque = NULL, MS_WORK_MODE mode = MS_MODE_DECODE_FRAMES); virtual ~videoLIVE555Source();
bool setSize(int Width, int Height); bool setProportionalByWidth(int Width); bool setProportionalByHeight(int Height); bool setFPS(int); bool setName(CSTR Name); bool setFrameAspectRatio(FrameAspectRatio AspectRatio); void consumeFrame(cv::Mat * frm);
MediaSession * getMS(void) { return m_pMediaSession; }};
#endif //!LRF_PROXY_ONLY
#endif
|