2015-02-15 13:28:31 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013-2015 winlin
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRS_APP_RTSP_HPP
|
|
|
|
#define SRS_APP_RTSP_HPP
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <srs_app_rtsp.hpp>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
2015-02-16 06:05:01 +00:00
|
|
|
#include <vector>
|
2015-02-17 13:10:06 +00:00
|
|
|
#include <map>
|
2015-02-16 06:05:01 +00:00
|
|
|
|
|
|
|
#include <srs_app_st.hpp>
|
|
|
|
#include <srs_app_thread.hpp>
|
2015-02-17 13:10:06 +00:00
|
|
|
#include <srs_app_listener.hpp>
|
2015-02-15 13:28:31 +00:00
|
|
|
|
|
|
|
#ifdef SRS_AUTO_STREAM_CASTER
|
|
|
|
|
2015-02-16 06:05:01 +00:00
|
|
|
class SrsStSocket;
|
2015-02-17 13:10:06 +00:00
|
|
|
class SrsRtspConn;
|
2015-02-16 06:05:01 +00:00
|
|
|
class SrsRtspStack;
|
|
|
|
class SrsRtspCaster;
|
2015-02-17 13:10:06 +00:00
|
|
|
class SrsConfDirective;
|
2015-02-18 03:49:42 +00:00
|
|
|
class SrsRtpPacket;
|
2015-02-18 14:28:39 +00:00
|
|
|
class SrsRequest;
|
|
|
|
class SrsStSocket;
|
|
|
|
class SrsRtmpClient;
|
|
|
|
class SrsRawH264Stream;
|
|
|
|
class SrsRawAacStream;
|
|
|
|
class SrsRawAacStreamCodec;
|
|
|
|
class SrsSharedPtrMessage;
|
|
|
|
class SrsCodecSample;
|
|
|
|
class SrsSimpleBuffer;
|
2015-02-19 10:56:21 +00:00
|
|
|
class SrsPithyPrint;
|
2015-02-15 13:28:31 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-17 13:10:06 +00:00
|
|
|
* a rtp connection which transport a stream.
|
2015-02-15 13:28:31 +00:00
|
|
|
*/
|
2015-02-17 13:10:06 +00:00
|
|
|
class SrsRtpConn: public ISrsUdpHandler
|
2015-02-15 13:28:31 +00:00
|
|
|
{
|
2015-02-17 13:10:06 +00:00
|
|
|
private:
|
2015-02-19 10:56:21 +00:00
|
|
|
SrsPithyPrint* pprint;
|
2015-02-17 13:10:06 +00:00
|
|
|
SrsUdpListener* listener;
|
|
|
|
SrsRtspConn* rtsp;
|
2015-02-18 03:49:42 +00:00
|
|
|
SrsRtpPacket* cache;
|
|
|
|
int stream_id;
|
2015-02-17 13:10:06 +00:00
|
|
|
int _port;
|
2015-02-15 13:28:31 +00:00
|
|
|
public:
|
2015-02-18 03:49:42 +00:00
|
|
|
SrsRtpConn(SrsRtspConn* r, int p, int sid);
|
2015-02-17 13:10:06 +00:00
|
|
|
virtual ~SrsRtpConn();
|
2015-02-16 06:05:01 +00:00
|
|
|
public:
|
2015-02-17 13:10:06 +00:00
|
|
|
virtual int port();
|
|
|
|
virtual int listen();
|
|
|
|
// interface ISrsUdpHandler
|
|
|
|
public:
|
|
|
|
virtual int on_udp_packet(sockaddr_in* from, char* buf, int nb_buf);
|
2015-02-15 13:28:31 +00:00
|
|
|
};
|
|
|
|
|
2015-02-18 14:28:39 +00:00
|
|
|
/**
|
|
|
|
* audio is group by frames.
|
|
|
|
*/
|
|
|
|
struct SrsRtspAudioCache
|
|
|
|
{
|
|
|
|
int64_t dts;
|
|
|
|
SrsCodecSample* audio_samples;
|
|
|
|
SrsSimpleBuffer* payload;
|
|
|
|
|
|
|
|
SrsRtspAudioCache();
|
|
|
|
virtual ~SrsRtspAudioCache();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the time jitter correct for rtsp.
|
|
|
|
*/
|
|
|
|
class SrsRtspJitter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
int64_t previous_timestamp;
|
|
|
|
int64_t pts;
|
|
|
|
int delta;
|
|
|
|
public:
|
|
|
|
SrsRtspJitter();
|
|
|
|
virtual ~SrsRtspJitter();
|
|
|
|
public:
|
|
|
|
virtual int64_t timestamp();
|
|
|
|
virtual int correct(int64_t& ts);
|
|
|
|
};
|
|
|
|
|
2015-02-15 13:28:31 +00:00
|
|
|
/**
|
2015-02-16 06:05:01 +00:00
|
|
|
* the rtsp connection serve the fd.
|
2015-02-15 13:28:31 +00:00
|
|
|
*/
|
2015-02-16 06:05:01 +00:00
|
|
|
class SrsRtspConn : public ISrsThreadHandler
|
2015-02-15 13:28:31 +00:00
|
|
|
{
|
|
|
|
private:
|
2015-02-18 14:28:39 +00:00
|
|
|
std::string output_template;
|
|
|
|
std::string rtsp_tcUrl;
|
|
|
|
std::string rtsp_stream;
|
2015-02-17 08:28:28 +00:00
|
|
|
private:
|
|
|
|
std::string session;
|
2015-02-17 13:10:06 +00:00
|
|
|
// video stream.
|
2015-02-17 23:27:11 +00:00
|
|
|
int video_id;
|
|
|
|
std::string video_codec;
|
2015-02-17 13:10:06 +00:00
|
|
|
SrsRtpConn* video_rtp;
|
|
|
|
// audio stream.
|
2015-02-17 23:27:11 +00:00
|
|
|
int audio_id;
|
|
|
|
std::string audio_codec;
|
|
|
|
int audio_sample_rate;
|
|
|
|
int audio_channel;
|
2015-02-17 13:10:06 +00:00
|
|
|
SrsRtpConn* audio_rtp;
|
2015-02-17 08:28:28 +00:00
|
|
|
private:
|
2015-02-16 06:05:01 +00:00
|
|
|
st_netfd_t stfd;
|
|
|
|
SrsStSocket* skt;
|
|
|
|
SrsRtspStack* rtsp;
|
|
|
|
SrsRtspCaster* caster;
|
|
|
|
SrsThread* trd;
|
2015-02-18 14:28:39 +00:00
|
|
|
private:
|
|
|
|
SrsRequest* req;
|
|
|
|
SrsStSocket* io;
|
|
|
|
SrsRtmpClient* client;
|
|
|
|
SrsRtspJitter* vjitter;
|
|
|
|
SrsRtspJitter* ajitter;
|
|
|
|
int stream_id;
|
|
|
|
private:
|
|
|
|
SrsRawH264Stream* avc;
|
|
|
|
std::string h264_sps;
|
|
|
|
std::string h264_pps;
|
|
|
|
private:
|
|
|
|
SrsRawAacStream* aac;
|
|
|
|
SrsRawAacStreamCodec* acodec;
|
|
|
|
std::string aac_specific_config;
|
|
|
|
SrsRtspAudioCache* acache;
|
2015-02-15 13:28:31 +00:00
|
|
|
public:
|
2015-02-17 13:10:06 +00:00
|
|
|
SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o);
|
2015-02-15 13:28:31 +00:00
|
|
|
virtual ~SrsRtspConn();
|
2015-02-16 06:05:01 +00:00
|
|
|
public:
|
|
|
|
virtual int serve();
|
|
|
|
private:
|
|
|
|
virtual int do_cycle();
|
2015-02-18 03:49:42 +00:00
|
|
|
// internal methods
|
|
|
|
public:
|
2015-02-18 14:28:39 +00:00
|
|
|
virtual int on_rtp_packet(SrsRtpPacket* pkt, int stream_id);
|
2015-02-16 06:05:01 +00:00
|
|
|
// interface ISrsThreadHandler
|
|
|
|
public:
|
|
|
|
virtual int cycle();
|
|
|
|
virtual void on_thread_stop();
|
2015-02-18 14:28:39 +00:00
|
|
|
private:
|
|
|
|
virtual int on_rtp_video(SrsRtpPacket* pkt, int64_t dts, int64_t pts);
|
|
|
|
virtual int on_rtp_audio(SrsRtpPacket* pkt, int64_t dts);
|
|
|
|
virtual int kickoff_audio_cache(SrsRtpPacket* pkt, int64_t dts);
|
|
|
|
private:
|
|
|
|
virtual int write_sequence_header();
|
|
|
|
virtual int write_h264_sps_pps(u_int32_t dts, u_int32_t pts);
|
|
|
|
virtual int write_h264_ipb_frame(char* frame, int frame_size, u_int32_t dts, u_int32_t pts);
|
|
|
|
virtual int write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, u_int32_t dts);
|
|
|
|
virtual int rtmp_write_packet(char type, u_int32_t timestamp, char* data, int size);
|
|
|
|
private:
|
|
|
|
// connect to rtmp output url.
|
|
|
|
// @remark ignore when not connected, reconnect when disconnected.
|
|
|
|
virtual int connect();
|
|
|
|
virtual int connect_app(std::string ep_server, std::string ep_port);
|
|
|
|
// close the connected io and rtmp to ready to be re-connect.
|
|
|
|
virtual void close();
|
2015-02-16 06:05:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the caster for rtsp.
|
|
|
|
*/
|
2015-02-17 13:10:06 +00:00
|
|
|
class SrsRtspCaster : public ISrsTcpHandler
|
2015-02-16 06:05:01 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string output;
|
2015-02-17 08:28:28 +00:00
|
|
|
int local_port_min;
|
|
|
|
int local_port_max;
|
2015-02-17 13:10:06 +00:00
|
|
|
// key: port, value: whether used.
|
|
|
|
std::map<int, bool> used_ports;
|
2015-02-17 08:28:28 +00:00
|
|
|
private:
|
2015-02-16 06:05:01 +00:00
|
|
|
std::vector<SrsRtspConn*> clients;
|
|
|
|
public:
|
|
|
|
SrsRtspCaster(SrsConfDirective* c);
|
|
|
|
virtual ~SrsRtspCaster();
|
|
|
|
public:
|
2015-02-17 13:10:06 +00:00
|
|
|
/**
|
|
|
|
* alloc a rtp port from local ports pool.
|
|
|
|
* @param pport output the rtp port.
|
|
|
|
*/
|
|
|
|
virtual int alloc_port(int* pport);
|
|
|
|
/**
|
|
|
|
* free the alloced rtp port.
|
|
|
|
*/
|
|
|
|
virtual void free_port(int lpmin, int lpmax);
|
|
|
|
// interface ISrsTcpHandler
|
|
|
|
public:
|
|
|
|
virtual int on_tcp_client(st_netfd_t stfd);
|
2015-02-16 06:05:01 +00:00
|
|
|
// internal methods.
|
|
|
|
public:
|
|
|
|
virtual void remove(SrsRtspConn* conn);
|
2015-02-15 13:28:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|