2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-03-25 13:29:29 +00:00
|
|
|
* Copyright (c) 2013-2017 OSSRS(winlin)
|
2017-03-25 09:21:39 +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-05-03 02:56:20 +00:00
|
|
|
|
|
|
|
#include <srs_app_caster_flv.hpp>
|
|
|
|
|
|
|
|
#ifdef SRS_AUTO_STREAM_CASTER
|
|
|
|
|
2015-05-03 15:34:59 +00:00
|
|
|
#include <algorithm>
|
|
|
|
using namespace std;
|
|
|
|
|
2015-05-03 02:56:20 +00:00
|
|
|
#include <srs_app_config.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_log.hpp>
|
|
|
|
#include <srs_app_config.hpp>
|
|
|
|
#include <srs_app_pithy_print.hpp>
|
2015-05-03 15:34:59 +00:00
|
|
|
#include <srs_app_http_conn.hpp>
|
2015-05-03 15:57:22 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2015-05-04 10:28:41 +00:00
|
|
|
#include <srs_kernel_flv.hpp>
|
2015-06-13 08:04:59 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-09-22 01:11:07 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2015-06-14 00:43:38 +00:00
|
|
|
#include <srs_app_st.hpp>
|
2015-05-04 11:06:38 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2015-09-22 01:05:21 +00:00
|
|
|
#include <srs_protocol_amf0.hpp>
|
2015-05-04 11:06:38 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-10-14 03:16:50 +00:00
|
|
|
#include <srs_app_rtmp_conn.hpp>
|
2017-01-16 08:20:34 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2015-05-03 02:56:20 +00:00
|
|
|
|
2015-05-04 10:11:52 +00:00
|
|
|
#define SRS_HTTP_FLV_STREAM_BUFFER 4096
|
|
|
|
|
2015-05-03 02:56:20 +00:00
|
|
|
SrsAppCasterFlv::SrsAppCasterFlv(SrsConfDirective* c)
|
|
|
|
{
|
2015-05-03 15:34:59 +00:00
|
|
|
http_mux = new SrsHttpServeMux();
|
|
|
|
output = _srs_config->get_stream_caster_output(c);
|
2015-05-03 02:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsAppCasterFlv::~SrsAppCasterFlv()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-03 15:34:59 +00:00
|
|
|
int SrsAppCasterFlv::initialize()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ((ret = http_mux->handle("/", this)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:56:20 +00:00
|
|
|
int SrsAppCasterFlv::on_tcp_client(st_netfd_t stfd)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2015-05-03 15:34:59 +00:00
|
|
|
|
2015-12-24 09:38:49 +00:00
|
|
|
string ip = srs_get_peer_ip(st_netfd_fileno(stfd));
|
|
|
|
SrsHttpConn* conn = new SrsDynamicHttpConn(this, stfd, http_mux, ip);
|
2015-05-03 15:34:59 +00:00
|
|
|
conns.push_back(conn);
|
|
|
|
|
|
|
|
if ((ret = conn->start()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-26 05:40:39 +00:00
|
|
|
void SrsAppCasterFlv::remove(ISrsConnection* c)
|
2015-05-03 15:34:59 +00:00
|
|
|
{
|
2017-03-26 05:40:39 +00:00
|
|
|
SrsConnection* conn = dynamic_cast<SrsConnection*>(c);
|
|
|
|
|
2015-05-03 15:34:59 +00:00
|
|
|
std::vector<SrsHttpConn*>::iterator it;
|
2017-03-26 05:40:39 +00:00
|
|
|
if ((it = std::find(conns.begin(), conns.end(), conn)) != conns.end()) {
|
2015-05-03 15:34:59 +00:00
|
|
|
conns.erase(it);
|
|
|
|
}
|
2017-04-15 08:56:28 +00:00
|
|
|
|
|
|
|
// fixbug: SrsHttpConn for CasterFlv is not freed, which could cause memory leak
|
|
|
|
// so, free conn which is not managed by SrsServer->conns;
|
|
|
|
// @see: https://github.com/ossrs/srs/issues/826
|
|
|
|
srs_freep(c);
|
2015-05-03 15:34:59 +00:00
|
|
|
}
|
2015-05-04 10:11:52 +00:00
|
|
|
|
2015-05-27 02:23:40 +00:00
|
|
|
int SrsAppCasterFlv::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
2015-05-04 11:06:38 +00:00
|
|
|
{
|
2015-05-27 02:23:40 +00:00
|
|
|
SrsHttpMessage* msg = dynamic_cast<SrsHttpMessage*>(r);
|
|
|
|
SrsDynamicHttpConn* conn = dynamic_cast<SrsDynamicHttpConn*>(msg->connection());
|
2015-05-04 11:06:38 +00:00
|
|
|
srs_assert(conn);
|
|
|
|
|
|
|
|
std::string app = srs_path_dirname(r->path());
|
2015-05-04 13:55:19 +00:00
|
|
|
app = srs_string_trim_start(app, "/");
|
|
|
|
|
2015-05-04 11:06:38 +00:00
|
|
|
std::string stream = srs_path_basename(r->path());
|
2015-05-04 13:55:19 +00:00
|
|
|
stream = srs_string_trim_start(stream, "/");
|
2015-05-04 11:06:38 +00:00
|
|
|
|
|
|
|
std::string o = output;
|
|
|
|
if (!app.empty() && app != "/") {
|
|
|
|
o = srs_string_replace(o, "[app]", app);
|
|
|
|
}
|
|
|
|
o = srs_string_replace(o, "[stream]", stream);
|
|
|
|
|
|
|
|
// remove the extension.
|
|
|
|
if (srs_string_ends_with(o, ".flv")) {
|
|
|
|
o = o.substr(0, o.length() - 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn->proxy(w, r, o);
|
|
|
|
}
|
|
|
|
|
2015-12-24 09:38:49 +00:00
|
|
|
SrsDynamicHttpConn::SrsDynamicHttpConn(IConnectionManager* cm, st_netfd_t fd, SrsHttpServeMux* m, string cip)
|
2017-03-25 09:21:39 +00:00
|
|
|
: SrsHttpConn(cm, fd, m, cip)
|
2015-05-04 11:06:38 +00:00
|
|
|
{
|
2017-01-17 02:44:13 +00:00
|
|
|
sdk = NULL;
|
2015-05-04 13:55:19 +00:00
|
|
|
pprint = SrsPithyPrint::create_caster();
|
2015-05-04 11:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsDynamicHttpConn::~SrsDynamicHttpConn()
|
|
|
|
{
|
2015-10-14 03:16:50 +00:00
|
|
|
srs_freep(sdk);
|
2015-05-04 13:55:19 +00:00
|
|
|
srs_freep(pprint);
|
2015-05-04 11:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 02:23:40 +00:00
|
|
|
int SrsDynamicHttpConn::on_got_http_message(ISrsHttpMessage* msg)
|
2015-05-04 11:06:38 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-27 02:23:40 +00:00
|
|
|
int SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string o)
|
2015-05-03 15:34:59 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2015-05-04 11:06:38 +00:00
|
|
|
output = o;
|
|
|
|
srs_trace("flv: proxy %s to %s", r->uri().c_str(), output.c_str());
|
2015-05-03 15:57:22 +00:00
|
|
|
|
|
|
|
char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER];
|
2015-11-02 03:29:20 +00:00
|
|
|
SrsAutoFreeA(char, buffer);
|
2015-05-03 15:57:22 +00:00
|
|
|
|
|
|
|
ISrsHttpResponseReader* rr = r->body_reader();
|
2015-05-04 10:28:41 +00:00
|
|
|
SrsHttpFileReader reader(rr);
|
|
|
|
SrsFlvDecoder dec;
|
|
|
|
|
|
|
|
if ((ret = dec.initialize(&reader)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-04 13:55:19 +00:00
|
|
|
char header[9];
|
|
|
|
if ((ret = dec.read_header(header)) != ERROR_SUCCESS) {
|
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy flv header failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_trace("flv: proxy drop flv header.");
|
|
|
|
|
|
|
|
char pps[4];
|
|
|
|
if ((ret = dec.read_previous_tag_size(pps)) != ERROR_SUCCESS) {
|
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy flv header pps failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:35:35 +00:00
|
|
|
ret = do_proxy(rr, &dec);
|
2015-10-14 03:16:50 +00:00
|
|
|
sdk->close();
|
2015-05-10 02:35:35 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDynamicHttpConn::do_proxy(ISrsHttpResponseReader* rr, SrsFlvDecoder* dec)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2017-01-17 02:44:13 +00:00
|
|
|
srs_freep(sdk);
|
|
|
|
|
2017-01-17 04:25:30 +00:00
|
|
|
int64_t cto = SRS_CONSTS_RTMP_TMMS;
|
|
|
|
int64_t sto = SRS_CONSTS_RTMP_PULSE_TMMS;
|
|
|
|
sdk = new SrsSimpleRtmpClient(output, cto, sto);
|
2017-01-17 02:44:13 +00:00
|
|
|
|
|
|
|
if ((ret = sdk->connect()) != ERROR_SUCCESS) {
|
2017-05-06 06:05:22 +00:00
|
|
|
srs_error("flv: connect %s failed, cto=%" PRId64 ", sto=%" PRId64 ". ret=%d", output.c_str(), cto, sto, ret);
|
2015-10-14 06:37:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = sdk->publish()) != ERROR_SUCCESS) {
|
|
|
|
srs_error("flv: publish failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:35:35 +00:00
|
|
|
char pps[4];
|
2015-05-03 15:57:22 +00:00
|
|
|
while (!rr->eof()) {
|
2015-05-04 13:55:19 +00:00
|
|
|
pprint->elapse();
|
|
|
|
|
|
|
|
char type;
|
|
|
|
int32_t size;
|
2017-01-30 09:32:18 +00:00
|
|
|
uint32_t time;
|
2015-05-10 02:35:35 +00:00
|
|
|
if ((ret = dec->read_tag_header(&type, &size, &time)) != ERROR_SUCCESS) {
|
2015-05-04 13:55:19 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy tag header failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* data = new char[size];
|
2015-05-10 02:35:35 +00:00
|
|
|
if ((ret = dec->read_tag_data(data, size)) != ERROR_SUCCESS) {
|
2015-11-02 03:05:39 +00:00
|
|
|
srs_freepa(data);
|
2015-05-04 13:55:19 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy tag data failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-14 07:51:01 +00:00
|
|
|
SrsSharedPtrMessage* msg = NULL;
|
2017-01-16 08:20:34 +00:00
|
|
|
if ((ret = srs_rtmp_create_msg(type, time, data, size, sdk->sid(), &msg)) != ERROR_SUCCESS) {
|
2015-10-14 07:51:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-14 03:22:12 +00:00
|
|
|
// TODO: FIXME: for post flv, reconnect when error.
|
2015-10-14 07:51:01 +00:00
|
|
|
if ((ret = sdk->send_and_free_message(msg)) != ERROR_SUCCESS) {
|
2015-05-04 13:55:19 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy rtmp packet failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-14 03:16:50 +00:00
|
|
|
if (pprint->can_print()) {
|
|
|
|
srs_trace("flv: send msg %d age=%d, dts=%d, size=%d", type, pprint->age(), time, size);
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:35:35 +00:00
|
|
|
if ((ret = dec->read_previous_tag_size(pps)) != ERROR_SUCCESS) {
|
2015-05-04 13:55:19 +00:00
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("flv: proxy tag header pps failed. ret=%d", ret);
|
|
|
|
}
|
2015-05-03 15:57:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-04 13:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-04 10:28:41 +00:00
|
|
|
SrsHttpFileReader::SrsHttpFileReader(ISrsHttpResponseReader* h)
|
|
|
|
{
|
|
|
|
http = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpFileReader::~SrsHttpFileReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpFileReader::open(std::string /*file*/)
|
|
|
|
{
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsHttpFileReader::close()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsHttpFileReader::is_open()
|
|
|
|
{
|
2015-05-04 11:06:38 +00:00
|
|
|
return true;
|
2015-05-04 10:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SrsHttpFileReader::tellg()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsHttpFileReader::skip(int64_t /*size*/)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:49:19 +00:00
|
|
|
int64_t SrsHttpFileReader::seek2(int64_t offset)
|
2015-05-04 10:28:41 +00:00
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SrsHttpFileReader::filesize()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsHttpFileReader::read(void* buf, size_t count, ssize_t* pnread)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (http->eof()) {
|
|
|
|
ret = ERROR_HTTP_REQUEST_EOF;
|
|
|
|
srs_error("flv: encoder EOF. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-04 13:55:19 +00:00
|
|
|
int total_read = 0;
|
2015-05-27 02:23:40 +00:00
|
|
|
while (total_read < (int)count) {
|
2015-05-04 13:55:19 +00:00
|
|
|
int nread = 0;
|
|
|
|
if ((ret = http->read((char*)buf + total_read, (int)(count - total_read), &nread)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:37:12 +00:00
|
|
|
if (nread == 0) {
|
|
|
|
ret = ERROR_HTTP_REQUEST_EOF;
|
|
|
|
srs_warn("flv: encoder read EOF. ret=%d", ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-04 13:55:19 +00:00
|
|
|
srs_assert(nread);
|
|
|
|
total_read += nread;
|
2015-05-04 10:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pnread) {
|
2015-05-04 13:55:19 +00:00
|
|
|
*pnread = total_read;
|
2015-05-04 10:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:49:19 +00:00
|
|
|
int SrsHttpFileReader::lseek(off_t offset, int whence, off_t* seeked)
|
|
|
|
{
|
|
|
|
// TODO: FIXME: Use HTTP range for seek.
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:56:20 +00:00
|
|
|
#endif
|