2014-03-01 07:59:55 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-04-29 09:38:23 +00:00
|
|
|
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
2014-03-01 07:59:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
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,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-09-22 01:11:07 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2014-03-01 07:59:55 +00:00
|
|
|
|
2015-05-24 17:02:06 +00:00
|
|
|
// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-19 06:58:29 +00:00
|
|
|
#include <stdlib.h>
|
2015-08-25 14:29:00 +00:00
|
|
|
#include <sstream>
|
2014-06-29 02:03:29 +00:00
|
|
|
using namespace std;
|
2014-03-19 06:58:29 +00:00
|
|
|
|
|
|
|
#include <srs_kernel_log.hpp>
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-09-22 00:48:55 +00:00
|
|
|
#include <srs_kernel_buffer.hpp>
|
2015-01-23 02:07:20 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-01-31 11:46:55 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2015-05-22 03:20:25 +00:00
|
|
|
#include <srs_kernel_consts.hpp>
|
2015-05-24 17:02:06 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-09-22 01:07:07 +00:00
|
|
|
#include <srs_protocol_io.hpp>
|
2014-03-19 06:58:29 +00:00
|
|
|
|
2014-06-29 03:45:31 +00:00
|
|
|
void srs_discovery_tc_url(
|
|
|
|
string tcUrl,
|
|
|
|
string& schema, string& host, string& vhost,
|
2014-07-13 08:10:06 +00:00
|
|
|
string& app, string& port, std::string& param
|
2014-06-29 03:45:31 +00:00
|
|
|
) {
|
|
|
|
size_t pos = std::string::npos;
|
|
|
|
std::string url = tcUrl;
|
|
|
|
|
|
|
|
if ((pos = url.find("://")) != std::string::npos) {
|
|
|
|
schema = url.substr(0, pos);
|
|
|
|
url = url.substr(schema.length() + 3);
|
|
|
|
srs_info("discovery schema=%s", schema.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pos = url.find("/")) != std::string::npos) {
|
|
|
|
host = url.substr(0, pos);
|
|
|
|
url = url.substr(host.length() + 1);
|
|
|
|
srs_info("discovery host=%s", host.c_str());
|
|
|
|
}
|
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
port = SRS_CONSTS_RTMP_DEFAULT_PORT;
|
2014-06-29 03:45:31 +00:00
|
|
|
if ((pos = host.find(":")) != std::string::npos) {
|
|
|
|
port = host.substr(pos + 1);
|
|
|
|
host = host.substr(0, pos);
|
|
|
|
srs_info("discovery host=%s, port=%s", host.c_str(), port.c_str());
|
|
|
|
}
|
2015-09-17 05:36:02 +00:00
|
|
|
|
|
|
|
if (url.empty()) {
|
|
|
|
app = SRS_CONSTS_RTMP_DEFAULT_APP;
|
|
|
|
} else {
|
|
|
|
app = url;
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:45:31 +00:00
|
|
|
vhost = host;
|
2014-07-13 08:10:06 +00:00
|
|
|
srs_vhost_resolve(vhost, app, param);
|
2014-06-29 03:45:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 08:10:06 +00:00
|
|
|
void srs_vhost_resolve(string& vhost, string& app, string& param)
|
2014-03-01 07:59:55 +00:00
|
|
|
{
|
2014-07-13 08:10:06 +00:00
|
|
|
// get original param
|
|
|
|
size_t pos = 0;
|
|
|
|
if ((pos = app.find("?")) != std::string::npos) {
|
|
|
|
param = app.substr(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter tcUrl
|
|
|
|
app = srs_string_replace(app, ",", "?");
|
2014-04-03 07:53:56 +00:00
|
|
|
app = srs_string_replace(app, "...", "?");
|
2014-06-29 03:45:31 +00:00
|
|
|
app = srs_string_replace(app, "&&", "?");
|
|
|
|
app = srs_string_replace(app, "=", "?");
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-05-19 10:06:20 +00:00
|
|
|
if ((pos = app.find("?")) != std::string::npos) {
|
|
|
|
std::string query = app.substr(pos + 1);
|
|
|
|
app = app.substr(0, pos);
|
|
|
|
|
|
|
|
if ((pos = query.find("vhost?")) != std::string::npos) {
|
|
|
|
query = query.substr(pos + 6);
|
|
|
|
if (!query.empty()) {
|
|
|
|
vhost = query;
|
|
|
|
}
|
|
|
|
if ((pos = vhost.find("?")) != std::string::npos) {
|
|
|
|
vhost = vhost.substr(0, pos);
|
|
|
|
}
|
2014-06-29 03:45:31 +00:00
|
|
|
}
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
2015-05-19 10:06:20 +00:00
|
|
|
|
|
|
|
/* others */
|
2014-03-01 07:59:55 +00:00
|
|
|
}
|
2014-03-19 06:58:29 +00:00
|
|
|
|
|
|
|
void srs_random_generate(char* bytes, int size)
|
|
|
|
{
|
|
|
|
static bool _random_initialized = false;
|
|
|
|
if (!_random_initialized) {
|
|
|
|
srand(0);
|
|
|
|
_random_initialized = true;
|
|
|
|
srs_trace("srand initialized the random.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
2014-08-21 01:48:19 +00:00
|
|
|
// the common value in [0x0f, 0xf0]
|
|
|
|
bytes[i] = 0x0f + (rand() % (256 - 0x0f - 0x0f));
|
2014-03-19 06:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-29 02:03:29 +00:00
|
|
|
|
2014-07-13 08:10:06 +00:00
|
|
|
string srs_generate_tc_url(string ip, string vhost, string app, string port, string param)
|
2014-06-29 02:03:29 +00:00
|
|
|
{
|
|
|
|
string tcUrl = "rtmp://";
|
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
if (vhost == SRS_CONSTS_RTMP_DEFAULT_VHOST) {
|
2014-06-29 02:03:29 +00:00
|
|
|
tcUrl += ip;
|
|
|
|
} else {
|
|
|
|
tcUrl += vhost;
|
|
|
|
}
|
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
if (port != SRS_CONSTS_RTMP_DEFAULT_PORT) {
|
2014-06-29 02:03:29 +00:00
|
|
|
tcUrl += ":";
|
|
|
|
tcUrl += port;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcUrl += "/";
|
|
|
|
tcUrl += app;
|
2014-07-13 08:10:06 +00:00
|
|
|
tcUrl += param;
|
2014-06-29 02:03:29 +00:00
|
|
|
|
|
|
|
return tcUrl;
|
|
|
|
}
|
2014-07-05 03:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* compare the memory in bytes.
|
|
|
|
*/
|
|
|
|
bool srs_bytes_equals(void* pa, void* pb, int size)
|
|
|
|
{
|
|
|
|
u_int8_t* a = (u_int8_t*)pa;
|
|
|
|
u_int8_t* b = (u_int8_t*)pb;
|
|
|
|
|
|
|
|
if (!a && !b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a || !b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < size; i++){
|
|
|
|
if(a[i] != b[i]){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-02 14:18:39 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
int srs_do_rtmp_create_msg(char type, u_int32_t timestamp, char* data, int size, int stream_id, SrsSharedPtrMessage** ppmsg)
|
2015-01-31 11:46:55 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
*ppmsg = NULL;
|
|
|
|
SrsSharedPtrMessage* msg = NULL;
|
|
|
|
|
|
|
|
if (type == SrsCodecFlvTagAudio) {
|
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_audio(size, timestamp, stream_id);
|
|
|
|
|
|
|
|
msg = new SrsSharedPtrMessage();
|
|
|
|
if ((ret = msg->create(&header, data, size)) != ERROR_SUCCESS) {
|
|
|
|
srs_freep(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else if (type == SrsCodecFlvTagVideo) {
|
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_video(size, timestamp, stream_id);
|
|
|
|
|
|
|
|
msg = new SrsSharedPtrMessage();
|
|
|
|
if ((ret = msg->create(&header, data, size)) != ERROR_SUCCESS) {
|
|
|
|
srs_freep(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else if (type == SrsCodecFlvTagScript) {
|
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_amf0_script(size, stream_id);
|
|
|
|
|
|
|
|
msg = new SrsSharedPtrMessage();
|
|
|
|
if ((ret = msg->create(&header, data, size)) != ERROR_SUCCESS) {
|
|
|
|
srs_freep(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = ERROR_STREAM_CASTER_FLV_TAG;
|
|
|
|
srs_error("rtmp unknown tag type=%#x. ret=%d", type, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppmsg = msg;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int srs_rtmp_create_msg(char type, u_int32_t timestamp, char* data, int size, int stream_id, SrsSharedPtrMessage** ppmsg)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
// only when failed, we must free the data.
|
2015-03-21 03:55:28 +00:00
|
|
|
if ((ret = srs_do_rtmp_create_msg(type, timestamp, data, size, stream_id, ppmsg)) != ERROR_SUCCESS) {
|
2015-01-31 11:46:55 +00:00
|
|
|
srs_freep(data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:25 +00:00
|
|
|
std::string srs_generate_stream_url(std::string vhost, std::string app, std::string stream)
|
|
|
|
{
|
|
|
|
std::string url = "";
|
|
|
|
|
|
|
|
if (SRS_CONSTS_RTMP_DEFAULT_VHOST != vhost){
|
2015-09-17 05:36:02 +00:00
|
|
|
url += vhost;
|
2015-05-22 03:20:25 +00:00
|
|
|
}
|
|
|
|
url += "/";
|
|
|
|
url += app;
|
|
|
|
url += "/";
|
|
|
|
url += stream;
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2015-08-25 14:29:00 +00:00
|
|
|
string srs_generate_rtmp_url(string server, int port, string vhost, string app, string stream)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << "rtmp://" << server << ":" << std::dec << port << "/" << app;
|
|
|
|
|
|
|
|
// when default or server is vhost, donot specifies the vhost in params.
|
|
|
|
if (SRS_CONSTS_RTMP_DEFAULT_VHOST != vhost && server != vhost) {
|
|
|
|
ss << "...vhost..." << vhost;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "/" << stream;
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2015-05-24 17:02:06 +00:00
|
|
|
int srs_write_large_iovs(ISrsProtocolReaderWriter* skt, iovec* iovs, int size, ssize_t* pnwrite)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
// the limits of writev iovs.
|
|
|
|
// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
|
|
|
|
#ifndef _WIN32
|
|
|
|
// for linux, generally it's 1024.
|
2015-08-22 05:57:34 +00:00
|
|
|
static int limits = (int)sysconf(_SC_IOV_MAX);
|
2015-05-24 17:02:06 +00:00
|
|
|
#else
|
|
|
|
static int limits = 1024;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// send in a time.
|
|
|
|
if (size < limits) {
|
|
|
|
if ((ret = skt->writev(iovs, size, pnwrite)) != ERROR_SUCCESS) {
|
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("send with writev failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send in multiple times.
|
|
|
|
int cur_iov = 0;
|
|
|
|
while (cur_iov < size) {
|
|
|
|
int cur_count = srs_min(limits, size - cur_iov);
|
|
|
|
if ((ret = skt->writev(iovs + cur_iov, cur_count, pnwrite)) != ERROR_SUCCESS) {
|
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("send with writev failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
cur_iov += cur_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|