1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_rtsp.hpp

206 lines
5.7 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-12-30 02:10:35 +00:00
* Copyright (c) 2013-2020 Winlin
2017-03-25 09:21:39 +00:00
*
* 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_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>
2015-02-17 13:10:06 +00:00
#include <srs_app_listener.hpp>
#include <srs_service_conn.hpp>
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;
class SrsRtpPacket;
class SrsRequest;
class SrsStSocket;
class SrsRtmpClient;
class SrsRawH264Stream;
class SrsRawAacStream;
2015-03-13 04:53:01 +00:00
struct SrsRawAacStreamCodec;
class SrsSharedPtrMessage;
class SrsAudioFrame;
class SrsSimpleStream;
class SrsPithyPrint;
class SrsSimpleRtmpClient;
class SrsConnectionManager;
2019-04-30 00:24:52 +00:00
// A rtp connection which transport a stream.
2015-02-17 13:10:06 +00:00
class SrsRtpConn: public ISrsUdpHandler
{
2015-02-17 13:10:06 +00:00
private:
SrsPithyPrint* pprint;
2015-02-17 13:10:06 +00:00
SrsUdpListener* listener;
SrsRtspConn* rtsp;
SrsRtpPacket* cache;
int stream_id;
2015-02-17 13:10:06 +00:00
int _port;
public:
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 srs_error_t listen();
2019-04-30 00:30:13 +00:00
// Interface ISrsUdpHandler
2015-02-17 13:10:06 +00:00
public:
virtual srs_error_t on_udp_packet(const sockaddr* from, const int fromlen, char* buf, int nb_buf);
};
2019-04-30 00:24:52 +00:00
// The audio cache, audio is grouped by frames.
struct SrsRtspAudioCache
{
int64_t dts;
SrsAudioFrame* audio;
SrsSimpleStream* payload;
2017-03-25 09:21:39 +00:00
SrsRtspAudioCache();
virtual ~SrsRtspAudioCache();
};
2019-04-30 00:24:52 +00:00
// 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();
2018-01-01 11:39:57 +00:00
virtual srs_error_t correct(int64_t& ts);
};
2019-04-30 00:24:52 +00:00
// The rtsp connection serve the fd.
class SrsRtspConn : public ISrsCoroutineHandler, public ISrsConnection
{
private:
std::string output_template;
std::string rtsp_tcUrl;
std::string rtsp_stream;
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;
private:
srs_netfd_t stfd;
2015-02-16 06:05:01 +00:00
SrsStSocket* skt;
SrsRtspStack* rtsp;
SrsRtspCaster* caster;
SrsCoroutine* trd;
private:
SrsRequest* req;
SrsSimpleRtmpClient* sdk;
SrsRtspJitter* vjitter;
SrsRtspJitter* ajitter;
private:
SrsRawH264Stream* avc;
std::string h264_sps;
std::string h264_pps;
private:
SrsRawAacStream* aac;
SrsRawAacStreamCodec* acodec;
std::string aac_specific_config;
SrsRtspAudioCache* acache;
public:
SrsRtspConn(SrsRtspCaster* c, srs_netfd_t fd, std::string o);
virtual ~SrsRtspConn();
2015-02-16 06:05:01 +00:00
public:
virtual srs_error_t serve();
virtual std::string remote_ip();
2015-02-16 06:05:01 +00:00
private:
virtual srs_error_t do_cycle();
2018-01-01 13:20:57 +00:00
// internal methods
public:
2018-01-01 13:20:57 +00:00
virtual srs_error_t on_rtp_packet(SrsRtpPacket* pkt, int stream_id);
2019-04-30 00:30:13 +00:00
// Interface ISrsOneCycleThreadHandler
2015-02-16 06:05:01 +00:00
public:
virtual srs_error_t cycle();
private:
2018-01-01 11:39:57 +00:00
virtual srs_error_t on_rtp_video(SrsRtpPacket* pkt, int64_t dts, int64_t pts);
virtual srs_error_t on_rtp_audio(SrsRtpPacket* pkt, int64_t dts);
virtual srs_error_t kickoff_audio_cache(SrsRtpPacket* pkt, int64_t dts);
private:
2018-01-01 11:39:57 +00:00
virtual srs_error_t write_sequence_header();
virtual srs_error_t write_h264_sps_pps(uint32_t dts, uint32_t pts);
virtual srs_error_t write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts, uint32_t pts);
virtual srs_error_t write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, uint32_t dts);
virtual srs_error_t rtmp_write_packet(char type, uint32_t timestamp, char* data, int size);
private:
// Connect to RTMP server.
2018-01-01 11:39:57 +00:00
virtual srs_error_t connect();
// Close the connection to RTMP server.
virtual void close();
2015-02-16 06:05:01 +00:00
};
2019-04-30 00:24:52 +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:
2020-06-03 01:16:11 +00:00
std::string engine;
2015-02-16 06:05:01 +00:00
std::string output;
int local_port_min;
int local_port_max;
2019-04-30 00:24:52 +00:00
// The key: port, value: whether used.
2015-02-17 13:10:06 +00:00
std::map<int, bool> used_ports;
private:
2015-02-16 06:05:01 +00:00
std::vector<SrsRtspConn*> clients;
SrsConnectionManager* manager;
2015-02-16 06:05:01 +00:00
public:
SrsRtspCaster(SrsConfDirective* c);
virtual ~SrsRtspCaster();
public:
2019-04-30 00:24:52 +00:00
// Alloc a rtp port from local ports pool.
// @param pport output the rtp port.
2018-01-01 11:39:57 +00:00
virtual srs_error_t alloc_port(int* pport);
2019-04-30 00:24:52 +00:00
// Free the alloced rtp port.
2015-02-17 13:10:06 +00:00
virtual void free_port(int lpmin, int lpmax);
virtual srs_error_t initialize();
2019-04-30 00:30:13 +00:00
// Interface ISrsTcpHandler
2015-02-17 13:10:06 +00:00
public:
virtual srs_error_t on_tcp_client(srs_netfd_t stfd);
2019-04-30 00:24:52 +00:00
// internal methods.
2015-02-16 06:05:01 +00:00
public:
virtual void remove(SrsRtspConn* conn);
};
#endif