winlin

srs-librtmp: implements handshake.

@@ -37,8 +37,7 @@ int main(int argc, char** argv) @@ -37,8 +37,7 @@ int main(int argc, char** argv)
37 37
38 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/show?vhost=__defaultVhost__/livestream"); 38 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/show?vhost=__defaultVhost__/livestream");
39 39
40 - //if (srs_simple_handshake(rtmp) != 0) {  
41 - if (srs_complex_handshake(rtmp) != 0) { 40 + if (srs_simple_handshake(rtmp) != 0) {
42 printf("simple handshake failed.\n"); 41 printf("simple handshake failed.\n");
43 goto rtmp_destroy; 42 goto rtmp_destroy;
44 } 43 }
@@ -81,20 +81,17 @@ void SimpleSocketStream::set_recv_timeout(int64_t timeout_us) @@ -81,20 +81,17 @@ void SimpleSocketStream::set_recv_timeout(int64_t timeout_us)
81 81
82 int64_t SimpleSocketStream::get_recv_timeout() 82 int64_t SimpleSocketStream::get_recv_timeout()
83 { 83 {
84 - int ret = ERROR_SUCCESS;  
85 - return ret; 84 + return -1;
86 } 85 }
87 86
88 int64_t SimpleSocketStream::get_recv_bytes() 87 int64_t SimpleSocketStream::get_recv_bytes()
89 { 88 {
90 - int ret = ERROR_SUCCESS;  
91 - return ret; 89 + return 0;
92 } 90 }
93 91
94 int SimpleSocketStream::get_recv_kbps() 92 int SimpleSocketStream::get_recv_kbps()
95 { 93 {
96 - int ret = ERROR_SUCCESS;  
97 - return ret; 94 + return 0;
98 } 95 }
99 96
100 // ISrsProtocolWriter 97 // ISrsProtocolWriter
@@ -104,20 +101,17 @@ void SimpleSocketStream::set_send_timeout(int64_t timeout_us) @@ -104,20 +101,17 @@ void SimpleSocketStream::set_send_timeout(int64_t timeout_us)
104 101
105 int64_t SimpleSocketStream::get_send_timeout() 102 int64_t SimpleSocketStream::get_send_timeout()
106 { 103 {
107 - int ret = ERROR_SUCCESS;  
108 - return ret; 104 + return -1;
109 } 105 }
110 106
111 int64_t SimpleSocketStream::get_send_bytes() 107 int64_t SimpleSocketStream::get_send_bytes()
112 { 108 {
113 - int ret = ERROR_SUCCESS;  
114 - return ret; 109 + return 0;
115 } 110 }
116 111
117 int SimpleSocketStream::get_send_kbps() 112 int SimpleSocketStream::get_send_kbps()
118 { 113 {
119 - int ret = ERROR_SUCCESS;  
120 - return ret; 114 + return 0;
121 } 115 }
122 116
123 int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite) 117 int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
@@ -170,6 +170,10 @@ int srs_simple_handshake(srs_rtmp_t rtmp) @@ -170,6 +170,10 @@ int srs_simple_handshake(srs_rtmp_t rtmp)
170 srs_freep(context->rtmp); 170 srs_freep(context->rtmp);
171 context->rtmp = new SrsRtmpClient(context->skt); 171 context->rtmp = new SrsRtmpClient(context->skt);
172 172
  173 + if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
  174 + return ret;
  175 + }
  176 +
173 return ret; 177 return ret;
174 } 178 }
175 179
@@ -197,6 +201,14 @@ int srs_publish_stream(srs_rtmp_t rtmp) @@ -197,6 +201,14 @@ int srs_publish_stream(srs_rtmp_t rtmp)
197 return ERROR_SUCCESS; 201 return ERROR_SUCCESS;
198 } 202 }
199 203
  204 +int srs_ssl_enabled()
  205 +{
  206 +#ifndef SRS_SSL
  207 + return false;
  208 +#endif
  209 + return true;
  210 +}
  211 +
200 int srs_version_major() 212 int srs_version_major()
201 { 213 {
202 return ::atoi(VERSION_MAJOR); 214 return ::atoi(VERSION_MAJOR);
@@ -66,7 +66,10 @@ void srs_rtmp_destroy(srs_rtmp_t rtmp); @@ -66,7 +66,10 @@ void srs_rtmp_destroy(srs_rtmp_t rtmp);
66 int srs_simple_handshake(srs_rtmp_t rtmp); 66 int srs_simple_handshake(srs_rtmp_t rtmp);
67 /** 67 /**
68 * complex handshake is specified by adobe Flash player, 68 * complex handshake is specified by adobe Flash player,
69 -* depends on ssl, user must link libssl.a and libcrypt.a 69 +* depends on ssl, user must compile srs with ssl, then
  70 +* link user program libssl.a and libcrypt.a
  71 +* @remark user can use srs_ssl_enabled() to detect
  72 +* whether ssl is ok.
70 */ 73 */
71 int srs_complex_handshake(srs_rtmp_t rtmp); 74 int srs_complex_handshake(srs_rtmp_t rtmp);
72 75
@@ -98,6 +101,14 @@ int srs_play_stream(srs_rtmp_t rtmp); @@ -98,6 +101,14 @@ int srs_play_stream(srs_rtmp_t rtmp);
98 int srs_publish_stream(srs_rtmp_t rtmp); 101 int srs_publish_stream(srs_rtmp_t rtmp);
99 102
100 /** 103 /**
  104 +* whether srs is compiled with ssl,
  105 +* that is, compile srs with ssl: ./configure --with-ssl,.
  106 +* if no ssl, complex handshake always error.
  107 +* @return 0 for false, otherwise, true.
  108 +*/
  109 +int srs_ssl_enabled();
  110 +
  111 +/**
101 * get protocol stack version 112 * get protocol stack version
102 */ 113 */
103 int srs_version_major(); 114 int srs_version_major();
@@ -1067,7 +1067,7 @@ SrsSimpleHandshake::~SrsSimpleHandshake() @@ -1067,7 +1067,7 @@ SrsSimpleHandshake::~SrsSimpleHandshake()
1067 { 1067 {
1068 } 1068 }
1069 1069
1070 -int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs) 1070 +int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, SrsComplexHandshake* complex_hs)
1071 { 1071 {
1072 int ret = ERROR_SUCCESS; 1072 int ret = ERROR_SUCCESS;
1073 1073
@@ -1090,16 +1090,18 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs @@ -1090,16 +1090,18 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs
1090 srs_verbose("check c0 success, required plain text."); 1090 srs_verbose("check c0 success, required plain text.");
1091 1091
1092 // try complex handshake 1092 // try complex handshake
1093 - ret = complex_hs.handshake_with_client(skt, c0c1 + 1);  
1094 - if (ret == ERROR_SUCCESS) {  
1095 - srs_trace("complex handshake success.");  
1096 - return ret; 1093 + if (complex_hs) {
  1094 + ret = complex_hs->handshake_with_client(skt, c0c1 + 1);
  1095 + if (ret == ERROR_SUCCESS) {
  1096 + srs_trace("complex handshake success.");
  1097 + return ret;
  1098 + }
  1099 + if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {
  1100 + srs_error("complex handshake failed. ret=%d", ret);
  1101 + return ret;
  1102 + }
  1103 + srs_info("rollback complex to simple handshake. ret=%d", ret);
1097 } 1104 }
1098 - if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {  
1099 - srs_error("complex handshake failed. ret=%d", ret);  
1100 - return ret;  
1101 - }  
1102 - srs_info("rollback complex to simple handshake. ret=%d", ret);  
1103 1105
1104 char* s0s1s2 = new char[3073]; 1106 char* s0s1s2 = new char[3073];
1105 srs_random_generate(s0s1s2, 3073); 1107 srs_random_generate(s0s1s2, 3073);
@@ -1125,21 +1127,23 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs @@ -1125,21 +1127,23 @@ int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, Srs
1125 return ret; 1127 return ret;
1126 } 1128 }
1127 1129
1128 -int SrsSimpleHandshake::handshake_with_server(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs) 1130 +int SrsSimpleHandshake::handshake_with_server(ISrsProtocolReaderWriter* skt, SrsComplexHandshake* complex_hs)
1129 { 1131 {
1130 int ret = ERROR_SUCCESS; 1132 int ret = ERROR_SUCCESS;
1131 1133
1132 // try complex handshake 1134 // try complex handshake
1133 - ret = complex_hs.handshake_with_server(skt);  
1134 - if (ret == ERROR_SUCCESS) {  
1135 - srs_trace("complex handshake success.");  
1136 - return ret;  
1137 - }  
1138 - if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {  
1139 - srs_error("complex handshake failed. ret=%d", ret);  
1140 - return ret; 1135 + if (complex_hs) {
  1136 + ret = complex_hs->handshake_with_server(skt);
  1137 + if (ret == ERROR_SUCCESS) {
  1138 + srs_trace("complex handshake success.");
  1139 + return ret;
  1140 + }
  1141 + if (ret != ERROR_RTMP_TRY_SIMPLE_HS) {
  1142 + srs_error("complex handshake failed. ret=%d", ret);
  1143 + return ret;
  1144 + }
  1145 + srs_info("rollback complex to simple handshake. ret=%d", ret);
1141 } 1146 }
1142 - srs_info("rollback complex to simple handshake. ret=%d", ret);  
1143 1147
1144 // simple handshake 1148 // simple handshake
1145 ssize_t nsize; 1149 ssize_t nsize;
@@ -45,10 +45,11 @@ public: @@ -45,10 +45,11 @@ public:
45 /** 45 /**
46 * simple handshake. 46 * simple handshake.
47 * @param complex_hs, try complex handshake first, 47 * @param complex_hs, try complex handshake first,
  48 + * if NULL, use simple handshake.
48 * if failed, rollback to simple handshake. 49 * if failed, rollback to simple handshake.
49 */ 50 */
50 - virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs);  
51 - virtual int handshake_with_server(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs); 51 + virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake* complex_hs);
  52 + virtual int handshake_with_server(ISrsProtocolReaderWriter* io, SrsComplexHandshake* complex_hs);
52 }; 53 };
53 54
54 /** 55 /**
@@ -233,13 +233,31 @@ int SrsRtmpClient::handshake() @@ -233,13 +233,31 @@ int SrsRtmpClient::handshake()
233 233
234 SrsComplexHandshake complex_hs; 234 SrsComplexHandshake complex_hs;
235 SrsSimpleHandshake simple_hs; 235 SrsSimpleHandshake simple_hs;
236 - if ((ret = simple_hs.handshake_with_server(io, complex_hs)) != ERROR_SUCCESS) { 236 + if ((ret = simple_hs.handshake_with_server(io, &complex_hs)) != ERROR_SUCCESS) {
237 return ret; 237 return ret;
238 } 238 }
239 239
240 return ret; 240 return ret;
241 } 241 }
242 242
  243 +int SrsRtmpClient::simple_handshake()
  244 +{
  245 + int ret = ERROR_SUCCESS;
  246 +
  247 + SrsSimpleHandshake simple_hs;
  248 + if ((ret = simple_hs.handshake_with_server(io, NULL)) != ERROR_SUCCESS) {
  249 + return ret;
  250 + }
  251 +
  252 + return ret;
  253 +}
  254 +
  255 +int SrsRtmpClient::complex_handshake()
  256 +{
  257 + // TODO: FIXME: only use complex handshake.
  258 + return handshake();
  259 +}
  260 +
243 int SrsRtmpClient::connect_app(string app, string tc_url) 261 int SrsRtmpClient::connect_app(string app, string tc_url)
244 { 262 {
245 int ret = ERROR_SUCCESS; 263 int ret = ERROR_SUCCESS;
@@ -495,7 +513,7 @@ int SrsRtmp::handshake() @@ -495,7 +513,7 @@ int SrsRtmp::handshake()
495 513
496 SrsComplexHandshake complex_hs; 514 SrsComplexHandshake complex_hs;
497 SrsSimpleHandshake simple_hs; 515 SrsSimpleHandshake simple_hs;
498 - if ((ret = simple_hs.handshake_with_client(io, complex_hs)) != ERROR_SUCCESS) { 516 + if ((ret = simple_hs.handshake_with_client(io, &complex_hs)) != ERROR_SUCCESS) {
499 return ret; 517 return ret;
500 } 518 }
501 519
@@ -128,7 +128,12 @@ public: @@ -128,7 +128,12 @@ public:
128 virtual int recv_message(SrsCommonMessage** pmsg); 128 virtual int recv_message(SrsCommonMessage** pmsg);
129 virtual int send_message(ISrsMessage* msg); 129 virtual int send_message(ISrsMessage* msg);
130 public: 130 public:
  131 + // try complex, then simple handshake.
131 virtual int handshake(); 132 virtual int handshake();
  133 + // only use simple handshake
  134 + virtual int simple_handshake();
  135 + // only use complex handshake
  136 + virtual int complex_handshake();
132 virtual int connect_app(std::string app, std::string tc_url); 137 virtual int connect_app(std::string app, std::string tc_url);
133 virtual int create_stream(int& stream_id); 138 virtual int create_stream(int& stream_id);
134 virtual int play(std::string stream, int stream_id); 139 virtual int play(std::string stream, int stream_id);