From 86267f854cbda4969bd4364cf1ac56a5895e4fd0 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 2 Mar 2014 12:35:15 +0800 Subject: [PATCH] srs-librtmp: implements handshake. --- trunk/configure | 0 trunk/research/librtmp/srs_play.c | 3 +- trunk/src/libs/srs_lib_simple_socket.cpp | 18 ++++------ trunk/src/libs/srs_librtmp.cpp | 12 +++++++ trunk/src/libs/srs_librtmp.hpp | 13 ++++++- trunk/src/rtmp/srs_protocol_handshake.cpp | 44 ++++++++++++----------- trunk/src/rtmp/srs_protocol_handshake.hpp | 5 +-- trunk/src/rtmp/srs_protocol_rtmp.cpp | 22 ++++++++++-- trunk/src/rtmp/srs_protocol_rtmp.hpp | 5 +++ 9 files changed, 83 insertions(+), 39 deletions(-) mode change 100644 => 100755 trunk/configure diff --git a/trunk/configure b/trunk/configure old mode 100644 new mode 100755 diff --git a/trunk/research/librtmp/srs_play.c b/trunk/research/librtmp/srs_play.c index aa1e3787e..586d8626d 100644 --- a/trunk/research/librtmp/srs_play.c +++ b/trunk/research/librtmp/srs_play.c @@ -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"); - //if (srs_simple_handshake(rtmp) != 0) { - if (srs_complex_handshake(rtmp) != 0) { + if (srs_simple_handshake(rtmp) != 0) { printf("simple handshake failed.\n"); goto rtmp_destroy; } diff --git a/trunk/src/libs/srs_lib_simple_socket.cpp b/trunk/src/libs/srs_lib_simple_socket.cpp index 7f513469d..df2373d6a 100644 --- a/trunk/src/libs/srs_lib_simple_socket.cpp +++ b/trunk/src/libs/srs_lib_simple_socket.cpp @@ -81,20 +81,17 @@ void SimpleSocketStream::set_recv_timeout(int64_t timeout_us) int64_t SimpleSocketStream::get_recv_timeout() { - int ret = ERROR_SUCCESS; - return ret; + return -1; } int64_t SimpleSocketStream::get_recv_bytes() { - int ret = ERROR_SUCCESS; - return ret; + return 0; } int SimpleSocketStream::get_recv_kbps() { - int ret = ERROR_SUCCESS; - return ret; + return 0; } // ISrsProtocolWriter @@ -104,20 +101,17 @@ void SimpleSocketStream::set_send_timeout(int64_t timeout_us) int64_t SimpleSocketStream::get_send_timeout() { - int ret = ERROR_SUCCESS; - return ret; + return -1; } int64_t SimpleSocketStream::get_send_bytes() { - int ret = ERROR_SUCCESS; - return ret; + return 0; } int SimpleSocketStream::get_send_kbps() { - int ret = ERROR_SUCCESS; - return ret; + return 0; } int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite) diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index 5b30c617d..3f2b91faa 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -170,6 +170,10 @@ int srs_simple_handshake(srs_rtmp_t rtmp) srs_freep(context->rtmp); context->rtmp = new SrsRtmpClient(context->skt); + if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) { + return ret; + } + return ret; } @@ -197,6 +201,14 @@ int srs_publish_stream(srs_rtmp_t rtmp) return ERROR_SUCCESS; } +int srs_ssl_enabled() +{ +#ifndef SRS_SSL + return false; +#endif + return true; +} + int srs_version_major() { return ::atoi(VERSION_MAJOR); diff --git a/trunk/src/libs/srs_librtmp.hpp b/trunk/src/libs/srs_librtmp.hpp index a439c6ca9..2d88d41a5 100644 --- a/trunk/src/libs/srs_librtmp.hpp +++ b/trunk/src/libs/srs_librtmp.hpp @@ -66,7 +66,10 @@ void srs_rtmp_destroy(srs_rtmp_t rtmp); int srs_simple_handshake(srs_rtmp_t rtmp); /** * 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); @@ -97,6 +100,14 @@ int srs_play_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 */ diff --git a/trunk/src/rtmp/srs_protocol_handshake.cpp b/trunk/src/rtmp/srs_protocol_handshake.cpp index 461872852..af7f8f109 100644 --- a/trunk/src/rtmp/srs_protocol_handshake.cpp +++ b/trunk/src/rtmp/srs_protocol_handshake.cpp @@ -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; @@ -1090,16 +1090,18 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs srs_verbose("check c0 success, required plain text."); // try complex handshake - ret = complex_hs.handshake_with_client(skt, c0c1 + 1); - if (ret == ERROR_SUCCESS) { - srs_trace("complex handshake success."); - return ret; + if (complex_hs) { + ret = complex_hs->handshake_with_client(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("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]; srs_random_generate(s0s1s2, 3073); @@ -1125,21 +1127,23 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs 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; // try complex handshake - ret = complex_hs.handshake_with_server(skt); - if (ret == ERROR_SUCCESS) { - srs_trace("complex handshake success."); - return ret; + if (complex_hs) { + ret = complex_hs->handshake_with_server(skt); + 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("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 ssize_t nsize; diff --git a/trunk/src/rtmp/srs_protocol_handshake.hpp b/trunk/src/rtmp/srs_protocol_handshake.hpp index beef43260..a6d22a20c 100644 --- a/trunk/src/rtmp/srs_protocol_handshake.hpp +++ b/trunk/src/rtmp/srs_protocol_handshake.hpp @@ -45,10 +45,11 @@ public: /** * simple handshake. * @param complex_hs, try complex handshake first, + * if NULL, use simple handshake. * if failed, rollback to simple handshake. */ - virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs); - virtual int handshake_with_server(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); }; /** diff --git a/trunk/src/rtmp/srs_protocol_rtmp.cpp b/trunk/src/rtmp/srs_protocol_rtmp.cpp index 426bd5b9f..c1025ca29 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp.cpp +++ b/trunk/src/rtmp/srs_protocol_rtmp.cpp @@ -233,13 +233,31 @@ int SrsRtmpClient::handshake() SrsComplexHandshake complex_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; } +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 ret = ERROR_SUCCESS; @@ -495,7 +513,7 @@ int SrsRtmp::handshake() SrsComplexHandshake complex_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; } diff --git a/trunk/src/rtmp/srs_protocol_rtmp.hpp b/trunk/src/rtmp/srs_protocol_rtmp.hpp index cd4d6e71e..4b4f2bf73 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp.hpp +++ b/trunk/src/rtmp/srs_protocol_rtmp.hpp @@ -128,7 +128,12 @@ public: virtual int recv_message(SrsCommonMessage** pmsg); virtual int send_message(ISrsMessage* msg); public: + // try complex, then simple 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 create_stream(int& stream_id); virtual int play(std::string stream, int stream_id);