2014-03-27 04:14:04 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-12-31 12:32:09 +00:00
|
|
|
Copyright (c) 2013-2015 winlin
|
2014-03-27 04:14:04 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRS_APP_HTTP_CONN_HPP
|
|
|
|
#define SRS_APP_HTTP_CONN_HPP
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <srs_app_http_conn.hpp>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_SERVER
|
2014-04-01 07:42:27 +00:00
|
|
|
|
2014-03-27 05:25:08 +00:00
|
|
|
#include <srs_app_st.hpp>
|
|
|
|
#include <srs_app_conn.hpp>
|
2014-04-04 10:55:21 +00:00
|
|
|
#include <srs_app_http.hpp>
|
2015-01-18 09:17:07 +00:00
|
|
|
#include <srs_app_reload.hpp>
|
2015-01-18 11:49:03 +00:00
|
|
|
#include <srs_kernel_file.hpp>
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2015-01-18 10:39:53 +00:00
|
|
|
class SrsSource;
|
|
|
|
class SrsRequest;
|
2014-07-26 12:08:37 +00:00
|
|
|
class SrsStSocket;
|
2015-01-18 14:51:07 +00:00
|
|
|
class SrsAacEncoder;
|
2015-01-18 11:49:03 +00:00
|
|
|
class SrsFlvEncoder;
|
2014-04-02 04:03:49 +00:00
|
|
|
class SrsHttpParser;
|
2014-04-01 10:40:24 +00:00
|
|
|
class SrsHttpMessage;
|
2014-04-02 10:07:34 +00:00
|
|
|
class SrsHttpHandler;
|
2015-01-18 11:49:03 +00:00
|
|
|
class SrsSharedPtrMessage;
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2015-01-18 01:12:53 +00:00
|
|
|
/**
|
|
|
|
* the flv vod stream supports flv?start=offset-bytes.
|
|
|
|
* for example, http://server/file.flv?start=10240
|
|
|
|
* server will write flv header and sequence header,
|
|
|
|
* then seek(10240) and response flv tag data.
|
|
|
|
*/
|
|
|
|
class SrsVodStream : public SrsGoHttpFileServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SrsVodStream(std::string root_dir);
|
|
|
|
virtual ~SrsVodStream();
|
|
|
|
protected:
|
|
|
|
virtual int serve_flv_stream(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r, std::string fullpath, int offset);
|
|
|
|
};
|
|
|
|
|
2015-01-18 14:51:07 +00:00
|
|
|
/**
|
|
|
|
* the stream encoder in some codec, for example, flv or aac.
|
|
|
|
*/
|
|
|
|
class ISrsStreamEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ISrsStreamEncoder();
|
|
|
|
virtual ~ISrsStreamEncoder();
|
|
|
|
public:
|
|
|
|
virtual int initialize(SrsFileWriter* w) = 0;
|
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the flv stream encoder, remux rtmp stream to flv stream.
|
|
|
|
*/
|
|
|
|
class SrsFlvStreamEncoder : public ISrsStreamEncoder
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsFlvEncoder* enc;
|
|
|
|
public:
|
|
|
|
SrsFlvStreamEncoder();
|
|
|
|
virtual ~SrsFlvStreamEncoder();
|
|
|
|
public:
|
|
|
|
virtual int initialize(SrsFileWriter* w);
|
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the aac stream encoder, remux rtmp stream to aac stream.
|
|
|
|
*/
|
|
|
|
class SrsAacStreamEncoder : public ISrsStreamEncoder
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsAacEncoder* enc;
|
|
|
|
public:
|
|
|
|
SrsAacStreamEncoder();
|
|
|
|
virtual ~SrsAacStreamEncoder();
|
|
|
|
public:
|
|
|
|
virtual int initialize(SrsFileWriter* w);
|
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
};
|
|
|
|
|
2015-01-18 11:49:03 +00:00
|
|
|
/**
|
|
|
|
* write stream to http response direclty.
|
|
|
|
*/
|
2015-01-18 14:51:07 +00:00
|
|
|
class SrsStreamWriter : public SrsFileWriter
|
2015-01-18 11:49:03 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
ISrsGoHttpResponseWriter* writer;
|
|
|
|
public:
|
2015-01-18 14:51:07 +00:00
|
|
|
SrsStreamWriter(ISrsGoHttpResponseWriter* w);
|
|
|
|
virtual ~SrsStreamWriter();
|
2015-01-18 11:49:03 +00:00
|
|
|
public:
|
|
|
|
virtual int open(std::string file);
|
|
|
|
virtual void close();
|
|
|
|
public:
|
|
|
|
virtual bool is_open();
|
|
|
|
virtual int64_t tellg();
|
|
|
|
public:
|
|
|
|
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
|
|
|
};
|
|
|
|
|
2015-01-18 10:00:40 +00:00
|
|
|
/**
|
|
|
|
* the flv live stream supports access rtmp in flv over http.
|
|
|
|
* srs will remux rtmp to flv streaming.
|
|
|
|
*/
|
|
|
|
class SrsLiveStream : public ISrsGoHttpHandler
|
|
|
|
{
|
2015-01-18 10:50:15 +00:00
|
|
|
private:
|
|
|
|
SrsRequest* req;
|
|
|
|
SrsSource* source;
|
2015-01-18 10:00:40 +00:00
|
|
|
public:
|
2015-01-18 10:50:15 +00:00
|
|
|
SrsLiveStream(SrsSource* s, SrsRequest* r);
|
2015-01-18 10:00:40 +00:00
|
|
|
virtual ~SrsLiveStream();
|
|
|
|
public:
|
|
|
|
virtual int serve_http(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r);
|
2015-01-18 11:49:03 +00:00
|
|
|
private:
|
2015-01-18 14:51:07 +00:00
|
|
|
virtual int streaming_send_messages(ISrsStreamEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs);
|
2015-01-18 10:00:40 +00:00
|
|
|
};
|
|
|
|
|
2015-01-18 12:07:54 +00:00
|
|
|
/**
|
|
|
|
* the srs live entry
|
|
|
|
*/
|
|
|
|
struct SrsLiveEntry
|
|
|
|
{
|
|
|
|
std::string vhost;
|
|
|
|
std::string mount;
|
|
|
|
SrsLiveStream* stream;
|
|
|
|
|
|
|
|
SrsLiveEntry();
|
|
|
|
};
|
|
|
|
|
2015-01-18 10:00:40 +00:00
|
|
|
/**
|
|
|
|
* the http server instance,
|
|
|
|
* serve http static file, flv vod stream and flv live stream.
|
|
|
|
*/
|
2015-01-18 09:17:07 +00:00
|
|
|
class SrsHttpServer : public ISrsReloadHandler
|
2014-04-04 10:55:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-01-17 15:00:40 +00:00
|
|
|
SrsGoHttpServeMux mux;
|
2015-01-18 10:00:40 +00:00
|
|
|
// the flv live streaming template.
|
2015-01-18 12:07:54 +00:00
|
|
|
std::map<std::string, SrsLiveEntry*> flvs;
|
2014-04-04 10:55:21 +00:00
|
|
|
public:
|
2015-01-17 15:00:40 +00:00
|
|
|
SrsHttpServer();
|
|
|
|
virtual ~SrsHttpServer();
|
2014-04-04 10:55:21 +00:00
|
|
|
public:
|
2015-01-17 15:00:40 +00:00
|
|
|
virtual int initialize();
|
2015-01-18 10:39:53 +00:00
|
|
|
public:
|
|
|
|
virtual int mount(SrsSource* s, SrsRequest* r);
|
|
|
|
virtual void unmount(SrsSource* s, SrsRequest* r);
|
2015-01-18 09:17:07 +00:00
|
|
|
// interface ISrsThreadHandler.
|
|
|
|
public:
|
|
|
|
virtual int on_reload_vhost_http_updated();
|
2015-01-18 10:00:40 +00:00
|
|
|
virtual int on_reload_vhost_http_flv_updated();
|
|
|
|
private:
|
|
|
|
virtual int mount_static_file();
|
|
|
|
virtual int mount_flv_streaming();
|
2014-04-04 10:55:21 +00:00
|
|
|
};
|
|
|
|
|
2014-03-27 05:25:08 +00:00
|
|
|
class SrsHttpConn : public SrsConnection
|
2014-03-27 04:14:04 +00:00
|
|
|
{
|
2014-04-01 04:36:56 +00:00
|
|
|
private:
|
2014-04-02 04:03:49 +00:00
|
|
|
SrsHttpParser* parser;
|
2015-01-17 15:00:40 +00:00
|
|
|
SrsHttpServer* mux;
|
2014-03-27 04:14:04 +00:00
|
|
|
public:
|
2015-01-17 15:00:40 +00:00
|
|
|
SrsHttpConn(SrsServer* svr, st_netfd_t fd, SrsHttpServer* m);
|
2014-03-27 04:14:04 +00:00
|
|
|
virtual ~SrsHttpConn();
|
2014-06-19 07:28:05 +00:00
|
|
|
public:
|
|
|
|
virtual void kbps_resample();
|
|
|
|
// interface IKbpsDelta
|
|
|
|
public:
|
|
|
|
virtual int64_t get_send_bytes_delta();
|
|
|
|
virtual int64_t get_recv_bytes_delta();
|
2014-03-27 05:25:08 +00:00
|
|
|
protected:
|
|
|
|
virtual int do_cycle();
|
2014-04-01 04:36:56 +00:00
|
|
|
private:
|
2015-01-17 15:00:40 +00:00
|
|
|
virtual int process_request(ISrsGoHttpResponseWriter* w, SrsHttpMessage* r);
|
2014-03-27 04:14:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2014-04-01 07:42:27 +00:00
|
|
|
|
|
|
|
#endif
|
2014-08-02 14:18:39 +00:00
|
|
|
|