mirror of
https://github.com/ossrs/srs.git
synced 2025-02-12 11:21:52 +00:00
add rtmp complex handshake classes
This commit is contained in:
parent
598aaa5c98
commit
e6ca039dd6
10 changed files with 176 additions and 28 deletions
12
README.md
12
README.md
|
@ -25,4 +25,16 @@ Compare:<br/>
|
|||
* nginx-rtmp v1.0.4: 26786 lines <br/>
|
||||
* nginx v1.5.0: 139524 lines <br/>
|
||||
|
||||
Features:<br/>
|
||||
* v0.2, 2013-10-25, support h264/avc codec by rtmp complex handshake(SrsComplexHandshake).
|
||||
* v0.2, 2013-10-24, support time jitter detect and correct algorithm(SrsConsumer::jitter_correct).
|
||||
* v0.2, 2013-10-24, support decode the video/audio codec type(SrsCodec), cache the h264/avc sequence header.
|
||||
* v0.1, 2013-10-23, support basic amf0 codec, simplify the api using c-style api.
|
||||
* v0.1, 2013-10-23, support shared ptr msg(SrsSharedPtrMessage) for zero memory copy.
|
||||
* v0.1, 2013-10-22, support vp6 codec with rtmp protocol specified simple handshake.
|
||||
* v0.1, 2013-10-20, support multiple flash client play live streaming.
|
||||
* v0.1, 2013-10-20, support FMLE/FFMPEG publish live streaming.
|
||||
* v0.1, 2013-10-18, support rtmp message2chunk protocol(send message).
|
||||
* v0.1, 2013-10-17, support rtmp chunk2message protocol(recv message).
|
||||
|
||||
Winlin
|
||||
|
|
3
trunk/configure
vendored
3
trunk/configure
vendored
|
@ -86,7 +86,8 @@ MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server"
|
|||
"srs_core_error" "srs_core_conn" "srs_core_client"
|
||||
"srs_core_rtmp" "srs_core_socket" "srs_core_buffer"
|
||||
"srs_core_auto_free" "srs_core_protocol" "srs_core_amf0"
|
||||
"srs_core_stream" "srs_core_source" "srs_core_codec")
|
||||
"srs_core_stream" "srs_core_source" "srs_core_codec"
|
||||
"srs_core_complex_handshake")
|
||||
MODULE_DIR="src/core" . auto/modules.sh
|
||||
CORE_OBJS="${MODULE_OBJS[@]}"
|
||||
|
||||
|
|
41
trunk/src/core/srs_core_complex_handshake.cpp
Executable file
41
trunk/src/core/srs_core_complex_handshake.cpp
Executable file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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.
|
||||
*/
|
||||
|
||||
#include <srs_core_complex_handshake.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
SrsComplexHandshake::SrsComplexHandshake()
|
||||
{
|
||||
}
|
||||
|
||||
SrsComplexHandshake::~SrsComplexHandshake()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsComplexHandshake::handshake(SrsSocket& skt, char* c1)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
return ret;
|
||||
}
|
||||
|
58
trunk/src/core/srs_core_complex_handshake.hpp
Executable file
58
trunk/src/core/srs_core_complex_handshake.hpp
Executable file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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_CORE_COMPLEX_HANDSHKAE_HPP
|
||||
#define SRS_CORE_COMPLEX_HANDSHKAE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_complex_handshake.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
class SrsSocket;
|
||||
|
||||
/**
|
||||
* rtmp complex handshake,
|
||||
* @see also crtmp(crtmpserver) or librtmp,
|
||||
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
|
||||
*/
|
||||
class SrsComplexHandshake
|
||||
{
|
||||
public:
|
||||
SrsComplexHandshake();
|
||||
virtual ~SrsComplexHandshake();
|
||||
public:
|
||||
/**
|
||||
* complex hanshake.
|
||||
* @c1, size of c1 must be 1536.
|
||||
* @remark, user must free the c1.
|
||||
* @return user must:
|
||||
* continue connect app if success,
|
||||
* try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS,
|
||||
* otherwise, disconnect
|
||||
*/
|
||||
virtual int handshake(SrsSocket& skt, char* c1);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -62,6 +62,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#define ERROR_RTMP_MESSAGE_ENCODE 308
|
||||
#define ERROR_RTMP_AMF0_ENCODE 309
|
||||
#define ERROR_RTMP_CHUNK_SIZE 310
|
||||
#define ERROR_RTMP_TRY_SIMPLE_HS 311
|
||||
|
||||
#define ERROR_SYSTEM_STREAM_INIT 400
|
||||
#define ERROR_SYSTEM_PACKET_INVALID 401
|
||||
|
|
|
@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_core_protocol.hpp>
|
||||
#include <srs_core_auto_free.hpp>
|
||||
#include <srs_core_amf0.hpp>
|
||||
#include <srs_core_complex_handshake.hpp>
|
||||
|
||||
/**
|
||||
* the signature for packets to client.
|
||||
|
@ -139,11 +140,13 @@ SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
|
|||
{
|
||||
protocol = new SrsProtocol(client_stfd);
|
||||
stfd = client_stfd;
|
||||
complex_handshake = new SrsComplexHandshake();
|
||||
}
|
||||
|
||||
SrsRtmp::~SrsRtmp()
|
||||
{
|
||||
srs_freep(protocol);
|
||||
srs_freep(complex_handshake);
|
||||
}
|
||||
|
||||
void SrsRtmp::set_recv_timeout(int timeout_ms)
|
||||
|
@ -190,6 +193,18 @@ int SrsRtmp::handshake()
|
|||
}
|
||||
srs_verbose("check c0 success, required plain text.");
|
||||
|
||||
// try complex handshake
|
||||
ret = complex_handshake->handshake(skt, c0c1 + 1);
|
||||
if (ret == ERROR_SUCCESS) {
|
||||
srs_trace("complex handshake success.");
|
||||
return ret;
|
||||
}
|
||||
if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {
|
||||
srs_error("complex handshake failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("complex handhskae failed, try simple. ret=%d", ret);
|
||||
|
||||
char* s0s1s2 = new char[3073];
|
||||
SrsAutoFree(char, s0s1s2, true);
|
||||
// plain text required.
|
||||
|
@ -208,7 +223,7 @@ int SrsRtmp::handshake()
|
|||
}
|
||||
srs_verbose("read c2 success.");
|
||||
|
||||
srs_trace("handshake success.");
|
||||
srs_trace("simple handshake success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ class ISrsMessage;
|
|||
class SrsCommonMessage;
|
||||
class SrsCreateStreamPacket;
|
||||
class SrsFMLEStartPacket;
|
||||
class SrsComplexHandshake;
|
||||
|
||||
/**
|
||||
* the original request from client.
|
||||
|
@ -95,6 +96,7 @@ enum SrsClientType
|
|||
class SrsRtmp
|
||||
{
|
||||
private:
|
||||
SrsComplexHandshake* complex_handshake;
|
||||
SrsProtocol* protocol;
|
||||
st_netfd_t stfd;
|
||||
public:
|
||||
|
|
|
@ -68,34 +68,10 @@ int SrsConsumer::enqueue(SrsSharedPtrMessage* msg)
|
|||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
/**
|
||||
* we use a very simple time jitter detect/correct algorithm:
|
||||
* 1. delta: ensure the delta is positive and valid,
|
||||
* we set the delta to DEFAULT_FRAME_TIME_MS,
|
||||
* if the delta of time is nagative or greater than CONST_MAX_JITTER_MS.
|
||||
* 2. last_pkt_time: specifies the original packet time,
|
||||
* is used to detect next jitter.
|
||||
* 3. last_pkt_correct_time: simply add the positive delta,
|
||||
* and enforce the time monotonically.
|
||||
*/
|
||||
int32_t time = msg->header.timestamp;
|
||||
int32_t delta = time - last_pkt_time;
|
||||
|
||||
// if jitter detected, reset the delta.
|
||||
if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
|
||||
delta = DEFAULT_FRAME_TIME_MS;
|
||||
|
||||
srs_info("jitter detected, delta=%d, last_pkt=%d, time=%d, correct_to=%d",
|
||||
delta, last_pkt_time, time, last_pkt_correct_time + delta);
|
||||
} else {
|
||||
srs_verbose("timestamp no jitter. time=%d, last_pkt=%d, correct_to=%d",
|
||||
time, last_pkt_time, last_pkt_correct_time + delta);
|
||||
if ((ret = jitter_correct(msg)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta);
|
||||
msg->header.timestamp = last_pkt_correct_time;
|
||||
last_pkt_time = time;
|
||||
|
||||
msgs.push_back(msg);
|
||||
|
||||
return ret;
|
||||
|
@ -130,6 +106,41 @@ int SrsConsumer::get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& c
|
|||
return ret;
|
||||
}
|
||||
|
||||
int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
/**
|
||||
* we use a very simple time jitter detect/correct algorithm:
|
||||
* 1. delta: ensure the delta is positive and valid,
|
||||
* we set the delta to DEFAULT_FRAME_TIME_MS,
|
||||
* if the delta of time is nagative or greater than CONST_MAX_JITTER_MS.
|
||||
* 2. last_pkt_time: specifies the original packet time,
|
||||
* is used to detect next jitter.
|
||||
* 3. last_pkt_correct_time: simply add the positive delta,
|
||||
* and enforce the time monotonically.
|
||||
*/
|
||||
int32_t time = msg->header.timestamp;
|
||||
int32_t delta = time - last_pkt_time;
|
||||
|
||||
// if jitter detected, reset the delta.
|
||||
if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
|
||||
delta = DEFAULT_FRAME_TIME_MS;
|
||||
|
||||
srs_info("jitter detected, delta=%d, last_pkt=%d, time=%d, correct_to=%d",
|
||||
delta, last_pkt_time, time, last_pkt_correct_time + delta);
|
||||
} else {
|
||||
srs_verbose("timestamp no jitter. time=%d, last_pkt=%d, correct_to=%d",
|
||||
time, last_pkt_time, last_pkt_correct_time + delta);
|
||||
}
|
||||
|
||||
last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta);
|
||||
msg->header.timestamp = last_pkt_correct_time;
|
||||
last_pkt_time = time;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsSource::SrsSource(std::string _stream_url)
|
||||
{
|
||||
stream_url = _stream_url;
|
||||
|
|
|
@ -65,6 +65,11 @@ public:
|
|||
* @max_count the max count to dequeue, 0 to dequeue all.
|
||||
*/
|
||||
virtual int get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count);
|
||||
private:
|
||||
/**
|
||||
* detect the time jitter and correct it.
|
||||
*/
|
||||
virtual int jitter_correct(SrsSharedPtrMessage* msg);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,6 +20,8 @@ file
|
|||
..\core\srs_core_codec.cpp,
|
||||
..\core\srs_core_rtmp.hpp,
|
||||
..\core\srs_core_rtmp.cpp,
|
||||
..\core\srs_core_complex_handshake.hpp,
|
||||
..\core\srs_core_complex_handshake.cpp,
|
||||
..\core\srs_core_protocol.hpp,
|
||||
..\core\srs_core_protocol.cpp,
|
||||
..\core\srs_core_amf0.hpp,
|
||||
|
|
Loading…
Reference in a new issue