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

srs-librtmp: implements play stream.

This commit is contained in:
winlin 2014-03-02 14:07:48 +08:00
parent 6b213b73eb
commit fce26d6690

555
trunk/src/libs/srs_librtmp.cpp Executable file → Normal file
View file

@ -1,275 +1,280 @@
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013-2014 winlin Copyright (c) 2013-2014 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of 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 this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 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, the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 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 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 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. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <srs_librtmp.hpp> #include <srs_librtmp.hpp>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
using namespace std; using namespace std;
#include <srs_kernel_error.hpp> #include <srs_kernel_error.hpp>
#include <srs_protocol_rtmp.hpp> #include <srs_protocol_rtmp.hpp>
#include <srs_lib_simple_socket.hpp> #include <srs_lib_simple_socket.hpp>
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
#include <srs_protocol_utility.hpp> #include <srs_protocol_utility.hpp>
// if user want to define log, define the folowing macro. // if user want to define log, define the folowing macro.
#ifndef SRS_RTMP_USER_DEFINED_LOG #ifndef SRS_RTMP_USER_DEFINED_LOG
// kernel module. // kernel module.
ISrsLog* _srs_log = new ISrsLog(); ISrsLog* _srs_log = new ISrsLog();
ISrsThreadContext* _srs_context = new ISrsThreadContext(); ISrsThreadContext* _srs_context = new ISrsThreadContext();
#endif #endif
/** /**
* export runtime context. * export runtime context.
*/ */
struct Context struct Context
{ {
std::string url; std::string url;
std::string tcUrl; std::string tcUrl;
std::string host; std::string host;
std::string port; std::string port;
std::string vhost; std::string vhost;
std::string app; std::string app;
std::string stream; std::string stream;
SrsRtmpClient* rtmp; SrsRtmpClient* rtmp;
SimpleSocketStream* skt; SimpleSocketStream* skt;
int stream_id; int stream_id;
Context() { Context() {
rtmp = NULL; rtmp = NULL;
skt = NULL; skt = NULL;
stream_id = 0; stream_id = 0;
} }
virtual ~Context() { virtual ~Context() {
srs_freep(rtmp); srs_freep(rtmp);
srs_freep(skt); srs_freep(skt);
} }
}; };
int srs_librtmp_context_connect(Context* context) int srs_librtmp_context_connect(Context* context)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// parse uri // parse uri
size_t pos = string::npos; size_t pos = string::npos;
string uri = context->url; string uri = context->url;
// tcUrl, stream // tcUrl, stream
if ((pos = uri.rfind("/")) != string::npos) { if ((pos = uri.rfind("/")) != string::npos) {
context->stream = uri.substr(pos + 1); context->stream = uri.substr(pos + 1);
context->tcUrl = uri = uri.substr(0, pos); context->tcUrl = uri = uri.substr(0, pos);
} }
// schema // schema
if ((pos = uri.find("rtmp://")) != string::npos) { if ((pos = uri.find("rtmp://")) != string::npos) {
uri = uri.substr(pos + 7); uri = uri.substr(pos + 7);
} }
// host/vhost/port // host/vhost/port
if ((pos = uri.find(":")) != string::npos) { if ((pos = uri.find(":")) != string::npos) {
context->vhost = context->host = uri.substr(0, pos); context->vhost = context->host = uri.substr(0, pos);
uri = uri.substr(pos + 1); uri = uri.substr(pos + 1);
if ((pos = uri.find("/")) != string::npos) { if ((pos = uri.find("/")) != string::npos) {
context->port = uri.substr(0, pos); context->port = uri.substr(0, pos);
uri = uri.substr(pos + 1); uri = uri.substr(pos + 1);
} }
} else { } else {
if ((pos = uri.find("/")) != string::npos) { if ((pos = uri.find("/")) != string::npos) {
context->vhost = context->host = uri.substr(0, pos); context->vhost = context->host = uri.substr(0, pos);
uri = uri.substr(pos + 1); uri = uri.substr(pos + 1);
} }
context->port = RTMP_DEFAULT_PORT; context->port = RTMP_DEFAULT_PORT;
} }
// app // app
context->app = uri; context->app = uri;
// query of app // query of app
if ((pos = uri.find("?")) != string::npos) { if ((pos = uri.find("?")) != string::npos) {
context->app = uri.substr(0, pos); context->app = uri.substr(0, pos);
string query = uri.substr(pos + 1); string query = uri.substr(pos + 1);
if ((pos = query.find("vhost=")) != string::npos) { if ((pos = query.find("vhost=")) != string::npos) {
context->vhost = query.substr(pos + 6); context->vhost = query.substr(pos + 6);
if ((pos = context->vhost.find("&")) != string::npos) { if ((pos = context->vhost.find("&")) != string::npos) {
context->vhost = context->vhost.substr(pos); context->vhost = context->vhost.substr(pos);
} }
} }
} }
// create socket // create socket
srs_freep(context->skt); srs_freep(context->skt);
context->skt = new SimpleSocketStream(); context->skt = new SimpleSocketStream();
if ((ret = context->skt->create_socket()) != ERROR_SUCCESS) { if ((ret = context->skt->create_socket()) != ERROR_SUCCESS) {
return ret; return ret;
} }
// connect to server:port // connect to server:port
string server = srs_dns_resolve(context->host); string server = srs_dns_resolve(context->host);
if (server.empty()) { if (server.empty()) {
return -1; return -1;
} }
if ((ret = context->skt->connect(server.c_str(), ::atoi(context->port.c_str()))) != ERROR_SUCCESS) { if ((ret = context->skt->connect(server.c_str(), ::atoi(context->port.c_str()))) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ret; return ret;
} }
#ifdef __cplusplus #ifdef __cplusplus
extern "C"{ extern "C"{
#endif #endif
srs_rtmp_t srs_rtmp_create(const char* url) srs_rtmp_t srs_rtmp_create(const char* url)
{ {
Context* context = new Context(); Context* context = new Context();
context->url = url; context->url = url;
return context; return context;
} }
void srs_rtmp_destroy(srs_rtmp_t rtmp) void srs_rtmp_destroy(srs_rtmp_t rtmp)
{ {
srs_assert(rtmp != NULL); srs_assert(rtmp != NULL);
Context* context = (Context*)rtmp; Context* context = (Context*)rtmp;
srs_freep(context); srs_freep(context);
} }
int srs_simple_handshake(srs_rtmp_t rtmp) int srs_simple_handshake(srs_rtmp_t rtmp)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(rtmp != NULL); srs_assert(rtmp != NULL);
Context* context = (Context*)rtmp; Context* context = (Context*)rtmp;
// parse uri, resolve host, connect to server:port // parse uri, resolve host, connect to server:port
if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) { if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) {
return ret; return ret;
} }
// simple handshake // simple handshake
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) { if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ret; return ret;
} }
int srs_complex_handshake(srs_rtmp_t rtmp) int srs_complex_handshake(srs_rtmp_t rtmp)
{ {
#ifndef SRS_SSL #ifndef SRS_SSL
return ERROR_RTMP_HS_SSL_REQUIRE; return ERROR_RTMP_HS_SSL_REQUIRE;
#endif #endif
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(rtmp != NULL); srs_assert(rtmp != NULL);
Context* context = (Context*)rtmp; Context* context = (Context*)rtmp;
// parse uri, resolve host, connect to server:port // parse uri, resolve host, connect to server:port
if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) { if ((ret = srs_librtmp_context_connect(context)) != ERROR_SUCCESS) {
return ret; return ret;
} }
// complex handshake // complex handshake
srs_freep(context->rtmp); srs_freep(context->rtmp);
context->rtmp = new SrsRtmpClient(context->skt); context->rtmp = new SrsRtmpClient(context->skt);
if ((ret = context->rtmp->complex_handshake()) != ERROR_SUCCESS) { if ((ret = context->rtmp->complex_handshake()) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ret; return ret;
} }
int srs_connect_app(srs_rtmp_t rtmp) int srs_connect_app(srs_rtmp_t rtmp)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(rtmp != NULL); srs_assert(rtmp != NULL);
Context* context = (Context*)rtmp; Context* context = (Context*)rtmp;
string tcUrl = "rtmp://"; string tcUrl = "rtmp://";
tcUrl += context->vhost; tcUrl += context->vhost;
tcUrl += ":"; tcUrl += ":";
tcUrl += context->port; tcUrl += context->port;
tcUrl += "/"; tcUrl += "/";
tcUrl += context->app; tcUrl += context->app;
if ((ret = context->rtmp->connect_app(context->app, tcUrl)) != ERROR_SUCCESS) { if ((ret = context->rtmp->connect_app(context->app, tcUrl)) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ret; return ret;
} }
int srs_play_stream(srs_rtmp_t rtmp) int srs_play_stream(srs_rtmp_t rtmp)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(rtmp != NULL); srs_assert(rtmp != NULL);
Context* context = (Context*)rtmp; Context* context = (Context*)rtmp;
if ((ret = context->rtmp->create_stream(context->stream_id)) != ERROR_SUCCESS) { if ((ret = context->rtmp->create_stream(context->stream_id)) != ERROR_SUCCESS) {
return ret; return ret;
} }
if ((ret = context->rtmp->play(context->stream, context->stream_id)) != ERROR_SUCCESS) { if ((ret = context->rtmp->play(context->stream, context->stream_id)) != ERROR_SUCCESS) {
return ret; return ret;
} }
return ERROR_SUCCESS; return ret;
} }
int srs_publish_stream(srs_rtmp_t rtmp) int srs_publish_stream(srs_rtmp_t rtmp)
{ {
return ERROR_SUCCESS; int ret = ERROR_SUCCESS;
}
srs_assert(rtmp != NULL);
int srs_ssl_enabled() Context* context = (Context*)rtmp;
{
#ifndef SRS_SSL return ret;
return false; }
#endif
return true; int srs_ssl_enabled()
} {
#ifndef SRS_SSL
int srs_version_major() return false;
{ #endif
return ::atoi(VERSION_MAJOR); return true;
} }
int srs_version_minor() int srs_version_major()
{ {
return ::atoi(VERSION_MINOR); return ::atoi(VERSION_MAJOR);
} }
int srs_version_revision() int srs_version_minor()
{ {
return ::atoi(VERSION_REVISION); return ::atoi(VERSION_MINOR);
} }
#ifdef __cplusplus int srs_version_revision()
} {
#endif return ::atoi(VERSION_REVISION);
}
#ifdef __cplusplus
}
#endif