1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #299, refine code.

This commit is contained in:
winlin 2017-03-25 17:21:39 +08:00
parent 62b7204514
commit f32aab3d92
175 changed files with 15529 additions and 15935 deletions

View file

@ -1,25 +1,25 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2017 SRS(ossrs)
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.
*/
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 SRS(ossrs)
*
* 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.
*/
#include <srs_app_rtsp.hpp>
@ -79,22 +79,22 @@ int SrsRtpConn::listen()
int SrsRtpConn::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
{
int ret = ERROR_SUCCESS;
pprint->elapse();
if (true) {
SrsBuffer stream;
if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
return ret;
}
SrsRtpPacket pkt;
if ((ret = pkt.decode(&stream)) != ERROR_SUCCESS) {
srs_error("rtsp: decode rtp packet failed. ret=%d", ret);
return ret;
}
if (pkt.chunked) {
if (!cache) {
cache = new SrsRtpPacket();
@ -102,10 +102,10 @@ int SrsRtpConn::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
cache->copy(&pkt);
cache->payload->append(pkt.payload->bytes(), pkt.payload->length());
if (!cache->completed && pprint->can_print()) {
srs_trace("<- "SRS_CONSTS_LOG_STREAM_CASTER" rtsp: rtp chunked %dB, age=%d, vt=%d/%u, sts=%u/%#x/%#x, paylod=%dB",
nb_buf, pprint->age(), cache->version, cache->payload_type, cache->sequence_number, cache->timestamp, cache->ssrc,
cache->payload->length()
);
srs_trace("<- "SRS_CONSTS_LOG_STREAM_CASTER" rtsp: rtp chunked %dB, age=%d, vt=%d/%u, sts=%u/%#x/%#x, paylod=%dB",
nb_buf, pprint->age(), cache->version, cache->payload_type, cache->sequence_number, cache->timestamp, cache->ssrc,
cache->payload->length()
);
return ret;
}
} else {
@ -114,14 +114,14 @@ int SrsRtpConn::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
cache->reap(&pkt);
}
}
if (pprint->can_print()) {
srs_trace("<- "SRS_CONSTS_LOG_STREAM_CASTER" rtsp: rtp #%d %dB, age=%d, vt=%d/%u, sts=%u/%u/%#x, paylod=%dB, chunked=%d",
stream_id, nb_buf, pprint->age(), cache->version, cache->payload_type, cache->sequence_number, cache->timestamp, cache->ssrc,
cache->payload->length(), cache->chunked
);
srs_trace("<- "SRS_CONSTS_LOG_STREAM_CASTER" rtsp: rtp #%d %dB, age=%d, vt=%d/%u, sts=%u/%u/%#x, paylod=%dB, chunked=%d",
stream_id, nb_buf, pprint->age(), cache->version, cache->payload_type, cache->sequence_number, cache->timestamp, cache->ssrc,
cache->payload->length(), cache->chunked
);
}
// always free it.
SrsAutoFree(SrsRtpPacket, cache);
@ -129,7 +129,7 @@ int SrsRtpConn::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
srs_error("rtsp: process rtp packet failed. ret=%d", ret);
return ret;
}
return ret;
}
@ -165,43 +165,43 @@ int64_t SrsRtspJitter::timestamp()
int SrsRtspJitter::correct(int64_t& ts)
{
int ret = ERROR_SUCCESS;
if (previous_timestamp == 0) {
previous_timestamp = ts;
}
delta = srs_max(0, (int)(ts - previous_timestamp));
if (delta > 90000) {
delta = 0;
}
previous_timestamp = ts;
ts = pts + delta;
pts = ts;
pts = ts;
return ret;
}
SrsRtspConn::SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o)
{
output_template = o;
session = "";
video_rtp = NULL;
audio_rtp = NULL;
caster = c;
stfd = fd;
skt = new SrsStSocket();
rtsp = new SrsRtspStack(skt);
trd = new SrsOneCycleThread("rtsp", this);
req = NULL;
sdk = NULL;
vjitter = new SrsRtspJitter();
ajitter = new SrsRtspJitter();
avc = new SrsRawH264Stream();
aac = new SrsRawAacStream();
acodec = new SrsRawAacStreamCodec();
@ -213,17 +213,17 @@ SrsRtspConn::~SrsRtspConn()
close();
srs_close_stfd(stfd);
srs_freep(video_rtp);
srs_freep(audio_rtp);
srs_freep(trd);
srs_freep(skt);
srs_freep(rtsp);
srs_freep(sdk);
srs_freep(req);
srs_freep(vjitter);
srs_freep(ajitter);
srs_freep(acodec);
@ -243,11 +243,11 @@ int SrsRtspConn::serve()
int SrsRtspConn::do_cycle()
{
int ret = ERROR_SUCCESS;
// retrieve ip of client.
std::string ip = srs_get_peer_ip(st_netfd_fileno(stfd));
srs_trace("rtsp: serve %s", ip.c_str());
// consume all rtsp messages.
for (;;) {
SrsRtspRequest* req = NULL;
@ -259,7 +259,7 @@ int SrsRtspConn::do_cycle()
}
SrsAutoFree(SrsRtspRequest, req);
srs_info("rtsp: got rtsp request");
if (req->is_options()) {
SrsRtspOptionsResponse* res = new SrsRtspOptionsResponse((int)req->seq);
res->session = session;
@ -278,7 +278,7 @@ int SrsRtspConn::do_cycle()
rtsp_tcUrl = rtsp_tcUrl.substr(0, pos);
}
srs_parse_rtmp_url(rtsp_tcUrl, rtsp_tcUrl, rtsp_stream);
srs_assert(req->sdp);
video_id = ::atoi(req->sdp->video_stream_id.c_str());
audio_id = ::atoi(req->sdp->audio_stream_id.c_str());
@ -289,12 +289,12 @@ int SrsRtspConn::do_cycle()
h264_sps = req->sdp->video_sps;
h264_pps = req->sdp->video_pps;
aac_specific_config = req->sdp->audio_sh;
srs_trace("rtsp: video(#%d, %s, %s/%s), audio(#%d, %s, %s/%s, %dHZ %dchannels), %s/%s",
video_id, video_codec.c_str(), req->sdp->video_protocol.c_str(), req->sdp->video_transport_format.c_str(),
audio_id, audio_codec.c_str(), req->sdp->audio_protocol.c_str(), req->sdp->audio_transport_format.c_str(),
audio_sample_rate, audio_channel, rtsp_tcUrl.c_str(), rtsp_stream.c_str()
);
srs_trace("rtsp: video(#%d, %s, %s/%s), audio(#%d, %s, %s/%s, %dHZ %dchannels), %s/%s",
video_id, video_codec.c_str(), req->sdp->video_protocol.c_str(), req->sdp->video_transport_format.c_str(),
audio_id, audio_codec.c_str(), req->sdp->audio_protocol.c_str(), req->sdp->audio_transport_format.c_str(),
audio_sample_rate, audio_channel, rtsp_tcUrl.c_str(), rtsp_stream.c_str()
);
SrsRtspResponse* res = new SrsRtspResponse((int)req->seq);
res->session = session;
if ((ret = rtsp->send_message(res)) != ERROR_SUCCESS) {
@ -310,7 +310,7 @@ int SrsRtspConn::do_cycle()
srs_error("rtsp: alloc port failed. ret=%d", ret);
return ret;
}
SrsRtpConn* rtp = NULL;
if (req->stream_id == video_id) {
srs_freep(video_rtp);
@ -323,18 +323,18 @@ int SrsRtspConn::do_cycle()
srs_error("rtsp: rtp listen at port=%d failed. ret=%d", lpm, ret);
return ret;
}
srs_trace("rtsp: #%d %s over %s/%s/%s %s client-port=%d-%d, server-port=%d-%d",
req->stream_id, (req->stream_id == video_id)? "Video":"Audio",
req->transport->transport.c_str(), req->transport->profile.c_str(), req->transport->lower_transport.c_str(),
req->transport->cast_type.c_str(), req->transport->client_port_min, req->transport->client_port_max,
lpm, lpm + 1
);
srs_trace("rtsp: #%d %s over %s/%s/%s %s client-port=%d-%d, server-port=%d-%d",
req->stream_id, (req->stream_id == video_id)? "Video":"Audio",
req->transport->transport.c_str(), req->transport->profile.c_str(), req->transport->lower_transport.c_str(),
req->transport->cast_type.c_str(), req->transport->client_port_min, req->transport->client_port_max,
lpm, lpm + 1
);
// create session.
if (session.empty()) {
session = "O9EaZ4bf"; // TODO: FIXME: generate session id.
}
SrsRtspSetupResponse* res = new SrsRtspSetupResponse((int)req->seq);
res->client_port_min = req->transport->client_port_min;
res->client_port_max = req->transport->client_port_max;
@ -358,19 +358,19 @@ int SrsRtspConn::do_cycle()
}
}
}
return ret;
}
int SrsRtspConn::on_rtp_packet(SrsRtpPacket* pkt, int stream_id)
{
int ret = ERROR_SUCCESS;
// ensure rtmp connected.
if ((ret = connect()) != ERROR_SUCCESS) {
return ret;
}
if (stream_id == video_id) {
// rtsp tbn is ts tbn.
int64_t pts = pkt->timestamp;
@ -378,10 +378,10 @@ int SrsRtspConn::on_rtp_packet(SrsRtpPacket* pkt, int stream_id)
srs_error("rtsp: correct by jitter failed. ret=%d", ret);
return ret;
}
// TODO: FIXME: set dts to pts, please finger out the right dts.
int64_t dts = pts;
return on_rtp_video(pkt, dts, pts);
} else {
// rtsp tbn is ts tbn.
@ -390,10 +390,10 @@ int SrsRtspConn::on_rtp_packet(SrsRtpPacket* pkt, int stream_id)
srs_error("rtsp: correct by jitter failed. ret=%d", ret);
return ret;
}
return on_rtp_audio(pkt, pts);
}
return ret;
}
@ -416,7 +416,7 @@ int SrsRtspConn::cycle()
if (ret == ERROR_SOCKET_CLOSED) {
srs_warn("client disconnect peer. ret=%d", ret);
}
return ERROR_SUCCESS;
}
@ -425,22 +425,22 @@ void SrsRtspConn::on_thread_stop()
if (video_rtp) {
caster->free_port(video_rtp->port(), video_rtp->port() + 1);
}
if (audio_rtp) {
caster->free_port(audio_rtp->port(), audio_rtp->port() + 1);
}
caster->remove(this);
}
int SrsRtspConn::on_rtp_video(SrsRtpPacket* pkt, int64_t dts, int64_t pts)
{
int ret = ERROR_SUCCESS;
if ((ret = kickoff_audio_cache(pkt, dts)) != ERROR_SUCCESS) {
return ret;
}
char* bytes = pkt->payload->bytes();
int length = pkt->payload->length();
uint32_t fdts = (uint32_t)(dts / 90);
@ -448,38 +448,38 @@ int SrsRtspConn::on_rtp_video(SrsRtpPacket* pkt, int64_t dts, int64_t pts)
if ((ret = write_h264_ipb_frame(bytes, length, fdts, fpts)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
int SrsRtspConn::on_rtp_audio(SrsRtpPacket* pkt, int64_t dts)
{
int ret = ERROR_SUCCESS;
if ((ret = kickoff_audio_cache(pkt, dts)) != ERROR_SUCCESS) {
return ret;
}
// cache current audio to kickoff.
acache->dts = dts;
acache->audio = pkt->audio;
acache->payload = pkt->payload;
pkt->audio = NULL;
pkt->payload = NULL;
return ret;
}
int SrsRtspConn::kickoff_audio_cache(SrsRtpPacket* pkt, int64_t dts)
{
int ret = ERROR_SUCCESS;
// nothing to kick off.
if (!acache->payload) {
return ret;
}
if (dts - acache->dts > 0 && acache->audio->nb_samples > 0) {
int64_t delta = (dts - acache->dts) / acache->audio->nb_samples;
for (int i = 0; i < acache->audio->nb_samples; i++) {
@ -492,30 +492,30 @@ int SrsRtspConn::kickoff_audio_cache(SrsRtpPacket* pkt, int64_t dts)
}
}
}
acache->dts = 0;
srs_freep(acache->audio);
srs_freep(acache->payload);
return ret;
}
int SrsRtspConn::write_sequence_header()
{
int ret = ERROR_SUCCESS;
// use the current dts.
int64_t dts = vjitter->timestamp() / 90;
// send video sps/pps
if ((ret = write_h264_sps_pps((uint32_t)dts, (uint32_t)dts)) != ERROR_SUCCESS) {
return ret;
}
// generate audio sh by audio specific config.
if (true) {
std::string sh = aac_specific_config;
SrsFormat* format = new SrsFormat();
SrsAutoFree(SrsFormat, format);
@ -524,12 +524,12 @@ int SrsRtspConn::write_sequence_header()
}
SrsAudioCodecConfig* dec = format->acodec;
acodec->sound_format = SrsAudioCodecIdAAC;
acodec->sound_type = (dec->aac_channels == 2)? SrsAudioChannelsStereo : SrsAudioChannelsMono;
acodec->sound_size = SrsAudioSampleBits16bit;
acodec->aac_packet_type = 0;
static int srs_aac_srates[] = {
96000, 88200, 64000, 48000,
44100, 32000, 24000, 22050,
@ -549,12 +549,12 @@ int SrsRtspConn::write_sequence_header()
default:
break;
};
if ((ret = write_audio_raw_frame((char*)sh.data(), (int)sh.length(), acodec, (uint32_t)dts)) != ERROR_SUCCESS) {
return ret;
}
}
return ret;
}
@ -582,11 +582,11 @@ int SrsRtspConn::write_h264_sps_pps(uint32_t dts, uint32_t pts)
if ((ret = rtmp_write_packet(SrsFrameTypeVideo, timestamp, flv, nb_flv)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts, uint32_t pts)
int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts, uint32_t pts)
{
int ret = ERROR_SUCCESS;
@ -600,7 +600,7 @@ int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts,
if (nal_unit_type == SrsAvcNaluTypeIDR) {
frame_type = SrsVideoAvcFrameTypeKeyFrame;
}
std::string ibp;
if ((ret = avc->mux_ipb_frame(frame, frame_size, ibp)) != ERROR_SUCCESS) {
return ret;
@ -621,7 +621,7 @@ int SrsRtspConn::write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts,
int SrsRtspConn::write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, uint32_t dts)
{
int ret = ERROR_SUCCESS;
char* data = NULL;
int size = 0;
if ((ret = aac->mux_aac2flv(frame, frame_size, codec, dts, &data, &size)) != ERROR_SUCCESS) {
@ -640,13 +640,13 @@ int SrsRtspConn::rtmp_write_packet(char type, uint32_t timestamp, char* data, in
}
SrsSharedPtrMessage* msg = NULL;
if ((ret = srs_rtmp_create_msg(type, timestamp, data, size, sdk->sid(), &msg)) != ERROR_SUCCESS) {
srs_error("rtsp: create shared ptr msg failed. ret=%d", ret);
return ret;
}
srs_assert(msg);
// send out encoded msg.
if ((ret = sdk->send_and_free_message(msg)) != ERROR_SUCCESS) {
close();
@ -659,7 +659,7 @@ int SrsRtspConn::rtmp_write_packet(char type, uint32_t timestamp, char* data, in
int SrsRtspConn::connect()
{
int ret = ERROR_SUCCESS;
// Ignore when connected.
if (sdk) {
return ret;
@ -671,13 +671,13 @@ int SrsRtspConn::connect()
std::string schema, host, vhost, app, param;
int port;
srs_discovery_tc_url(rtsp_tcUrl, schema, host, vhost, app, port, param);
// generate output by template.
std::string output = output_template;
output = srs_string_replace(output, "[app]", app);
output = srs_string_replace(output, "[stream]", rtsp_stream);
}
// connect host.
int64_t cto = SRS_CONSTS_RTMP_TMMS;
int64_t sto = SRS_CONSTS_RTMP_PULSE_TMMS;
@ -695,7 +695,7 @@ int SrsRtspConn::connect()
srs_error("rtsp: publish %s failed. ret=%d", url.c_str(), ret);
return ret;
}
return write_sequence_header();
}
@ -726,7 +726,7 @@ SrsRtspCaster::~SrsRtspCaster()
int SrsRtspCaster::alloc_port(int* pport)
{
int ret = ERROR_SUCCESS;
// use a pair of port.
for (int i = local_port_min; i < local_port_max - 1; i += 2) {
if (!used_ports[i]) {
@ -737,7 +737,7 @@ int SrsRtspCaster::alloc_port(int* pport)
}
}
srs_info("rtsp: alloc port=%d-%d", *pport, *pport + 1);
return ret;
}
@ -752,18 +752,18 @@ void SrsRtspCaster::free_port(int lpmin, int lpmax)
int SrsRtspCaster::on_tcp_client(st_netfd_t stfd)
{
int ret = ERROR_SUCCESS;
SrsRtspConn* conn = new SrsRtspConn(this, stfd, output);
if ((ret = conn->serve()) != ERROR_SUCCESS) {
srs_error("rtsp: serve client failed. ret=%d", ret);
srs_freep(conn);
return ret;
}
clients.push_back(conn);
srs_info("rtsp: start thread to serve client.");
return ret;
}
@ -774,7 +774,7 @@ void SrsRtspCaster::remove(SrsRtspConn* conn)
clients.erase(it);
}
srs_info("rtsp: remove connection from caster.");
srs_freep(conn);
}