1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_caster_flv.cpp

328 lines
8.6 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2018-01-07 02:58:53 +00:00
* Copyright (c) 2013-2018 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.
*/
#include <srs_app_caster_flv.hpp>
#ifdef SRS_AUTO_STREAM_CASTER
#include <algorithm>
using namespace std;
#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>
#include <srs_app_http_conn.hpp>
2015-05-03 15:57:22 +00:00
#include <srs_core_autofree.hpp>
#include <srs_kernel_flv.hpp>
2015-06-13 08:04:59 +00:00
#include <srs_rtmp_stack.hpp>
#include <srs_protocol_utility.hpp>
2015-06-14 00:43:38 +00:00
#include <srs_app_st.hpp>
#include <srs_app_utility.hpp>
2015-09-22 01:05:21 +00:00
#include <srs_protocol_amf0.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_rtmp_conn.hpp>
2017-01-16 08:20:34 +00:00
#include <srs_protocol_utility.hpp>
#define SRS_HTTP_FLV_STREAM_BUFFER 4096
SrsAppCasterFlv::SrsAppCasterFlv(SrsConfDirective* c)
{
http_mux = new SrsHttpServeMux();
output = _srs_config->get_stream_caster_output(c);
manager = new SrsCoroutineManager();
}
SrsAppCasterFlv::~SrsAppCasterFlv()
{
srs_freep(http_mux);
srs_freep(manager);
}
srs_error_t SrsAppCasterFlv::initialize()
{
2017-06-10 07:20:48 +00:00
srs_error_t err = srs_success;
2017-06-10 07:20:48 +00:00
if ((err = http_mux->handle("/", this)) != srs_success) {
return srs_error_wrap(err, "handle root");
}
if ((err = manager->start()) != srs_success) {
return srs_error_wrap(err, "start manager");
}
return err;
}
srs_error_t SrsAppCasterFlv::on_tcp_client(srs_netfd_t stfd)
{
srs_error_t err = srs_success;
string ip = srs_get_peer_ip(srs_netfd_fileno(stfd));
2015-12-24 09:38:49 +00:00
SrsHttpConn* conn = new SrsDynamicHttpConn(this, stfd, http_mux, ip);
conns.push_back(conn);
if ((err = conn->start()) != srs_success) {
return srs_error_wrap(err, "start tcp listener");
}
return err;
}
2017-03-26 05:40:39 +00:00
void SrsAppCasterFlv::remove(ISrsConnection* c)
{
2017-03-26 05:40:39 +00:00
SrsConnection* conn = dynamic_cast<SrsConnection*>(c);
std::vector<SrsHttpConn*>::iterator it;
2017-03-26 05:40:39 +00:00
if ((it = std::find(conns.begin(), conns.end(), conn)) != conns.end()) {
conns.erase(it);
}
// 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
manager->remove(c);
}
2017-07-29 13:39:57 +00:00
srs_error_t SrsAppCasterFlv::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
2015-05-27 02:23:40 +00:00
SrsHttpMessage* msg = dynamic_cast<SrsHttpMessage*>(r);
SrsDynamicHttpConn* conn = dynamic_cast<SrsDynamicHttpConn*>(msg->connection());
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, "/");
std::string stream = srs_path_basename(r->path());
2015-05-04 13:55:19 +00:00
stream = srs_string_trim_start(stream, "/");
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);
}
2018-01-01 13:20:57 +00:00
srs_error_t err = conn->proxy(w, r, o);
if (err != srs_success) {
return srs_error_wrap(err, "proxy");
2017-07-29 13:39:57 +00:00
}
2018-01-01 13:20:57 +00:00
return err;
}
SrsDynamicHttpConn::SrsDynamicHttpConn(IConnectionManager* cm, srs_netfd_t fd, SrsHttpServeMux* m, string cip)
2017-03-25 09:21:39 +00:00
: SrsHttpConn(cm, fd, m, cip)
{
sdk = NULL;
2015-05-04 13:55:19 +00:00
pprint = SrsPithyPrint::create_caster();
}
SrsDynamicHttpConn::~SrsDynamicHttpConn()
{
srs_freep(sdk);
2015-05-04 13:55:19 +00:00
srs_freep(pprint);
}
2017-07-29 13:39:57 +00:00
srs_error_t SrsDynamicHttpConn::on_got_http_message(ISrsHttpMessage* msg)
{
2017-07-29 13:39:57 +00:00
return srs_success;
}
2018-01-01 13:20:57 +00:00
srs_error_t SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string o)
{
2018-01-01 13:20:57 +00:00
srs_error_t err = srs_success;
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];
SrsAutoFreeA(char, buffer);
2015-05-03 15:57:22 +00:00
ISrsHttpResponseReader* rr = r->body_reader();
SrsHttpFileReader reader(rr);
SrsFlvDecoder dec;
2018-01-01 13:20:57 +00:00
if ((err = dec.initialize(&reader)) != srs_success) {
return srs_error_wrap(err, "init decoder");
}
2015-05-04 13:55:19 +00:00
char header[9];
2018-01-01 13:20:57 +00:00
if ((err = dec.read_header(header)) != srs_success) {
return srs_error_wrap(err, "read header");
2015-05-04 13:55:19 +00:00
}
char pps[4];
2018-01-01 13:20:57 +00:00
if ((err = dec.read_previous_tag_size(pps)) != srs_success) {
return srs_error_wrap(err, "read pts");
2015-05-04 13:55:19 +00:00
}
2018-01-01 13:20:57 +00:00
err = do_proxy(rr, &dec);
sdk->close();
2018-01-01 13:20:57 +00:00
return err;
}
2018-01-01 13:20:57 +00:00
srs_error_t SrsDynamicHttpConn::do_proxy(ISrsHttpResponseReader* rr, SrsFlvDecoder* dec)
{
2018-01-01 13:20:57 +00:00
srs_error_t err = srs_success;
srs_freep(sdk);
int64_t cto = SRS_CONSTS_RTMP_TMMS;
int64_t sto = SRS_CONSTS_RTMP_PULSE_TMMS;
sdk = new SrsSimpleRtmpClient(output, cto, sto);
2018-01-01 13:20:57 +00:00
if ((err = sdk->connect()) != srs_success) {
return srs_error_wrap(err, "connect %s failed, cto=%" PRId64 ", sto=%" PRId64, output.c_str(), cto, sto);
2015-10-14 06:37:24 +00:00
}
2018-01-01 13:20:57 +00:00
if ((err = sdk->publish()) != srs_success) {
return srs_error_wrap(err, "publish");
2015-10-14 06:37:24 +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;
uint32_t time;
2018-01-01 13:20:57 +00:00
if ((err = dec->read_tag_header(&type, &size, &time)) != srs_success) {
return srs_error_wrap(err, "read tag header");
2015-05-04 13:55:19 +00:00
}
char* data = new char[size];
2018-01-01 13:20:57 +00:00
if ((err = dec->read_tag_data(data, size)) != srs_success) {
2015-11-02 03:05:39 +00:00
srs_freepa(data);
2018-01-01 13:20:57 +00:00
return srs_error_wrap(err, "read tag data");
2015-05-04 13:55:19 +00:00
}
SrsSharedPtrMessage* msg = NULL;
2018-01-01 13:20:57 +00:00
if ((err = srs_rtmp_create_msg(type, time, data, size, sdk->sid(), &msg)) != srs_success) {
return srs_error_wrap(err, "create message");
}
// TODO: FIXME: for post flv, reconnect when error.
2018-01-01 13:20:57 +00:00
if ((err = sdk->send_and_free_message(msg)) != srs_success) {
return srs_error_wrap(err, "send message");
2015-05-04 13:55:19 +00:00
}
if (pprint->can_print()) {
srs_trace("flv: send msg %d age=%d, dts=%d, size=%d", type, pprint->age(), time, size);
}
2018-01-01 13:20:57 +00:00
if ((err = dec->read_previous_tag_size(pps)) != srs_success) {
return srs_error_wrap(err, "read pts");
2015-05-03 15:57:22 +00:00
}
2015-05-04 13:55:19 +00:00
}
2018-01-01 13:20:57 +00:00
return err;
2015-05-04 13:55:19 +00:00
}
SrsHttpFileReader::SrsHttpFileReader(ISrsHttpResponseReader* h)
{
http = h;
}
SrsHttpFileReader::~SrsHttpFileReader()
{
}
2018-01-01 13:20:57 +00:00
srs_error_t SrsHttpFileReader::open(std::string /*file*/)
{
2018-01-01 13:20:57 +00:00
return srs_success;
}
void SrsHttpFileReader::close()
{
}
bool SrsHttpFileReader::is_open()
{
return true;
}
int64_t SrsHttpFileReader::tellg()
{
return 0;
}
void SrsHttpFileReader::skip(int64_t /*size*/)
{
}
int64_t SrsHttpFileReader::seek2(int64_t offset)
{
return offset;
}
int64_t SrsHttpFileReader::filesize()
{
return 0;
}
2018-01-01 13:20:57 +00:00
srs_error_t SrsHttpFileReader::read(void* buf, size_t count, ssize_t* pnread)
{
2018-01-01 13:20:57 +00:00
srs_error_t err = srs_success;
if (http->eof()) {
2018-01-01 13:20:57 +00:00
return srs_error_new(ERROR_HTTP_REQUEST_EOF, "EOF");
}
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;
2018-01-01 13:20:57 +00:00
if ((err = http->read((char*)buf + total_read, (int)(count - total_read), &nread)) != srs_success) {
return srs_error_wrap(err, "read");
2015-05-04 13:55:19 +00:00
}
if (nread == 0) {
2018-01-01 13:20:57 +00:00
err = srs_error_new(ERROR_HTTP_REQUEST_EOF, "EOF");
break;
}
2015-05-04 13:55:19 +00:00
srs_assert(nread);
total_read += nread;
}
if (pnread) {
2015-05-04 13:55:19 +00:00
*pnread = total_read;
}
2018-01-01 13:20:57 +00:00
return err;
}
2018-01-01 13:20:57 +00:00
srs_error_t SrsHttpFileReader::lseek(off_t offset, int whence, off_t* seeked)
{
// TODO: FIXME: Use HTTP range for seek.
2018-01-01 13:20:57 +00:00
return srs_success;
}
#endif