Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Common/RTSPCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ typedef unsigned int u_int32_t;
typedef unsigned __int64 uint64_t;
typedef unsigned __int64 u_int64_t;
typedef __int64 int64_t;

#define _strcasecmp _strnicmp

#if _MSC_VER <= 1700
#define snprintf _snprintf
#endif

#else
#include <inttypes.h>
#define _strcasecmp strncasecmp
#endif

#pragma pack(push, 1)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RTSP
c++ RTSP Client/Server - win32/linux/android
c++ RTSP Client/Server - windows/linux/android

this project is RTSP client/server library for windows/linux/android.

Expand Down
8 changes: 4 additions & 4 deletions RTSPClient/RTP/AC3RTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void AC3RTPSource::processFrame(RTPPacketBuffer *packet)
{
uint8_t *buf = packet->payload();
int len = packet->payloadLen();
int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

unsigned char* headerStart = buf;

Expand All @@ -30,9 +30,9 @@ void AC3RTPSource::processFrame(RTPPacketBuffer *packet)
// The RTP "M" (marker) bit indicates the last fragment of a frame.
// In case the sender did not set the "M" bit correctly, we also test for FT == 0:
if (packet->markerBit() || FT == 0) {
if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
fBeginFrame = false;
}
}
24 changes: 12 additions & 12 deletions RTSPClient/RTP/H264RTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ H264RTPSource::~H264RTPSource()

void H264RTPSource::putStartCode()
{
fFrameBuf[fFrameBufPos++] = 0x00;
fFrameBuf[fFrameBufPos++] = 0x00;
fFrameBuf[fFrameBufPos++] = 0x00;
fFrameBuf[fFrameBufPos++] = 0x01;
fFrameBuffer[fFrameBufferPos++] = 0x00;
fFrameBuffer[fFrameBufferPos++] = 0x00;
fFrameBuffer[fFrameBufferPos++] = 0x00;
fFrameBuffer[fFrameBufferPos++] = 0x01;
}

void H264RTPSource::processFrame(RTPPacketBuffer *packet)
Expand All @@ -34,15 +34,15 @@ void H264RTPSource::processFrame(RTPPacketBuffer *packet)
uint8_t *buf_ptr = buf;
bool isCompleteFrame = false;

int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

uint8_t nalUnitType = (buf[0]&0x1F);

if (RTSPCommonEnv::nDebugFlag&DEBUG_FLAG_RTP_PAYLOAD)
DPRINTF("nal_type: %d, size: %d\n", nalUnitType, len);

if (!fIsStartFrame) {
if (fExtraData) {
if (fExtraDataSize > 0) {
putStartCode();
offset = trimStartCode(fExtraData, fExtraDataSize);
copyToFrameBuffer(&fExtraData[offset], fExtraDataSize - offset);
Expand Down Expand Up @@ -104,9 +104,9 @@ void H264RTPSource::processFrame(RTPPacketBuffer *packet)

buf_ptr += staplen; len -= staplen;

if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}
break;
}
Expand All @@ -118,9 +118,9 @@ void H264RTPSource::processFrame(RTPPacketBuffer *packet)
}

if (isCompleteFrame) {
if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}
}

Expand Down
14 changes: 7 additions & 7 deletions RTSPClient/RTP/H265RTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void H265RTPSource::processFrame(RTPPacketBuffer* packet)
uint8_t* headerStart = buf;
bool isCompleteFrame = false;

int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

uint8_t nalUnitType = (headerStart[0] & 0x7E) >> 1;

Expand All @@ -48,9 +48,9 @@ void H265RTPSource::processFrame(RTPPacketBuffer* packet)

buf_ptr += nalUSize; len -= nalUSize;

if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}
} break;
case 49: { // Fragmentation Unit (FU)
Expand Down Expand Up @@ -82,8 +82,8 @@ void H265RTPSource::processFrame(RTPPacketBuffer* packet)
}

if (isCompleteFrame) {
if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}
}
8 changes: 4 additions & 4 deletions RTSPClient/RTP/JPEGRTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void JPEGRTPSource::processFrame(RTPPacketBuffer *packet)
memcpy(&jpeg_buf[MAX_JPEG_HEADER_SIZE], buf, len);

uint8_t *buf_ptr = &jpeg_buf[MAX_JPEG_HEADER_SIZE];
int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

unsigned char* qtables = NULL;
unsigned qtlen = 0;
Expand Down Expand Up @@ -390,9 +390,9 @@ void JPEGRTPSource::processFrame(RTPPacketBuffer *packet)
copyToFrameBuffer(buf_ptr, len);

if (packet->markerBit()) {
if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}

delete[] jpeg_buf;
Expand Down
8 changes: 4 additions & 4 deletions RTSPClient/RTP/MPEG4ESRTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void MPEG4ESRTPSource::processFrame(RTPPacketBuffer *packet)
{
uint8_t *buf = (uint8_t *)packet->payload();
int len = packet->payloadLen();
int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

if (len >= 4) {
if (buf[0] == 0 && buf[1] == 0 && buf[2] == 1)
Expand All @@ -34,9 +34,9 @@ void MPEG4ESRTPSource::processFrame(RTPPacketBuffer *packet)
copyToFrameBuffer(buf, len);

if (packet->markerBit()) {
if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
fBeginFrame = false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions RTSPClient/RTP/MPEG4GenericRTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void MPEG4GenericRTPSource::processFrame(RTPPacketBuffer *packet)
uint8_t *buf = (uint8_t *)packet->payload();
int len = packet->payloadLen();

int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

unsigned char* headerStart = buf;
unsigned packetSize = len;
Expand Down Expand Up @@ -79,8 +79,8 @@ void MPEG4GenericRTPSource::processFrame(RTPPacketBuffer *packet)
copyToFrameBuffer(ptr, fAUHeaders[i].size);
ptr += fAUHeaders[i].size;

if (fFrameHandlerFunc)
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
resetFrameBuf();
if (fFrameHandler)
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
resetFrameBuffer();
}
}
66 changes: 33 additions & 33 deletions RTSPClient/RTP/RTPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ RTPSource::RTPSource(int streamType, MediaSubsession &subsession, TaskScheduler
: fStreamType(streamType), fRecvBuf(NULL), fRTPPayloadFormat(subsession.rtpPayloadFormat()), fTimestampFrequency(subsession.rtpTimestampFrequency()),
fSSRC(rand()), fTask(&task), fSvrAddr(0), fRtspSock(NULL), fRtcpChannelId(subsession.rtcpChannelId), fCodecName(NULL),
fReceptionStatsDB(NULL), fRtcpInstance(NULL),
fFrameHandlerFunc(NULL), fFrameHandlerFuncData(NULL), fIsStartFrame(false), fBeginFrame(false), fExtraData(NULL), fExtraDataSize(0),
fRtpHandlerFunc(NULL), fRtpHandlerFuncData(NULL), fRtcpHandlerFunc(NULL), fRtcpHandlerFuncData(NULL), fFrameType(FRAME_TYPE_ETC)
fFrameHandler(NULL), fFrameHandlerData(NULL), fIsStartFrame(false), fBeginFrame(false), fExtraData(NULL), fExtraDataSize(0),
fRtpHandler(NULL), fRtpHandlerData(NULL), fRtcpHandler(NULL), fRtcpHandlerData(NULL), fFrameType(FRAME_TYPE_ETC)
{
fReorderingBuffer = new ReorderingPacketBuffer();

Expand All @@ -34,8 +34,8 @@ fRtpHandlerFunc(NULL), fRtpHandlerFuncData(NULL), fRtcpHandlerFunc(NULL), fRtcpH
else if (!strcmp(subsession.mediumName(), "audio"))
fFrameType = FRAME_TYPE_AUDIO;

fFrameBuf = new uint8_t[FRAME_BUFFER_SIZE];
fFrameBufPos = 0;
fFrameBuffer = new uint8_t[FRAME_BUFFER_SIZE];
fFrameBufferPos = 0;

fLastSeqNum = fLastSeqNum2 = 0;
fLastTimestamp = 0;
Expand Down Expand Up @@ -89,7 +89,7 @@ RTPSource::~RTPSource()
DELETE_OBJECT(fRtcpInstance);

DELETE_ARRAY(fRecvBuf);
DELETE_ARRAY(fFrameBuf);
DELETE_ARRAY(fFrameBuffer);
DELETE_ARRAY(fCodecName);
DELETE_ARRAY(fExtraData);
DELETE_ARRAY(fTrackId);
Expand All @@ -101,14 +101,14 @@ void RTPSource::startNetworkReading(FrameHandlerFunc frameHandler, void *frameHa
RTPHandlerFunc rtpHandler, void *rtpHandlerData,
RTPHandlerFunc rtcpHandler, void *rtcpHandlerData)
{
fFrameHandlerFunc = frameHandler;
fFrameHandlerFuncData = frameHandlerData;
fFrameHandler = frameHandler;
fFrameHandlerData = frameHandlerData;

fRtpHandlerFunc = rtpHandler;
fRtpHandlerFuncData = rtpHandlerData;
fRtpHandler = rtpHandler;
fRtpHandlerData = rtpHandlerData;

fRtcpHandlerFunc = rtcpHandler;
fRtcpHandlerFuncData = rtcpHandlerData;
fRtcpHandler = rtcpHandler;
fRtcpHandlerData = rtcpHandlerData;

if (fRtpSock.isOpened())
fTask->turnOnBackgroundReadHandling(fRtpSock.sock(), &incomingRtpPacketHandler, this);
Expand All @@ -125,14 +125,14 @@ void RTPSource::stopNetworkReading()
if (fRtcpSock.isOpened())
fTask->turnOffBackgroundReadHandling(fRtcpSock.sock());

fFrameHandlerFunc = NULL;
fFrameHandlerFuncData = NULL;
fFrameHandler = NULL;
fFrameHandlerData = NULL;

fRtpHandlerFunc = NULL;
fRtpHandlerFuncData = NULL;
fRtpHandler = NULL;
fRtpHandlerData = NULL;

fRtcpHandlerFunc = NULL;
fRtcpHandlerFuncData = NULL;
fRtcpHandler = NULL;
fRtcpHandlerData = NULL;

fReorderingBuffer->reset();
}
Expand Down Expand Up @@ -211,10 +211,10 @@ void RTPSource::processNextPacket()

fLastSeqNum = seqnum;

if (fRtpHandlerFunc)
fRtpHandlerFunc(fRtpHandlerFuncData, fTrackId, (char *)nextPacket->buf(), nextPacket->length());
if (fRtpHandler)
fRtpHandler(fRtpHandlerData, fTrackId, (char *)nextPacket->buf(), nextPacket->length());

if (fFrameHandlerFunc)
if (fFrameHandler)
processFrame(nextPacket);

fReorderingBuffer->releaseUsedPacket(nextPacket);
Expand All @@ -240,15 +240,15 @@ void RTPSource::processFrame(RTPPacketBuffer *packet)
{
uint8_t *buf = (uint8_t *)packet->payload();
int len = packet->payloadLen();
int64_t media_timestamp = packet->extTimestamp() == 0 ? getMediaTimestamp(packet->timestamp()) : packet->extTimestamp();
int64_t timestamp = packet->extTimestamp() == 0 ? getRealTimestamp(packet->timestamp()) : packet->extTimestamp();

copyToFrameBuffer(buf, len);

if (packet->markerBit() == 1 || fLastTimestamp != packet->timestamp()) {
if (fFrameHandlerFunc) {
fFrameHandlerFunc(fFrameHandlerFuncData, fFrameType, media_timestamp, fFrameBuf, fFrameBufPos);
if (fFrameHandler) {
fFrameHandler(fFrameHandlerData, fFrameType, timestamp, fFrameBuffer, fFrameBufferPos);
}
resetFrameBuf();
resetFrameBuffer();
}
}

Expand Down Expand Up @@ -321,8 +321,8 @@ void RTPSource::rtcpReadHandler(char *buf, int len, struct sockaddr_in &fromAddr
RTCPInstance::onExpire(fRtcpInstance);
}

if (fRtcpHandlerFunc)
fRtcpHandlerFunc(fRtcpHandlerFuncData, fTrackId, buf, len);
if (fRtcpHandler)
fRtcpHandler(fRtcpHandlerData, fTrackId, buf, len);
}

void RTPSource::sendRtcpReport(char *buf, int len)
Expand Down Expand Up @@ -350,20 +350,20 @@ void RTPSource::sendRtcpReport(char *buf, int len)

void RTPSource::copyToFrameBuffer(uint8_t *buf, int len)
{
if (fFrameBufPos+len >= FRAME_BUFFER_SIZE) {
if (fFrameBufferPos+len >= FRAME_BUFFER_SIZE) {
DPRINTF("RTP Frame Buffer overflow %s\n", fCodecName);
fFrameBufPos = 0;
fFrameBufferPos = 0;
}
memmove(&fFrameBuf[fFrameBufPos], buf, len);
fFrameBufPos += len;
memmove(&fFrameBuffer[fFrameBufferPos], buf, len);
fFrameBufferPos += len;
}

void RTPSource::resetFrameBuf()
void RTPSource::resetFrameBuffer()
{
fFrameBufPos = 0;
fFrameBufferPos = 0;
}

uint64_t RTPSource::getMediaTimestamp(uint32_t timestamp)
uint64_t RTPSource::getRealTimestamp(uint32_t timestamp)
{
uint64_t msec = 1000;
uint64_t time_msec = timestamp*msec/fTimestampFrequency;
Expand Down
20 changes: 10 additions & 10 deletions RTSPClient/RTP/RTPSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class RTPSource

protected:
void copyToFrameBuffer(uint8_t *buf, int len);
void resetFrameBuf();
void resetFrameBuffer();

protected:
uint64_t getMediaTimestamp(uint32_t timestamp);
uint64_t getRealTimestamp(uint32_t timestamp);

protected:
ReorderingPacketBuffer *fReorderingBuffer;
Expand All @@ -84,10 +84,10 @@ class RTPSource
uint32_t fSvrAddr;
time_t fLastRtcpSendTime;

uint8_t* fFrameBuf;
int fFrameBufPos;
FrameHandlerFunc fFrameHandlerFunc;
void* fFrameHandlerFuncData;
uint8_t* fFrameBuffer;
int fFrameBufferPos;
FrameHandlerFunc fFrameHandler;
void* fFrameHandlerData;

// TCP �϶��� ���
MySock* fRtspSock;
Expand All @@ -100,11 +100,11 @@ class RTPSource
TaskScheduler* fTask;
char* fRecvBuf;

RTPHandlerFunc fRtpHandlerFunc;
void* fRtpHandlerFuncData;
RTPHandlerFunc fRtpHandler;
void* fRtpHandlerData;

RTPHandlerFunc fRtcpHandlerFunc;
void* fRtcpHandlerFuncData;
RTPHandlerFunc fRtcpHandler;
void* fRtcpHandlerData;
};

#endif
Loading