协议圣经 四 rtspclient

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了协议圣经 四 rtspclient相关的知识,希望对你有一定的参考价值。

live555 rtsp client

为了让读者快速掌握或者说快速做出一个rtspclient ,我们使用live555 来做一个客户端,live555是一个比较出名的rtsp协议实现库,虽然说他代码c++中继承较多,但不失为一个比较认真和全面的库。

rtsp client 注意点

1 、要做一个rtsp客户端,必须实现rtp协议,rtcp协议和rtsp协议
2、 rtcp可以用来保活和统计
3、可以使用 rtp over tcp 和 rtp over udp
4、在局域网里面,可以使用udp 甚至 multicast 组播,外网,请使用tcp

show me the code

这里提供一个rtsp client ,非常简便,一个头文件搞定。


/* ---------------------------------------------------------------------------
**
2021-02-13
email 418511899@qq.com
** 
** -------------------------------------------------------------------------*/

#pragma once

#include "BasicUsageEnvironment.hh"
#include "liveMedia.hh"
#include <string>
#include <iostream>
#ifdef WIN32
#pragma warning (disable: 4512) 
#pragma warning (disable: 4100) 
#pragma warning (disable: 4091) 
#include <stdint.h>
typedef int ssize_t;
#endif



#define RTSP_CALLBACK(uri, resultCode, resultString) \\
static void continueAfter ## uri(RTSPClient* rtspClient, int resultCode, char* resultString) { static_cast<c_rtsp::RTSPClientConnection*>(rtspClient)->continueAfter ## uri(resultCode, resultString); } \\
void continueAfter ## uri (int resultCode, char* resultString); \\
/**/

#define TASK_CALLBACK(class,task) \\
TaskToken m_ ## task ## Task; \\
static void Task ## task(void* rtspClient) { static_cast<class*>(rtspClient)->Task ## task(); } \\
void Task ## task (); \\
/**/


#if LIVEMEDIA_LIBRARY_VERSION_INT > 1371168000 
	#define RTSPClientConstrutor(env, url, verbosity, appname, httpTunnelPort) RTSPClient(env, url, verbosity, appname, httpTunnelPort ,-1)
#else					
	#define RTSPClientConstrutor(env, url, verbosity, appname, httpTunnelPort) RTSPClient(env, url, verbosity, appname, httpTunnelPort)
#endif



class Environment : public BasicUsageEnvironment
{
public:
	Environment(char * stop) : BasicUsageEnvironment(*BasicTaskScheduler::createNew()), m_stop(stop)
	{
		m_stop = stop;
	}

	~Environment()
	{
		TaskScheduler* scheduler = &this->taskScheduler();
		delete scheduler;
	}

	void mainloop()
	{
		this->taskScheduler().doEventLoop(m_stop);
	}

	void stop()
	{
		*m_stop = 1;
	}


protected:
	char* m_stop;
};


typedef void (*callback_onData)(void * puser, uint8_t*, ssize_t len);


class c_rtsp
{
	public:
		class Callback
		{
			public:
				virtual bool    onNewSession(const char* id, const char* media, 
					const char* codec, const char* sdp) { return true; }
				virtual bool    onData(const char* id, unsigned char* buffer,
					ssize_t size, struct timeval presentationTime)
				{
					if (v_callback != NULL) {
						//回调函数,传回名称和数据,数据大小+头部的大小
						v_callback(v_user, buffer, size);
						return true;
					}
					return false;
				}
				virtual ssize_t onNewBuffer(unsigned char* buffer, ssize_t size) 
				{
					return 0; 
				}
				virtual void    onError(c_rtsp&, const char*message)  {
					std::cout << v_name << ":Error:" << message << std::endl;
				}
				virtual void    onConnectionTimeout(c_rtsp& connection)
				{
					std::cout << v_name << ":Connection timeout -> retry" << std::endl;
					connection.start();
				}
				virtual void    onDataTimeout(c_rtsp& connection)
				{
					std::cout << v_name << ":Data timeout -> retry" << std::endl;
					connection.start();
				}
				//增加的头部长度
				int v_header = 0;
				std::string v_name= "empty" ;
				void RegisterCallBack(void * puser, int headlen, callback_onData cb) {
					v_user = puser;
					v_header = headlen;
					v_callback = cb;
				}
				callback_onData v_callback = NULL;
				void * v_user = NULL;
		};

	protected:

		class SessionSink: public MediaSink 
		{
			public:
				static SessionSink* createNew(UsageEnvironment& env, Callback* callback) { return new SessionSink(env, callback); }

			private:
				SessionSink(UsageEnvironment& env, Callback* callback);
				virtual ~SessionSink();

				void allocate(ssize_t bufferSize);

				static void afterGettingFrame(void* clientData, unsigned frameSize,
							unsigned numTruncatedBytes,
							struct timeval presentationTime,
							unsigned durationInMicroseconds)
				{
					static_cast<SessionSink*>(clientData)->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds);
				}
				
				void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds);

				virtual Boolean continuePlaying();

			private:
				u_int8_t*              m_buffer;
				ssize_t                 m_bufferSize;
				Callback*              m_callback; 	
		};
	
	
		class RTSPClientConnection : public RTSPClient
		{
			public:
				RTSPClientConnection(c_rtsp& connection, Environment& env, Callback* callback, const char* rtspURL, int timeout, bool rtpovertcp, int verbosityLevel);
				virtual ~RTSPClientConnection(); 
			
			protected:
				void sendNextCommand(); 
						
				RTSP_CALLBACK(DESCRIBE,resultCode,resultString);
				RTSP_CALLBACK(SETUP,resultCode,resultString);
				RTSP_CALLBACK(PLAY,resultCode,resultString);
			
				TASK_CALLBACK(c_rtsp::RTSPClientConnection,ConnectionTimeout);
				TASK_CALLBACK(c_rtsp::RTSPClientConnection,DataArrivalTimeout);
				
			protected:
				c_rtsp&          m_connection;
				int                      m_timeout;
				bool                     m_rtpovertcp;
				MediaSession*            m_session;                   
				MediaSubsession*         m_subSession;             
				MediaSubsessionIterator* m_subSessionIter;
				Callback               * m_callback; 	
				unsigned int             m_nbPacket;
		};
		
	public:
		c_rtsp(const char* rtspURL,
			int header = 4,
			int timeout = 5, 
			bool rtpovertcp = false, 
			int verbosityLevel = 0);
		virtual ~c_rtsp();

		void Register_Callcack(void * puser,const char * name, int headlen, callback_onData cb)
		{
			if (name != NULL)
				m_callback.v_name = name;
			else
				m_callback.v_name = "empty";
			m_callback.RegisterCallBack(puser, headlen, cb);
		}
		void start(unsigned int delay = 0);
		void stop();

	protected:
		TASK_CALLBACK(c_rtsp,startCallback);
	
	protected:
		Environment              m_env;
		Callback                 m_callback; 	
		std::string              m_url;
		int                      m_timeout = 2;
		bool                     m_rtpovertcp = false;
		int                      m_verbosity = 0;
	
		RTSPClientConnection*    m_rtspClient;
};

调用

请听下回分解

以上是关于协议圣经 四 rtspclient的主要内容,如果未能解决你的问题,请参考以下文章

协议圣经-谈端口和四元组

流媒体协议之RTSP客户端的实现20171014

一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:用户接口层之RtspClient类及其构造函数

协议圣经 RTSP

协议圣经 RTP组播音视频技巧

协议圣经 -协议之服务编写