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

srs-librtmp: implements handshake.

This commit is contained in:
winlin 2014-03-02 12:35:15 +08:00
parent f24f27deb9
commit 86267f854c
9 changed files with 83 additions and 39 deletions

0
trunk/configure vendored Normal file → Executable file
View file

View file

@ -37,8 +37,7 @@ int main(int argc, char** argv)
rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/show?vhost=__defaultVhost__/livestream"); rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/show?vhost=__defaultVhost__/livestream");
//if (srs_simple_handshake(rtmp) != 0) { if (srs_simple_handshake(rtmp) != 0) {
if (srs_complex_handshake(rtmp) != 0) {
printf("simple handshake failed.\n"); printf("simple handshake failed.\n");
goto rtmp_destroy; goto rtmp_destroy;
} }

View file

@ -81,20 +81,17 @@ void SimpleSocketStream::set_recv_timeout(int64_t timeout_us)
int64_t SimpleSocketStream::get_recv_timeout() int64_t SimpleSocketStream::get_recv_timeout()
{ {
int ret = ERROR_SUCCESS; return -1;
return ret;
} }
int64_t SimpleSocketStream::get_recv_bytes() int64_t SimpleSocketStream::get_recv_bytes()
{ {
int ret = ERROR_SUCCESS; return 0;
return ret;
} }
int SimpleSocketStream::get_recv_kbps() int SimpleSocketStream::get_recv_kbps()
{ {
int ret = ERROR_SUCCESS; return 0;
return ret;
} }
// ISrsProtocolWriter // ISrsProtocolWriter
@ -104,20 +101,17 @@ void SimpleSocketStream::set_send_timeout(int64_t timeout_us)
int64_t SimpleSocketStream::get_send_timeout() int64_t SimpleSocketStream::get_send_timeout()
{ {
int ret = ERROR_SUCCESS; return -1;
return ret;
} }
int64_t SimpleSocketStream::get_send_bytes() int64_t SimpleSocketStream::get_send_bytes()
{ {
int ret = ERROR_SUCCESS; return 0;
return ret;
} }
int SimpleSocketStream::get_send_kbps() int SimpleSocketStream::get_send_kbps()
{ {
int ret = ERROR_SUCCESS; return 0;
return ret;
} }
int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite) int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite)

View file

@ -170,6 +170,10 @@ int srs_simple_handshake(srs_rtmp_t rtmp)
srs_freep(context->rtmp); srs_freep(context->rtmp);
context->rtmp = new SrsRtmpClient(context->skt); context->rtmp = new SrsRtmpClient(context->skt);
if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
return ret;
}
return ret; return ret;
} }
@ -197,6 +201,14 @@ int srs_publish_stream(srs_rtmp_t rtmp)
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
int srs_ssl_enabled()
{
#ifndef SRS_SSL
return false;
#endif
return true;
}
int srs_version_major() int srs_version_major()
{ {
return ::atoi(VERSION_MAJOR); return ::atoi(VERSION_MAJOR);

View file

@ -66,7 +66,10 @@ void srs_rtmp_destroy(srs_rtmp_t rtmp);
int srs_simple_handshake(srs_rtmp_t rtmp); int srs_simple_handshake(srs_rtmp_t rtmp);
/** /**
* complex handshake is specified by adobe Flash player, * complex handshake is specified by adobe Flash player,
* depends on ssl, user must link libssl.a and libcrypt.a * depends on ssl, user must compile srs with ssl, then
* link user program libssl.a and libcrypt.a
* @remark user can use srs_ssl_enabled() to detect
* whether ssl is ok.
*/ */
int srs_complex_handshake(srs_rtmp_t rtmp); int srs_complex_handshake(srs_rtmp_t rtmp);
@ -97,6 +100,14 @@ int srs_play_stream(srs_rtmp_t rtmp);
*/ */
int srs_publish_stream(srs_rtmp_t rtmp); int srs_publish_stream(srs_rtmp_t rtmp);
/**
* whether srs is compiled with ssl,
* that is, compile srs with ssl: ./configure --with-ssl,.
* if no ssl, complex handshake always error.
* @return 0 for false, otherwise, true.
*/
int srs_ssl_enabled();
/** /**
* get protocol stack version * get protocol stack version
*/ */

View file

@ -1067,7 +1067,7 @@ SrsSimpleHandshake::~SrsSimpleHandshake()
{ {
} }
int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs) int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, SrsComplexHandshake* complex_hs)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -1090,16 +1090,18 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs
srs_verbose("check c0 success, required plain text."); srs_verbose("check c0 success, required plain text.");
// try complex handshake // try complex handshake
ret = complex_hs.handshake_with_client(skt, c0c1 + 1); if (complex_hs) {
if (ret == ERROR_SUCCESS) { ret = complex_hs->handshake_with_client(skt, c0c1 + 1);
srs_trace("complex handshake success."); if (ret == ERROR_SUCCESS) {
return ret; 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("rollback complex to simple handshake. ret=%d", ret);
} }
if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {
srs_error("complex handshake failed. ret=%d", ret);
return ret;
}
srs_info("rollback complex to simple handshake. ret=%d", ret);
char* s0s1s2 = new char[3073]; char* s0s1s2 = new char[3073];
srs_random_generate(s0s1s2, 3073); srs_random_generate(s0s1s2, 3073);
@ -1125,21 +1127,23 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs
return ret; return ret;
} }
int SrsSimpleHandshake::handshake_with_server(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs) int SrsSimpleHandshake::handshake_with_server(ISrsProtocolReaderWriter* skt, SrsComplexHandshake* complex_hs)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// try complex handshake // try complex handshake
ret = complex_hs.handshake_with_server(skt); if (complex_hs) {
if (ret == ERROR_SUCCESS) { ret = complex_hs->handshake_with_server(skt);
srs_trace("complex handshake success."); if (ret == ERROR_SUCCESS) {
return ret; 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("rollback complex to simple handshake. ret=%d", ret);
} }
if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {
srs_error("complex handshake failed. ret=%d", ret);
return ret;
}
srs_info("rollback complex to simple handshake. ret=%d", ret);
// simple handshake // simple handshake
ssize_t nsize; ssize_t nsize;

View file

@ -45,10 +45,11 @@ public:
/** /**
* simple handshake. * simple handshake.
* @param complex_hs, try complex handshake first, * @param complex_hs, try complex handshake first,
* if NULL, use simple handshake.
* if failed, rollback to simple handshake. * if failed, rollback to simple handshake.
*/ */
virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs); virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake* complex_hs);
virtual int handshake_with_server(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs); virtual int handshake_with_server(ISrsProtocolReaderWriter* io, SrsComplexHandshake* complex_hs);
}; };
/** /**

View file

@ -233,13 +233,31 @@ int SrsRtmpClient::handshake()
SrsComplexHandshake complex_hs; SrsComplexHandshake complex_hs;
SrsSimpleHandshake simple_hs; SrsSimpleHandshake simple_hs;
if ((ret = simple_hs.handshake_with_server(io, complex_hs)) != ERROR_SUCCESS) { if ((ret = simple_hs.handshake_with_server(io, &complex_hs)) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ret; return ret;
} }
int SrsRtmpClient::simple_handshake()
{
int ret = ERROR_SUCCESS;
SrsSimpleHandshake simple_hs;
if ((ret = simple_hs.handshake_with_server(io, NULL)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
int SrsRtmpClient::complex_handshake()
{
// TODO: FIXME: only use complex handshake.
return handshake();
}
int SrsRtmpClient::connect_app(string app, string tc_url) int SrsRtmpClient::connect_app(string app, string tc_url)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -495,7 +513,7 @@ int SrsRtmp::handshake()
SrsComplexHandshake complex_hs; SrsComplexHandshake complex_hs;
SrsSimpleHandshake simple_hs; SrsSimpleHandshake simple_hs;
if ((ret = simple_hs.handshake_with_client(io, complex_hs)) != ERROR_SUCCESS) { if ((ret = simple_hs.handshake_with_client(io, &complex_hs)) != ERROR_SUCCESS) {
return ret; return ret;
} }

View file

@ -128,7 +128,12 @@ public:
virtual int recv_message(SrsCommonMessage** pmsg); virtual int recv_message(SrsCommonMessage** pmsg);
virtual int send_message(ISrsMessage* msg); virtual int send_message(ISrsMessage* msg);
public: public:
// try complex, then simple handshake.
virtual int handshake(); virtual int handshake();
// only use simple handshake
virtual int simple_handshake();
// only use complex handshake
virtual int complex_handshake();
virtual int connect_app(std::string app, std::string tc_url); virtual int connect_app(std::string app, std::string tc_url);
virtual int create_stream(int& stream_id); virtual int create_stream(int& stream_id);
virtual int play(std::string stream, int stream_id); virtual int play(std::string stream, int stream_id);