use system utility for string finds

pull/510/head
winlin 9 years ago
parent ca73534d7e
commit d9f991ed2f

@ -268,19 +268,8 @@ int SrsDynamicHttpConn::connect()
// parse uri // parse uri
if (!req) { if (!req) {
req = new SrsRequest(); req = new SrsRequest();
srs_parse_rtmp_url(output, req->tcUrl, req->stream);
size_t pos = string::npos; srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->port, req->param);
string uri = req->tcUrl = output;
// tcUrl, stream
if ((pos = uri.rfind("/")) != string::npos) {
req->stream = uri.substr(pos + 1);
req->tcUrl = uri = uri.substr(0, pos);
}
srs_discovery_tc_url(req->tcUrl,
req->schema, req->host, req->vhost, req->app, req->port,
req->param);
} }
// connect host. // connect host.

@ -107,7 +107,7 @@ int SrsFlvSegment::open(bool use_tmp_file)
bool fresh_flv_file = !srs_path_exists(path); bool fresh_flv_file = !srs_path_exists(path);
// create dir first. // create dir first.
std::string dir = path.substr(0, path.rfind("/")); std::string dir = srs_path_dirname(path);
if ((ret = srs_create_dir_recursively(dir)) != ERROR_SUCCESS) { if ((ret = srs_create_dir_recursively(dir)) != ERROR_SUCCESS) {
srs_error("create dir=%s failed. ret=%d", dir.c_str(), ret); srs_error("create dir=%s failed. ret=%d", dir.c_str(), ret);
return ret; return ret;

@ -572,12 +572,7 @@ int SrsHttpMessage::update(string url, bool allow_jsonp, http_parser* header, Sr
} }
// parse ext. // parse ext.
_ext = _uri->get_path(); _ext = srs_path_filext(_uri->get_path());
if ((pos = _ext.rfind(".")) != string::npos) {
_ext = _ext.substr(pos);
} else {
_ext = "";
}
// parse jsonp request message. // parse jsonp request message.
if (allow_jsonp) { if (allow_jsonp) {
@ -816,23 +811,23 @@ SrsRequest* SrsHttpMessage::to_request(string vhost)
{ {
SrsRequest* req = new SrsRequest(); SrsRequest* req = new SrsRequest();
req->app = _uri->get_path(); // http path, for instance, /live/livestream.flv, parse to
size_t pos = string::npos; // app: /live
if ((pos = req->app.rfind("/")) != string::npos) { // stream: livestream.flv
req->stream = req->app.substr(pos + 1); srs_parse_rtmp_url(_uri->get_path(), req->app, req->stream);
req->app = req->app.substr(0, pos);
} // trim the start slash, for instance, /live to live
if ((pos = req->stream.rfind(".")) != string::npos) { req->app = srs_string_trim_start(req->app, "/");
req->stream = req->stream.substr(0, pos);
} // remove the extension, for instance, livestream.flv to livestream
req->stream = srs_path_filename(req->stream);
req->tcUrl = "rtmp://" + vhost + req->app; // generate others.
req->tcUrl = "rtmp://" + vhost + "/" + req->app;
req->pageUrl = get_request_header("Referer"); req->pageUrl = get_request_header("Referer");
req->objectEncoding = 0; req->objectEncoding = 0;
srs_discovery_tc_url(req->tcUrl, srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->port, req->param);
req->schema, req->host, req->vhost, req->app, req->port,
req->param);
req->strip(); req->strip();
return req; return req;

@ -242,7 +242,7 @@ int SrsHttpStaticServer::initialize()
mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/"); mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
// the dir mount must always ends with "/" // the dir mount must always ends with "/"
if (mount != "/" && mount.rfind("/") != mount.length() - 1) { if (mount != "/" && !srs_string_ends_with(mount, "/")) {
mount += "/"; mount += "/";
} }

@ -611,11 +611,7 @@ SrsLiveEntry::SrsLiveEntry(std::string m, bool h)
req = NULL; req = NULL;
source = NULL; source = NULL;
std::string ext; std::string ext = srs_path_filext(m);
size_t pos = string::npos;
if ((pos = m.rfind(".")) != string::npos) {
ext = m.substr(pos);
}
_is_flv = (ext == ".flv"); _is_flv = (ext == ".flv");
_is_ts = (ext == ".ts"); _is_ts = (ext == ".ts");
_is_mp3 = (ext == ".mp3"); _is_mp3 = (ext == ".mp3");
@ -1319,10 +1315,7 @@ string SrsHttpStreamServer::hls_mount_generate(SrsRequest* r, string uri, string
std::string mount = tmpl; std::string mount = tmpl;
// the ts is relative from the m3u8, the same start dir. // the ts is relative from the m3u8, the same start dir.
size_t pos = string::npos; mount = srs_path_dirname(mount);
if ((pos = mount.rfind("/")) != string::npos) {
mount = mount.substr(0, pos);
}
// replace the vhost variable // replace the vhost variable
mount = srs_string_replace(mount, "[vhost]", r->vhost); mount = srs_string_replace(mount, "[vhost]", r->vhost);

@ -35,6 +35,7 @@ using namespace std;
#include <srs_app_pithy_print.hpp> #include <srs_app_pithy_print.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp> #include <srs_app_utility.hpp>
#include <srs_protocol_utility.hpp>
// when error, ingester sleep for a while and retry. // when error, ingester sleep for a while and retry.
// ingest never sleep a long time, for we must start the stream ASAP. // ingest never sleep a long time, for we must start the stream ASAP.
@ -354,17 +355,9 @@ int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* vhost, S
} }
// find the app and stream in rtmp url // find the app and stream in rtmp url
std::string url = output;
std::string app, stream; std::string app, stream;
size_t pos = std::string::npos; srs_parse_rtmp_url(output, app, stream);
if ((pos = url.rfind("/")) != std::string::npos) { size_t pos;
stream = url.substr(pos + 1);
url = url.substr(0, pos);
}
if ((pos = url.rfind("/")) != std::string::npos) {
app = url.substr(pos + 1);
url = url.substr(0, pos);
}
if ((pos = app.rfind("?")) != std::string::npos) { if ((pos = app.rfind("?")) != std::string::npos) {
app = app.substr(0, pos); app = app.substr(0, pos);
} }

@ -611,19 +611,8 @@ int SrsMpegtsOverUdp::connect()
// parse uri // parse uri
if (!req) { if (!req) {
req = new SrsRequest(); req = new SrsRequest();
srs_parse_rtmp_url(output, req->tcUrl, req->stream);
size_t pos = string::npos; srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->port, req->param);
string uri = req->tcUrl = output;
// tcUrl, stream
if ((pos = uri.rfind("/")) != string::npos) {
req->stream = uri.substr(pos + 1);
req->tcUrl = uri = uri.substr(0, pos);
}
srs_discovery_tc_url(req->tcUrl,
req->schema, req->host, req->vhost, req->app, req->port,
req->param);
} }
// connect host. // connect host.

@ -270,10 +270,7 @@ int SrsRtspConn::do_cycle()
if ((pos = rtsp_tcUrl.rfind(".sdp")) != string::npos) { if ((pos = rtsp_tcUrl.rfind(".sdp")) != string::npos) {
rtsp_tcUrl = rtsp_tcUrl.substr(0, pos); rtsp_tcUrl = rtsp_tcUrl.substr(0, pos);
} }
if ((pos = rtsp_tcUrl.rfind("/")) != string::npos) { srs_parse_rtmp_url(rtsp_tcUrl, rtsp_tcUrl, rtsp_stream);
rtsp_stream = rtsp_tcUrl.substr(pos + 1);
rtsp_tcUrl = rtsp_tcUrl.substr(0, pos);
}
srs_assert(req->sdp); srs_assert(req->sdp);
video_id = ::atoi(req->sdp->video_stream_id.c_str()); video_id = ::atoi(req->sdp->video_stream_id.c_str());
@ -651,8 +648,6 @@ int SrsRtspConn::connect()
// parse uri // parse uri
if (!req) { if (!req) {
req = new SrsRequest();
std::string schema, host, vhost, app, port, param; std::string schema, host, vhost, app, port, param;
srs_discovery_tc_url(rtsp_tcUrl, schema, host, vhost, app, port, param); srs_discovery_tc_url(rtsp_tcUrl, schema, host, vhost, app, port, param);
@ -661,18 +656,9 @@ int SrsRtspConn::connect()
output = srs_string_replace(output, "[app]", app); output = srs_string_replace(output, "[app]", app);
output = srs_string_replace(output, "[stream]", rtsp_stream); output = srs_string_replace(output, "[stream]", rtsp_stream);
size_t pos = string::npos; req = new SrsRequest();
string uri = req->tcUrl = output; srs_parse_rtmp_url(output, req->tcUrl, req->stream);
srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->port, req->param);
// tcUrl, stream
if ((pos = uri.rfind("/")) != string::npos) {
req->stream = uri.substr(pos + 1);
req->tcUrl = uri = uri.substr(0, pos);
}
srs_discovery_tc_url(req->tcUrl,
req->schema, req->host, req->vhost, req->app, req->port,
req->param);
} }
// connect host. // connect host.

@ -202,6 +202,17 @@ public:
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
}; };
/**
* the common tcp client, to connect to specified TCP server,
* reconnect and close the connection.
*/
class SrsTcpClient
{
public:
SrsTcpClient();
virtual ~SrsTcpClient();
};
// initialize st, requires epoll. // initialize st, requires epoll.
extern int srs_st_init(); extern int srs_st_init();

@ -465,13 +465,36 @@ string srs_path_basename(string path)
return dirname; return dirname;
} }
string srs_path_filename(string path)
{
std::string filename = path;
size_t pos = string::npos;
if ((pos = filename.rfind(".")) != string::npos) {
return filename.substr(0, pos);
}
return filename;
}
string srs_path_filext(string path)
{
size_t pos = string::npos;
if ((pos = path.rfind(".")) != string::npos) {
return path.substr(pos);
}
return "";
}
bool srs_avc_startswith_annexb(SrsBuffer* stream, int* pnb_start_code) bool srs_avc_startswith_annexb(SrsBuffer* stream, int* pnb_start_code)
{ {
char* bytes = stream->data() + stream->pos(); char* bytes = stream->data() + stream->pos();
char* p = bytes; char* p = bytes;
for (;;) { for (;;) {
if (!stream->require(p - bytes + 3)) { if (!stream->require((int)(p - bytes + 3))) {
return false; return false;
} }

@ -96,10 +96,14 @@ extern int srs_create_dir_recursively(std::string dir);
// whether path exists. // whether path exists.
extern bool srs_path_exists(std::string path); extern bool srs_path_exists(std::string path);
// get the dirname of path // get the dirname of path, for instance, filename("/live/livestream")="/live"
extern std::string srs_path_dirname(std::string path); extern std::string srs_path_dirname(std::string path);
// get the basename of path // get the basename of path, for instance, filename("/live/livestream")="livestream"
extern std::string srs_path_basename(std::string path); extern std::string srs_path_basename(std::string path);
// get the filename of path, for instance, filename("livestream.flv")="livestream"
extern std::string srs_path_filename(std::string path);
// get the file extension of path, for instance, filext("live.flv")=".flv"
extern std::string srs_path_filext(std::string path);
/** /**
* whether stream starts with the avc NALU in "AnnexB" * whether stream starts with the avc NALU in "AnnexB"

@ -464,16 +464,9 @@ int srs_librtmp_context_parse_uri(Context* context)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// parse uri
size_t pos = string::npos;
string uri = context->url;
// tcUrl, stream
if ((pos = uri.rfind("/")) != string::npos) {
context->stream = uri.substr(pos + 1);
context->tcUrl = uri = uri.substr(0, pos);
}
std::string schema; std::string schema;
srs_parse_rtmp_url(context->url, context->tcUrl, context->stream);
srs_discovery_tc_url(context->tcUrl, srs_discovery_tc_url(context->tcUrl,
schema, context->host, context->vhost, context->app, context->port, schema, context->host, context->vhost, context->app, context->port,
context->param); context->param);

@ -377,11 +377,7 @@ int SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r,
} }
if (true) { if (true) {
size_t pos; std::string ext = srs_path_filext(fullpath);
std::string ext = fullpath;
if ((pos = ext.rfind(".")) != string::npos) {
ext = ext.substr(pos);
}
if (_mime.find(ext) == _mime.end()) { if (_mime.find(ext) == _mime.end()) {
w->header()->set_content_type("application/octet-stream"); w->header()->set_content_type("application/octet-stream");

@ -44,7 +44,7 @@ using namespace std;
void srs_discovery_tc_url( void srs_discovery_tc_url(
string tcUrl, string tcUrl,
string& schema, string& host, string& vhost, string& schema, string& host, string& vhost,
string& app, int& port, std::string& param string& app, int& port, string& param
) { ) {
size_t pos = std::string::npos; size_t pos = std::string::npos;
std::string url = tcUrl; std::string url = tcUrl;
@ -229,7 +229,7 @@ int srs_rtmp_create_msg(char type, u_int32_t timestamp, char* data, int size, in
return ret; return ret;
} }
std::string srs_generate_stream_url(std::string vhost, std::string app, std::string stream) string srs_generate_stream_url(string vhost, string app, string stream)
{ {
std::string url = ""; std::string url = "";
@ -244,6 +244,18 @@ std::string srs_generate_stream_url(std::string vhost, std::string app, std::str
return url; return url;
} }
void srs_parse_rtmp_url(string url, string& tcUrl, string& stream)
{
size_t pos;
if ((pos = url.rfind("/")) != string::npos) {
stream = url.substr(pos + 1);
tcUrl = url.substr(0, pos);
} else {
tcUrl = url;
}
}
string srs_generate_rtmp_url(string server, int port, string vhost, string app, string stream) string srs_generate_rtmp_url(string server, int port, string vhost, string app, string stream)
{ {
std::stringstream ss; std::stringstream ss;

@ -101,16 +101,34 @@ extern bool srs_bytes_equals(void* pa, void* pb, int size);
* @param data the packet bytes. user should never free it. * @param data the packet bytes. user should never free it.
* @param ppmsg output the shared ptr message. user should free it. * @param ppmsg output the shared ptr message. user should free it.
*/ */
extern int srs_rtmp_create_msg(char type, u_int32_t timestamp, char* data, int size, int stream_id, SrsSharedPtrMessage** ppmsg); extern int srs_rtmp_create_msg(
char type, u_int32_t timestamp, char* data, int size, int stream_id,
SrsSharedPtrMessage** ppmsg
);
// get the stream identify, vhost/app/stream. // get the stream identify, vhost/app/stream.
extern std::string srs_generate_stream_url(std::string vhost, std::string app, std::string stream); extern std::string srs_generate_stream_url(
std::string vhost, std::string app, std::string stream
);
// parse the rtmp url to tcUrl/stream,
// for example, rtmp://v.ossrs.net/live/livestream to
// tcUrl: rtmp://v.ossrs.net/live
// stream: livestream
extern void srs_parse_rtmp_url(
std::string url, std::string& tcUrl, std::string& stream
);
// genereate the rtmp url, for instance, rtmp://server:port/app...vhost...vhost/stream // genereate the rtmp url, for instance, rtmp://server:port/app...vhost...vhost/stream
extern std::string srs_generate_rtmp_url(std::string server, int port, std::string vhost, std::string app, std::string stream); extern std::string srs_generate_rtmp_url(
std::string server, int port, std::string vhost, std::string app, std::string stream
);
// write large numbers of iovs. // write large numbers of iovs.
extern int srs_write_large_iovs(ISrsProtocolReaderWriter* skt, iovec* iovs, int size, ssize_t* pnwrite = NULL); extern int srs_write_large_iovs(
ISrsProtocolReaderWriter* skt, iovec* iovs, int size,
ssize_t* pnwrite = NULL
);
#endif #endif

@ -988,10 +988,7 @@ int SrsRtspStack::do_recv_message(SrsRtspRequest* req)
// for setup, parse the stream id from uri. // for setup, parse the stream id from uri.
if (req->is_setup()) { if (req->is_setup()) {
size_t pos = string::npos; size_t pos = string::npos;
std::string stream_id; std::string stream_id = srs_path_basename(req->uri);
if ((pos = req->uri.rfind("/")) != string::npos) {
stream_id = req->uri.substr(pos + 1);
}
if ((pos = stream_id.find("=")) != string::npos) { if ((pos = stream_id.find("=")) != string::npos) {
stream_id = stream_id.substr(pos + 1); stream_id = stream_id.substr(pos + 1);
} }

Loading…
Cancel
Save