mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Refine api for srs-librtmp
This commit is contained in:
parent
af73e8ee65
commit
c849010fe2
6 changed files with 245 additions and 212 deletions
|
@ -63,18 +63,27 @@ ISrsThreadContext* _srs_context = new ISrsThreadContext();
|
|||
*/
|
||||
struct Context
|
||||
{
|
||||
// The original RTMP url.
|
||||
std::string url;
|
||||
|
||||
// Parse from url.
|
||||
std::string tcUrl;
|
||||
std::string host;
|
||||
std::string ip;
|
||||
int port;
|
||||
std::string vhost;
|
||||
std::string app;
|
||||
std::string stream;
|
||||
std::string param;
|
||||
|
||||
// Parse ip:port from host.
|
||||
std::string ip;
|
||||
int port;
|
||||
|
||||
// The URL schema, about vhost/app/stream?param
|
||||
srs_url_schema schema;
|
||||
// The server information, response by connect app.
|
||||
SrsServerInfo si;
|
||||
|
||||
// extra request object for connect to server, NULL to ignore.
|
||||
// The extra request object for connect to server, NULL to ignore.
|
||||
SrsRequest* req;
|
||||
|
||||
// the message received cache,
|
||||
|
@ -123,7 +132,7 @@ struct Context
|
|||
h264_sps_changed = false;
|
||||
h264_pps_changed = false;
|
||||
rtimeout = stimeout = SRS_CONSTS_NO_TMMS;
|
||||
schema = srs_url_schema_forbidden;
|
||||
schema = srs_url_schema_normal;
|
||||
}
|
||||
virtual ~Context() {
|
||||
srs_freep(req);
|
||||
|
@ -550,28 +559,6 @@ srs_rtmp_t srs_rtmp_create(const char* url)
|
|||
|
||||
return context;
|
||||
}
|
||||
|
||||
srs_rtmp_t srs_rtmp_create2(const char* url)
|
||||
{
|
||||
Context* context = new Context();
|
||||
|
||||
// use url as tcUrl.
|
||||
context->url = url;
|
||||
// auto append stream.
|
||||
context->url += "/livestream";
|
||||
|
||||
// create socket
|
||||
srs_freep(context->skt);
|
||||
context->skt = new SimpleSocketStream();
|
||||
|
||||
if (context->skt->create_socket(context) != ERROR_SUCCESS) {
|
||||
// free the context and return NULL
|
||||
srs_freep(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
int srs_rtmp_set_timeout(srs_rtmp_t rtmp, int recv_timeout_ms, int send_timeout_ms)
|
||||
{
|
||||
|
@ -690,6 +677,26 @@ int srs_rtmp_do_complex_handshake(srs_rtmp_t rtmp)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_do_simple_handshake(srs_rtmp_t rtmp)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(rtmp != NULL);
|
||||
Context* context = (Context*)rtmp;
|
||||
|
||||
srs_assert(context->skt != NULL);
|
||||
|
||||
// simple handshake
|
||||
srs_freep(context->rtmp);
|
||||
context->rtmp = new SrsRtmpClient(context->skt);
|
||||
|
||||
if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_set_connect_args(srs_rtmp_t rtmp,
|
||||
const char* tcUrl, const char* swfUrl, const char* pageUrl, srs_amf0_t args
|
||||
) {
|
||||
|
@ -716,23 +723,15 @@ int srs_rtmp_set_connect_args(srs_rtmp_t rtmp,
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_do_simple_handshake(srs_rtmp_t rtmp)
|
||||
|
||||
int srs_rtmp_set_schema(srs_rtmp_t rtmp, enum srs_url_schema schema)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(rtmp != NULL);
|
||||
Context* context = (Context*)rtmp;
|
||||
|
||||
srs_assert(context->skt != NULL);
|
||||
|
||||
// simple handshake
|
||||
srs_freep(context->rtmp);
|
||||
context->rtmp = new SrsRtmpClient(context->skt);
|
||||
|
||||
if ((ret = context->rtmp->simple_handshake()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
context->schema = schema;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -760,74 +759,46 @@ int srs_rtmp_connect_app(srs_rtmp_t rtmp)
|
|||
break;
|
||||
}
|
||||
|
||||
if ((ret = context->rtmp->connect_app(
|
||||
context->app, tcUrl, context->req, true)) != ERROR_SUCCESS)
|
||||
{
|
||||
Context* c = context;
|
||||
if ((ret = context->rtmp->connect_app(c->app, tcUrl, c->req, true, &c->si)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_connect_app2(srs_rtmp_t rtmp,
|
||||
char srs_server_ip[128],char srs_server[128],
|
||||
char srs_primary[128], char srs_authors[128],
|
||||
char srs_version[32], int* srs_id, int* srs_pid
|
||||
) {
|
||||
srs_server_ip[0] = 0;
|
||||
srs_server[0] = 0;
|
||||
srs_primary[0] = 0;
|
||||
srs_authors[0] = 0;
|
||||
srs_version[0] = 0;
|
||||
*srs_id = 0;
|
||||
*srs_pid = 0;
|
||||
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(rtmp != NULL);
|
||||
Context* context = (Context*)rtmp;
|
||||
|
||||
string tcUrl;
|
||||
switch(context->schema) {
|
||||
case srs_url_schema_normal:
|
||||
tcUrl=srs_generate_normal_tc_url(context->ip, context->vhost, context->app, context->port);
|
||||
break;
|
||||
case srs_url_schema_via:
|
||||
tcUrl=srs_generate_via_tc_url(context->ip, context->vhost, context->app, context->port);
|
||||
break;
|
||||
case srs_url_schema_vis:
|
||||
case srs_url_schema_vis2:
|
||||
tcUrl=srs_generate_vis_tc_url(context->ip, context->vhost, context->app, context->port);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
std::string sip, sserver, sprimary, sauthors, sversion;
|
||||
|
||||
if ((ret = context->rtmp->connect_app2(context->app, tcUrl, NULL, true,
|
||||
sip, sserver, sprimary, sauthors, sversion, *srs_id, *srs_pid)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
snprintf(srs_server_ip, 128, "%s", sip.c_str());
|
||||
snprintf(srs_server, 128, "%s", sserver.c_str());
|
||||
snprintf(srs_primary, 128, "%s", sprimary.c_str());
|
||||
snprintf(srs_authors, 128, "%s", sauthors.c_str());
|
||||
snprintf(srs_version, 32, "%s", sversion.c_str());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_set_schema(srs_rtmp_t rtmp, enum srs_url_schema schema)
|
||||
int srs_rtmp_get_server_id(srs_rtmp_t rtmp, char** ip, int* pid, int* cid)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(rtmp != NULL);
|
||||
Context* context = (Context*)rtmp;
|
||||
|
||||
context->schema = schema;
|
||||
|
||||
Context* context = (Context*)rtmp;
|
||||
*pid = context->si.pid;
|
||||
*cid = context->si.cid;
|
||||
*ip = context->si.ip.empty()? NULL:(char*)context->si.ip.c_str();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_get_server_sig(srs_rtmp_t rtmp, char** sig)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
Context* context = (Context*)rtmp;
|
||||
*sig = context->si.sig.empty()? NULL:(char*)context->si.sig.c_str();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int srs_rtmp_get_server_version(srs_rtmp_t rtmp, int* major, int* minor, int* revision, int* build)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
Context* context = (Context*)rtmp;
|
||||
*major = context->si.major;
|
||||
*minor = context->si.minor;
|
||||
*revision = context->si.revision;
|
||||
*build = context->si.build;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -2713,6 +2684,58 @@ srs_hijack_io_t srs_hijack_io_get(srs_rtmp_t rtmp)
|
|||
}
|
||||
#endif
|
||||
|
||||
srs_rtmp_t srs_rtmp_create2(const char* url)
|
||||
{
|
||||
Context* context = new Context();
|
||||
|
||||
// use url as tcUrl.
|
||||
context->url = url;
|
||||
// auto append stream.
|
||||
context->url += "/livestream";
|
||||
|
||||
// create socket
|
||||
srs_freep(context->skt);
|
||||
context->skt = new SimpleSocketStream();
|
||||
|
||||
if (context->skt->create_socket(context) != ERROR_SUCCESS) {
|
||||
// free the context and return NULL
|
||||
srs_freep(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
int srs_rtmp_connect_app2(srs_rtmp_t rtmp,
|
||||
char srs_server_ip[128],char srs_server[128],
|
||||
char srs_primary[128], char srs_authors[128],
|
||||
char srs_version[32], int* srs_id, int* srs_pid
|
||||
) {
|
||||
srs_server_ip[0] = 0;
|
||||
srs_server[0] = 0;
|
||||
srs_primary[0] = 0;
|
||||
srs_authors[0] = 0;
|
||||
srs_version[0] = 0;
|
||||
*srs_id = 0;
|
||||
*srs_pid = 0;
|
||||
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = srs_rtmp_connect_app(rtmp)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_assert(rtmp != NULL);
|
||||
Context* context = (Context*)rtmp;
|
||||
SrsServerInfo* si = &context->si;
|
||||
|
||||
snprintf(srs_server_ip, 128, "%s", si->ip.c_str());
|
||||
snprintf(srs_server, 128, "%s", si->sig.c_str());
|
||||
snprintf(srs_version, 32, "%d.%d.%d.%d", si->major, si->minor, si->revision, si->build);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -123,25 +123,14 @@ typedef void* srs_rtmp_t;
|
|||
typedef void* srs_amf0_t;
|
||||
|
||||
/**
|
||||
* create/destroy a rtmp protocol stack.
|
||||
* @url rtmp url, for example:
|
||||
* rtmp://localhost/live/livestream
|
||||
* Create a RTMP handler.
|
||||
* @param url The RTMP url, for example, rtmp://localhost/live/livestream
|
||||
* @remark default timeout to 30s if not set by srs_rtmp_set_timeout.
|
||||
* @remark default schema to srs_url_schema_normal, use srs_rtmp_set_schema to change it.
|
||||
*
|
||||
* @return a rtmp handler, or NULL if error occured.
|
||||
*/
|
||||
extern srs_rtmp_t srs_rtmp_create(const char* url);
|
||||
/**
|
||||
* create rtmp with url, used for connection specified application.
|
||||
* @param url the tcUrl, for exmple:
|
||||
* rtmp://localhost/live
|
||||
* @remark this is used to create application connection-oriented,
|
||||
* for example, the bandwidth client used this, no stream specified.
|
||||
* @remark default timeout to 30s if not set by srs_rtmp_set_timeout.
|
||||
*
|
||||
* @return a rtmp handler, or NULL if error occured.
|
||||
*/
|
||||
extern srs_rtmp_t srs_rtmp_create2(const char* url);
|
||||
/**
|
||||
* set socket timeout
|
||||
* @param recv_timeout_ms the timeout for receiving messages in ms.
|
||||
|
@ -202,43 +191,47 @@ extern int srs_rtmp_do_complex_handshake(srs_rtmp_t rtmp);
|
|||
extern int srs_rtmp_set_connect_args(srs_rtmp_t rtmp,
|
||||
const char* tcUrl, const char* swfUrl, const char* pageUrl, srs_amf0_t args
|
||||
);
|
||||
|
||||
/**
|
||||
* connect to rtmp vhost/app
|
||||
* category: publish/play
|
||||
* previous: handshake
|
||||
* next: publish or play
|
||||
*
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_connect_app(srs_rtmp_t rtmp);
|
||||
|
||||
/**
|
||||
* connect to server, get the debug srs info.
|
||||
*
|
||||
* SRS debug info:
|
||||
* @param srs_server_ip, 128bytes, debug info, server ip client connected at.
|
||||
* @param srs_server, 128bytes, server info.
|
||||
* @param srs_primary, 128bytes, primary authors.
|
||||
* @param srs_authors, 128bytes, authors.
|
||||
* @param srs_version, 32bytes, server version.
|
||||
* @param srs_id, int, debug info, client id in server log.
|
||||
* @param srs_pid, int, debug info, server pid in log.
|
||||
*
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_connect_app2(srs_rtmp_t rtmp,
|
||||
char srs_server_ip[128], char srs_server[128],
|
||||
char srs_primary[128], char srs_authors[128],
|
||||
char srs_version[32], int* srs_id, int* srs_pid
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the schema of URL when connect to server.
|
||||
* Set the schema of URL when connect to tcUrl by srs_rtmp_connect_app.
|
||||
* @param schema, The schema of URL, @see srs_url_schema.
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_set_schema(srs_rtmp_t rtmp, enum srs_url_schema schema);
|
||||
|
||||
/**
|
||||
* Connect to RTMP tcUrl(Vhost/App), similar to flash AS3 NetConnection.connect(tcUrl).
|
||||
* @remark When connected to server, user can retrieve informations from RTMP handler,
|
||||
* for example, use srs_rtmp_get_server_id to get server ip/pid/cid.
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_connect_app(srs_rtmp_t rtmp);
|
||||
|
||||
/**
|
||||
* Retrieve server ip from RTMP handler.
|
||||
* @Param ip A NULL terminated string specifies the server ip.
|
||||
* @Param pid An int specifies the PID of server. -1 is no PID information.
|
||||
* @Param cid An int specifies the CID of connection. -1 is no CID information.
|
||||
* @remark For SRS, ip/pid/cid is the UUID of a client. For other server, these values maybe unknown.
|
||||
* @remark When connected to server by srs_rtmp_connect_app, the information is ready to be retrieved.
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_get_server_id(srs_rtmp_t rtmp, char** ip, int* pid, int* cid);
|
||||
|
||||
/**
|
||||
* Retrieve server signature from RTMP handler.
|
||||
* @Param sig A NULL terminated string specifies the server signature.
|
||||
* @remark When connected to server by srs_rtmp_connect_app, the information is ready to be retrieved.
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_get_server_sig(srs_rtmp_t rtmp, char** sig);
|
||||
|
||||
/**
|
||||
* Retrieve server version from RTMP handler, which in major.minor.revision.build format.
|
||||
* @remark When connected to server by srs_rtmp_connect_app, the information is ready to be retrieved.
|
||||
* @return 0, success; otherswise, failed.
|
||||
*/
|
||||
extern int srs_rtmp_get_server_version(srs_rtmp_t rtmp, int* major, int* minor, int* revision, int* build);
|
||||
|
||||
/**
|
||||
* play a live/vod stream.
|
||||
|
@ -1235,6 +1228,26 @@ typedef void* srs_hijack_io_t;
|
|||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
/*************************************************************
|
||||
*************************************************************
|
||||
* Deprecated APIs.
|
||||
*************************************************************
|
||||
*************************************************************/
|
||||
/**
|
||||
* @Deprecated For bandwidth test check only.
|
||||
*/
|
||||
extern srs_rtmp_t srs_rtmp_create2(const char* url);
|
||||
|
||||
/**
|
||||
* @Deprecated Use seperate function to retrieve information from rtmp,
|
||||
* for example, use srs_rtmp_get_server_ip to get server ip.
|
||||
*/
|
||||
extern int srs_rtmp_connect_app2(srs_rtmp_t rtmp,
|
||||
char srs_server_ip[128], char srs_server[128],
|
||||
char srs_primary[128], char srs_authors[128],
|
||||
char srs_version[32], int* srs_id, int* srs_pid
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue