mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
rename core to app
This commit is contained in:
parent
6616dfecfc
commit
c2ccb83912
55 changed files with 57 additions and 54 deletions
|
@ -1,127 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core.hpp>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
|
||||
static int64_t _srs_system_time_us_cache = 0;
|
||||
|
||||
int64_t srs_get_system_time_ms()
|
||||
{
|
||||
return _srs_system_time_us_cache / 1000;
|
||||
}
|
||||
|
||||
void srs_update_system_time_ms()
|
||||
{
|
||||
timeval now;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
// we must convert the tv_sec/tv_usec to int64_t.
|
||||
_srs_system_time_us_cache = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
|
||||
|
||||
_srs_system_time_us_cache = srs_max(0, _srs_system_time_us_cache);
|
||||
}
|
||||
|
||||
std::string srs_replace(std::string str, std::string old_str, std::string new_str)
|
||||
{
|
||||
std::string ret = str;
|
||||
|
||||
if (old_str == new_str) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = ret.find(old_str, pos)) != std::string::npos) {
|
||||
ret = ret.replace(pos, old_str.length(), new_str);
|
||||
pos += new_str.length();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string srs_dns_resolve(std::string host)
|
||||
{
|
||||
if (inet_addr(host.c_str()) != INADDR_NONE) {
|
||||
return host;
|
||||
}
|
||||
|
||||
hostent* answer = gethostbyname(host.c_str());
|
||||
if (answer == NULL) {
|
||||
srs_error("dns resolve host %s error.", host.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
char ipv4[16];
|
||||
memset(ipv4, 0, sizeof(ipv4));
|
||||
for (int i = 0; i < answer->h_length; i++) {
|
||||
inet_ntop(AF_INET, answer->h_addr_list[i], ipv4, sizeof(ipv4));
|
||||
srs_info("dns resolve host %s to %s.", host.c_str(), ipv4);
|
||||
break;
|
||||
}
|
||||
|
||||
return ipv4;
|
||||
}
|
||||
|
||||
void srs_vhost_resolve(std::string& vhost, std::string& app)
|
||||
{
|
||||
app = srs_replace(app, "...", "?");
|
||||
|
||||
size_t pos = 0;
|
||||
if ((pos = app.find("?")) == std::string::npos) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string query = app.substr(pos + 1);
|
||||
app = app.substr(0, pos);
|
||||
|
||||
if ((pos = query.find("vhost?")) != std::string::npos
|
||||
|| (pos = query.find("vhost=")) != std::string::npos
|
||||
|| (pos = query.find("Vhost?")) != std::string::npos
|
||||
|| (pos = query.find("Vhost=")) != std::string::npos
|
||||
) {
|
||||
query = query.substr(pos + 6);
|
||||
if (!query.empty()) {
|
||||
vhost = query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void srs_close_stfd(st_netfd_t& stfd)
|
||||
{
|
||||
if (stfd) {
|
||||
int fd = st_netfd_fileno(stfd);
|
||||
st_netfd_close(stfd);
|
||||
stfd = NULL;
|
||||
|
||||
// st does not close it sometimes,
|
||||
// close it manually.
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_HPP
|
||||
#define SRS_CORE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core.hpp>
|
||||
*/
|
||||
|
||||
/**
|
||||
* the core provides the common defined macros, utilities,
|
||||
* user must include the srs_core.hpp before any header, or maybe
|
||||
* build failed.
|
||||
*/
|
||||
|
||||
// for int64_t print using PRId64 format.
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#endif
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <assert.h>
|
||||
#define srs_assert(expression) assert(expression)
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <st.h>
|
||||
|
||||
// generated by configure.
|
||||
#include <srs_auto_headers.hpp>
|
||||
|
||||
// free the p and set to NULL.
|
||||
// p must be a T*.
|
||||
#define srs_freep(p) \
|
||||
if (p) { \
|
||||
delete p; \
|
||||
p = NULL; \
|
||||
} \
|
||||
(void)0
|
||||
// free the p which represents a array
|
||||
#define srs_freepa(p) \
|
||||
if (p) { \
|
||||
delete[] p; \
|
||||
p = NULL; \
|
||||
} \
|
||||
(void)0
|
||||
|
||||
// current release version
|
||||
#define RTMP_SIG_SRS_VERSION "0.9.3"
|
||||
// server info.
|
||||
#define RTMP_SIG_SRS_KEY "srs"
|
||||
#define RTMP_SIG_SRS_ROLE "origin server"
|
||||
#define RTMP_SIG_SRS_NAME RTMP_SIG_SRS_KEY"(simple rtmp server)"
|
||||
#define RTMP_SIG_SRS_URL "https://"RTMP_SIG_SRS_URL_SHORT
|
||||
#define RTMP_SIG_SRS_URL_SHORT "github.com/winlinvip/simple-rtmp-server"
|
||||
#define RTMP_SIG_SRS_WEB "http://blog.csdn.net/win_lin"
|
||||
#define RTMP_SIG_SRS_EMAIL "winlin@vip.126.com"
|
||||
#define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)"
|
||||
#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2014 winlin"
|
||||
#define RTMP_SIG_SRS_PRIMARY_AUTHROS "winlin,wenjiegit"
|
||||
|
||||
// compare
|
||||
#define srs_min(a, b) (((a) < (b))? (a) : (b))
|
||||
#define srs_max(a, b) (((a) < (b))? (b) : (a))
|
||||
|
||||
// get current system time in ms, use cache to avoid performance problem
|
||||
extern int64_t srs_get_system_time_ms();
|
||||
// the deamon st-thread will update it.
|
||||
extern void srs_update_system_time_ms();
|
||||
|
||||
// signal defines.
|
||||
#define SIGNAL_RELOAD SIGHUP
|
||||
|
||||
#include <string>
|
||||
// replace utility
|
||||
extern std::string srs_replace(std::string str, std::string old_str, std::string new_str);
|
||||
// dns resolve utility, return the resolved ip address.
|
||||
extern std::string srs_dns_resolve(std::string host);
|
||||
// resolve the vhost in query string
|
||||
// @param app, may contains the vhost in query string format:
|
||||
// app?vhost=request_vhost
|
||||
// app...vhost...request_vhost
|
||||
extern void srs_vhost_resolve(std::string& vhost, std::string& app);
|
||||
|
||||
// close the netfd, and close the underlayer fd.
|
||||
extern void srs_close_stfd(st_netfd_t& stfd);
|
||||
|
||||
/**
|
||||
* disable copy constructor of class
|
||||
*/
|
||||
#define disable_default_copy(className)\
|
||||
private:\
|
||||
/** \
|
||||
* disable the copy constructor and operator=, donot allow directly copy. \
|
||||
*/ \
|
||||
className(const className&); \
|
||||
className& operator= (const className&)
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,319 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_AMF0_HPP
|
||||
#define SRS_CORE_AMF0_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_amf0.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class SrsStream;
|
||||
class SrsAmf0Object;
|
||||
|
||||
/**
|
||||
* any amf0 value.
|
||||
* 2.1 Types Overview
|
||||
* value-type = number-type | boolean-type | string-type | object-type
|
||||
* | null-marker | undefined-marker | reference-type | ecma-array-type
|
||||
* | strict-array-type | date-type | long-string-type | xml-document-type
|
||||
* | typed-object-type
|
||||
*/
|
||||
struct SrsAmf0Any
|
||||
{
|
||||
char marker;
|
||||
|
||||
SrsAmf0Any();
|
||||
virtual ~SrsAmf0Any();
|
||||
|
||||
virtual bool is_string();
|
||||
virtual bool is_boolean();
|
||||
virtual bool is_number();
|
||||
virtual bool is_null();
|
||||
virtual bool is_undefined();
|
||||
virtual bool is_object();
|
||||
virtual bool is_object_eof();
|
||||
virtual bool is_ecma_array();
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 string from stream.
|
||||
* 2.4 String Type
|
||||
* string-type = string-marker UTF-8
|
||||
* @return default value is empty string.
|
||||
*/
|
||||
struct SrsAmf0String : public SrsAmf0Any
|
||||
{
|
||||
std::string value;
|
||||
|
||||
SrsAmf0String(const char* _value = NULL);
|
||||
virtual ~SrsAmf0String();
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 boolean from stream.
|
||||
* 2.4 String Type
|
||||
* boolean-type = boolean-marker U8
|
||||
* 0 is false, <> 0 is true
|
||||
* @return default value is false.
|
||||
*/
|
||||
struct SrsAmf0Boolean : public SrsAmf0Any
|
||||
{
|
||||
bool value;
|
||||
|
||||
SrsAmf0Boolean(bool _value = false);
|
||||
virtual ~SrsAmf0Boolean();
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 number from stream.
|
||||
* 2.2 Number Type
|
||||
* number-type = number-marker DOUBLE
|
||||
* @return default value is 0.
|
||||
*/
|
||||
struct SrsAmf0Number : public SrsAmf0Any
|
||||
{
|
||||
double value;
|
||||
|
||||
SrsAmf0Number(double _value = 0.0);
|
||||
virtual ~SrsAmf0Number();
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 null from stream.
|
||||
* 2.7 null Type
|
||||
* null-type = null-marker
|
||||
*/
|
||||
struct SrsAmf0Null : public SrsAmf0Any
|
||||
{
|
||||
SrsAmf0Null();
|
||||
virtual ~SrsAmf0Null();
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 undefined from stream.
|
||||
* 2.8 undefined Type
|
||||
* undefined-type = undefined-marker
|
||||
*/
|
||||
struct SrsAmf0Undefined : public SrsAmf0Any
|
||||
{
|
||||
SrsAmf0Undefined();
|
||||
virtual ~SrsAmf0Undefined();
|
||||
};
|
||||
|
||||
/**
|
||||
* 2.11 Object End Type
|
||||
* object-end-type = UTF-8-empty object-end-marker
|
||||
* 0x00 0x00 0x09
|
||||
*/
|
||||
struct SrsAmf0ObjectEOF : public SrsAmf0Any
|
||||
{
|
||||
int16_t utf8_empty;
|
||||
|
||||
SrsAmf0ObjectEOF();
|
||||
virtual ~SrsAmf0ObjectEOF();
|
||||
};
|
||||
|
||||
/**
|
||||
* to ensure in inserted order.
|
||||
* for the FMLE will crash when AMF0Object is not ordered by inserted,
|
||||
* if ordered in map, the string compare order, the FMLE will creash when
|
||||
* get the response of connect app.
|
||||
*/
|
||||
struct SrsUnSortedHashtable
|
||||
{
|
||||
private:
|
||||
typedef std::pair<std::string, SrsAmf0Any*> SrsObjectPropertyType;
|
||||
std::vector<SrsObjectPropertyType> properties;
|
||||
public:
|
||||
SrsUnSortedHashtable();
|
||||
virtual ~SrsUnSortedHashtable();
|
||||
|
||||
virtual int size();
|
||||
virtual void clear();
|
||||
virtual std::string key_at(int index);
|
||||
virtual SrsAmf0Any* value_at(int index);
|
||||
virtual void set(std::string key, SrsAmf0Any* value);
|
||||
|
||||
virtual SrsAmf0Any* get_property(std::string name);
|
||||
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
||||
virtual SrsAmf0Any* ensure_property_number(std::string name);
|
||||
};
|
||||
|
||||
/**
|
||||
* 2.5 Object Type
|
||||
* anonymous-object-type = object-marker *(object-property)
|
||||
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
|
||||
*/
|
||||
struct SrsAmf0Object : public SrsAmf0Any
|
||||
{
|
||||
private:
|
||||
SrsUnSortedHashtable properties;
|
||||
public:
|
||||
SrsAmf0ObjectEOF eof;
|
||||
|
||||
SrsAmf0Object();
|
||||
virtual ~SrsAmf0Object();
|
||||
|
||||
virtual int size();
|
||||
virtual std::string key_at(int index);
|
||||
virtual SrsAmf0Any* value_at(int index);
|
||||
virtual void set(std::string key, SrsAmf0Any* value);
|
||||
|
||||
virtual SrsAmf0Any* get_property(std::string name);
|
||||
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
||||
virtual SrsAmf0Any* ensure_property_number(std::string name);
|
||||
};
|
||||
|
||||
/**
|
||||
* 2.10 ECMA Array Type
|
||||
* ecma-array-type = associative-count *(object-property)
|
||||
* associative-count = U32
|
||||
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
|
||||
*/
|
||||
struct SrsASrsAmf0EcmaArray : public SrsAmf0Any
|
||||
{
|
||||
private:
|
||||
SrsUnSortedHashtable properties;
|
||||
public:
|
||||
int32_t count;
|
||||
SrsAmf0ObjectEOF eof;
|
||||
|
||||
SrsASrsAmf0EcmaArray();
|
||||
virtual ~SrsASrsAmf0EcmaArray();
|
||||
|
||||
virtual int size();
|
||||
virtual void clear();
|
||||
virtual std::string key_at(int index);
|
||||
virtual SrsAmf0Any* value_at(int index);
|
||||
virtual void set(std::string key, SrsAmf0Any* value);
|
||||
|
||||
virtual SrsAmf0Any* get_property(std::string name);
|
||||
virtual SrsAmf0Any* ensure_property_string(std::string name);
|
||||
};
|
||||
|
||||
/**
|
||||
* read amf0 utf8 string from stream.
|
||||
* 1.3.1 Strings and UTF-8
|
||||
* UTF-8 = U16 *(UTF8-char)
|
||||
* UTF8-char = UTF8-1 | UTF8-2 | UTF8-3 | UTF8-4
|
||||
* UTF8-1 = %x00-7F
|
||||
* @remark only support UTF8-1 char.
|
||||
*/
|
||||
extern int srs_amf0_read_utf8(SrsStream* stream, std::string& value);
|
||||
extern int srs_amf0_write_utf8(SrsStream* stream, std::string value);
|
||||
|
||||
/**
|
||||
* read amf0 string from stream.
|
||||
* 2.4 String Type
|
||||
* string-type = string-marker UTF-8
|
||||
*/
|
||||
extern int srs_amf0_read_string(SrsStream* stream, std::string& value);
|
||||
extern int srs_amf0_write_string(SrsStream* stream, std::string value);
|
||||
|
||||
/**
|
||||
* read amf0 boolean from stream.
|
||||
* 2.4 String Type
|
||||
* boolean-type = boolean-marker U8
|
||||
* 0 is false, <> 0 is true
|
||||
*/
|
||||
extern int srs_amf0_read_boolean(SrsStream* stream, bool& value);
|
||||
extern int srs_amf0_write_boolean(SrsStream* stream, bool value);
|
||||
|
||||
/**
|
||||
* read amf0 number from stream.
|
||||
* 2.2 Number Type
|
||||
* number-type = number-marker DOUBLE
|
||||
*/
|
||||
extern int srs_amf0_read_number(SrsStream* stream, double& value);
|
||||
extern int srs_amf0_write_number(SrsStream* stream, double value);
|
||||
|
||||
/**
|
||||
* read amf0 null from stream.
|
||||
* 2.7 null Type
|
||||
* null-type = null-marker
|
||||
*/
|
||||
extern int srs_amf0_read_null(SrsStream* stream);
|
||||
extern int srs_amf0_write_null(SrsStream* stream);
|
||||
|
||||
/**
|
||||
* read amf0 undefined from stream.
|
||||
* 2.8 undefined Type
|
||||
* undefined-type = undefined-marker
|
||||
*/
|
||||
extern int srs_amf0_read_undefined(SrsStream* stream);
|
||||
extern int srs_amf0_write_undefined(SrsStream* stream);
|
||||
|
||||
extern int srs_amf0_read_any(SrsStream* stream, SrsAmf0Any*& value);
|
||||
|
||||
/**
|
||||
* read amf0 object from stream.
|
||||
* 2.5 Object Type
|
||||
* anonymous-object-type = object-marker *(object-property)
|
||||
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
|
||||
*/
|
||||
extern int srs_amf0_read_object(SrsStream* stream, SrsAmf0Object*& value);
|
||||
extern int srs_amf0_write_object(SrsStream* stream, SrsAmf0Object* value);
|
||||
|
||||
/**
|
||||
* read amf0 object from stream.
|
||||
* 2.10 ECMA Array Type
|
||||
* ecma-array-type = associative-count *(object-property)
|
||||
* associative-count = U32
|
||||
* object-property = (UTF-8 value-type) | (UTF-8-empty object-end-marker)
|
||||
*/
|
||||
extern int srs_amf0_read_ecma_array(SrsStream* stream, SrsASrsAmf0EcmaArray*& value);
|
||||
extern int srs_amf0_write_ecma_array(SrsStream* stream, SrsASrsAmf0EcmaArray* value);
|
||||
|
||||
/**
|
||||
* get amf0 objects size.
|
||||
*/
|
||||
extern int srs_amf0_get_utf8_size(std::string value);
|
||||
extern int srs_amf0_get_string_size(std::string value);
|
||||
extern int srs_amf0_get_number_size();
|
||||
extern int srs_amf0_get_null_size();
|
||||
extern int srs_amf0_get_undefined_size();
|
||||
extern int srs_amf0_get_boolean_size();
|
||||
extern int srs_amf0_get_object_size(SrsAmf0Object* obj);
|
||||
extern int srs_amf0_get_ecma_array_size(SrsASrsAmf0EcmaArray* arr);
|
||||
|
||||
/**
|
||||
* convert the any to specified object.
|
||||
* @return T*, the converted object. never NULL.
|
||||
* @remark, user must ensure the current object type,
|
||||
* or the covert will cause assert failed.
|
||||
*/
|
||||
template<class T>
|
||||
T* srs_amf0_convert(SrsAmf0Any* any)
|
||||
{
|
||||
T* p = dynamic_cast<T*>(any);
|
||||
srs_assert(p != NULL);
|
||||
return p;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_autofree.hpp>
|
|
@ -1,71 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_AUTO_FREE_HPP
|
||||
#define SRS_CORE_AUTO_FREE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_autofree.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
/**
|
||||
* auto free the instance in the current scope.
|
||||
*/
|
||||
#define SrsAutoFree(className, instance, is_array) \
|
||||
__SrsAutoFree<className> _auto_free_##instance(&instance, is_array)
|
||||
|
||||
template<class T>
|
||||
class __SrsAutoFree
|
||||
{
|
||||
private:
|
||||
T** ptr;
|
||||
bool is_array;
|
||||
public:
|
||||
/**
|
||||
* auto delete the ptr.
|
||||
* @is_array a bool value indicates whether the ptr is a array.
|
||||
*/
|
||||
__SrsAutoFree(T** _ptr, bool _is_array){
|
||||
ptr = _ptr;
|
||||
is_array = _is_array;
|
||||
}
|
||||
|
||||
virtual ~__SrsAutoFree(){
|
||||
if (ptr == NULL || *ptr == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_array) {
|
||||
delete[] *ptr;
|
||||
} else {
|
||||
delete *ptr;
|
||||
}
|
||||
|
||||
*ptr = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -1,454 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 wenjiegit
|
||||
|
||||
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_core_bandwidth.hpp>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_amf0.hpp>
|
||||
#include <srs_core_protocol.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
|
||||
SrsBandwidth::SrsBandwidth()
|
||||
{
|
||||
}
|
||||
|
||||
SrsBandwidth::~SrsBandwidth()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmp* _rtmp)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
rtmp = _rtmp;
|
||||
req = _req;
|
||||
|
||||
if (!config->get_bw_check_enabled(req->vhost)) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// validate the bandwidth check key
|
||||
std::string key = "key=" + config->get_bw_check_key(req->vhost);
|
||||
if (req->tcUrl.find(key) == std::string::npos) {
|
||||
ret = ERROR_SYSTEM_BANDWIDTH_KEY;
|
||||
srs_error("check the vhost=%s %s failed, tcUrl=%s, ret=%d",
|
||||
req->vhost.c_str(), key.c_str(), req->tcUrl.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// shared global last check time,
|
||||
// to avoid attach by bandwidth check,
|
||||
// if client request check in the window(specifeid by interval),
|
||||
// directly reject the request.
|
||||
static int64_t last_check_time = 0;
|
||||
int interval_ms = config->get_bw_check_interval_ms(req->vhost);
|
||||
|
||||
int64_t time_now = srs_get_system_time_ms();
|
||||
// reject the connection in the interval window.
|
||||
if (last_check_time > 0 && time_now - last_check_time < interval_ms) {
|
||||
ret = ERROR_SYSTEM_BANDWIDTH_DENIED;
|
||||
srs_trace("bandcheck denied, "
|
||||
"last_check=%"PRId64", now=%"PRId64", interval=%d",
|
||||
last_check_time, time_now, interval_ms);
|
||||
|
||||
rtmp->response_connect_reject(req, "bandcheck rejected");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// accept and do bandwidth check.
|
||||
last_check_time = time_now;
|
||||
|
||||
char* local_ip = 0;
|
||||
if ((ret = get_local_ip(stfd, local_ip)) != ERROR_SUCCESS) {
|
||||
srs_error("get local ip failed. ret = %d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = rtmp->response_connect_app(req, local_ip)) != ERROR_SUCCESS) {
|
||||
srs_error("response connect app failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return do_bandwidth_check();
|
||||
}
|
||||
|
||||
int SrsBandwidth::get_local_ip(st_netfd_t stfd, char *&local_ip)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int fd = st_netfd_fileno(stfd);
|
||||
|
||||
// discovery client information
|
||||
sockaddr_in addr;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {
|
||||
ret = ERROR_SOCKET_GET_LOCAL_IP;
|
||||
srs_error("discovery local ip information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get local ip success.");
|
||||
|
||||
// ip v4 or v6
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
|
||||
ret = ERROR_SOCKET_GET_LOCAL_IP;
|
||||
srs_error("convert local ip information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
local_ip = new char[strlen(buf) + 1];
|
||||
strcpy(local_ip, buf);
|
||||
|
||||
srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsBandwidth::do_bandwidth_check()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsProtocol* protocol = rtmp->get_protocol();
|
||||
|
||||
int play_duration_ms = 3000;
|
||||
int play_interval_ms = 0;
|
||||
int play_actual_duration_ms = 0;
|
||||
int play_bytes = 0;
|
||||
|
||||
int publish_duration_ms = 3000;
|
||||
int publish_interval_ms = 0;
|
||||
int publish_actual_duration_ms = 0;
|
||||
int publish_bytes = 0;
|
||||
|
||||
int limit_kbps = config->get_bw_check_limit_kbps(req->vhost);
|
||||
|
||||
int64_t start_time = srs_get_system_time_ms();
|
||||
|
||||
ret = check_play(play_duration_ms,
|
||||
play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps);
|
||||
if (ret != ERROR_SUCCESS) {
|
||||
srs_error("band width play check failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = check_publish(publish_duration_ms,
|
||||
publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps);
|
||||
if (ret != ERROR_SUCCESS) {
|
||||
srs_error("band width publish check failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int64_t end_time = srs_get_system_time_ms();
|
||||
int play_kbps = play_bytes * 8 / play_actual_duration_ms;
|
||||
int publish_kbps = publish_bytes * 8 / publish_actual_duration_ms;
|
||||
|
||||
srs_trace("bandwidth check finished. start=%"PRId64"ms, end=%"PRId64"ms, "
|
||||
"duartion=%dms, play=%dkbps, publish=%dkbps, tcUrl=%s, ret=%#x",
|
||||
start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps,
|
||||
req->tcUrl.c_str(), ret);
|
||||
|
||||
// send finished msg
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_finish();
|
||||
pkt->data->set("code", new SrsAmf0Number(ERROR_SUCCESS));
|
||||
pkt->data->set("start_time", new SrsAmf0Number(start_time));
|
||||
pkt->data->set("end_time", new SrsAmf0Number(end_time));
|
||||
pkt->data->set("play_kbps", new SrsAmf0Number(play_kbps));
|
||||
pkt->data->set("publish_kbps", new SrsAmf0Number(publish_kbps));
|
||||
pkt->data->set("play_bytes", new SrsAmf0Number(play_bytes));
|
||||
pkt->data->set("play_time", new SrsAmf0Number(play_actual_duration_ms));
|
||||
pkt->data->set("publish_bytes", new SrsAmf0Number(publish_bytes));
|
||||
pkt->data->set("publish_time", new SrsAmf0Number(publish_actual_duration_ms));
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check finish message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// if flash, we notice the result, and expect a final packet.
|
||||
while (true) {
|
||||
SrsCommonMessage* msg = NULL;
|
||||
SrsBandwidthPacket* pkt = NULL;
|
||||
if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
|
||||
// info level to ignore and return success.
|
||||
srs_info("expect final message failed. ret=%d", ret);
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
srs_info("get final message success.");
|
||||
|
||||
if (pkt->is_flash_final()) {
|
||||
srs_info("BW check recv flash final response.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
srs_info("BW check finished.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsBandwidth::check_play(
|
||||
int duration_ms, int interval_ms, int& actual_duration_ms,
|
||||
int& play_bytes, int max_play_kbps)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsProtocol* protocol = rtmp->get_protocol();
|
||||
|
||||
if (true) {
|
||||
// send start play command to client
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play();
|
||||
|
||||
pkt->data->set("duration_ms", new SrsAmf0Number(duration_ms));
|
||||
pkt->data->set("interval_ms", new SrsAmf0Number(interval_ms));
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check start play message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("BW check begin.");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// recv client's starting play response
|
||||
SrsCommonMessage* msg = NULL;
|
||||
SrsBandwidthPacket* pkt = NULL;
|
||||
if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
|
||||
srs_error("expect bandwidth message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
srs_info("get bandwidth message succes.");
|
||||
|
||||
if (pkt->is_starting_play()) {
|
||||
srs_info("BW check recv play begin response.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// send play data to client
|
||||
int64_t current_time = srs_get_system_time_ms();
|
||||
int size = 1024; // TODO: FIXME: magic number
|
||||
char random_data[size];
|
||||
memset(random_data, 'A', size);
|
||||
|
||||
int interval = 0;
|
||||
int data_count = 1;
|
||||
while ( (srs_get_system_time_ms() - current_time) < duration_ms ) {
|
||||
st_usleep(interval);
|
||||
|
||||
// TODO: FIXME: use shared ptr message.
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_playing();
|
||||
|
||||
// TODO: FIXME: magic number
|
||||
for (int i = 0; i < data_count; ++i) {
|
||||
std::stringstream seq;
|
||||
seq << i;
|
||||
std::string play_data = "SrS band check data from server's playing......";
|
||||
pkt->data->set(seq.str(), new SrsAmf0String(play_data.c_str()));
|
||||
}
|
||||
data_count += 2;
|
||||
|
||||
// TODO: FIXME: get length from the rtmp protocol stack.
|
||||
play_bytes += pkt->get_payload_length();
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check play messages failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// sleep while current kbps <= max_play_kbps
|
||||
int kbps = 0;
|
||||
while (true) {
|
||||
if(srs_get_system_time_ms() - current_time != 0)
|
||||
kbps = play_bytes * 8 / (srs_get_system_time_ms() - current_time);
|
||||
|
||||
if (kbps > max_play_kbps) {
|
||||
st_usleep(500);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
actual_duration_ms = srs_get_system_time_ms() - current_time;
|
||||
srs_info("BW check send play bytes over.");
|
||||
|
||||
if (true) {
|
||||
// notify client to stop play
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play();
|
||||
pkt->data->set("duration_ms", new SrsAmf0Number(duration_ms));
|
||||
pkt->data->set("interval_ms", new SrsAmf0Number(interval_ms));
|
||||
pkt->data->set("duration_delta", new SrsAmf0Number(actual_duration_ms));
|
||||
pkt->data->set("bytes_delta", new SrsAmf0Number(play_bytes));
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check stop play message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("BW check stop play bytes.");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// recv client's stop play response.
|
||||
SrsCommonMessage* msg = NULL;
|
||||
SrsBandwidthPacket* pkt = NULL;
|
||||
if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
|
||||
srs_error("expect bandwidth message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
srs_info("get bandwidth message succes.");
|
||||
|
||||
if (pkt->is_stopped_play()) {
|
||||
srs_info("BW check recv stop play response.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsBandwidth::check_publish(
|
||||
int duration_ms, int interval_ms, int& actual_duration_ms,
|
||||
int& publish_bytes, int max_pub_kbps)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsProtocol* protocol = rtmp->get_protocol();
|
||||
|
||||
if (true) {
|
||||
// notify client to start publish
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish();
|
||||
|
||||
pkt->data->set("duration_ms", new SrsAmf0Number(duration_ms));
|
||||
pkt->data->set("interval_ms", new SrsAmf0Number(interval_ms));
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check start publish message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("BW check publish begin.");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// read client's notification of starting publish
|
||||
SrsCommonMessage* msg = NULL;
|
||||
SrsBandwidthPacket* pkt = NULL;
|
||||
if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
|
||||
srs_error("expect bandwidth message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
srs_info("get bandwidth message succes.");
|
||||
|
||||
if (pkt->is_starting_publish()) {
|
||||
srs_info("BW check recv publish begin response.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// recv publish msgs until @duration_ms ms
|
||||
int64_t current_time = srs_get_system_time_ms();
|
||||
while ( (srs_get_system_time_ms() - current_time) < duration_ms ) {
|
||||
st_usleep(0);
|
||||
|
||||
SrsCommonMessage* msg = NULL;
|
||||
if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
|
||||
srs_error("recv message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
|
||||
// TODO: FIXME.
|
||||
publish_bytes += msg->header.payload_length;
|
||||
|
||||
int kbps = 0;
|
||||
while (true) {
|
||||
if(srs_get_system_time_ms() - current_time != 0)
|
||||
kbps = publish_bytes * 8 / (srs_get_system_time_ms() - current_time);
|
||||
|
||||
if (kbps > max_pub_kbps) {
|
||||
st_usleep(500);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
actual_duration_ms = srs_get_system_time_ms() - current_time;
|
||||
srs_info("BW check recv publish data over.");
|
||||
|
||||
if (true) {
|
||||
// notify client to stop publish
|
||||
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish();
|
||||
pkt->data->set("duration_ms", new SrsAmf0Number(duration_ms));
|
||||
pkt->data->set("interval_ms", new SrsAmf0Number(interval_ms));
|
||||
pkt->data->set("duration_delta", new SrsAmf0Number(actual_duration_ms));
|
||||
pkt->data->set("bytes_delta", new SrsAmf0Number(publish_bytes));
|
||||
|
||||
SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send bandwidth check stop publish message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("BW check stop publish bytes.");
|
||||
}
|
||||
|
||||
// expect client to stop publish
|
||||
// if flash client, we never expect the client stop publish bytes,
|
||||
// for the flash send call packet to test publish bandwidth,
|
||||
// there are many many packets in the queue.
|
||||
// we just ignore the packet and send the bandwidth test data.
|
||||
// TODO: FIXME: check whether flash client.
|
||||
while (false) {
|
||||
// recv client's stop publish response.
|
||||
SrsCommonMessage* msg = NULL;
|
||||
SrsBandwidthPacket* pkt = NULL;
|
||||
if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
|
||||
srs_error("expect bandwidth message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
srs_info("get bandwidth message succes.");
|
||||
|
||||
if (pkt->is_stopped_publish()) {
|
||||
srs_info("BW check recv stop publish response.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 wenjiegit
|
||||
|
||||
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_CORE_BANDWIDTH_HPP
|
||||
#define SRS_CORE_BANDWIDTH_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_bandwidth.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
class SrsRequest;
|
||||
class SrsRtmp;
|
||||
|
||||
/**
|
||||
* bandwidth test agent which provides the interfaces for bandwidth check.
|
||||
* 1. if vhost disabled bandwidth check, ignore.
|
||||
* 2. otherwise, check the key, error if verify failed.
|
||||
* 3. check the interval limit, error if bandwidth in the interval window.
|
||||
* 4. check the bandwidth under the max kbps.
|
||||
* 5. send the bandwidth data to client.
|
||||
* bandwidth workflow:
|
||||
* +------------+ +----------+
|
||||
* | Client | | Server |
|
||||
* +-----+------+ +-----+----+
|
||||
* | |
|
||||
* | connect vhost------> | if vhost enable bandwidth,
|
||||
* | <-----result(success) | do bandwidth check.
|
||||
* | |
|
||||
* | <----call(start play) | onSrsBandCheckStartPlayBytes
|
||||
* | result(playing)-----> | onSrsBandCheckStartingPlayBytes
|
||||
* | <-------data(playing) | onSrsBandCheckStartingPlayBytes
|
||||
* | <-----call(stop play) | onSrsBandCheckStopPlayBytes
|
||||
* | result(stopped)-----> | onSrsBandCheckStoppedPlayBytes
|
||||
* | |
|
||||
* | <-call(start publish) | onSrsBandCheckStartPublishBytes
|
||||
* | result(publishing)--> | onSrsBandCheckStartingPublishBytes
|
||||
* | data(publishing)----> | onSrsBandCheckStartingPublishBytes
|
||||
* | <--call(stop publish) | onSrsBandCheckStopPublishBytes
|
||||
* | result(stopped)(1)--> | onSrsBandCheckStoppedPublishBytes
|
||||
* | |
|
||||
* | <--------------report |
|
||||
* | final(2)------------> | finalClientPacket
|
||||
* | <END> |
|
||||
*
|
||||
* 1. when flash client, server ignore the publish stopped result,
|
||||
* and directly send the report to flash client.
|
||||
* 2. flash client only. when got report, flash client should send out
|
||||
* a final packet and close the connection immediately.
|
||||
*/
|
||||
class SrsBandwidth
|
||||
{
|
||||
private:
|
||||
SrsRequest* req;
|
||||
SrsRtmp* rtmp;
|
||||
public:
|
||||
SrsBandwidth();
|
||||
virtual ~SrsBandwidth();
|
||||
public:
|
||||
/**
|
||||
* do the bandwidth test.
|
||||
*/
|
||||
virtual int bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmp* _rtmp);
|
||||
private:
|
||||
virtual int get_local_ip(st_netfd_t stfd, char *&local_ip);
|
||||
/**
|
||||
* used to process band width check from client.
|
||||
*/
|
||||
virtual int do_bandwidth_check();
|
||||
virtual int check_play(int duration_ms, int interval_ms, int& actual_duration_ms, int& play_bytes, int max_play_kbps);
|
||||
virtual int check_publish(int duration_ms, int interval_ms, int& actual_duration_ms, int& publish_bytes, int max_pub_kbps);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_buffer.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_socket.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
|
||||
#define SOCKET_READ_SIZE 4096
|
||||
|
||||
SrsBuffer::SrsBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
SrsBuffer::~SrsBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsBuffer::size()
|
||||
{
|
||||
return (int)data.size();
|
||||
}
|
||||
|
||||
char* SrsBuffer::bytes()
|
||||
{
|
||||
return &data.at(0);
|
||||
}
|
||||
|
||||
void SrsBuffer::erase(int size)
|
||||
{
|
||||
data.erase(data.begin(), data.begin() + size);
|
||||
}
|
||||
|
||||
void SrsBuffer::append(char* bytes, int size)
|
||||
{
|
||||
data.insert(data.end(), bytes, bytes + size);
|
||||
}
|
||||
|
||||
int SrsBuffer::ensure_buffer_bytes(SrsSocket* skt, int required_size)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (required_size < 0) {
|
||||
ret = ERROR_SYSTEM_SIZE_NEGATIVE;
|
||||
srs_error("size is negative. size=%d, ret=%d", required_size, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
while (size() < required_size) {
|
||||
char buffer[SOCKET_READ_SIZE];
|
||||
|
||||
ssize_t nread;
|
||||
if ((ret = skt->read(buffer, SOCKET_READ_SIZE, &nread)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_assert((int)nread > 0);
|
||||
append(buffer, (int)nread);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_BUFFER_HPP
|
||||
#define SRS_CORE_BUFFER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_buffer.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class SrsSocket;
|
||||
|
||||
/**
|
||||
* the buffer provices bytes cache for protocol. generally,
|
||||
* protocol recv data from socket, put into buffer, decode to RTMP message.
|
||||
* protocol encode RTMP message to bytes, put into buffer, send to socket.
|
||||
*/
|
||||
class SrsBuffer
|
||||
{
|
||||
private:
|
||||
std::vector<char> data;
|
||||
public:
|
||||
SrsBuffer();
|
||||
virtual ~SrsBuffer();
|
||||
public:
|
||||
virtual int size();
|
||||
virtual char* bytes();
|
||||
virtual void erase(int size);
|
||||
private:
|
||||
virtual void append(char* bytes, int size);
|
||||
public:
|
||||
virtual int ensure_buffer_bytes(SrsSocket* skt, int required_size);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,751 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_client.hpp>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_protocol.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_core_source.hpp>
|
||||
#include <srs_core_server.hpp>
|
||||
#include <srs_core_pithy_print.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_refer.hpp>
|
||||
#include <srs_core_hls.hpp>
|
||||
#include <srs_core_http.hpp>
|
||||
#include <srs_core_bandwidth.hpp>
|
||||
|
||||
SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||
: SrsConnection(srs_server, client_stfd)
|
||||
{
|
||||
ip = NULL;
|
||||
req = new SrsRequest();
|
||||
res = new SrsResponse();
|
||||
rtmp = new SrsRtmp(client_stfd);
|
||||
refer = new SrsRefer();
|
||||
#ifdef SRS_HTTP
|
||||
http_hooks = new SrsHttpHooks();
|
||||
#endif
|
||||
bandwidth = new SrsBandwidth();
|
||||
|
||||
config->subscribe(this);
|
||||
}
|
||||
|
||||
SrsClient::~SrsClient()
|
||||
{
|
||||
config->unsubscribe(this);
|
||||
|
||||
srs_freepa(ip);
|
||||
srs_freep(req);
|
||||
srs_freep(res);
|
||||
srs_freep(rtmp);
|
||||
srs_freep(refer);
|
||||
#ifdef SRS_HTTP
|
||||
srs_freep(http_hooks);
|
||||
#endif
|
||||
srs_freep(bandwidth);
|
||||
}
|
||||
|
||||
// TODO: return detail message when error for client.
|
||||
int SrsClient::do_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
||||
srs_error("get peer ip failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"",
|
||||
ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);
|
||||
|
||||
rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
|
||||
rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);
|
||||
|
||||
if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {
|
||||
srs_error("rtmp handshake failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("rtmp handshake success");
|
||||
|
||||
if ((ret = rtmp->connect_app(req)) != ERROR_SUCCESS) {
|
||||
srs_error("rtmp connect vhost/app failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("rtmp connect app success");
|
||||
|
||||
if ((ret = check_vhost()) != ERROR_SUCCESS) {
|
||||
srs_error("check vhost failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("check vhost success.");
|
||||
|
||||
srs_trace("rtmp connect app success. "
|
||||
"tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s",
|
||||
req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(),
|
||||
req->schema.c_str(), req->vhost.c_str(), req->port.c_str(),
|
||||
req->app.c_str());
|
||||
|
||||
ret = service_cycle();
|
||||
on_close();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::on_reload_vhost_removed(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// if the vhost connected is removed, disconnect the client.
|
||||
srs_trace("vhost %s removed/disabled, close client url=%s",
|
||||
vhost.c_str(), req->get_stream_url().c_str());
|
||||
|
||||
srs_close_stfd(stfd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::service_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = rtmp->set_window_ack_size(2.5 * 1000 * 1000)) != ERROR_SUCCESS) {
|
||||
srs_error("set window acknowledgement size failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("set window acknowledgement size success");
|
||||
|
||||
if ((ret = rtmp->set_peer_bandwidth(2.5 * 1000 * 1000, 2)) != ERROR_SUCCESS) {
|
||||
srs_error("set peer bandwidth failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("set peer bandwidth success");
|
||||
|
||||
// do bandwidth test if connect to the vhost which is for bandwidth check.
|
||||
if (config->get_bw_check_enabled(req->vhost)) {
|
||||
return bandwidth->bandwidth_test(req, stfd, rtmp);
|
||||
}
|
||||
|
||||
if ((ret = rtmp->response_connect_app(req)) != ERROR_SUCCESS) {
|
||||
srs_error("response connect app failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("response connect app success");
|
||||
|
||||
if ((ret = rtmp->on_bw_done()) != ERROR_SUCCESS) {
|
||||
srs_error("on_bw_done failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("on_bw_done success");
|
||||
|
||||
while (true) {
|
||||
ret = stream_service_cycle();
|
||||
|
||||
// stream service must terminated with error, never success.
|
||||
srs_assert(ret != ERROR_SUCCESS);
|
||||
|
||||
// when not system control error, fatal error, return.
|
||||
if (!srs_is_system_control_error(ret)) {
|
||||
srs_error("stream service cycle failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// for "some" system control error,
|
||||
// logical accept and retry stream service.
|
||||
if (ret == ERROR_CONTROL_RTMP_CLOSE) {
|
||||
// set timeout to a larger value, for user paused.
|
||||
rtmp->set_recv_timeout(SRS_PAUSED_RECV_TIMEOUT_US);
|
||||
rtmp->set_send_timeout(SRS_PAUSED_SEND_TIMEOUT_US);
|
||||
|
||||
srs_trace("control message(close) accept, retry stream service.");
|
||||
continue;
|
||||
}
|
||||
|
||||
// for other system control message, fatal error.
|
||||
srs_error("control message(%d) reject as error. ret=%d", ret, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::stream_service_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsClientType type;
|
||||
if ((ret = rtmp->identify_client(res->stream_id, type, req->stream)) != ERROR_SUCCESS) {
|
||||
srs_error("identify client failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
req->strip();
|
||||
srs_trace("identify client success. type=%d, stream_name=%s", type, req->stream.c_str());
|
||||
|
||||
// client is identified, set the timeout to service timeout.
|
||||
rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
|
||||
rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);
|
||||
|
||||
// set chunk size to larger.
|
||||
int chunk_size = config->get_chunk_size(req->vhost);
|
||||
if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) {
|
||||
srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("set chunk_size=%d success", chunk_size);
|
||||
|
||||
// find a source to serve.
|
||||
SrsSource* source = SrsSource::find(req);
|
||||
srs_assert(source != NULL);
|
||||
|
||||
// check publish available.
|
||||
if (type != SrsClientPlay && !source->can_publish()) {
|
||||
ret = ERROR_SYSTEM_STREAM_BUSY;
|
||||
srs_warn("stream %s is already publishing. ret=%d",
|
||||
req->get_stream_url().c_str(), ret);
|
||||
// to delay request
|
||||
st_usleep(SRS_STREAM_BUSY_SLEEP_US);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool enabled_cache = config->get_gop_cache(req->vhost);
|
||||
srs_info("source found, url=%s, enabled_cache=%d", req->get_stream_url().c_str(), enabled_cache);
|
||||
source->set_cache(enabled_cache);
|
||||
|
||||
switch (type) {
|
||||
case SrsClientPlay: {
|
||||
srs_verbose("start to play stream %s.", req->stream.c_str());
|
||||
|
||||
if ((ret = rtmp->start_play(res->stream_id)) != ERROR_SUCCESS) {
|
||||
srs_error("start to play stream failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = on_play()) != ERROR_SUCCESS) {
|
||||
srs_error("http hook on_play failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("start to play stream %s success", req->stream.c_str());
|
||||
ret = playing(source);
|
||||
on_stop();
|
||||
return ret;
|
||||
}
|
||||
case SrsClientFMLEPublish: {
|
||||
srs_verbose("FMLE start to publish stream %s.", req->stream.c_str());
|
||||
|
||||
if ((ret = rtmp->start_fmle_publish(res->stream_id)) != ERROR_SUCCESS) {
|
||||
srs_error("start to publish stream failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = on_publish()) != ERROR_SUCCESS) {
|
||||
srs_error("http hook on_publish failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("start to publish stream %s success", req->stream.c_str());
|
||||
ret = publish(source, true);
|
||||
source->on_unpublish();
|
||||
on_unpublish();
|
||||
return ret;
|
||||
}
|
||||
case SrsClientFlashPublish: {
|
||||
srs_verbose("flash start to publish stream %s.", req->stream.c_str());
|
||||
|
||||
if ((ret = rtmp->start_flash_publish(res->stream_id)) != ERROR_SUCCESS) {
|
||||
srs_error("flash start to publish stream failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = on_publish()) != ERROR_SUCCESS) {
|
||||
srs_error("http hook on_publish failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("flash start to publish stream %s success", req->stream.c_str());
|
||||
ret = publish(source, false);
|
||||
source->on_unpublish();
|
||||
on_unpublish();
|
||||
return ret;
|
||||
}
|
||||
default: {
|
||||
ret = ERROR_SYSTEM_CLIENT_INVALID;
|
||||
srs_info("invalid client type=%d. ret=%d", type, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::check_vhost()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(req != NULL);
|
||||
|
||||
SrsConfDirective* vhost = config->get_vhost(req->vhost);
|
||||
if (vhost == NULL) {
|
||||
ret = ERROR_RTMP_VHOST_NOT_FOUND;
|
||||
srs_error("vhost %s not found. ret=%d", req->vhost.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!config->get_vhost_enabled(req->vhost)) {
|
||||
ret = ERROR_RTMP_VHOST_NOT_FOUND;
|
||||
srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (req->vhost != vhost->arg0()) {
|
||||
srs_trace("vhost change from %s to %s", req->vhost.c_str(), vhost->arg0().c_str());
|
||||
req->vhost = vhost->arg0();
|
||||
}
|
||||
|
||||
if ((ret = refer->check(req->pageUrl, config->get_refer(req->vhost))) != ERROR_SUCCESS) {
|
||||
srs_error("check refer failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("check refer success.");
|
||||
|
||||
if ((ret = on_connect()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::playing(SrsSource* source)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = refer->check(req->pageUrl, config->get_refer_play(req->vhost))) != ERROR_SUCCESS) {
|
||||
srs_error("check play_refer failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("check play_refer success.");
|
||||
|
||||
SrsConsumer* consumer = NULL;
|
||||
if ((ret = source->create_consumer(consumer)) != ERROR_SUCCESS) {
|
||||
srs_error("create consumer failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_assert(consumer != NULL);
|
||||
SrsAutoFree(SrsConsumer, consumer, false);
|
||||
srs_verbose("consumer created success.");
|
||||
|
||||
rtmp->set_recv_timeout(SRS_PULSE_TIMEOUT_US);
|
||||
|
||||
SrsPithyPrint pithy_print(SRS_STAGE_PLAY_USER);
|
||||
|
||||
while (true) {
|
||||
pithy_print.elapse(SRS_PULSE_TIMEOUT_US / 1000);
|
||||
|
||||
// switch to other st-threads.
|
||||
st_usleep(0);
|
||||
|
||||
// read from client.
|
||||
int ctl_msg_ret = ERROR_SUCCESS;
|
||||
if (true) {
|
||||
SrsCommonMessage* msg = NULL;
|
||||
ctl_msg_ret = ret = rtmp->recv_message(&msg);
|
||||
|
||||
srs_verbose("play loop recv message. ret=%d", ret);
|
||||
if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {
|
||||
srs_error("recv client control message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = process_play_control_msg(consumer, msg)) != ERROR_SUCCESS) {
|
||||
if (!srs_is_system_control_error(ret)) {
|
||||
srs_error("process play control message failed. ret=%d", ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// get messages from consumer.
|
||||
SrsSharedPtrMessage** msgs = NULL;
|
||||
int count = 0;
|
||||
if ((ret = consumer->get_packets(0, msgs, count)) != ERROR_SUCCESS) {
|
||||
srs_error("get messages from consumer failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// reportable
|
||||
if (pithy_print.can_print()) {
|
||||
srs_trace("-> time=%"PRId64", cmr=%d, msgs=%d, obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
|
||||
pithy_print.get_age(), ctl_msg_ret, count, rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
|
||||
}
|
||||
|
||||
if (count <= 0) {
|
||||
srs_verbose("no packets in queue.");
|
||||
continue;
|
||||
}
|
||||
SrsAutoFree(SrsSharedPtrMessage*, msgs, true);
|
||||
|
||||
// sendout messages
|
||||
for (int i = 0; i < count; i++) {
|
||||
SrsSharedPtrMessage* msg = msgs[i];
|
||||
|
||||
// the send_message will free the msg,
|
||||
// so set the msgs[i] to NULL.
|
||||
msgs[i] = NULL;
|
||||
|
||||
if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("send message to client failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::publish(SrsSource* source, bool is_fmle)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = refer->check(req->pageUrl, config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) {
|
||||
srs_error("check publish_refer failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("check publish_refer success.");
|
||||
|
||||
SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);
|
||||
|
||||
// notify the hls to prepare when publish start.
|
||||
if ((ret = source->on_publish(req)) != ERROR_SUCCESS) {
|
||||
srs_error("hls on_publish failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("hls on_publish success.");
|
||||
|
||||
while (true) {
|
||||
// switch to other st-threads.
|
||||
st_usleep(0);
|
||||
|
||||
SrsCommonMessage* msg = NULL;
|
||||
if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
|
||||
srs_error("recv identify client message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
|
||||
pithy_print.set_age(msg->header.timestamp);
|
||||
|
||||
// reportable
|
||||
if (pithy_print.can_print()) {
|
||||
srs_trace("<- time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
|
||||
pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
|
||||
}
|
||||
|
||||
if ((ret = process_publish_message(source, msg, is_fmle)) != ERROR_SUCCESS) {
|
||||
srs_error("process publish message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::process_publish_message(SrsSource* source, SrsCommonMessage* msg, bool is_fmle)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// process audio packet
|
||||
if (msg->header.is_audio()) {
|
||||
if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("source process audio message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
// process video packet
|
||||
if (msg->header.is_video()) {
|
||||
if ((ret = source->on_video(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("source process video message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// process onMetaData
|
||||
if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
|
||||
if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
|
||||
srs_error("decode onMetaData message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsPacket* pkt = msg->get_packet();
|
||||
if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {
|
||||
SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);
|
||||
if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {
|
||||
srs_error("source process onMetaData message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("process onMetaData message success.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("ignore AMF0/AMF3 data message.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// process UnPublish event.
|
||||
if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {
|
||||
if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
|
||||
srs_error("decode unpublish message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// flash unpublish.
|
||||
if (!is_fmle) {
|
||||
srs_trace("flash publish finished.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsPacket* pkt = msg->get_packet();
|
||||
if (dynamic_cast<SrsFMLEStartPacket*>(pkt)) {
|
||||
SrsFMLEStartPacket* unpublish = dynamic_cast<SrsFMLEStartPacket*>(pkt);
|
||||
return rtmp->fmle_unpublish(res->stream_id, unpublish->transaction_id);
|
||||
}
|
||||
|
||||
srs_trace("ignore AMF0/AMF3 command message.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::get_peer_ip()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int fd = st_netfd_fileno(stfd);
|
||||
|
||||
// discovery client information
|
||||
sockaddr_in addr;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
|
||||
ret = ERROR_SOCKET_GET_PEER_NAME;
|
||||
srs_error("discovery client information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get peer name success.");
|
||||
|
||||
// ip v4 or v6
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
|
||||
ret = ERROR_SOCKET_GET_PEER_IP;
|
||||
srs_error("convert client information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
|
||||
|
||||
ip = new char[strlen(buf) + 1];
|
||||
strcpy(ip, buf);
|
||||
|
||||
srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (!msg) {
|
||||
srs_verbose("ignore all empty message.");
|
||||
return ret;
|
||||
}
|
||||
SrsAutoFree(SrsCommonMessage, msg, false);
|
||||
|
||||
if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) {
|
||||
srs_info("ignore all message except amf0/amf3 command.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
|
||||
srs_error("decode the amf0/amf3 command packet failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("decode the amf0/amf3 command packet success.");
|
||||
|
||||
SrsCloseStreamPacket* close = dynamic_cast<SrsCloseStreamPacket*>(msg->get_packet());
|
||||
if (close) {
|
||||
ret = ERROR_CONTROL_RTMP_CLOSE;
|
||||
srs_trace("system control message: rtmp close stream. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsPausePacket* pause = dynamic_cast<SrsPausePacket*>(msg->get_packet());
|
||||
if (!pause) {
|
||||
srs_info("ignore all amf0/amf3 command except pause.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = rtmp->on_play_client_pause(res->stream_id, pause->is_pause)) != ERROR_SUCCESS) {
|
||||
srs_error("rtmp process play client pause failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = consumer->on_play_client_pause(pause->is_pause)) != ERROR_SUCCESS) {
|
||||
srs_error("consumer process play client pause failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("process pause success, is_pause=%d, time=%d.", pause->is_pause, pause->time_ms);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsClient::on_connect()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
#ifdef SRS_HTTP
|
||||
// HTTP: on_connect
|
||||
SrsConfDirective* on_connect = config->get_vhost_on_connect(req->vhost);
|
||||
if (!on_connect) {
|
||||
srs_info("ignore the empty http callback: on_connect");
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_connect->args.size(); i++) {
|
||||
std::string url = on_connect->args.at(i);
|
||||
if ((ret = http_hooks->on_connect(url, connection_id, ip, req)) != ERROR_SUCCESS) {
|
||||
srs_error("hook client on_connect failed. url=%s, ret=%d", url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsClient::on_close()
|
||||
{
|
||||
#ifdef SRS_HTTP
|
||||
// whatever the ret code, notify the api hooks.
|
||||
// HTTP: on_close
|
||||
SrsConfDirective* on_close = config->get_vhost_on_close(req->vhost);
|
||||
if (!on_close) {
|
||||
srs_info("ignore the empty http callback: on_close");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_close->args.size(); i++) {
|
||||
std::string url = on_close->args.at(i);
|
||||
http_hooks->on_close(url, connection_id, ip, req);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int SrsClient::on_publish()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
#ifdef SRS_HTTP
|
||||
// HTTP: on_publish
|
||||
SrsConfDirective* on_publish = config->get_vhost_on_publish(req->vhost);
|
||||
if (!on_publish) {
|
||||
srs_info("ignore the empty http callback: on_publish");
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_publish->args.size(); i++) {
|
||||
std::string url = on_publish->args.at(i);
|
||||
if ((ret = http_hooks->on_publish(url, connection_id, ip, req)) != ERROR_SUCCESS) {
|
||||
srs_error("hook client on_publish failed. url=%s, ret=%d", url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsClient::on_unpublish()
|
||||
{
|
||||
#ifdef SRS_HTTP
|
||||
// whatever the ret code, notify the api hooks.
|
||||
// HTTP: on_unpublish
|
||||
SrsConfDirective* on_unpublish = config->get_vhost_on_unpublish(req->vhost);
|
||||
if (!on_unpublish) {
|
||||
srs_info("ignore the empty http callback: on_unpublish");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_unpublish->args.size(); i++) {
|
||||
std::string url = on_unpublish->args.at(i);
|
||||
http_hooks->on_unpublish(url, connection_id, ip, req);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int SrsClient::on_play()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
#ifdef SRS_HTTP
|
||||
// HTTP: on_play
|
||||
SrsConfDirective* on_play = config->get_vhost_on_play(req->vhost);
|
||||
if (!on_play) {
|
||||
srs_info("ignore the empty http callback: on_play");
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_play->args.size(); i++) {
|
||||
std::string url = on_play->args.at(i);
|
||||
if ((ret = http_hooks->on_play(url, connection_id, ip, req)) != ERROR_SUCCESS) {
|
||||
srs_error("hook client on_play failed. url=%s, ret=%d", url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsClient::on_stop()
|
||||
{
|
||||
#ifdef SRS_HTTP
|
||||
// whatever the ret code, notify the api hooks.
|
||||
// HTTP: on_stop
|
||||
SrsConfDirective* on_stop = config->get_vhost_on_stop(req->vhost);
|
||||
if (!on_stop) {
|
||||
srs_info("ignore the empty http callback: on_stop");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)on_stop->args.size(); i++) {
|
||||
std::string url = on_stop->args.at(i);
|
||||
http_hooks->on_stop(url, connection_id, ip, req);
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_CLIENT_HPP
|
||||
#define SRS_CORE_CLIENT_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_client.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <srs_core_conn.hpp>
|
||||
#include <srs_core_reload.hpp>
|
||||
|
||||
class SrsRtmp;
|
||||
class SrsRequest;
|
||||
class SrsResponse;
|
||||
class SrsSource;
|
||||
class SrsRefer;
|
||||
class SrsConsumer;
|
||||
class SrsCommonMessage;
|
||||
#ifdef SRS_HTTP
|
||||
class SrsHttpHooks;
|
||||
#endif
|
||||
class SrsBandwidth;
|
||||
|
||||
/**
|
||||
* the client provides the main logic control for RTMP clients.
|
||||
*/
|
||||
class SrsClient : public SrsConnection, public ISrsReloadHandler
|
||||
{
|
||||
private:
|
||||
char* ip;
|
||||
SrsRequest* req;
|
||||
SrsResponse* res;
|
||||
SrsRtmp* rtmp;
|
||||
SrsRefer* refer;
|
||||
#ifdef SRS_HTTP
|
||||
SrsHttpHooks* http_hooks;
|
||||
#endif
|
||||
SrsBandwidth* bandwidth;
|
||||
public:
|
||||
SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);
|
||||
virtual ~SrsClient();
|
||||
protected:
|
||||
virtual int do_cycle();
|
||||
// interface ISrsReloadHandler
|
||||
public:
|
||||
virtual int on_reload_vhost_removed(std::string vhost);
|
||||
private:
|
||||
// when valid and connected to vhost/app, service the client.
|
||||
virtual int service_cycle();
|
||||
// stream(play/publish) service cycle, identify client first.
|
||||
virtual int stream_service_cycle();
|
||||
virtual int check_vhost();
|
||||
virtual int playing(SrsSource* source);
|
||||
virtual int publish(SrsSource* source, bool is_fmle);
|
||||
virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg, bool is_fmle);
|
||||
virtual int get_peer_ip();
|
||||
virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
|
||||
private:
|
||||
virtual int on_connect();
|
||||
virtual void on_close();
|
||||
virtual int on_publish();
|
||||
virtual void on_unpublish();
|
||||
virtual int on_play();
|
||||
virtual void on_stop();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,531 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_codec.hpp>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_stream.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
|
||||
SrsCodecBuffer::SrsCodecBuffer()
|
||||
{
|
||||
size = 0;
|
||||
bytes = NULL;
|
||||
}
|
||||
|
||||
void SrsCodecBuffer::append(void* data, int len)
|
||||
{
|
||||
srs_assert(data);
|
||||
srs_assert(len > 0);
|
||||
|
||||
bytes = (char*)realloc(bytes, size + len);
|
||||
memcpy(bytes + size, data, len);
|
||||
size += len;
|
||||
}
|
||||
|
||||
void SrsCodecBuffer::free()
|
||||
{
|
||||
size = 0;
|
||||
srs_freepa(bytes);
|
||||
}
|
||||
|
||||
SrsCodecSample::SrsCodecSample()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
SrsCodecSample::~SrsCodecSample()
|
||||
{
|
||||
}
|
||||
|
||||
void SrsCodecSample::clear()
|
||||
{
|
||||
is_video = false;
|
||||
nb_buffers = 0;
|
||||
|
||||
cts = 0;
|
||||
frame_type = SrsCodecVideoAVCFrameReserved;
|
||||
avc_packet_type = SrsCodecVideoAVCTypeReserved;
|
||||
|
||||
sound_rate = SrsCodecAudioSampleRateReserved;
|
||||
sound_size = SrsCodecAudioSampleSizeReserved;
|
||||
sound_type = SrsCodecAudioSoundTypeReserved;
|
||||
aac_packet_type = SrsCodecAudioTypeReserved;
|
||||
}
|
||||
|
||||
int SrsCodecSample::add_sample(char* bytes, int size)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (nb_buffers >= SRS_MAX_CODEC_SAMPLE) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode samples error, "
|
||||
"exceed the max count: %d, ret=%d", SRS_MAX_CODEC_SAMPLE, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsCodecBuffer* buf = &buffers[nb_buffers++];
|
||||
buf->bytes = bytes;
|
||||
buf->size = size;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsCodec::SrsCodec()
|
||||
{
|
||||
width = 0;
|
||||
height = 0;
|
||||
duration = 0;
|
||||
NAL_unit_length = 0;
|
||||
frame_rate = 0;
|
||||
video_data_rate = 0;
|
||||
video_codec_id = 0;
|
||||
audio_data_rate = 0;
|
||||
audio_codec_id = 0;
|
||||
avc_profile = 0;
|
||||
avc_level = 0;
|
||||
aac_profile = 0;
|
||||
aac_sample_rate = 0;
|
||||
aac_channels = 0;
|
||||
avc_extra_size = 0;
|
||||
avc_extra_data = NULL;
|
||||
aac_extra_size = 0;
|
||||
aac_extra_data = NULL;
|
||||
sequenceParameterSetLength = 0;
|
||||
sequenceParameterSetNALUnit = NULL;
|
||||
pictureParameterSetLength = 0;
|
||||
pictureParameterSetNALUnit = NULL;
|
||||
|
||||
stream = new SrsStream();
|
||||
}
|
||||
|
||||
SrsCodec::~SrsCodec()
|
||||
{
|
||||
srs_freepa(avc_extra_data);
|
||||
srs_freepa(aac_extra_data);
|
||||
|
||||
srs_freep(stream);
|
||||
srs_freepa(sequenceParameterSetNALUnit);
|
||||
srs_freepa(pictureParameterSetNALUnit);
|
||||
}
|
||||
|
||||
int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
sample->is_video = false;
|
||||
|
||||
if (!data || size <= 0) {
|
||||
srs_trace("no audio present, hls ignore it.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// audio decode
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode audio sound_format failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int8_t sound_format = stream->read_1bytes();
|
||||
|
||||
int8_t sound_type = sound_format & 0x01;
|
||||
int8_t sound_size = (sound_format >> 1) & 0x01;
|
||||
int8_t sound_rate = (sound_format >> 2) & 0x03;
|
||||
sound_format = (sound_format >> 4) & 0x0f;
|
||||
|
||||
audio_codec_id = sound_format;
|
||||
sample->sound_type = (SrsCodecAudioSoundType)sound_type;
|
||||
sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;
|
||||
sample->sound_size = (SrsCodecAudioSampleSize)sound_size;
|
||||
|
||||
// reset the sample rate by sequence header
|
||||
static int aac_sample_rates[] = {
|
||||
96000, 88200, 64000, 48000,
|
||||
44100, 32000, 24000, 22050,
|
||||
16000, 12000, 11025, 8000,
|
||||
7350, 0, 0, 0
|
||||
};
|
||||
switch (aac_sample_rates[aac_sample_rate]) {
|
||||
case 11025:
|
||||
sample->sound_rate = SrsCodecAudioSampleRate11025;
|
||||
break;
|
||||
case 22050:
|
||||
sample->sound_rate = SrsCodecAudioSampleRate22050;
|
||||
break;
|
||||
case 44100:
|
||||
sample->sound_rate = SrsCodecAudioSampleRate44100;
|
||||
break;
|
||||
};
|
||||
|
||||
// only support aac
|
||||
if (audio_codec_id != SrsCodecAudioAAC) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls only support audio aac codec. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode audio aac_packet_type failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int8_t aac_packet_type = stream->read_1bytes();
|
||||
sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
|
||||
|
||||
if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
|
||||
// AudioSpecificConfig
|
||||
// 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
|
||||
aac_extra_size = stream->left();
|
||||
if (aac_extra_size > 0) {
|
||||
srs_freepa(aac_extra_data);
|
||||
aac_extra_data = new char[aac_extra_size];
|
||||
memcpy(aac_extra_data, stream->current(), aac_extra_size);
|
||||
}
|
||||
|
||||
// only need to decode the first 2bytes:
|
||||
// audioObjectType, aac_profile, 5bits.
|
||||
// samplingFrequencyIndex, aac_sample_rate, 4bits.
|
||||
// channelConfiguration, aac_channels, 4bits
|
||||
if (!stream->require(2)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode audio aac sequence header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
aac_profile = stream->read_1bytes();
|
||||
aac_sample_rate = stream->read_1bytes();
|
||||
|
||||
aac_channels = (aac_sample_rate >> 3) & 0x0f;
|
||||
aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
|
||||
aac_profile = (aac_profile >> 3) & 0x1f;
|
||||
|
||||
if (aac_profile == 0 || aac_profile == 0x1f) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode audio aac sequence header failed, "
|
||||
"adts object=%d invalid. ret=%d", aac_profile, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// aac_profile = audioObjectType - 1
|
||||
aac_profile--;
|
||||
|
||||
if (aac_profile > 3) {
|
||||
// Mark all extended profiles as LC
|
||||
// to make Android as happy as possible.
|
||||
// @see: ngx_rtmp_hls_parse_aac_header
|
||||
aac_profile = 1;
|
||||
}
|
||||
} else if (aac_packet_type == SrsCodecAudioTypeRawData) {
|
||||
// ensure the sequence header demuxed
|
||||
if (aac_extra_size <= 0 || !aac_extra_data) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode audio aac failed, sequence header not found. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Raw AAC frame data in UI8 []
|
||||
// 6.3 Raw Data, aac-iso-13818-7.pdf, page 28
|
||||
if ((ret = sample->add_sample(stream->current(), stream->left())) != ERROR_SUCCESS) {
|
||||
srs_error("hls add audio sample failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
// ignored.
|
||||
}
|
||||
|
||||
srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",
|
||||
sound_type, audio_codec_id, sound_size, sound_rate, sound_format, size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
sample->is_video = true;
|
||||
|
||||
if (!data || size <= 0) {
|
||||
srs_trace("no video present, hls ignore it.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// video decode
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video frame_type failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int8_t frame_type = stream->read_1bytes();
|
||||
int8_t codec_id = frame_type & 0x0f;
|
||||
frame_type = (frame_type >> 4) & 0x0f;
|
||||
|
||||
sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
|
||||
|
||||
// only support h.264/avc
|
||||
if (codec_id != SrsCodecVideoAVC) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls only support video h.264/avc codec. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
video_codec_id = codec_id;
|
||||
|
||||
if (!stream->require(4)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc_packet_type failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
int8_t avc_packet_type = stream->read_1bytes();
|
||||
int32_t composition_time = stream->read_3bytes();
|
||||
|
||||
// pts = dts + cts.
|
||||
sample->cts = composition_time;
|
||||
sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;
|
||||
|
||||
if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
|
||||
// AVCDecoderConfigurationRecord
|
||||
// 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
|
||||
avc_extra_size = stream->left();
|
||||
if (avc_extra_size > 0) {
|
||||
srs_freepa(avc_extra_data);
|
||||
avc_extra_data = new char[avc_extra_size];
|
||||
memcpy(avc_extra_data, stream->current(), avc_extra_size);
|
||||
}
|
||||
|
||||
if (!stream->require(6)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
//int8_t configurationVersion = stream->read_1bytes();
|
||||
//int8_t AVCProfileIndication = stream->read_1bytes();
|
||||
//int8_t profile_compatibility = stream->read_1bytes();
|
||||
//int8_t AVCLevelIndication = stream->read_1bytes();
|
||||
stream->skip(4);
|
||||
// parse the NALU size.
|
||||
int8_t lengthSizeMinusOne = stream->read_1bytes();
|
||||
lengthSizeMinusOne &= 0x03;
|
||||
NAL_unit_length = lengthSizeMinusOne;
|
||||
|
||||
// 1 sps
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
int8_t numOfSequenceParameterSets = stream->read_1bytes();
|
||||
numOfSequenceParameterSets &= 0x1f;
|
||||
if (numOfSequenceParameterSets != 1) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (!stream->require(2)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header sps size failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
sequenceParameterSetLength = stream->read_2bytes();
|
||||
if (!stream->require(sequenceParameterSetLength)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header sps data failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (sequenceParameterSetLength > 0) {
|
||||
srs_freepa(sequenceParameterSetNALUnit);
|
||||
sequenceParameterSetNALUnit = new char[sequenceParameterSetLength];
|
||||
memcpy(sequenceParameterSetNALUnit, stream->current(), sequenceParameterSetLength);
|
||||
stream->skip(sequenceParameterSetLength);
|
||||
}
|
||||
// 1 pps
|
||||
if (!stream->require(1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
int8_t numOfPictureParameterSets = stream->read_1bytes();
|
||||
numOfPictureParameterSets &= 0x1f;
|
||||
if (numOfPictureParameterSets != 1) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (!stream->require(2)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header pps size failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
pictureParameterSetLength = stream->read_2bytes();
|
||||
if (!stream->require(pictureParameterSetLength)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc sequenc header pps data failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (pictureParameterSetLength > 0) {
|
||||
srs_freepa(pictureParameterSetNALUnit);
|
||||
pictureParameterSetNALUnit = new char[pictureParameterSetLength];
|
||||
memcpy(pictureParameterSetNALUnit, stream->current(), pictureParameterSetLength);
|
||||
stream->skip(pictureParameterSetLength);
|
||||
}
|
||||
} else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){
|
||||
// ensure the sequence header demuxed
|
||||
if (avc_extra_size <= 0 || !avc_extra_data) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc failed, sequence header not found. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// One or more NALUs (Full frames are required)
|
||||
// 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20
|
||||
int PictureLength = stream->left();
|
||||
for (int i = 0; i < PictureLength;) {
|
||||
if (!stream->require(NAL_unit_length + 1)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc NALU size failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
int32_t NALUnitLength = 0;
|
||||
if (NAL_unit_length == 3) {
|
||||
NALUnitLength = stream->read_4bytes();
|
||||
} else if (NALUnitLength == 2) {
|
||||
NALUnitLength = stream->read_3bytes();
|
||||
} else if (NALUnitLength == 1) {
|
||||
NALUnitLength = stream->read_2bytes();
|
||||
} else {
|
||||
NALUnitLength = stream->read_1bytes();
|
||||
}
|
||||
// NALUnit
|
||||
if (!stream->require(NALUnitLength)) {
|
||||
ret = ERROR_HLS_DECODE_ERROR;
|
||||
srs_error("hls decode video avc NALU data failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
// 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
|
||||
if ((ret = sample->add_sample(stream->current(), NALUnitLength)) != ERROR_SUCCESS) {
|
||||
srs_error("hls add video sample failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
stream->skip(NALUnitLength);
|
||||
|
||||
i += NAL_unit_length + 1 + NALUnitLength;
|
||||
}
|
||||
} else {
|
||||
// ignored.
|
||||
}
|
||||
|
||||
srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",
|
||||
frame_type, video_codec_id, avc_packet_type, composition_time, size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SrsCodec::video_is_keyframe(int8_t* data, int size)
|
||||
{
|
||||
// 2bytes required.
|
||||
if (size < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char frame_type = *(char*)data;
|
||||
frame_type = (frame_type >> 4) & 0x0F;
|
||||
|
||||
return frame_type == SrsCodecVideoAVCFrameKeyFrame;
|
||||
}
|
||||
|
||||
bool SrsCodec::video_is_sequence_header(int8_t* data, int size)
|
||||
{
|
||||
// sequence header only for h264
|
||||
if (!video_is_h264(data, size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2bytes required.
|
||||
if (size < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char frame_type = *(char*)data;
|
||||
frame_type = (frame_type >> 4) & 0x0F;
|
||||
|
||||
char avc_packet_type = *(char*)(data + 1);
|
||||
|
||||
return frame_type == SrsCodecVideoAVCFrameKeyFrame
|
||||
&& avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader;
|
||||
}
|
||||
|
||||
bool SrsCodec::audio_is_sequence_header(int8_t* data, int size)
|
||||
{
|
||||
// sequence header only for aac
|
||||
if (!audio_is_aac(data, size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2bytes required.
|
||||
if (size < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char aac_packet_type = *(char*)(data + 1);
|
||||
|
||||
return aac_packet_type == SrsCodecAudioTypeSequenceHeader;
|
||||
}
|
||||
|
||||
bool SrsCodec::video_is_h264(int8_t* data, int size)
|
||||
{
|
||||
// 1bytes required.
|
||||
if (size < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char codec_id = *(char*)data;
|
||||
codec_id = codec_id & 0x0F;
|
||||
|
||||
return codec_id == SrsCodecVideoAVC;
|
||||
}
|
||||
|
||||
bool SrsCodec::audio_is_aac(int8_t* data, int size)
|
||||
{
|
||||
// 1bytes required.
|
||||
if (size < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char sound_format = *(char*)data;
|
||||
sound_format = (sound_format >> 4) & 0x0F;
|
||||
|
||||
return sound_format == SrsCodecAudioAAC;
|
||||
}
|
|
@ -1,312 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_CODEC_HPP
|
||||
#define SRS_CORE_CODEC_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_codec.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#define SRS_MAX_CODEC_SAMPLE 128
|
||||
|
||||
class SrsStream;
|
||||
|
||||
// E.4.3.1 VIDEODATA
|
||||
// CodecID UB [4]
|
||||
// Codec Identifier. The following values are defined:
|
||||
// 2 = Sorenson H.263
|
||||
// 3 = Screen video
|
||||
// 4 = On2 VP6
|
||||
// 5 = On2 VP6 with alpha channel
|
||||
// 6 = Screen video version 2
|
||||
// 7 = AVC
|
||||
enum SrsCodecVideo
|
||||
{
|
||||
SrsCodecVideoReserved = 0,
|
||||
|
||||
SrsCodecVideoSorensonH263 = 2,
|
||||
SrsCodecVideoScreenVideo = 3,
|
||||
SrsCodecVideoOn2VP6 = 4,
|
||||
SrsCodecVideoOn2VP6WithAlphaChannel = 5,
|
||||
SrsCodecVideoScreenVideoVersion2 = 6,
|
||||
SrsCodecVideoAVC = 7,
|
||||
};
|
||||
|
||||
// E.4.3.1 VIDEODATA
|
||||
// Frame Type UB [4]
|
||||
// Type of video frame. The following values are defined:
|
||||
// 1 = key frame (for AVC, a seekable frame)
|
||||
// 2 = inter frame (for AVC, a non-seekable frame)
|
||||
// 3 = disposable inter frame (H.263 only)
|
||||
// 4 = generated key frame (reserved for server use only)
|
||||
// 5 = video info/command frame
|
||||
enum SrsCodecVideoAVCFrame
|
||||
{
|
||||
SrsCodecVideoAVCFrameReserved = 0,
|
||||
|
||||
SrsCodecVideoAVCFrameKeyFrame = 1,
|
||||
SrsCodecVideoAVCFrameInterFrame = 2,
|
||||
SrsCodecVideoAVCFrameDisposableInterFrame = 3,
|
||||
SrsCodecVideoAVCFrameGeneratedKeyFrame = 4,
|
||||
SrsCodecVideoAVCFrameVideoInfoFrame = 5,
|
||||
};
|
||||
|
||||
// AVCPacketType IF CodecID == 7 UI8
|
||||
// The following values are defined:
|
||||
// 0 = AVC sequence header
|
||||
// 1 = AVC NALU
|
||||
// 2 = AVC end of sequence (lower level NALU sequence ender is
|
||||
// not required or supported)
|
||||
enum SrsCodecVideoAVCType
|
||||
{
|
||||
SrsCodecVideoAVCTypeReserved = -1,
|
||||
|
||||
SrsCodecVideoAVCTypeSequenceHeader = 0,
|
||||
SrsCodecVideoAVCTypeNALU = 1,
|
||||
SrsCodecVideoAVCTypeSequenceHeaderEOF = 2,
|
||||
};
|
||||
|
||||
// SoundFormat UB [4]
|
||||
// Format of SoundData. The following values are defined:
|
||||
// 0 = Linear PCM, platform endian
|
||||
// 1 = ADPCM
|
||||
// 2 = MP3
|
||||
// 3 = Linear PCM, little endian
|
||||
// 4 = Nellymoser 16 kHz mono
|
||||
// 5 = Nellymoser 8 kHz mono
|
||||
// 6 = Nellymoser
|
||||
// 7 = G.711 A-law logarithmic PCM
|
||||
// 8 = G.711 mu-law logarithmic PCM
|
||||
// 9 = reserved
|
||||
// 10 = AAC
|
||||
// 11 = Speex
|
||||
// 14 = MP3 8 kHz
|
||||
// 15 = Device-specific sound
|
||||
// Formats 7, 8, 14, and 15 are reserved.
|
||||
// AAC is supported in Flash Player 9,0,115,0 and higher.
|
||||
// Speex is supported in Flash Player 10 and higher.
|
||||
enum SrsCodecAudio
|
||||
{
|
||||
SrsCodecAudioLinearPCMPlatformEndian = 0,
|
||||
SrsCodecAudioADPCM = 1,
|
||||
SrsCodecAudioMP3 = 2,
|
||||
SrsCodecAudioLinearPCMLittleEndian = 3,
|
||||
SrsCodecAudioNellymoser16kHzMono = 4,
|
||||
SrsCodecAudioNellymoser8kHzMono = 5,
|
||||
SrsCodecAudioNellymoser = 6,
|
||||
SrsCodecAudioReservedG711AlawLogarithmicPCM = 7,
|
||||
SrsCodecAudioReservedG711MuLawLogarithmicPCM = 8,
|
||||
SrsCodecAudioReserved = 9,
|
||||
SrsCodecAudioAAC = 10,
|
||||
SrsCodecAudioSpeex = 11,
|
||||
SrsCodecAudioReservedMP3_8kHz = 14,
|
||||
SrsCodecAudioReservedDeviceSpecificSound = 15,
|
||||
};
|
||||
|
||||
// AACPacketType IF SoundFormat == 10 UI8
|
||||
// The following values are defined:
|
||||
// 0 = AAC sequence header
|
||||
// 1 = AAC raw
|
||||
enum SrsCodecAudioType
|
||||
{
|
||||
SrsCodecAudioTypeReserved = -1,
|
||||
SrsCodecAudioTypeSequenceHeader = 0,
|
||||
SrsCodecAudioTypeRawData = 1,
|
||||
};
|
||||
|
||||
// Sampling rate. The following values are defined:
|
||||
// 0 = 5.5 kHz = 5512 Hz
|
||||
// 1 = 11 kHz = 11025 Hz
|
||||
// 2 = 22 kHz = 22050 Hz
|
||||
// 3 = 44 kHz = 44100 Hz
|
||||
enum SrsCodecAudioSampleRate
|
||||
{
|
||||
SrsCodecAudioSampleRateReserved = -1,
|
||||
|
||||
SrsCodecAudioSampleRate5512 = 0,
|
||||
SrsCodecAudioSampleRate11025 = 1,
|
||||
SrsCodecAudioSampleRate22050 = 2,
|
||||
SrsCodecAudioSampleRate44100 = 3,
|
||||
};
|
||||
|
||||
// Size of each audio sample. This parameter only pertains to
|
||||
// uncompressed formats. Compressed formats always decode
|
||||
// to 16 bits internally.
|
||||
// 0 = 8-bit samples
|
||||
// 1 = 16-bit samples
|
||||
enum SrsCodecAudioSampleSize
|
||||
{
|
||||
SrsCodecAudioSampleSizeReserved = -1,
|
||||
|
||||
SrsCodecAudioSampleSize8bit = 0,
|
||||
SrsCodecAudioSampleSize16bit = 1,
|
||||
};
|
||||
|
||||
// Mono or stereo sound
|
||||
// 0 = Mono sound
|
||||
// 1 = Stereo sound
|
||||
enum SrsCodecAudioSoundType
|
||||
{
|
||||
SrsCodecAudioSoundTypeReserved = -1,
|
||||
|
||||
SrsCodecAudioSoundTypeMono = 0,
|
||||
SrsCodecAudioSoundTypeStereo = 1,
|
||||
};
|
||||
|
||||
/**
|
||||
* buffer indicates the position and size.
|
||||
*/
|
||||
struct SrsCodecBuffer
|
||||
{
|
||||
/**
|
||||
* @remark user must manage the bytes.
|
||||
*/
|
||||
int size;
|
||||
char* bytes;
|
||||
|
||||
SrsCodecBuffer();
|
||||
void append(void* data, int len);
|
||||
|
||||
/**
|
||||
* free the bytes,
|
||||
* user can invoke it to free the bytes,
|
||||
* the SrsCodecBuffer never free automatically.
|
||||
*/
|
||||
void free();
|
||||
};
|
||||
|
||||
/**
|
||||
* the samples in the flv audio/video packet.
|
||||
*/
|
||||
class SrsCodecSample
|
||||
{
|
||||
public:
|
||||
int nb_buffers;
|
||||
SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
|
||||
public:
|
||||
bool is_video;
|
||||
// video specified
|
||||
SrsCodecVideoAVCFrame frame_type;
|
||||
SrsCodecVideoAVCType avc_packet_type;
|
||||
// CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
|
||||
// cts = pts - dts, where dts = flvheader->timestamp.
|
||||
int32_t cts;
|
||||
// audio specified
|
||||
SrsCodecAudioSampleRate sound_rate;
|
||||
SrsCodecAudioSampleSize sound_size;
|
||||
SrsCodecAudioSoundType sound_type;
|
||||
SrsCodecAudioType aac_packet_type;
|
||||
public:
|
||||
SrsCodecSample();
|
||||
virtual ~SrsCodecSample();
|
||||
void clear();
|
||||
int add_sample(char* bytes, int size);
|
||||
};
|
||||
|
||||
/**
|
||||
* Annex E. The FLV File Format
|
||||
*/
|
||||
class SrsCodec
|
||||
{
|
||||
private:
|
||||
SrsStream* stream;
|
||||
public:
|
||||
/**
|
||||
* video specified
|
||||
*/
|
||||
// @see: SrsCodecVideo
|
||||
int video_codec_id;
|
||||
// profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
|
||||
u_int8_t avc_profile;
|
||||
// level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
|
||||
u_int8_t avc_level;
|
||||
int width;
|
||||
int height;
|
||||
int video_data_rate; // in bps
|
||||
int frame_rate;
|
||||
int duration;
|
||||
// lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
|
||||
int8_t NAL_unit_length;
|
||||
u_int16_t sequenceParameterSetLength;
|
||||
char* sequenceParameterSetNALUnit;
|
||||
u_int16_t pictureParameterSetLength;
|
||||
char* pictureParameterSetNALUnit;
|
||||
/**
|
||||
* audio specified
|
||||
*/
|
||||
// @see: SrsCodecAudioType
|
||||
int audio_codec_id;
|
||||
int audio_data_rate; // in bps
|
||||
// 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
|
||||
// audioObjectType, value defines in 7.1 Profiles, aac-iso-13818-7.pdf, page 40.
|
||||
u_int8_t aac_profile;
|
||||
// samplingFrequencyIndex
|
||||
u_int8_t aac_sample_rate;
|
||||
// channelConfiguration
|
||||
u_int8_t aac_channels;
|
||||
// the avc extra data, the AVC sequence header,
|
||||
// without the flv codec header,
|
||||
// @see: ffmpeg, AVCodecContext::extradata
|
||||
int avc_extra_size;
|
||||
char* avc_extra_data;
|
||||
// the aac extra data, the AAC sequence header,
|
||||
// without the flv codec header,
|
||||
// @see: ffmpeg, AVCodecContext::extradata
|
||||
int aac_extra_size;
|
||||
char* aac_extra_data;
|
||||
public:
|
||||
SrsCodec();
|
||||
virtual ~SrsCodec();
|
||||
// the following function used for hls to build the codec info.
|
||||
public:
|
||||
virtual int audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample);
|
||||
virtual int video_avc_demux(int8_t* data, int size, SrsCodecSample* sample);
|
||||
// the following function used to finger out the flv/rtmp packet detail.
|
||||
public:
|
||||
/**
|
||||
* only check the frame_type, not check the codec type.
|
||||
*/
|
||||
static bool video_is_keyframe(int8_t* data, int size);
|
||||
/**
|
||||
* check codec h264, keyframe, sequence header
|
||||
*/
|
||||
static bool video_is_sequence_header(int8_t* data, int size);
|
||||
/**
|
||||
* check codec aac, sequence header
|
||||
*/
|
||||
static bool audio_is_sequence_header(int8_t* data, int size);
|
||||
/**
|
||||
* check codec h264.
|
||||
*/
|
||||
static bool video_is_h264(int8_t* data, int size);
|
||||
private:
|
||||
/**
|
||||
* check codec aac.
|
||||
*/
|
||||
static bool audio_is_aac(int8_t* data, int size);
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,190 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_CONIFG_HPP
|
||||
#define SRS_CORE_CONIFG_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_config.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <srs_core_reload.hpp>
|
||||
|
||||
// default vhost for rtmp
|
||||
#define RTMP_VHOST_DEFAULT "__defaultVhost__"
|
||||
|
||||
#define SRS_LOCALHOST "127.0.0.1"
|
||||
#define RTMP_DEFAULT_PORT 1935
|
||||
#define RTMP_DEFAULT_PORTS "1935"
|
||||
|
||||
#define SRS_CONF_DEFAULT_HLS_PATH "./objs/nginx/html"
|
||||
#define SRS_CONF_DEFAULT_HLS_FRAGMENT 10
|
||||
#define SRS_CONF_DEFAULT_HLS_WINDOW 60
|
||||
// in ms, for HLS aac sync time.
|
||||
#define SRS_CONF_DEFAULT_AAC_SYNC 100
|
||||
// in ms, for HLS aac flush the audio
|
||||
#define SRS_CONF_DEFAULT_AAC_DELAY 300
|
||||
// in seconds, the live queue length.
|
||||
#define SRS_CONF_DEFAULT_QUEUE_LENGTH 30
|
||||
// in seconds, the paused queue length.
|
||||
#define SRS_CONF_DEFAULT_PAUSED_LENGTH 10
|
||||
// the interval in seconds for bandwidth check
|
||||
#define SRS_CONF_DEFAULT_BANDWIDTH_INTERVAL 30
|
||||
// the interval in seconds for bandwidth check
|
||||
#define SRS_CONF_DEFAULT_BANDWIDTH_LIMIT_KBPS 1000
|
||||
|
||||
// the default chunk size for system.
|
||||
#define SRS_CONF_DEFAULT_CHUNK_SIZE 60000
|
||||
|
||||
#define SRS_STAGE_PLAY_USER_INTERVAL_MS 1300
|
||||
#define SRS_STAGE_PUBLISH_USER_INTERVAL_MS 1100
|
||||
#define SRS_STAGE_FORWARDER_INTERVAL_MS 2000
|
||||
#define SRS_STAGE_ENCODER_INTERVAL_MS 2000
|
||||
#define SRS_STAGE_HLS_INTERVAL_MS 2000
|
||||
|
||||
class SrsFileBuffer;
|
||||
|
||||
class SrsConfDirective
|
||||
{
|
||||
public:
|
||||
int conf_line;
|
||||
std::string name;
|
||||
std::vector<std::string> args;
|
||||
std::vector<SrsConfDirective*> directives;
|
||||
public:
|
||||
SrsConfDirective();
|
||||
virtual ~SrsConfDirective();
|
||||
std::string arg0();
|
||||
std::string arg1();
|
||||
std::string arg2();
|
||||
SrsConfDirective* at(int index);
|
||||
SrsConfDirective* get(std::string _name);
|
||||
SrsConfDirective* get(std::string _name, std::string _arg0);
|
||||
public:
|
||||
virtual int parse(const char* filename);
|
||||
public:
|
||||
enum SrsDirectiveType{parse_file, parse_block};
|
||||
virtual int parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type);
|
||||
virtual int read_token(SrsFileBuffer* buffer, std::vector<std::string>& args);
|
||||
};
|
||||
|
||||
/**
|
||||
* the config parser.
|
||||
* for the config supports reload, so never keep the reference cross st-thread,
|
||||
* that is, never save the SrsConfDirective* get by any api of config,
|
||||
* for it maybe free in the reload st-thread cycle.
|
||||
* you can keep it before st-thread switch, or simply never keep it.
|
||||
*/
|
||||
class SrsConfig
|
||||
{
|
||||
private:
|
||||
bool show_help;
|
||||
bool show_version;
|
||||
std::string config_file;
|
||||
SrsConfDirective* root;
|
||||
std::vector<ISrsReloadHandler*> subscribes;
|
||||
public:
|
||||
SrsConfig();
|
||||
virtual ~SrsConfig();
|
||||
public:
|
||||
virtual int reload();
|
||||
virtual void subscribe(ISrsReloadHandler* handler);
|
||||
virtual void unsubscribe(ISrsReloadHandler* handler);
|
||||
public:
|
||||
virtual int parse_options(int argc, char** argv);
|
||||
private:
|
||||
virtual int parse_file(const char* filename);
|
||||
virtual int parse_argv(int& i, char** argv);
|
||||
virtual void print_help(char** argv);
|
||||
public:
|
||||
virtual SrsConfDirective* get_vhost(std::string vhost);
|
||||
virtual bool get_vhost_enabled(std::string vhost);
|
||||
virtual bool get_vhost_enabled(SrsConfDirective* vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_connect(std::string vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_close(std::string vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_publish(std::string vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_unpublish(std::string vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_play(std::string vhost);
|
||||
virtual SrsConfDirective* get_vhost_on_stop(std::string vhost);
|
||||
virtual SrsConfDirective* get_transcode(std::string vhost, std::string scope);
|
||||
virtual bool get_transcode_enabled(SrsConfDirective* transcode);
|
||||
virtual std::string get_transcode_ffmpeg(SrsConfDirective* transcode);
|
||||
virtual void get_transcode_engines(SrsConfDirective* transcode, std::vector<SrsConfDirective*>& engines);
|
||||
virtual bool get_engine_enabled(SrsConfDirective* engine);
|
||||
virtual std::string get_engine_vcodec(SrsConfDirective* engine);
|
||||
virtual int get_engine_vbitrate(SrsConfDirective* engine);
|
||||
virtual double get_engine_vfps(SrsConfDirective* engine);
|
||||
virtual int get_engine_vwidth(SrsConfDirective* engine);
|
||||
virtual int get_engine_vheight(SrsConfDirective* engine);
|
||||
virtual int get_engine_vthreads(SrsConfDirective* engine);
|
||||
virtual std::string get_engine_vprofile(SrsConfDirective* engine);
|
||||
virtual std::string get_engine_vpreset(SrsConfDirective* engine);
|
||||
virtual void get_engine_vparams(SrsConfDirective* engine, std::vector<std::string>& vparams);
|
||||
virtual void get_engine_vfilter(SrsConfDirective* engine, std::vector<std::string>& vfilter);
|
||||
virtual std::string get_engine_acodec(SrsConfDirective* engine);
|
||||
virtual int get_engine_abitrate(SrsConfDirective* engine);
|
||||
virtual int get_engine_asample_rate(SrsConfDirective* engine);
|
||||
virtual int get_engine_achannels(SrsConfDirective* engine);
|
||||
virtual void get_engine_aparams(SrsConfDirective* engine, std::vector<std::string>& aparams);
|
||||
virtual std::string get_engine_output(SrsConfDirective* engine);
|
||||
virtual std::string get_log_dir();
|
||||
virtual int get_max_connections();
|
||||
virtual bool get_gop_cache(std::string vhost);
|
||||
virtual double get_queue_length(std::string vhost);
|
||||
virtual SrsConfDirective* get_forward(std::string vhost);
|
||||
private:
|
||||
virtual SrsConfDirective* get_hls(std::string vhost);
|
||||
public:
|
||||
virtual bool get_hls_enabled(std::string vhost);
|
||||
virtual std::string get_hls_path(std::string vhost);
|
||||
virtual double get_hls_fragment(std::string vhost);
|
||||
virtual double get_hls_window(std::string vhost);
|
||||
virtual SrsConfDirective* get_refer(std::string vhost);
|
||||
virtual SrsConfDirective* get_refer_play(std::string vhost);
|
||||
virtual SrsConfDirective* get_refer_publish(std::string vhost);
|
||||
virtual SrsConfDirective* get_listen();
|
||||
virtual int get_chunk_size(const std::string& vhost);
|
||||
virtual int get_pithy_print_publish();
|
||||
virtual int get_pithy_print_forwarder();
|
||||
virtual int get_pithy_print_encoder();
|
||||
virtual int get_pithy_print_hls();
|
||||
virtual int get_pithy_print_play();
|
||||
virtual bool get_bw_check_enabled(const std::string& vhost);
|
||||
virtual std::string get_bw_check_key(const std::string& vhost);
|
||||
virtual int get_bw_check_interval_ms(const std::string& vhost);
|
||||
virtual int get_bw_check_limit_kbps(const std::string& vhost);
|
||||
};
|
||||
|
||||
/**
|
||||
* deep compare directive.
|
||||
*/
|
||||
bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b);
|
||||
|
||||
// global config
|
||||
extern SrsConfig* config;
|
||||
|
||||
#endif
|
|
@ -1,92 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_conn.hpp>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_server.hpp>
|
||||
|
||||
SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||
{
|
||||
server = srs_server;
|
||||
stfd = client_stfd;
|
||||
connection_id = 0;
|
||||
}
|
||||
|
||||
SrsConnection::~SrsConnection()
|
||||
{
|
||||
srs_close_stfd(stfd);
|
||||
}
|
||||
|
||||
int SrsConnection::start()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (st_thread_create(cycle_thread, this, 0, 0) == NULL) {
|
||||
ret = ERROR_ST_CREATE_CYCLE_THREAD;
|
||||
srs_error("st_thread_create conn cycle thread error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("create st conn cycle thread success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsConnection::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
log_context->generate_id();
|
||||
connection_id = log_context->get_id();
|
||||
|
||||
ret = do_cycle();
|
||||
|
||||
// if socket io error, set to closed.
|
||||
if (ret == ERROR_SOCKET_READ || ret == ERROR_SOCKET_READ_FULLY || ret == ERROR_SOCKET_WRITE) {
|
||||
ret = ERROR_SOCKET_CLOSED;
|
||||
}
|
||||
|
||||
// success.
|
||||
if (ret == ERROR_SUCCESS) {
|
||||
srs_trace("client process normally finished. ret=%d", ret);
|
||||
}
|
||||
|
||||
// client close peer.
|
||||
if (ret == ERROR_SOCKET_CLOSED) {
|
||||
srs_trace("client disconnect peer. ret=%d", ret);
|
||||
}
|
||||
|
||||
server->remove(this);
|
||||
}
|
||||
|
||||
void* SrsConnection::cycle_thread(void* arg)
|
||||
{
|
||||
SrsConnection* conn = (SrsConnection*)arg;
|
||||
srs_assert(conn != NULL);
|
||||
|
||||
conn->cycle();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_CONN_HPP
|
||||
#define SRS_CORE_CONN_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_conn.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
class SrsServer;
|
||||
class SrsConnection
|
||||
{
|
||||
protected:
|
||||
SrsServer* server;
|
||||
st_netfd_t stfd;
|
||||
int connection_id;
|
||||
public:
|
||||
SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd);
|
||||
virtual ~SrsConnection();
|
||||
public:
|
||||
virtual int start();
|
||||
protected:
|
||||
virtual int do_cycle() = 0;
|
||||
private:
|
||||
virtual void cycle();
|
||||
static void* cycle_thread(void* arg);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,693 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_encoder.hpp>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_pithy_print.hpp>
|
||||
#include <srs_core_protocol.hpp>
|
||||
|
||||
#ifdef SRS_FFMPEG
|
||||
|
||||
#define SRS_ENCODER_COPY "copy"
|
||||
#define SRS_ENCODER_VCODEC "libx264"
|
||||
#define SRS_ENCODER_ACODEC "libaacplus"
|
||||
|
||||
// for encoder to detect the dead loop
|
||||
static std::vector<std::string> _transcoded_url;
|
||||
|
||||
SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin)
|
||||
{
|
||||
started = false;
|
||||
pid = -1;
|
||||
ffmpeg = ffmpeg_bin;
|
||||
|
||||
vbitrate = 0;
|
||||
vfps = 0;
|
||||
vwidth = 0;
|
||||
vheight = 0;
|
||||
vthreads = 0;
|
||||
abitrate = 0;
|
||||
asample_rate = 0;
|
||||
achannels = 0;
|
||||
|
||||
log_fd = -1;
|
||||
}
|
||||
|
||||
SrsFFMPEG::~SrsFFMPEG()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
config->get_engine_vfilter(engine, vfilter);
|
||||
vcodec = config->get_engine_vcodec(engine);
|
||||
vbitrate = config->get_engine_vbitrate(engine);
|
||||
vfps = config->get_engine_vfps(engine);
|
||||
vwidth = config->get_engine_vwidth(engine);
|
||||
vheight = config->get_engine_vheight(engine);
|
||||
vthreads = config->get_engine_vthreads(engine);
|
||||
vprofile = config->get_engine_vprofile(engine);
|
||||
vpreset = config->get_engine_vpreset(engine);
|
||||
config->get_engine_vparams(engine, vparams);
|
||||
acodec = config->get_engine_acodec(engine);
|
||||
abitrate = config->get_engine_abitrate(engine);
|
||||
asample_rate = config->get_engine_asample_rate(engine);
|
||||
achannels = config->get_engine_achannels(engine);
|
||||
config->get_engine_aparams(engine, aparams);
|
||||
output = config->get_engine_output(engine);
|
||||
|
||||
// ensure the size is even.
|
||||
vwidth -= vwidth % 2;
|
||||
vheight -= vheight % 2;
|
||||
|
||||
// input stream, from local.
|
||||
// ie. rtmp://127.0.0.1:1935/live/livestream
|
||||
input = "rtmp://127.0.0.1:";
|
||||
input += req->port;
|
||||
input += "/";
|
||||
input += req->app;
|
||||
input += "?vhost=";
|
||||
input += req->vhost;
|
||||
input += "/";
|
||||
input += req->stream;
|
||||
|
||||
// output stream, to other/self server
|
||||
// ie. rtmp://127.0.0.1:1935/live/livestream_sd
|
||||
output = srs_replace(output, "[vhost]", req->vhost);
|
||||
output = srs_replace(output, "[port]", req->port);
|
||||
output = srs_replace(output, "[app]", req->app);
|
||||
output = srs_replace(output, "[stream]", req->stream);
|
||||
output = srs_replace(output, "[engine]", engine->arg0());
|
||||
|
||||
// write ffmpeg info to log file.
|
||||
log_file = config->get_log_dir();
|
||||
log_file += "/";
|
||||
log_file += "encoder";
|
||||
log_file += "-";
|
||||
log_file += req->vhost;
|
||||
log_file += "-";
|
||||
log_file += req->app;
|
||||
log_file += "-";
|
||||
log_file += req->stream;
|
||||
log_file += ".log";
|
||||
|
||||
// important: loop check, donot transcode again.
|
||||
std::vector<std::string>::iterator it;
|
||||
it = std::find(_transcoded_url.begin(), _transcoded_url.end(), input);
|
||||
if (it != _transcoded_url.end()) {
|
||||
ret = ERROR_ENCODER_LOOP;
|
||||
srs_info("detect a loop cycle, input=%s, output=%s, ignore it. ret=%d",
|
||||
input.c_str(), output.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
_transcoded_url.push_back(output);
|
||||
|
||||
if (vcodec != SRS_ENCODER_COPY) {
|
||||
if (vcodec != SRS_ENCODER_VCODEC) {
|
||||
ret = ERROR_ENCODER_VCODEC;
|
||||
srs_error("invalid vcodec, must be %s, actual %s, ret=%d",
|
||||
SRS_ENCODER_VCODEC, vcodec.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
if (vbitrate <= 0) {
|
||||
ret = ERROR_ENCODER_VBITRATE;
|
||||
srs_error("invalid vbitrate: %d, ret=%d", vbitrate, ret);
|
||||
return ret;
|
||||
}
|
||||
if (vfps <= 0) {
|
||||
ret = ERROR_ENCODER_VFPS;
|
||||
srs_error("invalid vfps: %.2f, ret=%d", vfps, ret);
|
||||
return ret;
|
||||
}
|
||||
if (vwidth <= 0) {
|
||||
ret = ERROR_ENCODER_VWIDTH;
|
||||
srs_error("invalid vwidth: %d, ret=%d", vwidth, ret);
|
||||
return ret;
|
||||
}
|
||||
if (vheight <= 0) {
|
||||
ret = ERROR_ENCODER_VHEIGHT;
|
||||
srs_error("invalid vheight: %d, ret=%d", vheight, ret);
|
||||
return ret;
|
||||
}
|
||||
if (vthreads < 0) {
|
||||
ret = ERROR_ENCODER_VTHREADS;
|
||||
srs_error("invalid vthreads: %d, ret=%d", vthreads, ret);
|
||||
return ret;
|
||||
}
|
||||
if (vprofile.empty()) {
|
||||
ret = ERROR_ENCODER_VPROFILE;
|
||||
srs_error("invalid vprofile: %s, ret=%d", vprofile.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
if (vpreset.empty()) {
|
||||
ret = ERROR_ENCODER_VPRESET;
|
||||
srs_error("invalid vpreset: %s, ret=%d", vpreset.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (acodec != SRS_ENCODER_COPY) {
|
||||
if (acodec != SRS_ENCODER_ACODEC) {
|
||||
ret = ERROR_ENCODER_ACODEC;
|
||||
srs_error("invalid acodec, must be %s, actual %s, ret=%d",
|
||||
SRS_ENCODER_ACODEC, acodec.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
if (abitrate <= 0) {
|
||||
ret = ERROR_ENCODER_ABITRATE;
|
||||
srs_error("invalid abitrate: %d, ret=%d",
|
||||
abitrate, ret);
|
||||
return ret;
|
||||
}
|
||||
if (asample_rate <= 0) {
|
||||
ret = ERROR_ENCODER_ASAMPLE_RATE;
|
||||
srs_error("invalid sample rate: %d, ret=%d",
|
||||
asample_rate, ret);
|
||||
return ret;
|
||||
}
|
||||
if (achannels != 1 && achannels != 2) {
|
||||
ret = ERROR_ENCODER_ACHANNELS;
|
||||
srs_error("invalid achannels, must be 1 or 2, actual %d, ret=%d",
|
||||
achannels, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (output.empty()) {
|
||||
ret = ERROR_ENCODER_OUTPUT;
|
||||
srs_error("invalid empty output, ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsFFMPEG::start()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (started) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// prepare exec params
|
||||
char tmp[256];
|
||||
std::vector<std::string> params;
|
||||
|
||||
// argv[0], set to ffmpeg bin.
|
||||
// The execv() and execvp() functions ....
|
||||
// The first argument, by convention, should point to
|
||||
// the filename associated with the file being executed.
|
||||
params.push_back(ffmpeg);
|
||||
|
||||
// input.
|
||||
params.push_back("-f");
|
||||
params.push_back("flv");
|
||||
|
||||
params.push_back("-i");
|
||||
params.push_back(input);
|
||||
|
||||
// build the filter
|
||||
if (!vfilter.empty()) {
|
||||
std::vector<std::string>::iterator it;
|
||||
for (it = vfilter.begin(); it != vfilter.end(); ++it) {
|
||||
std::string p = *it;
|
||||
if (!p.empty()) {
|
||||
params.push_back(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// video specified.
|
||||
params.push_back("-vcodec");
|
||||
params.push_back(vcodec);
|
||||
|
||||
// the codec params is disabled when copy
|
||||
if (vcodec != SRS_ENCODER_COPY) {
|
||||
params.push_back("-b:v");
|
||||
snprintf(tmp, sizeof(tmp), "%d", vbitrate * 1000);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-r");
|
||||
snprintf(tmp, sizeof(tmp), "%.2f", vfps);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-s");
|
||||
snprintf(tmp, sizeof(tmp), "%dx%d", vwidth, vheight);
|
||||
params.push_back(tmp);
|
||||
|
||||
// TODO: add aspect if needed.
|
||||
params.push_back("-aspect");
|
||||
snprintf(tmp, sizeof(tmp), "%d:%d", vwidth, vheight);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-threads");
|
||||
snprintf(tmp, sizeof(tmp), "%d", vthreads);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-profile:v");
|
||||
params.push_back(vprofile);
|
||||
|
||||
params.push_back("-preset");
|
||||
params.push_back(vpreset);
|
||||
|
||||
// vparams
|
||||
if (!vparams.empty()) {
|
||||
std::vector<std::string>::iterator it;
|
||||
for (it = vparams.begin(); it != vparams.end(); ++it) {
|
||||
std::string p = *it;
|
||||
if (!p.empty()) {
|
||||
params.push_back(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// audio specified.
|
||||
params.push_back("-acodec");
|
||||
params.push_back(acodec);
|
||||
|
||||
// the codec params is disabled when copy
|
||||
if (acodec != SRS_ENCODER_COPY) {
|
||||
params.push_back("-b:a");
|
||||
snprintf(tmp, sizeof(tmp), "%d", abitrate * 1000);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-ar");
|
||||
snprintf(tmp, sizeof(tmp), "%d", asample_rate);
|
||||
params.push_back(tmp);
|
||||
|
||||
params.push_back("-ac");
|
||||
snprintf(tmp, sizeof(tmp), "%d", achannels);
|
||||
params.push_back(tmp);
|
||||
|
||||
// aparams
|
||||
if (!aparams.empty()) {
|
||||
std::vector<std::string>::iterator it;
|
||||
for (it = aparams.begin(); it != aparams.end(); ++it) {
|
||||
std::string p = *it;
|
||||
if (!p.empty()) {
|
||||
params.push_back(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output
|
||||
params.push_back("-f");
|
||||
params.push_back("flv");
|
||||
|
||||
params.push_back("-y");
|
||||
params.push_back(output);
|
||||
|
||||
if (true) {
|
||||
int pparam_size = 8 * 1024;
|
||||
char* pparam = new char[pparam_size];
|
||||
char* p = pparam;
|
||||
char* last = pparam + pparam_size;
|
||||
for (int i = 0; i < (int)params.size(); i++) {
|
||||
std::string ffp = params[i];
|
||||
snprintf(p, last - p, "%s ", ffp.c_str());
|
||||
p += ffp.length() + 1;
|
||||
}
|
||||
srs_trace("start transcoder, log: %s, params: %s",
|
||||
log_file.c_str(), pparam);
|
||||
srs_freepa(pparam);
|
||||
}
|
||||
|
||||
// TODO: fork or vfork?
|
||||
if ((pid = fork()) < 0) {
|
||||
ret = ERROR_ENCODER_FORK;
|
||||
srs_error("vfork process failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// child process: ffmpeg encoder engine.
|
||||
if (pid == 0) {
|
||||
// redirect logs to file.
|
||||
int flags = O_CREAT|O_WRONLY|O_APPEND;
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
|
||||
if ((log_fd = ::open(log_file.c_str(), flags, mode)) < 0) {
|
||||
ret = ERROR_ENCODER_OPEN;
|
||||
srs_error("open encoder file %s failed. ret=%d", log_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
if (dup2(log_fd, STDOUT_FILENO) < 0) {
|
||||
ret = ERROR_ENCODER_DUP2;
|
||||
srs_error("dup2 encoder file failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (dup2(log_fd, STDERR_FILENO) < 0) {
|
||||
ret = ERROR_ENCODER_DUP2;
|
||||
srs_error("dup2 encoder file failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
// close other fds
|
||||
// TODO: do in right way.
|
||||
for (int i = 3; i < 1024; i++) {
|
||||
::close(i);
|
||||
}
|
||||
|
||||
// memory leak in child process, it's ok.
|
||||
char** charpv_params = new char*[params.size() + 1];
|
||||
for (int i = 0; i < (int)params.size(); i++) {
|
||||
std::string p = params[i];
|
||||
charpv_params[i] = (char*)p.c_str();
|
||||
}
|
||||
// EOF: NULL
|
||||
charpv_params[params.size()] = NULL;
|
||||
|
||||
// TODO: execv or execvp
|
||||
ret = execv(ffmpeg.c_str(), charpv_params);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "fork ffmpeg failed, errno=%d(%s)",
|
||||
errno, strerror(errno));
|
||||
}
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
// parent.
|
||||
if (pid > 0) {
|
||||
started = true;
|
||||
srs_trace("vfored ffmpeg encoder engine, pid=%d", pid);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsFFMPEG::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (!started) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
pid_t p = waitpid(pid, &status, WNOHANG);
|
||||
|
||||
if (p < 0) {
|
||||
ret = ERROR_SYSTEM_WAITPID;
|
||||
srs_error("transcode waitpid failed, pid=%d, ret=%d", pid, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (p == 0) {
|
||||
srs_info("transcode process pid=%d is running.", pid);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("transcode process pid=%d terminate, restart it.", pid);
|
||||
started = false;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsFFMPEG::stop()
|
||||
{
|
||||
if (log_fd > 0) {
|
||||
::close(log_fd);
|
||||
log_fd = -1;
|
||||
}
|
||||
|
||||
if (!started) {
|
||||
return;
|
||||
}
|
||||
|
||||
// kill the ffmpeg,
|
||||
// when rewind, upstream will stop publish(unpublish),
|
||||
// unpublish event will stop all ffmpeg encoders,
|
||||
// then publish will start all ffmpeg encoders.
|
||||
if (pid > 0) {
|
||||
if (kill(pid, SIGKILL) < 0) {
|
||||
srs_warn("kill the encoder failed, ignored. pid=%d", pid);
|
||||
}
|
||||
|
||||
// wait for the ffmpeg to quit.
|
||||
// ffmpeg will gracefully quit if signal is:
|
||||
// 1) SIGHUP 2) SIGINT 3) SIGQUIT
|
||||
// other signals, directly exit(123), for example:
|
||||
// 9) SIGKILL 15) SIGTERM
|
||||
int status = 0;
|
||||
if (waitpid(pid, &status, 0) < 0) {
|
||||
srs_warn("wait the encoder quit failed, ignored. pid=%d", pid);
|
||||
}
|
||||
|
||||
srs_trace("stop the encoder success. pid=%d", pid);
|
||||
pid = -1;
|
||||
}
|
||||
|
||||
std::vector<std::string>::iterator it;
|
||||
it = std::find(_transcoded_url.begin(), _transcoded_url.end(), output);
|
||||
if (it != _transcoded_url.end()) {
|
||||
_transcoded_url.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
SrsEncoder::SrsEncoder()
|
||||
{
|
||||
pthread = new SrsThread(this, SRS_ENCODER_SLEEP_US);
|
||||
pithy_print = new SrsPithyPrint(SRS_STAGE_ENCODER);
|
||||
}
|
||||
|
||||
SrsEncoder::~SrsEncoder()
|
||||
{
|
||||
on_unpublish();
|
||||
|
||||
srs_freep(pthread);
|
||||
}
|
||||
|
||||
int SrsEncoder::on_publish(SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
ret = parse_scope_engines(req);
|
||||
|
||||
// ignore the loop encoder
|
||||
if (ret == ERROR_ENCODER_LOOP) {
|
||||
clear_engines();
|
||||
ret = ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
// return for error or no engine.
|
||||
if (ret != ERROR_SUCCESS || ffmpegs.empty()) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// start thread to run all encoding engines.
|
||||
if ((ret = pthread->start()) != ERROR_SUCCESS) {
|
||||
srs_error("st_thread_create failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsEncoder::on_unpublish()
|
||||
{
|
||||
pthread->stop();
|
||||
clear_engines();
|
||||
}
|
||||
|
||||
int SrsEncoder::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
std::vector<SrsFFMPEG*>::iterator it;
|
||||
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
|
||||
SrsFFMPEG* ffmpeg = *it;
|
||||
|
||||
// start all ffmpegs.
|
||||
if ((ret = ffmpeg->start()) != ERROR_SUCCESS) {
|
||||
srs_error("ffmpeg start failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// check ffmpeg status.
|
||||
if ((ret = ffmpeg->cycle()) != ERROR_SUCCESS) {
|
||||
srs_error("ffmpeg cycle failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// pithy print
|
||||
encoder();
|
||||
pithy_print->elapse(SRS_ENCODER_SLEEP_US / 1000);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsEncoder::on_leave_loop()
|
||||
{
|
||||
// kill ffmpeg when finished and it alive
|
||||
std::vector<SrsFFMPEG*>::iterator it;
|
||||
|
||||
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
|
||||
SrsFFMPEG* ffmpeg = *it;
|
||||
ffmpeg->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void SrsEncoder::clear_engines()
|
||||
{
|
||||
std::vector<SrsFFMPEG*>::iterator it;
|
||||
|
||||
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
|
||||
SrsFFMPEG* ffmpeg = *it;
|
||||
srs_freep(ffmpeg);
|
||||
}
|
||||
|
||||
ffmpegs.clear();
|
||||
}
|
||||
|
||||
SrsFFMPEG* SrsEncoder::at(int index)
|
||||
{
|
||||
return ffmpegs[index];
|
||||
}
|
||||
|
||||
int SrsEncoder::parse_scope_engines(SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// parse all transcode engines.
|
||||
SrsConfDirective* conf = NULL;
|
||||
|
||||
// parse vhost scope engines
|
||||
std::string scope = "";
|
||||
if ((conf = config->get_transcode(req->vhost, scope)) != NULL) {
|
||||
if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
|
||||
srs_error("parse vhost scope=%s transcode engines failed. "
|
||||
"ret=%d", scope.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
// parse app scope engines
|
||||
scope = req->app;
|
||||
if ((conf = config->get_transcode(req->vhost, scope)) != NULL) {
|
||||
if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
|
||||
srs_error("parse app scope=%s transcode engines failed. "
|
||||
"ret=%d", scope.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
// parse stream scope engines
|
||||
scope += "/";
|
||||
scope += req->stream;
|
||||
if ((conf = config->get_transcode(req->vhost, scope)) != NULL) {
|
||||
if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
|
||||
srs_error("parse stream scope=%s transcode engines failed. "
|
||||
"ret=%d", scope.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(conf);
|
||||
|
||||
// enabled
|
||||
if (!config->get_transcode_enabled(conf)) {
|
||||
srs_trace("ignore the disabled transcode: %s",
|
||||
conf->arg0().c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ffmpeg
|
||||
std::string ffmpeg_bin = config->get_transcode_ffmpeg(conf);
|
||||
if (ffmpeg_bin.empty()) {
|
||||
srs_trace("ignore the empty ffmpeg transcode: %s",
|
||||
conf->arg0().c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
// get all engines.
|
||||
std::vector<SrsConfDirective*> engines;
|
||||
config->get_transcode_engines(conf, engines);
|
||||
if (engines.empty()) {
|
||||
srs_trace("ignore the empty transcode engine: %s",
|
||||
conf->arg0().c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
// create engine
|
||||
for (int i = 0; i < (int)engines.size(); i++) {
|
||||
SrsConfDirective* engine = engines[i];
|
||||
if (!config->get_engine_enabled(engine)) {
|
||||
srs_trace("ignore the diabled transcode engine: %s %s",
|
||||
conf->arg0().c_str(), engine->arg0().c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
|
||||
|
||||
if ((ret = ffmpeg->initialize(req, engine)) != ERROR_SUCCESS) {
|
||||
srs_freep(ffmpeg);
|
||||
|
||||
// if got a loop, donot transcode the whole stream.
|
||||
if (ret == ERROR_ENCODER_LOOP) {
|
||||
break;
|
||||
}
|
||||
|
||||
srs_error("invalid transcode engine: %s %s",
|
||||
conf->arg0().c_str(), engine->arg0().c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
ffmpegs.push_back(ffmpeg);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsEncoder::encoder()
|
||||
{
|
||||
// reportable
|
||||
if (pithy_print->can_print()) {
|
||||
// TODO: FIXME: show more info.
|
||||
srs_trace("-> time=%"PRId64", encoders=%d", pithy_print->get_age(), (int)ffmpegs.size());
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,115 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_ENCODER_HPP
|
||||
#define SRS_CORE_ENCODER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_encoder.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <srs_core_thread.hpp>
|
||||
|
||||
class SrsConfDirective;
|
||||
class SrsRequest;
|
||||
class SrsPithyPrint;
|
||||
|
||||
#ifdef SRS_FFMPEG
|
||||
|
||||
/**
|
||||
* a transcode engine: ffmepg,
|
||||
* used to transcode a stream to another.
|
||||
*/
|
||||
class SrsFFMPEG
|
||||
{
|
||||
private:
|
||||
bool started;
|
||||
pid_t pid;
|
||||
private:
|
||||
std::string log_file;
|
||||
int log_fd;
|
||||
private:
|
||||
std::string ffmpeg;
|
||||
std::vector<std::string> vfilter;
|
||||
std::string vcodec;
|
||||
int vbitrate;
|
||||
double vfps;
|
||||
int vwidth;
|
||||
int vheight;
|
||||
int vthreads;
|
||||
std::string vprofile;
|
||||
std::string vpreset;
|
||||
std::vector<std::string> vparams;
|
||||
std::string acodec;
|
||||
int abitrate;
|
||||
int asample_rate;
|
||||
int achannels;
|
||||
std::vector<std::string> aparams;
|
||||
std::string output;
|
||||
std::string input;
|
||||
public:
|
||||
SrsFFMPEG(std::string ffmpeg_bin);
|
||||
virtual ~SrsFFMPEG();
|
||||
public:
|
||||
virtual int initialize(SrsRequest* req, SrsConfDirective* engine);
|
||||
virtual int start();
|
||||
virtual int cycle();
|
||||
virtual void stop();
|
||||
};
|
||||
|
||||
/**
|
||||
* the encoder for a stream,
|
||||
* may use multiple ffmpegs to transcode the specified stream.
|
||||
*/
|
||||
class SrsEncoder : public ISrsThreadHandler
|
||||
{
|
||||
private:
|
||||
std::vector<SrsFFMPEG*> ffmpegs;
|
||||
private:
|
||||
SrsThread* pthread;
|
||||
SrsPithyPrint* pithy_print;
|
||||
public:
|
||||
SrsEncoder();
|
||||
virtual ~SrsEncoder();
|
||||
public:
|
||||
virtual int on_publish(SrsRequest* req);
|
||||
virtual void on_unpublish();
|
||||
// interface ISrsThreadHandler.
|
||||
public:
|
||||
virtual int cycle();
|
||||
virtual void on_leave_loop();
|
||||
private:
|
||||
virtual void clear_engines();
|
||||
virtual SrsFFMPEG* at(int index);
|
||||
virtual int parse_scope_engines(SrsRequest* req);
|
||||
virtual int parse_transcode(SrsRequest* req, SrsConfDirective* conf);
|
||||
virtual void encoder();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_error.hpp>
|
||||
|
||||
bool srs_is_system_control_error(int error_code)
|
||||
{
|
||||
return error_code == ERROR_CONTROL_RTMP_CLOSE;
|
||||
}
|
||||
|
|
@ -1,160 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_ERROR_HPP
|
||||
#define SRS_CORE_ERROR_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_error.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#define ERROR_SUCCESS 0
|
||||
|
||||
#define ERROR_ST_SET_EPOLL 100
|
||||
#define ERROR_ST_INITIALIZE 101
|
||||
#define ERROR_ST_OPEN_SOCKET 102
|
||||
#define ERROR_ST_CREATE_LISTEN_THREAD 103
|
||||
#define ERROR_ST_CREATE_CYCLE_THREAD 104
|
||||
#define ERROR_ST_CONNECT 105
|
||||
|
||||
#define ERROR_SOCKET_CREATE 200
|
||||
#define ERROR_SOCKET_SETREUSE 201
|
||||
#define ERROR_SOCKET_BIND 202
|
||||
#define ERROR_SOCKET_LISTEN 203
|
||||
#define ERROR_SOCKET_CLOSED 204
|
||||
#define ERROR_SOCKET_GET_PEER_NAME 205
|
||||
#define ERROR_SOCKET_GET_PEER_IP 206
|
||||
#define ERROR_SOCKET_READ 207
|
||||
#define ERROR_SOCKET_READ_FULLY 208
|
||||
#define ERROR_SOCKET_WRITE 209
|
||||
#define ERROR_SOCKET_WAIT 210
|
||||
#define ERROR_SOCKET_TIMEOUT 211
|
||||
#define ERROR_SOCKET_GET_LOCAL_IP 212
|
||||
|
||||
#define ERROR_RTMP_PLAIN_REQUIRED 300
|
||||
#define ERROR_RTMP_CHUNK_START 301
|
||||
#define ERROR_RTMP_MSG_INVLIAD_SIZE 302
|
||||
#define ERROR_RTMP_AMF0_DECODE 303
|
||||
#define ERROR_RTMP_AMF0_INVALID 304
|
||||
#define ERROR_RTMP_REQ_CONNECT 305
|
||||
#define ERROR_RTMP_REQ_TCURL 306
|
||||
#define ERROR_RTMP_MESSAGE_DECODE 307
|
||||
#define ERROR_RTMP_MESSAGE_ENCODE 308
|
||||
#define ERROR_RTMP_AMF0_ENCODE 309
|
||||
#define ERROR_RTMP_CHUNK_SIZE 310
|
||||
#define ERROR_RTMP_TRY_SIMPLE_HS 311
|
||||
#define ERROR_RTMP_CH_SCHEMA 312
|
||||
#define ERROR_RTMP_PACKET_SIZE 313
|
||||
#define ERROR_RTMP_VHOST_NOT_FOUND 314
|
||||
#define ERROR_RTMP_ACCESS_DENIED 315
|
||||
#define ERROR_RTMP_HANDSHAKE 316
|
||||
#define ERROR_RTMP_NO_REQUEST 317
|
||||
|
||||
#define ERROR_SYSTEM_STREAM_INIT 400
|
||||
#define ERROR_SYSTEM_PACKET_INVALID 401
|
||||
#define ERROR_SYSTEM_CLIENT_INVALID 402
|
||||
#define ERROR_SYSTEM_ASSERT_FAILED 403
|
||||
#define ERROR_SYSTEM_SIZE_NEGATIVE 404
|
||||
#define ERROR_SYSTEM_CONFIG_INVALID 405
|
||||
#define ERROR_SYSTEM_CONFIG_DIRECTIVE 406
|
||||
#define ERROR_SYSTEM_CONFIG_BLOCK_START 407
|
||||
#define ERROR_SYSTEM_CONFIG_BLOCK_END 408
|
||||
#define ERROR_SYSTEM_CONFIG_EOF 409
|
||||
#define ERROR_SYSTEM_STREAM_BUSY 410
|
||||
#define ERROR_SYSTEM_IP_INVALID 411
|
||||
#define ERROR_SYSTEM_FORWARD_LOOP 412
|
||||
#define ERROR_SYSTEM_WAITPID 413
|
||||
#define ERROR_SYSTEM_BANDWIDTH_KEY 414
|
||||
#define ERROR_SYSTEM_BANDWIDTH_DENIED 415
|
||||
|
||||
// see librtmp.
|
||||
// failed when open ssl create the dh
|
||||
#define ERROR_OpenSslCreateDH 500
|
||||
// failed when open ssl create the Private key.
|
||||
#define ERROR_OpenSslCreateP 501
|
||||
// when open ssl create G.
|
||||
#define ERROR_OpenSslCreateG 502
|
||||
// when open ssl parse P1024
|
||||
#define ERROR_OpenSslParseP1024 503
|
||||
// when open ssl set G
|
||||
#define ERROR_OpenSslSetG 504
|
||||
// when open ssl generate DHKeys
|
||||
#define ERROR_OpenSslGenerateDHKeys 505
|
||||
// when open ssl share key already computed.
|
||||
#define ERROR_OpenSslShareKeyComputed 506
|
||||
// when open ssl get shared key size.
|
||||
#define ERROR_OpenSslGetSharedKeySize 507
|
||||
// when open ssl get peer public key.
|
||||
#define ERROR_OpenSslGetPeerPublicKey 508
|
||||
// when open ssl compute shared key.
|
||||
#define ERROR_OpenSslComputeSharedKey 509
|
||||
// when open ssl is invalid DH state.
|
||||
#define ERROR_OpenSslInvalidDHState 510
|
||||
// when open ssl copy key
|
||||
#define ERROR_OpenSslCopyKey 511
|
||||
// when open ssl sha256 digest key invalid size.
|
||||
#define ERROR_OpenSslSha256DigestSize 512
|
||||
|
||||
#define ERROR_HLS_METADATA 600
|
||||
#define ERROR_HLS_DECODE_ERROR 601
|
||||
#define ERROR_HLS_CREATE_DIR 602
|
||||
#define ERROR_HLS_OPEN_FAILED 603
|
||||
#define ERROR_HLS_WRITE_FAILED 604
|
||||
#define ERROR_HLS_AAC_FRAME_LENGTH 605
|
||||
#define ERROR_HLS_AVC_SAMPLE_SIZE 606
|
||||
|
||||
#define ERROR_ENCODER_VCODEC 700
|
||||
#define ERROR_ENCODER_OUTPUT 701
|
||||
#define ERROR_ENCODER_ACHANNELS 702
|
||||
#define ERROR_ENCODER_ASAMPLE_RATE 703
|
||||
#define ERROR_ENCODER_ABITRATE 704
|
||||
#define ERROR_ENCODER_ACODEC 705
|
||||
#define ERROR_ENCODER_VPRESET 706
|
||||
#define ERROR_ENCODER_VPROFILE 707
|
||||
#define ERROR_ENCODER_VTHREADS 708
|
||||
#define ERROR_ENCODER_VHEIGHT 709
|
||||
#define ERROR_ENCODER_VWIDTH 710
|
||||
#define ERROR_ENCODER_VFPS 711
|
||||
#define ERROR_ENCODER_VBITRATE 712
|
||||
#define ERROR_ENCODER_FORK 713
|
||||
#define ERROR_ENCODER_LOOP 714
|
||||
#define ERROR_ENCODER_OPEN 715
|
||||
#define ERROR_ENCODER_DUP2 716
|
||||
|
||||
#define ERROR_HTTP_PARSE_URI 800
|
||||
#define ERROR_HTTP_DATA_INVLIAD 801
|
||||
#define ERROR_HTTP_PARSE_HEADER 802
|
||||
|
||||
// system control message,
|
||||
// not an error, but special control logic.
|
||||
// sys ctl: rtmp close stream, support replay.
|
||||
#define ERROR_CONTROL_RTMP_CLOSE 900
|
||||
|
||||
/**
|
||||
* whether the error code is an system control error.
|
||||
*/
|
||||
extern bool srs_is_system_control_error(int error_code);
|
||||
|
||||
#endif
|
|
@ -1,342 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_forward.hpp>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_protocol.hpp>
|
||||
#include <srs_core_pithy_print.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_source.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
|
||||
SrsForwarder::SrsForwarder(SrsSource* _source)
|
||||
{
|
||||
source = _source;
|
||||
|
||||
client = NULL;
|
||||
stfd = NULL;
|
||||
stream_id = 0;
|
||||
|
||||
pthread = new SrsThread(this, SRS_FORWARDER_SLEEP_US);
|
||||
queue = new SrsMessageQueue();
|
||||
jitter = new SrsRtmpJitter();
|
||||
}
|
||||
|
||||
SrsForwarder::~SrsForwarder()
|
||||
{
|
||||
on_unpublish();
|
||||
|
||||
srs_freep(pthread);
|
||||
srs_freep(queue);
|
||||
srs_freep(jitter);
|
||||
}
|
||||
|
||||
void SrsForwarder::set_queue_size(double queue_size)
|
||||
{
|
||||
queue->set_queue_size(queue_size);
|
||||
}
|
||||
|
||||
int SrsForwarder::on_publish(SrsRequest* req, std::string forward_server)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// forward app
|
||||
app = req->app;
|
||||
|
||||
stream_name = req->stream;
|
||||
server = forward_server;
|
||||
std::string s_port = RTMP_DEFAULT_PORTS;
|
||||
port = RTMP_DEFAULT_PORT;
|
||||
|
||||
size_t pos = forward_server.find(":");
|
||||
if (pos != std::string::npos) {
|
||||
s_port = forward_server.substr(pos + 1);
|
||||
server = forward_server.substr(0, pos);
|
||||
}
|
||||
// discovery vhost
|
||||
std::string vhost = req->vhost;
|
||||
srs_vhost_resolve(vhost, s_port);
|
||||
port = ::atoi(s_port.c_str());
|
||||
|
||||
// generate tcUrl
|
||||
tc_url = "rtmp://";
|
||||
tc_url += vhost;
|
||||
tc_url += "/";
|
||||
tc_url += req->app;
|
||||
|
||||
// dead loop check
|
||||
std::string source_ep = req->vhost;
|
||||
source_ep += ":";
|
||||
source_ep += req->port;
|
||||
|
||||
std::string dest_ep = vhost;
|
||||
dest_ep += ":";
|
||||
dest_ep += s_port;
|
||||
|
||||
if (source_ep == dest_ep) {
|
||||
ret = ERROR_SYSTEM_FORWARD_LOOP;
|
||||
srs_warn("farder loop detected. src=%s, dest=%s, ret=%d",
|
||||
source_ep.c_str(), dest_ep.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("start forward %s to %s, stream: %s/%s",
|
||||
source_ep.c_str(), dest_ep.c_str(), tc_url.c_str(),
|
||||
stream_name.c_str());
|
||||
|
||||
if ((ret = pthread->start()) != ERROR_SUCCESS) {
|
||||
srs_error("start srs thread failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsForwarder::on_unpublish()
|
||||
{
|
||||
pthread->stop();
|
||||
|
||||
close_underlayer_socket();
|
||||
|
||||
srs_freep(client);
|
||||
}
|
||||
|
||||
int SrsForwarder::on_meta_data(SrsSharedPtrMessage* metadata)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = jitter->correct(metadata, 0, 0)) != ERROR_SUCCESS) {
|
||||
srs_freep(metadata);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = queue->enqueue(metadata)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsForwarder::on_audio(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = jitter->correct(msg, 0, 0)) != ERROR_SUCCESS) {
|
||||
srs_freep(msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsForwarder::on_video(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = jitter->correct(msg, 0, 0)) != ERROR_SUCCESS) {
|
||||
srs_freep(msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsForwarder::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = connect_server()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
srs_assert(client);
|
||||
|
||||
client->set_recv_timeout(SRS_RECV_TIMEOUT_US);
|
||||
client->set_send_timeout(SRS_SEND_TIMEOUT_US);
|
||||
|
||||
if ((ret = client->handshake()) != ERROR_SUCCESS) {
|
||||
srs_error("handshake with server failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = client->connect_app(app, tc_url)) != ERROR_SUCCESS) {
|
||||
srs_error("connect with server failed, tcUrl=%s. ret=%d", tc_url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
if ((ret = client->create_stream(stream_id)) != ERROR_SUCCESS) {
|
||||
srs_error("connect with server failed, stream_id=%d. ret=%d", stream_id, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = client->publish(stream_name, stream_id)) != ERROR_SUCCESS) {
|
||||
srs_error("connect with server failed, stream_name=%s, stream_id=%d. ret=%d",
|
||||
stream_name.c_str(), stream_id, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = source->on_forwarder_start(this)) != ERROR_SUCCESS) {
|
||||
srs_error("callback the source to feed the sequence header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = forward()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsForwarder::close_underlayer_socket()
|
||||
{
|
||||
srs_close_stfd(stfd);
|
||||
}
|
||||
|
||||
int SrsForwarder::connect_server()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// reopen
|
||||
close_underlayer_socket();
|
||||
|
||||
// open socket.
|
||||
srs_trace("forward stream=%s, tcUrl=%s to server=%s, port=%d",
|
||||
stream_name.c_str(), tc_url.c_str(), server.c_str(), port);
|
||||
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(sock == -1){
|
||||
ret = ERROR_SOCKET_CREATE;
|
||||
srs_error("create socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_assert(!stfd);
|
||||
stfd = st_netfd_open_socket(sock);
|
||||
if(stfd == NULL){
|
||||
ret = ERROR_ST_OPEN_SOCKET;
|
||||
srs_error("st_netfd_open_socket failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_freep(client);
|
||||
client = new SrsRtmpClient(stfd);
|
||||
|
||||
// connect to server.
|
||||
std::string ip = srs_dns_resolve(server);
|
||||
if (ip.empty()) {
|
||||
ret = ERROR_SYSTEM_IP_INVALID;
|
||||
srs_error("dns resolve server error, ip empty. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = inet_addr(ip.c_str());
|
||||
|
||||
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){
|
||||
ret = ERROR_ST_CONNECT;
|
||||
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsForwarder::forward()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
client->set_recv_timeout(SRS_PULSE_TIMEOUT_US);
|
||||
|
||||
SrsPithyPrint pithy_print(SRS_STAGE_FORWARDER);
|
||||
|
||||
while (pthread->can_loop()) {
|
||||
// switch to other st-threads.
|
||||
st_usleep(0);
|
||||
|
||||
// read from client.
|
||||
if (true) {
|
||||
SrsCommonMessage* msg = NULL;
|
||||
ret = client->recv_message(&msg);
|
||||
|
||||
srs_verbose("play loop recv message. ret=%d", ret);
|
||||
if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {
|
||||
srs_error("recv server control message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// forward all messages.
|
||||
int count = 0;
|
||||
SrsSharedPtrMessage** msgs = NULL;
|
||||
if ((ret = queue->get_packets(0, msgs, count)) != ERROR_SUCCESS) {
|
||||
srs_error("get message to forward failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ignore when no messages.
|
||||
if (count <= 0) {
|
||||
srs_verbose("no packets to forward.");
|
||||
continue;
|
||||
}
|
||||
SrsAutoFree(SrsSharedPtrMessage*, msgs, true);
|
||||
|
||||
// pithy print
|
||||
pithy_print.elapse(SRS_PULSE_TIMEOUT_US / 1000);
|
||||
if (pithy_print.can_print()) {
|
||||
srs_trace("-> time=%"PRId64", msgs=%d, obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
|
||||
pithy_print.get_age(), count, client->get_send_bytes(), client->get_recv_bytes(), client->get_send_kbps(), client->get_recv_kbps());
|
||||
}
|
||||
|
||||
// all msgs to forward.
|
||||
for (int i = 0; i < count; i++) {
|
||||
SrsSharedPtrMessage* msg = msgs[i];
|
||||
|
||||
srs_assert(msg);
|
||||
msgs[i] = NULL;
|
||||
|
||||
if ((ret = client->send_message(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder send message to server failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_FORWARD_HPP
|
||||
#define SRS_CORE_FORWARD_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_forward.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <srs_core_thread.hpp>
|
||||
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsOnMetaDataPacket;
|
||||
class SrsMessageQueue;
|
||||
class SrsRtmpJitter;
|
||||
class SrsRtmpClient;
|
||||
class SrsRequest;
|
||||
class SrsSource;
|
||||
|
||||
/**
|
||||
* forward the stream to other servers.
|
||||
*/
|
||||
class SrsForwarder : public ISrsThreadHandler
|
||||
{
|
||||
private:
|
||||
std::string app;
|
||||
std::string tc_url;
|
||||
std::string stream_name;
|
||||
int stream_id;
|
||||
std::string server;
|
||||
int port;
|
||||
private:
|
||||
st_netfd_t stfd;
|
||||
SrsThread* pthread;
|
||||
private:
|
||||
SrsSource* source;
|
||||
SrsRtmpClient* client;
|
||||
SrsRtmpJitter* jitter;
|
||||
SrsMessageQueue* queue;
|
||||
public:
|
||||
SrsForwarder(SrsSource* _source);
|
||||
virtual ~SrsForwarder();
|
||||
public:
|
||||
virtual void set_queue_size(double queue_size);
|
||||
public:
|
||||
virtual int on_publish(SrsRequest* req, std::string forward_server);
|
||||
virtual void on_unpublish();
|
||||
virtual int on_meta_data(SrsSharedPtrMessage* metadata);
|
||||
virtual int on_audio(SrsSharedPtrMessage* msg);
|
||||
virtual int on_video(SrsSharedPtrMessage* msg);
|
||||
// interface ISrsThreadHandler.
|
||||
public:
|
||||
virtual int cycle();
|
||||
private:
|
||||
virtual void close_underlayer_socket();
|
||||
virtual int connect_server();
|
||||
virtual int forward();
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_HANDSHKAE_HPP
|
||||
#define SRS_CORE_HANDSHKAE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_complex_handshake.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
class SrsSocket;
|
||||
class SrsComplexHandshake;
|
||||
|
||||
/**
|
||||
* try complex handshake, if failed, fallback to simple handshake.
|
||||
*/
|
||||
class SrsSimpleHandshake
|
||||
{
|
||||
public:
|
||||
SrsSimpleHandshake();
|
||||
virtual ~SrsSimpleHandshake();
|
||||
public:
|
||||
/**
|
||||
* simple handshake.
|
||||
* @param complex_hs, try complex handshake first,
|
||||
* if failed, rollback to simple handshake.
|
||||
*/
|
||||
virtual int handshake_with_client(SrsSocket& skt, SrsComplexHandshake& complex_hs);
|
||||
virtual int handshake_with_server(SrsSocket& skt, SrsComplexHandshake& complex_hs);
|
||||
};
|
||||
|
||||
/**
|
||||
* rtmp complex handshake,
|
||||
* @see also crtmp(crtmpserver) or librtmp,
|
||||
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
|
||||
*/
|
||||
class SrsComplexHandshake
|
||||
{
|
||||
public:
|
||||
SrsComplexHandshake();
|
||||
virtual ~SrsComplexHandshake();
|
||||
public:
|
||||
/**
|
||||
* complex hanshake.
|
||||
* @_c1, size of c1 must be 1536.
|
||||
* @remark, user must free the c1.
|
||||
* @return user must:
|
||||
* continue connect app if success,
|
||||
* try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS,
|
||||
* otherwise, disconnect
|
||||
*/
|
||||
virtual int handshake_with_client(SrsSocket& skt, char* _c1);
|
||||
virtual int handshake_with_server(SrsSocket& skt);
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,248 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_HLS_HPP
|
||||
#define SRS_CORE_HLS_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_hls.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#ifdef SRS_HLS
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsCodecSample;
|
||||
class SrsCodecBuffer;
|
||||
class SrsMpegtsFrame;
|
||||
class SrsAmf0Object;
|
||||
class SrsRtmpJitter;
|
||||
class SrsTSMuxer;
|
||||
class SrsCodec;
|
||||
class SrsRequest;
|
||||
class SrsPithyPrint;
|
||||
class SrsSource;
|
||||
|
||||
/**
|
||||
* jitter correct for audio,
|
||||
* the sample rate 44100/32000 will lost precise,
|
||||
* when mp4/ts(tbn=90000) covert to flv/rtmp(1000),
|
||||
* so the Hls on ipad or iphone will corrupt,
|
||||
* @see nginx-rtmp: est_pts
|
||||
*/
|
||||
class SrsHlsAacJitter
|
||||
{
|
||||
private:
|
||||
int64_t base_pts;
|
||||
int64_t nb_samples;
|
||||
int sync_ms;
|
||||
public:
|
||||
SrsHlsAacJitter();
|
||||
virtual ~SrsHlsAacJitter();
|
||||
/**
|
||||
* when buffer start, calc the "correct" pts for ts,
|
||||
* @param flv_pts, the flv pts calc from flv header timestamp,
|
||||
* @return the calc correct pts.
|
||||
*/
|
||||
virtual int64_t on_buffer_start(int64_t flv_pts, int sample_rate);
|
||||
/**
|
||||
* when buffer continue, muxer donot write to file,
|
||||
* the audio buffer continue grow and donot need a pts,
|
||||
* for the ts audio PES packet only has one pts at the first time.
|
||||
*/
|
||||
virtual void on_buffer_continue();
|
||||
};
|
||||
|
||||
//TODO: refine the ts muxer, do more jobs.
|
||||
class SrsTSMuxer
|
||||
{
|
||||
private:
|
||||
int fd;
|
||||
std::string path;
|
||||
public:
|
||||
SrsTSMuxer();
|
||||
virtual ~SrsTSMuxer();
|
||||
public:
|
||||
virtual int open(std::string _path);
|
||||
virtual int write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab);
|
||||
virtual int write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb);
|
||||
virtual void close();
|
||||
};
|
||||
|
||||
/**
|
||||
* 3.3.2. EXTINF
|
||||
* The EXTINF tag specifies the duration of a media segment.
|
||||
*/
|
||||
struct SrsM3u8Segment
|
||||
{
|
||||
// duration in seconds in m3u8.
|
||||
double duration;
|
||||
// sequence number in m3u8.
|
||||
int sequence_no;
|
||||
// ts uri in m3u8.
|
||||
std::string uri;
|
||||
// ts full file to write.
|
||||
std::string full_path;
|
||||
// the muxer to write ts.
|
||||
SrsTSMuxer* muxer;
|
||||
// current segment start dts for m3u8
|
||||
int64_t segment_start_dts;
|
||||
|
||||
SrsM3u8Segment();
|
||||
virtual ~SrsM3u8Segment();
|
||||
|
||||
/**
|
||||
* update the segment duration.
|
||||
*/
|
||||
virtual double update_duration(int64_t video_stream_dts);
|
||||
};
|
||||
|
||||
/**
|
||||
* muxer the m3u8 and ts files.
|
||||
*/
|
||||
class SrsM3u8Muxer
|
||||
{
|
||||
private:
|
||||
std::string app;
|
||||
std::string stream;
|
||||
private:
|
||||
std::string hls_path;
|
||||
int hls_fragment;
|
||||
int hls_window;
|
||||
private:
|
||||
int file_index;
|
||||
std::string m3u8;
|
||||
private:
|
||||
/**
|
||||
* m3u8 segments.
|
||||
*/
|
||||
std::vector<SrsM3u8Segment*> segments;
|
||||
/**
|
||||
* current writing segment.
|
||||
*/
|
||||
SrsM3u8Segment* current;
|
||||
// last known dts
|
||||
int64_t video_stream_dts;
|
||||
public:
|
||||
SrsM3u8Muxer();
|
||||
virtual ~SrsM3u8Muxer();
|
||||
public:
|
||||
virtual int update_config(std::string _app, std::string _stream, std::string path, int fragment, int window);
|
||||
virtual int segment_open();
|
||||
virtual int flush_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab);
|
||||
virtual int flush_video(SrsMpegtsFrame* af, SrsCodecBuffer* ab, SrsMpegtsFrame* vf, SrsCodecBuffer* vb);
|
||||
virtual int segment_close();
|
||||
private:
|
||||
virtual int refresh_m3u8();
|
||||
virtual int _refresh_m3u8(int& fd, std::string m3u8_file);
|
||||
virtual int create_dir();
|
||||
};
|
||||
|
||||
/**
|
||||
* ts need to cache some audio then flush
|
||||
*/
|
||||
class SrsTSCache
|
||||
{
|
||||
private:
|
||||
// current frame and buffer
|
||||
SrsMpegtsFrame* af;
|
||||
SrsCodecBuffer* ab;
|
||||
SrsMpegtsFrame* vf;
|
||||
SrsCodecBuffer* vb;
|
||||
private:
|
||||
// the audio cache buffer start pts, to flush audio if full.
|
||||
int64_t audio_buffer_start_pts;
|
||||
// time jitter for aac
|
||||
SrsHlsAacJitter* aac_jitter;
|
||||
public:
|
||||
SrsTSCache();
|
||||
virtual ~SrsTSCache();
|
||||
public:
|
||||
/**
|
||||
* write audio to cache, if need to flush, flush to muxer.
|
||||
*/
|
||||
virtual int write_audio(SrsCodec* codec, SrsM3u8Muxer* muxer, int64_t pts, SrsCodecSample* sample);
|
||||
/**
|
||||
* write video to muxer.
|
||||
*/
|
||||
virtual int write_video(SrsCodec* codec, SrsM3u8Muxer* muxer, int64_t dts, SrsCodecSample* sample);
|
||||
/**
|
||||
* flush audio in cache to muxer.
|
||||
*/
|
||||
virtual int flush_audio(SrsM3u8Muxer* muxer);
|
||||
private:
|
||||
virtual int cache_audio(SrsCodec* codec, SrsCodecSample* sample);
|
||||
virtual int cache_video(SrsCodec* codec, SrsCodecSample* sample);
|
||||
};
|
||||
|
||||
/**
|
||||
* write m3u8 hls.
|
||||
*/
|
||||
class SrsHls
|
||||
{
|
||||
private:
|
||||
SrsM3u8Muxer* muxer;
|
||||
SrsTSCache* ts_cache;
|
||||
private:
|
||||
bool hls_enabled;
|
||||
SrsSource* source;
|
||||
SrsCodec* codec;
|
||||
SrsCodecSample* sample;
|
||||
SrsRtmpJitter* jitter;
|
||||
SrsPithyPrint* pithy_print;
|
||||
public:
|
||||
SrsHls(SrsSource* _source);
|
||||
virtual ~SrsHls();
|
||||
public:
|
||||
/**
|
||||
* publish stream event, continue to write the m3u8,
|
||||
* for the muxer object not destroyed.
|
||||
*/
|
||||
virtual int on_publish(SrsRequest* req);
|
||||
/**
|
||||
* the unpublish event, only close the muxer, donot destroy the
|
||||
* muxer, for when we continue to publish, the m3u8 will continue.
|
||||
*/
|
||||
virtual void on_unpublish();
|
||||
/**
|
||||
* get some information from metadata, it's optinal.
|
||||
*/
|
||||
virtual int on_meta_data(SrsAmf0Object* metadata);
|
||||
/**
|
||||
* mux the audio packets to ts.
|
||||
*/
|
||||
virtual int on_audio(SrsSharedPtrMessage* audio);
|
||||
/**
|
||||
* mux the video packets to ts.
|
||||
*/
|
||||
virtual int on_video(SrsSharedPtrMessage* video);
|
||||
private:
|
||||
virtual void hls_mux();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,817 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_http.hpp>
|
||||
|
||||
#ifdef SRS_HTTP
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_socket.hpp>
|
||||
|
||||
#define SRS_DEFAULT_HTTP_PORT 80
|
||||
#define SRS_HTTP_RESPONSE_OK "0"
|
||||
|
||||
SrsHttpUri::SrsHttpUri()
|
||||
{
|
||||
port = SRS_DEFAULT_HTTP_PORT;
|
||||
}
|
||||
|
||||
SrsHttpUri::~SrsHttpUri()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsHttpUri::initialize(std::string _url)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
url = _url;
|
||||
const char* purl = url.c_str();
|
||||
|
||||
http_parser_url hp_u;
|
||||
if((ret = http_parser_parse_url(purl, url.length(), 0, &hp_u)) != 0){
|
||||
int code = ret;
|
||||
ret = ERROR_HTTP_PARSE_URI;
|
||||
|
||||
srs_error("parse url %s failed, code=%d, ret=%d", purl, code, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string field = get_uri_field(url, &hp_u, UF_SCHEMA);
|
||||
if(!field.empty()){
|
||||
schema = field;
|
||||
}
|
||||
|
||||
host = get_uri_field(url, &hp_u, UF_HOST);
|
||||
|
||||
field = get_uri_field(url, &hp_u, UF_PORT);
|
||||
if(!field.empty()){
|
||||
port = atoi(field.c_str());
|
||||
}
|
||||
|
||||
path = get_uri_field(url, &hp_u, UF_PATH);
|
||||
srs_info("parse url %s success", purl);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char* SrsHttpUri::get_url()
|
||||
{
|
||||
return url.c_str();
|
||||
}
|
||||
|
||||
const char* SrsHttpUri::get_schema()
|
||||
{
|
||||
return schema.c_str();
|
||||
}
|
||||
|
||||
const char* SrsHttpUri::get_host()
|
||||
{
|
||||
return host.c_str();
|
||||
}
|
||||
|
||||
int SrsHttpUri::get_port()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
const char* SrsHttpUri::get_path()
|
||||
{
|
||||
return path.c_str();
|
||||
}
|
||||
|
||||
std::string SrsHttpUri::get_uri_field(std::string uri, http_parser_url* hp_u, http_parser_url_fields field)
|
||||
{
|
||||
if((hp_u->field_set & (1 << field)) == 0){
|
||||
return "";
|
||||
}
|
||||
|
||||
srs_verbose("uri field matched, off=%d, len=%d, value=%.*s",
|
||||
hp_u->field_data[field].off,
|
||||
hp_u->field_data[field].len,
|
||||
hp_u->field_data[field].len,
|
||||
uri.c_str() + hp_u->field_data[field].off);
|
||||
|
||||
int offset = hp_u->field_data[field].off;
|
||||
int len = hp_u->field_data[field].len;
|
||||
|
||||
return uri.substr(offset, len);
|
||||
}
|
||||
|
||||
SrsHttpClient::SrsHttpClient()
|
||||
{
|
||||
connected = false;
|
||||
stfd = NULL;
|
||||
}
|
||||
|
||||
SrsHttpClient::~SrsHttpClient()
|
||||
{
|
||||
disconnect();
|
||||
}
|
||||
|
||||
int SrsHttpClient::post(SrsHttpUri* uri, std::string req, std::string& res)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = connect(uri)) != ERROR_SUCCESS) {
|
||||
srs_error("http connect server failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// send POST request to uri
|
||||
// POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s
|
||||
std::stringstream ss;
|
||||
ss << "POST " << uri->get_path() << " "
|
||||
<< "HTTP/1.1\r\n"
|
||||
<< "Host: " << uri->get_host() << "\r\n"
|
||||
<< "Connection: Keep-Alive" << "\r\n"
|
||||
<< "Content-Length: " << std::dec << req.length() << "\r\n"
|
||||
<< "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << "\r\n"
|
||||
<< "Content-Type: text/html" << "\r\n"
|
||||
<< "\r\n"
|
||||
<< req;
|
||||
|
||||
SrsSocket skt(stfd);
|
||||
|
||||
std::string data = ss.str();
|
||||
ssize_t nwrite;
|
||||
if ((ret = skt.write(data.c_str(), data.length(), &nwrite)) != ERROR_SUCCESS) {
|
||||
// disconnect when error.
|
||||
disconnect();
|
||||
|
||||
srs_error("write http post failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = parse_response(uri, &skt, &res)) != ERROR_SUCCESS) {
|
||||
srs_error("parse http post response failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("parse http post response success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsHttpClient::disconnect()
|
||||
{
|
||||
connected = false;
|
||||
|
||||
srs_close_stfd(stfd);
|
||||
}
|
||||
|
||||
int SrsHttpClient::connect(SrsHttpUri* uri)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (connected) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
disconnect();
|
||||
|
||||
std::string ip = srs_dns_resolve(uri->get_host());
|
||||
if (ip.empty()) {
|
||||
ret = ERROR_SYSTEM_IP_INVALID;
|
||||
srs_error("dns resolve server error, ip empty. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(sock == -1){
|
||||
ret = ERROR_SOCKET_CREATE;
|
||||
srs_error("create socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
stfd = st_netfd_open_socket(sock);
|
||||
if(stfd == NULL){
|
||||
ret = ERROR_ST_OPEN_SOCKET;
|
||||
srs_error("st_netfd_open_socket failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(uri->get_port());
|
||||
addr.sin_addr.s_addr = inet_addr(ip.c_str());
|
||||
|
||||
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){
|
||||
ret = ERROR_ST_CONNECT;
|
||||
srs_error("connect to server error. "
|
||||
"ip=%s, port=%d, ret=%d", ip.c_str(), uri->get_port(), ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("connect to server success. "
|
||||
"http url=%s, server=%s, ip=%s, port=%d",
|
||||
uri->get_url(), uri->get_host(), ip.c_str(), uri->get_port());
|
||||
|
||||
connected = true;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::parse_response(SrsHttpUri* uri, SrsSocket* skt, std::string* response)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int body_received = 0;
|
||||
if ((ret = parse_response_header(skt, response, body_received)) != ERROR_SUCCESS) {
|
||||
srs_error("parse response header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = parse_response_body(uri, skt, response, body_received)) != ERROR_SUCCESS) {
|
||||
srs_error("parse response body failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_info("url %s download, body size=%"PRId64, uri->get_url(), http_header.content_length);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::parse_response_header(SrsSocket* skt, std::string* response, int& body_received)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
http_parser_settings settings;
|
||||
|
||||
memset(&settings, 0, sizeof(settings));
|
||||
settings.on_headers_complete = on_headers_complete;
|
||||
|
||||
http_parser parser;
|
||||
http_parser_init(&parser, HTTP_RESPONSE);
|
||||
// callback object ptr.
|
||||
parser.data = (void*)this;
|
||||
|
||||
// reset response header.
|
||||
memset(&http_header, 0, sizeof(http_header));
|
||||
|
||||
// parser header.
|
||||
char buf[SRS_HTTP_HEADER_BUFFER];
|
||||
for (;;) {
|
||||
ssize_t nread;
|
||||
if ((ret = skt->read(buf, (size_t)sizeof(buf), &nread)) != ERROR_SUCCESS) {
|
||||
srs_error("read body from server failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t nparsed = http_parser_execute(&parser, &settings, buf, nread);
|
||||
srs_info("read_size=%d, nparsed=%d", (int)nread, (int)nparsed);
|
||||
|
||||
// check header size.
|
||||
if (http_header.nread != 0) {
|
||||
body_received = nread - nparsed;
|
||||
|
||||
srs_info("http header parsed, size=%d, content-length=%"PRId64", body-received=%d",
|
||||
http_header.nread, http_header.content_length, body_received);
|
||||
|
||||
if(response != NULL && body_received > 0){
|
||||
response->append(buf + nparsed, body_received);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (nparsed != nread) {
|
||||
ret = ERROR_HTTP_PARSE_HEADER;
|
||||
srs_error("parse response error, parsed(%d)!=read(%d), ret=%d", (int)nparsed, (int)nread, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::parse_response_body(SrsHttpUri* uri, SrsSocket* skt, std::string* response, int body_received)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(uri != NULL);
|
||||
|
||||
uint64_t body_left = http_header.content_length - body_received;
|
||||
|
||||
if (body_left <= 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (response != NULL) {
|
||||
char buf[SRS_HTTP_BODY_BUFFER];
|
||||
|
||||
return parse_response_body_data(
|
||||
uri, skt, response, (size_t)body_left,
|
||||
(const void*)buf, (size_t)SRS_HTTP_BODY_BUFFER
|
||||
);
|
||||
} else {
|
||||
// if ignore response, use shared fast memory.
|
||||
static char buf[SRS_HTTP_BODY_BUFFER];
|
||||
|
||||
return parse_response_body_data(
|
||||
uri, skt, response, (size_t)body_left,
|
||||
(const void*)buf, (size_t)SRS_HTTP_BODY_BUFFER
|
||||
);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::parse_response_body_data(SrsHttpUri* uri, SrsSocket* skt, std::string* response, size_t body_left, const void* buf, size_t size)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(uri != NULL);
|
||||
|
||||
while (body_left > 0) {
|
||||
ssize_t nread;
|
||||
int size_to_read = srs_min(size, body_left);
|
||||
if ((ret = skt->read(buf, size_to_read, &nread)) != ERROR_SUCCESS) {
|
||||
srs_error("read header from server failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (response != NULL && nread > 0) {
|
||||
response->append((char*)buf, nread);
|
||||
}
|
||||
|
||||
body_left -= nread;
|
||||
srs_info("read url(%s) content partial %"PRId64"/%"PRId64"",
|
||||
uri->get_url(), http_header.content_length - body_left, http_header.content_length);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsHttpClient::on_headers_complete(http_parser* parser)
|
||||
{
|
||||
SrsHttpClient* obj = (SrsHttpClient*)parser->data;
|
||||
obj->comple_header(parser);
|
||||
|
||||
// see http_parser.c:1570, return 1 to skip body.
|
||||
return 1;
|
||||
}
|
||||
|
||||
void SrsHttpClient::comple_header(http_parser* parser)
|
||||
{
|
||||
// save the parser status when header parse completed.
|
||||
memcpy(&http_header, parser, sizeof(http_header));
|
||||
}
|
||||
|
||||
SrsHttpHooks::SrsHttpHooks()
|
||||
{
|
||||
}
|
||||
|
||||
SrsHttpHooks::~SrsHttpHooks()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsHttpHooks::on_connect(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_error("http uri parse on_connect url failed. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_connect",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"pageUrl": "http://www.test.com/live.html"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_connect" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
<< ','
|
||||
// pageUrl
|
||||
<< '"' << "pageUrl" << '"' << ':'
|
||||
<< '"' << req->pageUrl << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_error("http post on_connect uri failed. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_error("http hook on_connect validate failed. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_connect success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsHttpHooks::on_close(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_warn("http uri parse on_close url failed, ignored. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_close",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"stream": "livestream"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_close" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_warn("http post on_close uri failed, ignored. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_warn("http hook on_close validate failed, ignored. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_close success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int SrsHttpHooks::on_publish(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_error("http uri parse on_publish url failed. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_publish",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"stream": "livestream"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_publish" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
<< ','
|
||||
// stream
|
||||
<< '"' << "stream" << '"' << ':'
|
||||
<< '"' << req->stream << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_error("http post on_publish uri failed. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_error("http hook on_publish validate failed. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_publish success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsHttpHooks::on_unpublish(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_warn("http uri parse on_unpublish url failed, ignored. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_unpublish",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"stream": "livestream"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_unpublish" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
<< ','
|
||||
// stream
|
||||
<< '"' << "stream" << '"' << ':'
|
||||
<< '"' << req->stream << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_warn("http post on_unpublish uri failed, ignored. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_warn("http hook on_unpublish validate failed, ignored. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_unpublish success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int SrsHttpHooks::on_play(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_error("http uri parse on_play url failed. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_play",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"stream": "livestream"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_play" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
<< ','
|
||||
// stream
|
||||
<< '"' << "stream" << '"' << ':'
|
||||
<< '"' << req->stream << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_error("http post on_play uri failed. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_error("http hook on_play validate failed. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_play success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsHttpHooks::on_stop(std::string url, int client_id, std::string ip, SrsRequest* req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsHttpUri uri;
|
||||
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
||||
srs_warn("http uri parse on_stop url failed, ignored. "
|
||||
"client_id=%d, url=%s, ret=%d", client_id, url.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"action": "on_stop",
|
||||
"client_id": 1985,
|
||||
"ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
|
||||
"stream": "livestream"
|
||||
}
|
||||
*/
|
||||
std::stringstream ss;
|
||||
ss << "{"
|
||||
// action
|
||||
<< '"' << "action" << '"' << ':'
|
||||
<< '"' << "on_stop" << '"'
|
||||
<< ','
|
||||
// client_id
|
||||
<< '"' << "client_id" << '"' << ':'
|
||||
<< std::dec << client_id
|
||||
<< ','
|
||||
// ip
|
||||
<< '"' << "ip" << '"' << ':'
|
||||
<< '"' << ip << '"'
|
||||
<< ','
|
||||
// vhost
|
||||
<< '"' << "vhost" << '"' << ':'
|
||||
<< '"' << req->vhost << '"'
|
||||
<< ','
|
||||
// app
|
||||
<< '"' << "app" << '"' << ':'
|
||||
<< '"' << req->app << '"'
|
||||
<< ','
|
||||
// stream
|
||||
<< '"' << "stream" << '"' << ':'
|
||||
<< '"' << req->stream << '"'
|
||||
//<< ','
|
||||
<< "}";
|
||||
std::string data = ss.str();
|
||||
std::string res;
|
||||
|
||||
SrsHttpClient http;
|
||||
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
|
||||
srs_warn("http post on_stop uri failed, ignored. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.empty() || res != SRS_HTTP_RESPONSE_OK) {
|
||||
ret = ERROR_HTTP_DATA_INVLIAD;
|
||||
srs_warn("http hook on_stop validate failed, ignored. "
|
||||
"client_id=%d, res=%s, ret=%d", client_id, res.c_str(), ret);
|
||||
return;
|
||||
}
|
||||
|
||||
srs_trace("http hook on_stop success. "
|
||||
"client_id=%d, url=%s, request=%s, response=%s, ret=%d",
|
||||
client_id, url.c_str(), data.c_str(), res.c_str(), ret);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,170 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_HTTP_HPP
|
||||
#define SRS_CORE_HTTP_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_http.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#ifdef SRS_HTTP
|
||||
|
||||
class SrsRequest;
|
||||
class SrsSocket;
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <http_parser.h>
|
||||
|
||||
#define SRS_HTTP_HEADER_BUFFER 1024
|
||||
#define SRS_HTTP_BODY_BUFFER 32 * 1024
|
||||
|
||||
/**
|
||||
* used to resolve the http uri.
|
||||
*/
|
||||
class SrsHttpUri
|
||||
{
|
||||
private:
|
||||
std::string url;
|
||||
std::string schema;
|
||||
std::string host;
|
||||
int port;
|
||||
std::string path;
|
||||
public:
|
||||
SrsHttpUri();
|
||||
virtual ~SrsHttpUri();
|
||||
public:
|
||||
/**
|
||||
* initialize the http uri.
|
||||
*/
|
||||
virtual int initialize(std::string _url);
|
||||
public:
|
||||
virtual const char* get_url();
|
||||
virtual const char* get_schema();
|
||||
virtual const char* get_host();
|
||||
virtual int get_port();
|
||||
virtual const char* get_path();
|
||||
private:
|
||||
/**
|
||||
* get the parsed url field.
|
||||
* @return return empty string if not set.
|
||||
*/
|
||||
virtual std::string get_uri_field(std::string uri, http_parser_url* hp_u, http_parser_url_fields field);
|
||||
};
|
||||
|
||||
/**
|
||||
* http client to GET/POST/PUT/DELETE uri
|
||||
*/
|
||||
class SrsHttpClient
|
||||
{
|
||||
private:
|
||||
bool connected;
|
||||
st_netfd_t stfd;
|
||||
private:
|
||||
http_parser http_header;
|
||||
public:
|
||||
SrsHttpClient();
|
||||
virtual ~SrsHttpClient();
|
||||
public:
|
||||
/**
|
||||
* to post data to the uri.
|
||||
* @param req the data post to uri.
|
||||
* @param res the response data from server.
|
||||
*/
|
||||
virtual int post(SrsHttpUri* uri, std::string req, std::string& res);
|
||||
private:
|
||||
virtual void disconnect();
|
||||
virtual int connect(SrsHttpUri* uri);
|
||||
private:
|
||||
virtual int parse_response(SrsHttpUri* uri, SrsSocket* skt, std::string* response);
|
||||
virtual int parse_response_header(SrsSocket* skt, std::string* response, int& body_received);
|
||||
virtual int parse_response_body(SrsHttpUri* uri, SrsSocket* skt, std::string* response, int body_received);
|
||||
virtual int parse_response_body_data(SrsHttpUri* uri, SrsSocket* skt, std::string* response, size_t body_left, const void* buf, size_t size);
|
||||
private:
|
||||
static int on_headers_complete(http_parser* parser);
|
||||
virtual void comple_header(http_parser* parser);
|
||||
};
|
||||
|
||||
/**
|
||||
* the http hooks, http callback api,
|
||||
* for some event, such as on_connect, call
|
||||
* a http api(hooks).
|
||||
*/
|
||||
class SrsHttpHooks
|
||||
{
|
||||
public:
|
||||
SrsHttpHooks();
|
||||
virtual ~SrsHttpHooks();
|
||||
public:
|
||||
/**
|
||||
* on_connect hook, when client connect to srs.
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to valid the client.
|
||||
* ignore if empty.
|
||||
* @return valid failed or connect to the url failed.
|
||||
*/
|
||||
virtual int on_connect(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
/**
|
||||
* on_close hook, when client disconnect to srs, where client is valid by on_connect.
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to process the event.
|
||||
* ignore if empty.
|
||||
*/
|
||||
virtual void on_close(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
/**
|
||||
* on_publish hook, when client(encoder) start to publish stream
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to valid the client.
|
||||
* ignore if empty.
|
||||
* @return valid failed or connect to the url failed.
|
||||
*/
|
||||
virtual int on_publish(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
/**
|
||||
* on_unpublish hook, when client(encoder) stop publish stream.
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to process the event.
|
||||
* ignore if empty.
|
||||
*/
|
||||
virtual void on_unpublish(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
/**
|
||||
* on_play hook, when client start to play stream.
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to valid the client.
|
||||
* ignore if empty.
|
||||
* @return valid failed or connect to the url failed.
|
||||
*/
|
||||
virtual int on_play(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
/**
|
||||
* on_stop hook, when client stop to play the stream.
|
||||
* @param client_id the id of client on server.
|
||||
* @param url the api server url, to process the event.
|
||||
* ignore if empty.
|
||||
*/
|
||||
virtual void on_stop(std::string url, int client_id, std::string ip, SrsRequest* req);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,124 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_log.hpp>
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
ILogContext::ILogContext()
|
||||
{
|
||||
}
|
||||
|
||||
ILogContext::~ILogContext()
|
||||
{
|
||||
}
|
||||
|
||||
class LogContext : public ILogContext
|
||||
{
|
||||
private:
|
||||
class DateTime
|
||||
{
|
||||
private:
|
||||
// %d-%02d-%02d %02d:%02d:%02d.%03d
|
||||
#define DATE_LEN 24
|
||||
char time_data[DATE_LEN];
|
||||
public:
|
||||
DateTime();
|
||||
virtual ~DateTime();
|
||||
public:
|
||||
virtual const char* format_time();
|
||||
};
|
||||
private:
|
||||
DateTime time;
|
||||
std::map<st_thread_t, int> cache;
|
||||
public:
|
||||
LogContext();
|
||||
virtual ~LogContext();
|
||||
public:
|
||||
virtual void generate_id();
|
||||
virtual int get_id();
|
||||
public:
|
||||
virtual const char* format_time();
|
||||
};
|
||||
|
||||
ILogContext* log_context = new LogContext();
|
||||
|
||||
LogContext::DateTime::DateTime()
|
||||
{
|
||||
memset(time_data, 0, DATE_LEN);
|
||||
}
|
||||
|
||||
LogContext::DateTime::~DateTime()
|
||||
{
|
||||
}
|
||||
|
||||
const char* LogContext::DateTime::format_time()
|
||||
{
|
||||
// clock time
|
||||
timeval tv;
|
||||
if (gettimeofday(&tv, NULL) == -1) {
|
||||
return "";
|
||||
}
|
||||
// to calendar time
|
||||
struct tm* tm;
|
||||
if ((tm = localtime(&tv.tv_sec)) == NULL) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// log header, the time/pid/level of log
|
||||
// reserved 1bytes for the new line.
|
||||
snprintf(time_data, DATE_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
|
||||
(int)(tv.tv_usec / 1000));
|
||||
|
||||
return time_data;
|
||||
}
|
||||
|
||||
LogContext::LogContext()
|
||||
{
|
||||
}
|
||||
|
||||
LogContext::~LogContext()
|
||||
{
|
||||
}
|
||||
|
||||
void LogContext::generate_id()
|
||||
{
|
||||
static int id = 1;
|
||||
cache[st_thread_self()] = id++;
|
||||
}
|
||||
|
||||
int LogContext::get_id()
|
||||
{
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
const char* LogContext::format_time()
|
||||
{
|
||||
return time.format_time();
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_LOG_HPP
|
||||
#define SRS_CORE_LOG_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_log.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
// the context for multiple clients.
|
||||
class ILogContext
|
||||
{
|
||||
public:
|
||||
ILogContext();
|
||||
virtual ~ILogContext();
|
||||
public:
|
||||
virtual void generate_id() = 0;
|
||||
virtual int get_id() = 0;
|
||||
public:
|
||||
virtual const char* format_time() = 0;
|
||||
};
|
||||
|
||||
// user must implements the LogContext and define a global instance.
|
||||
extern ILogContext* log_context;
|
||||
|
||||
// donot print method
|
||||
#if 0
|
||||
#define srs_verbose(msg, ...) printf("[%s][%d][verbs] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_info(msg, ...) printf("[%s][%d][infos] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_trace(msg, ...) printf("[%s][%d][trace] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_warn(msg, ...) printf("[%s][%d][warns] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_error(msg, ...) printf("[%s][%d][error] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
// use __FUNCTION__ to print c method
|
||||
#elif 1
|
||||
#define srs_verbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_info(msg, ...) printf("[%s][%d][infos][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_trace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_warn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_error(msg, ...) printf("[%s][%d][error][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
// use __PRETTY_FUNCTION__ to print c++ class:method
|
||||
#else
|
||||
#define srs_verbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->format_time(), log_context->get_id(), __PRETTY_FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_info(msg, ...) printf("[%s][%d][infos][%s] ", log_context->format_time(), log_context->get_id(), __PRETTY_FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_trace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->format_time(), log_context->get_id(), __PRETTY_FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_warn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->format_time(), log_context->get_id(), __PRETTY_FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_error(msg, ...) printf("[%s][%d][error][%s] ", log_context->format_time(), log_context->get_id(), __PRETTY_FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
#undef srs_verbose
|
||||
#define srs_verbose(msg, ...) (void)0
|
||||
#endif
|
||||
#if 1
|
||||
#undef srs_info
|
||||
#define srs_info(msg, ...) (void)0
|
||||
#endif
|
||||
#if 0
|
||||
#undef srs_trace
|
||||
#define srs_trace(msg, ...) (void)0
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -1,166 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_pithy_print.hpp>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <map>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_reload.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
#define SRS_STAGE_DEFAULT_INTERVAL_MS 1200
|
||||
|
||||
struct SrsStageInfo : public ISrsReloadHandler
|
||||
{
|
||||
int stage_id;
|
||||
int pithy_print_time_ms;
|
||||
int nb_clients;
|
||||
|
||||
SrsStageInfo(int _stage_id)
|
||||
{
|
||||
stage_id = _stage_id;
|
||||
nb_clients = 0;
|
||||
|
||||
update_print_time();
|
||||
|
||||
config->subscribe(this);
|
||||
}
|
||||
virtual ~SrsStageInfo()
|
||||
{
|
||||
config->unsubscribe(this);
|
||||
}
|
||||
void update_print_time()
|
||||
{
|
||||
switch (stage_id) {
|
||||
case SRS_STAGE_PLAY_USER: {
|
||||
pithy_print_time_ms = config->get_pithy_print_play();
|
||||
break;
|
||||
}
|
||||
case SRS_STAGE_PUBLISH_USER: {
|
||||
pithy_print_time_ms = config->get_pithy_print_publish();
|
||||
break;
|
||||
}
|
||||
case SRS_STAGE_FORWARDER: {
|
||||
pithy_print_time_ms = config->get_pithy_print_forwarder();
|
||||
break;
|
||||
}
|
||||
case SRS_STAGE_ENCODER: {
|
||||
pithy_print_time_ms = config->get_pithy_print_encoder();
|
||||
break;
|
||||
}
|
||||
case SRS_STAGE_HLS: {
|
||||
pithy_print_time_ms = config->get_pithy_print_hls();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pithy_print_time_ms = SRS_STAGE_DEFAULT_INTERVAL_MS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public:
|
||||
virtual int on_reload_pithy_print()
|
||||
{
|
||||
update_print_time();
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
};
|
||||
static std::map<int, SrsStageInfo*> _srs_stages;
|
||||
|
||||
SrsPithyPrint::SrsPithyPrint(int _stage_id)
|
||||
{
|
||||
stage_id = _stage_id;
|
||||
client_id = enter_stage();
|
||||
printed_age = age = 0;
|
||||
}
|
||||
|
||||
SrsPithyPrint::~SrsPithyPrint()
|
||||
{
|
||||
leave_stage();
|
||||
}
|
||||
|
||||
int SrsPithyPrint::enter_stage()
|
||||
{
|
||||
SrsStageInfo* stage = NULL;
|
||||
|
||||
std::map<int, SrsStageInfo*>::iterator it = _srs_stages.find(stage_id);
|
||||
if (it == _srs_stages.end()) {
|
||||
stage = _srs_stages[stage_id] = new SrsStageInfo(stage_id);
|
||||
} else {
|
||||
stage = it->second;
|
||||
}
|
||||
|
||||
srs_assert(stage != NULL);
|
||||
client_id = stage->nb_clients++;
|
||||
|
||||
srs_verbose("enter stage, stage_id=%d, client_id=%d, nb_clients=%d, time_ms=%d",
|
||||
stage->stage_id, client_id, stage->nb_clients, stage->pithy_print_time_ms);
|
||||
|
||||
return client_id;
|
||||
}
|
||||
|
||||
void SrsPithyPrint::leave_stage()
|
||||
{
|
||||
SrsStageInfo* stage = _srs_stages[stage_id];
|
||||
srs_assert(stage != NULL);
|
||||
|
||||
stage->nb_clients--;
|
||||
|
||||
srs_verbose("leave stage, stage_id=%d, client_id=%d, nb_clients=%d, time_ms=%d",
|
||||
stage->stage_id, client_id, stage->nb_clients, stage->pithy_print_time_ms);
|
||||
}
|
||||
|
||||
void SrsPithyPrint::elapse(int64_t time_ms)
|
||||
{
|
||||
age += time_ms;
|
||||
}
|
||||
|
||||
bool SrsPithyPrint::can_print()
|
||||
{
|
||||
SrsStageInfo* stage = _srs_stages[stage_id];
|
||||
srs_assert(stage != NULL);
|
||||
|
||||
int64_t alive_age = age - printed_age;
|
||||
int64_t can_print_age = stage->nb_clients * stage->pithy_print_time_ms;
|
||||
|
||||
bool can_print = alive_age >= can_print_age;
|
||||
if (can_print) {
|
||||
printed_age = age;
|
||||
}
|
||||
|
||||
return can_print;
|
||||
}
|
||||
|
||||
int64_t SrsPithyPrint::get_age()
|
||||
{
|
||||
return age;
|
||||
}
|
||||
|
||||
void SrsPithyPrint::set_age(int64_t _age)
|
||||
{
|
||||
age = _age;
|
||||
}
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_PITHY_PRINT_HPP
|
||||
#define SRS_CORE_PITHY_PRINT_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_pithy_print.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
// the pithy stage for all play clients.
|
||||
#define SRS_STAGE_PLAY_USER 1
|
||||
// the pithy stage for all publish clients.
|
||||
#define SRS_STAGE_PUBLISH_USER 2
|
||||
// the pithy stage for all forward clients.
|
||||
#define SRS_STAGE_FORWARDER 3
|
||||
// the pithy stage for all encoders.
|
||||
#define SRS_STAGE_ENCODER 4
|
||||
// the pithy stage for all hls.
|
||||
#define SRS_STAGE_HLS 5
|
||||
|
||||
/**
|
||||
* the stage is used for a collection of object to do print,
|
||||
* the print time in a stage is constant and not changed.
|
||||
* for example, stage #1 for all play clients, print time is 3s,
|
||||
* if there is 10clients, then all clients should print in 10*3s.
|
||||
*/
|
||||
class SrsPithyPrint
|
||||
{
|
||||
private:
|
||||
int client_id;
|
||||
int stage_id;
|
||||
int64_t age;
|
||||
int64_t printed_age;
|
||||
public:
|
||||
/**
|
||||
* @param _stage_id defined in SRS_STAGE_xxx, eg. SRS_STAGE_PLAY_USER.
|
||||
*/
|
||||
SrsPithyPrint(int _stage_id);
|
||||
virtual ~SrsPithyPrint();
|
||||
private:
|
||||
/**
|
||||
* enter the specified stage, return the client id.
|
||||
*/
|
||||
virtual int enter_stage();
|
||||
/**
|
||||
* leave the specified stage, release the client id.
|
||||
*/
|
||||
virtual void leave_stage();
|
||||
public:
|
||||
/**
|
||||
* specified client elapse some time.
|
||||
*/
|
||||
virtual void elapse(int64_t time_ms);
|
||||
/**
|
||||
* whether current client can print.
|
||||
*/
|
||||
virtual bool can_print();
|
||||
/**
|
||||
* get the elapsed time in ms.
|
||||
*/
|
||||
virtual int64_t get_age();
|
||||
virtual void set_age(int64_t _age);
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1,88 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_refer.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
|
||||
int SrsRefer::check(std::string page_url, SrsConfDirective* refer)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (!refer) {
|
||||
srs_verbose("ignore refer check for page_url=%s", page_url.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)refer->args.size(); i++) {
|
||||
if ((ret = check_single_refer(page_url, refer->args.at(i))) == ERROR_SUCCESS) {
|
||||
srs_verbose("check refer success. page_url=%s, refer=%s",
|
||||
page_url.c_str(), refer->args.at(i).c_str());
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
ret = ERROR_RTMP_ACCESS_DENIED;
|
||||
srs_error("check refer failed. ret=%d", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsRefer::check_single_refer(std::string page_url, std::string refer)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
size_t pos = std::string::npos;
|
||||
|
||||
std::string domain_name = page_url;
|
||||
if ((pos = domain_name.find("://")) != std::string::npos) {
|
||||
domain_name = domain_name.substr(pos + 3);
|
||||
}
|
||||
|
||||
if ((pos = domain_name.find("/")) != std::string::npos) {
|
||||
domain_name = domain_name.substr(0, pos);
|
||||
}
|
||||
|
||||
if ((pos = domain_name.find(":")) != std::string::npos) {
|
||||
domain_name = domain_name.substr(0, pos);
|
||||
}
|
||||
|
||||
pos = domain_name.find(refer);
|
||||
if (pos == std::string::npos) {
|
||||
ret = ERROR_RTMP_ACCESS_DENIED;
|
||||
}
|
||||
// match primary domain.
|
||||
if (pos != domain_name.length() - refer.length()) {
|
||||
ret = ERROR_RTMP_ACCESS_DENIED;
|
||||
}
|
||||
|
||||
if (ret != ERROR_SUCCESS) {
|
||||
srs_verbose("access denied, page_url=%s, domain_name=%s, refer=%s, ret=%d",
|
||||
page_url.c_str(), domain_name.c_str(), refer.c_str(), ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_REFER_HPP
|
||||
#define SRS_CORE_REFER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_refer.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
class SrsConfDirective;
|
||||
|
||||
class SrsRefer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* to check the refer.
|
||||
* @param page_url the client page url.
|
||||
* @param refer the refer in config.
|
||||
*/
|
||||
virtual int check(std::string page_url, SrsConfDirective* refer);
|
||||
private:
|
||||
virtual int check_single_refer(std::string page_url, std::string refer);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,77 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_reload.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
ISrsReloadHandler::ISrsReloadHandler()
|
||||
{
|
||||
}
|
||||
|
||||
ISrsReloadHandler::~ISrsReloadHandler()
|
||||
{
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_listen()
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_pithy_print()
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_vhost_removed(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_gop_cache(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_queue_length(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_forward(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_hls(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int ISrsReloadHandler::on_reload_transcode(string /*vhost*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_RELOAD_HPP
|
||||
#define SRS_CORE_RELOAD_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_reload.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* the handler for config reload.
|
||||
*/
|
||||
class ISrsReloadHandler
|
||||
{
|
||||
public:
|
||||
ISrsReloadHandler();
|
||||
virtual ~ISrsReloadHandler();
|
||||
public:
|
||||
virtual int on_reload_listen();
|
||||
virtual int on_reload_pithy_print();
|
||||
virtual int on_reload_vhost_removed(std::string vhost);
|
||||
virtual int on_reload_gop_cache(std::string vhost);
|
||||
virtual int on_reload_queue_length(std::string vhost);
|
||||
virtual int on_reload_forward(std::string vhost);
|
||||
virtual int on_reload_hls(std::string vhost);
|
||||
virtual int on_reload_transcode(std::string vhost);
|
||||
};
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load diff
|
@ -1,234 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_RTMP_HPP
|
||||
#define SRS_CORE_RTMP_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_rtmp.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
class SrsProtocol;
|
||||
class ISrsMessage;
|
||||
class SrsCommonMessage;
|
||||
class SrsCreateStreamPacket;
|
||||
class SrsFMLEStartPacket;
|
||||
class SrsPublishPacket;
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsOnMetaDataPacket;
|
||||
class SrsPlayPacket;
|
||||
|
||||
/**
|
||||
* the original request from client.
|
||||
*/
|
||||
struct SrsRequest
|
||||
{
|
||||
/**
|
||||
* tcUrl: rtmp://request_vhost:port/app/stream
|
||||
* support pass vhost in query string, such as:
|
||||
* rtmp://ip:port/app?vhost=request_vhost/stream
|
||||
* rtmp://ip:port/app...vhost...request_vhost/stream
|
||||
*/
|
||||
std::string tcUrl;
|
||||
std::string pageUrl;
|
||||
std::string swfUrl;
|
||||
double objectEncoding;
|
||||
|
||||
std::string schema;
|
||||
std::string vhost;
|
||||
std::string port;
|
||||
std::string app;
|
||||
std::string stream;
|
||||
|
||||
SrsRequest();
|
||||
virtual ~SrsRequest();
|
||||
|
||||
/**
|
||||
* deep copy the request, for source to use it to support reload,
|
||||
* for when initialize the source, the request is valid,
|
||||
* when reload it, the request maybe invalid, so need to copy it.
|
||||
*/
|
||||
virtual SrsRequest* copy();
|
||||
|
||||
/**
|
||||
* disconvery vhost/app from tcUrl.
|
||||
*/
|
||||
virtual int discovery_app();
|
||||
virtual std::string get_stream_url();
|
||||
virtual void strip();
|
||||
private:
|
||||
std::string& trim(std::string& str, std::string chs);
|
||||
};
|
||||
|
||||
/**
|
||||
* the response to client.
|
||||
*/
|
||||
struct SrsResponse
|
||||
{
|
||||
int stream_id;
|
||||
|
||||
SrsResponse();
|
||||
virtual ~SrsResponse();
|
||||
};
|
||||
|
||||
/**
|
||||
* the rtmp client type.
|
||||
*/
|
||||
enum SrsClientType
|
||||
{
|
||||
SrsClientUnknown,
|
||||
SrsClientPlay,
|
||||
SrsClientFMLEPublish,
|
||||
SrsClientFlashPublish,
|
||||
};
|
||||
|
||||
/**
|
||||
* implements the client role protocol.
|
||||
*/
|
||||
class SrsRtmpClient
|
||||
{
|
||||
protected:
|
||||
SrsProtocol* protocol;
|
||||
st_netfd_t stfd;
|
||||
public:
|
||||
SrsRtmpClient(st_netfd_t _stfd);
|
||||
virtual ~SrsRtmpClient();
|
||||
public:
|
||||
virtual void set_recv_timeout(int64_t timeout_us);
|
||||
virtual void set_send_timeout(int64_t timeout_us);
|
||||
virtual int64_t get_recv_bytes();
|
||||
virtual int64_t get_send_bytes();
|
||||
virtual int get_recv_kbps();
|
||||
virtual int get_send_kbps();
|
||||
virtual int recv_message(SrsCommonMessage** pmsg);
|
||||
virtual int send_message(ISrsMessage* msg);
|
||||
public:
|
||||
virtual int handshake();
|
||||
virtual int connect_app(std::string app, std::string tc_url);
|
||||
virtual int create_stream(int& stream_id);
|
||||
virtual int play(std::string stream, int stream_id);
|
||||
virtual int publish(std::string stream, int stream_id);
|
||||
};
|
||||
|
||||
/**
|
||||
* the rtmp provices rtmp-command-protocol services,
|
||||
* a high level protocol, media stream oriented services,
|
||||
* such as connect to vhost/app, play stream, get audio/video data.
|
||||
*/
|
||||
class SrsRtmp
|
||||
{
|
||||
private:
|
||||
SrsProtocol* protocol;
|
||||
st_netfd_t stfd;
|
||||
public:
|
||||
SrsRtmp(st_netfd_t client_stfd);
|
||||
virtual ~SrsRtmp();
|
||||
public:
|
||||
virtual SrsProtocol* get_protocol();
|
||||
virtual void set_recv_timeout(int64_t timeout_us);
|
||||
virtual int64_t get_recv_timeout();
|
||||
virtual void set_send_timeout(int64_t timeout_us);
|
||||
virtual int64_t get_send_timeout();
|
||||
virtual int64_t get_recv_bytes();
|
||||
virtual int64_t get_send_bytes();
|
||||
virtual int get_recv_kbps();
|
||||
virtual int get_send_kbps();
|
||||
virtual int recv_message(SrsCommonMessage** pmsg);
|
||||
virtual int send_message(ISrsMessage* msg);
|
||||
public:
|
||||
virtual int handshake();
|
||||
virtual int connect_app(SrsRequest* req);
|
||||
virtual int set_window_ack_size(int ack_size);
|
||||
/**
|
||||
* @type: The sender can mark this message hard (0), soft (1), or dynamic (2)
|
||||
* using the Limit type field.
|
||||
*/
|
||||
virtual int set_peer_bandwidth(int bandwidth, int type);
|
||||
/**
|
||||
* @param server_ip the ip of server.
|
||||
*/
|
||||
virtual int response_connect_app(SrsRequest* req, const char* server_ip = NULL);
|
||||
virtual void response_connect_reject(SrsRequest* req, const char* desc);
|
||||
virtual int on_bw_done();
|
||||
/**
|
||||
* recv some message to identify the client.
|
||||
* @stream_id, client will createStream to play or publish by flash,
|
||||
* the stream_id used to response the createStream request.
|
||||
* @type, output the client type.
|
||||
*/
|
||||
virtual int identify_client(int stream_id, SrsClientType& type, std::string& stream_name);
|
||||
/**
|
||||
* set the chunk size when client type identified.
|
||||
*/
|
||||
virtual int set_chunk_size(int chunk_size);
|
||||
/**
|
||||
* when client type is play, response with packets:
|
||||
* StreamBegin,
|
||||
* onStatus(NetStream.Play.Reset), onStatus(NetStream.Play.Start).,
|
||||
* |RtmpSampleAccess(false, false),
|
||||
* onStatus(NetStream.Data.Start).
|
||||
*/
|
||||
virtual int start_play(int stream_id);
|
||||
/**
|
||||
* when client(type is play) send pause message,
|
||||
* if is_pause, response the following packets:
|
||||
* onStatus(NetStream.Pause.Notify)
|
||||
* StreamEOF
|
||||
* if not is_pause, response the following packets:
|
||||
* onStatus(NetStream.Unpause.Notify)
|
||||
* StreamBegin
|
||||
*/
|
||||
virtual int on_play_client_pause(int stream_id, bool is_pause);
|
||||
/**
|
||||
* when client type is publish, response with packets:
|
||||
* releaseStream response
|
||||
* FCPublish
|
||||
* FCPublish response
|
||||
* createStream response
|
||||
* onFCPublish(NetStream.Publish.Start)
|
||||
* onStatus(NetStream.Publish.Start)
|
||||
*/
|
||||
virtual int start_fmle_publish(int stream_id);
|
||||
/**
|
||||
* process the FMLE unpublish event.
|
||||
* @unpublish_tid the unpublish request transaction id.
|
||||
*/
|
||||
virtual int fmle_unpublish(int stream_id, double unpublish_tid);
|
||||
/**
|
||||
* when client type is publish, response with packets:
|
||||
* onStatus(NetStream.Publish.Start)
|
||||
*/
|
||||
virtual int start_flash_publish(int stream_id);
|
||||
private:
|
||||
virtual int identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsClientType& type, std::string& stream_name);
|
||||
virtual int identify_fmle_publish_client(SrsFMLEStartPacket* req, SrsClientType& type, std::string& stream_name);
|
||||
virtual int identify_flash_publish_client(SrsPublishPacket* req, SrsClientType& type, std::string& stream_name);
|
||||
private:
|
||||
virtual int identify_play_client(SrsPlayPacket* req, SrsClientType& type, std::string& stream_name);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,330 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_server.hpp>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_client.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
|
||||
#define SERVER_LISTEN_BACKLOG 512
|
||||
#define SRS_TIME_RESOLUTION_MS 500
|
||||
|
||||
SrsListener::SrsListener(SrsServer* _server, SrsListenerType _type)
|
||||
{
|
||||
fd = -1;
|
||||
stfd = NULL;
|
||||
|
||||
port = 0;
|
||||
server = _server;
|
||||
type = _type;
|
||||
|
||||
pthread = new SrsThread(this, 0);
|
||||
}
|
||||
|
||||
SrsListener::~SrsListener()
|
||||
{
|
||||
srs_close_stfd(stfd);
|
||||
|
||||
pthread->stop();
|
||||
srs_freep(pthread);
|
||||
|
||||
// st does not close it sometimes,
|
||||
// close it manually.
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int SrsListener::listen(int _port)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
port = _port;
|
||||
|
||||
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
ret = ERROR_SOCKET_CREATE;
|
||||
srs_error("create linux socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("create linux socket success. fd=%d", fd);
|
||||
|
||||
int reuse_socket = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
|
||||
ret = ERROR_SOCKET_SETREUSE;
|
||||
srs_error("setsockopt reuse-addr error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("setsockopt reuse-addr success. fd=%d", fd);
|
||||
|
||||
sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
|
||||
ret = ERROR_SOCKET_BIND;
|
||||
srs_error("bind socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("bind socket success. fd=%d", fd);
|
||||
|
||||
if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
||||
ret = ERROR_SOCKET_LISTEN;
|
||||
srs_error("listen socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("listen socket success. fd=%d", fd);
|
||||
|
||||
if ((stfd = st_netfd_open_socket(fd)) == NULL){
|
||||
ret = ERROR_ST_OPEN_SOCKET;
|
||||
srs_error("st_netfd_open_socket open socket failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("st open socket success. fd=%d", fd);
|
||||
|
||||
if ((ret = pthread->start()) != ERROR_SUCCESS) {
|
||||
srs_error("st_thread_create listen thread error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("create st listen thread success.");
|
||||
|
||||
srs_trace("server started, listen at port=%d, fd=%d", port, fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsListener::on_enter_loop()
|
||||
{
|
||||
srs_trace("listen cycle start, port=%d, type=%d, fd=%d", port, type, fd);
|
||||
}
|
||||
|
||||
int SrsListener::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
|
||||
|
||||
if(client_stfd == NULL){
|
||||
// ignore error.
|
||||
srs_warn("ignore accept thread stoppped for accept client error");
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd));
|
||||
|
||||
if ((ret = server->accept_client(type, client_stfd)) != ERROR_SUCCESS) {
|
||||
srs_warn("accept client error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsServer::SrsServer()
|
||||
{
|
||||
signal_reload = false;
|
||||
|
||||
srs_assert(config);
|
||||
config->subscribe(this);
|
||||
}
|
||||
|
||||
SrsServer::~SrsServer()
|
||||
{
|
||||
config->unsubscribe(this);
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsConnection*>::iterator it;
|
||||
for (it = conns.begin(); it != conns.end(); ++it) {
|
||||
SrsConnection* conn = *it;
|
||||
srs_freep(conn);
|
||||
}
|
||||
conns.clear();
|
||||
}
|
||||
|
||||
close_listeners();
|
||||
}
|
||||
|
||||
int SrsServer::initialize()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// use linux epoll.
|
||||
if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
|
||||
ret = ERROR_ST_SET_EPOLL;
|
||||
srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("st_set_eventsys use linux epoll success");
|
||||
|
||||
if(st_init() != 0){
|
||||
ret = ERROR_ST_INITIALIZE;
|
||||
srs_error("st_init failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("st_init success");
|
||||
|
||||
// set current log id.
|
||||
log_context->generate_id();
|
||||
srs_info("log set id success");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsServer::listen()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsConfDirective* conf = NULL;
|
||||
|
||||
// stream service port.
|
||||
conf = config->get_listen();
|
||||
srs_assert(conf);
|
||||
|
||||
close_listeners();
|
||||
|
||||
for (int i = 0; i < (int)conf->args.size(); i++) {
|
||||
SrsListener* listener = new SrsListener(this, SrsListenerStream);
|
||||
listeners.push_back(listener);
|
||||
|
||||
int port = ::atoi(conf->args.at(i).c_str());
|
||||
if ((ret = listener->listen(port)) != ERROR_SUCCESS) {
|
||||
srs_error("listen at port %d failed. ret=%d", port, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsServer::cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// the deamon thread, update the time cache
|
||||
while (true) {
|
||||
st_usleep(SRS_TIME_RESOLUTION_MS * 1000);
|
||||
srs_update_system_time_ms();
|
||||
|
||||
if (signal_reload) {
|
||||
signal_reload = false;
|
||||
srs_info("get signal reload, to reload the config.");
|
||||
|
||||
if ((ret = config->reload()) != ERROR_SUCCESS) {
|
||||
srs_error("reload config failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("reload config success.");
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsServer::remove(SrsConnection* conn)
|
||||
{
|
||||
std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn);
|
||||
|
||||
if (it != conns.end()) {
|
||||
conns.erase(it);
|
||||
}
|
||||
|
||||
srs_info("conn removed. conns=%d", (int)conns.size());
|
||||
|
||||
// all connections are created by server,
|
||||
// so we free it here.
|
||||
srs_freep(conn);
|
||||
}
|
||||
|
||||
void SrsServer::on_signal(int signo)
|
||||
{
|
||||
if (signo == SIGNAL_RELOAD) {
|
||||
signal_reload = true;
|
||||
}
|
||||
|
||||
// TODO: handle the SIGINT, SIGTERM.
|
||||
}
|
||||
|
||||
void SrsServer::close_listeners()
|
||||
{
|
||||
std::vector<SrsListener*>::iterator it;
|
||||
for (it = listeners.begin(); it != listeners.end(); ++it) {
|
||||
SrsListener* listener = *it;
|
||||
srs_freep(listener);
|
||||
}
|
||||
listeners.clear();
|
||||
}
|
||||
|
||||
int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int max_connections = config->get_max_connections();
|
||||
if ((int)conns.size() >= max_connections) {
|
||||
int fd = st_netfd_fileno(client_stfd);
|
||||
|
||||
srs_error("exceed the max connections, drop client: "
|
||||
"clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd);
|
||||
|
||||
srs_close_stfd(client_stfd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsConnection* conn = NULL;
|
||||
if (type == SrsListenerStream) {
|
||||
conn = new SrsClient(this, client_stfd);
|
||||
} else {
|
||||
// handler others
|
||||
}
|
||||
srs_assert(conn);
|
||||
|
||||
// directly enqueue, the cycle thread will remove the client.
|
||||
conns.push_back(conn);
|
||||
srs_verbose("add conn from port %d to vector. conns=%d", port, (int)conns.size());
|
||||
|
||||
// cycle will start process thread and when finished remove the client.
|
||||
if ((ret = conn->start()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("conn start finished. ret=%d", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsServer::on_reload_listen()
|
||||
{
|
||||
return listen();
|
||||
}
|
||||
|
||||
SrsServer* _server()
|
||||
{
|
||||
static SrsServer server;
|
||||
return &server;
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_SERVER_HPP
|
||||
#define SRS_CORE_SERVER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_server.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <srs_core_reload.hpp>
|
||||
#include <srs_core_thread.hpp>
|
||||
|
||||
class SrsServer;
|
||||
class SrsConnection;
|
||||
|
||||
enum SrsListenerType
|
||||
{
|
||||
SrsListenerStream = 0,
|
||||
SrsListenerApi
|
||||
};
|
||||
|
||||
class SrsListener : public ISrsThreadHandler
|
||||
{
|
||||
public:
|
||||
SrsListenerType type;
|
||||
private:
|
||||
int fd;
|
||||
st_netfd_t stfd;
|
||||
int port;
|
||||
SrsServer* server;
|
||||
SrsThread* pthread;
|
||||
public:
|
||||
SrsListener(SrsServer* _server, SrsListenerType _type);
|
||||
virtual ~SrsListener();
|
||||
public:
|
||||
virtual int listen(int port);
|
||||
// interface ISrsThreadHandler.
|
||||
public:
|
||||
virtual void on_enter_loop();
|
||||
virtual int cycle();
|
||||
};
|
||||
|
||||
class SrsServer : public ISrsReloadHandler
|
||||
{
|
||||
friend class SrsListener;
|
||||
private:
|
||||
std::vector<SrsConnection*> conns;
|
||||
std::vector<SrsListener*> listeners;
|
||||
bool signal_reload;
|
||||
public:
|
||||
SrsServer();
|
||||
virtual ~SrsServer();
|
||||
public:
|
||||
virtual int initialize();
|
||||
virtual int listen();
|
||||
virtual int cycle();
|
||||
virtual void remove(SrsConnection* conn);
|
||||
virtual void on_signal(int signo);
|
||||
private:
|
||||
virtual void close_listeners();
|
||||
virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd);
|
||||
public:
|
||||
virtual int on_reload_listen();
|
||||
};
|
||||
|
||||
SrsServer* _server();
|
||||
|
||||
#endif
|
|
@ -1,179 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_socket.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
SrsSocket::SrsSocket(st_netfd_t client_stfd)
|
||||
{
|
||||
stfd = client_stfd;
|
||||
send_timeout = recv_timeout = ST_UTIME_NO_TIMEOUT;
|
||||
recv_bytes = send_bytes = 0;
|
||||
start_time_ms = srs_get_system_time_ms();
|
||||
}
|
||||
|
||||
SrsSocket::~SrsSocket()
|
||||
{
|
||||
}
|
||||
|
||||
void SrsSocket::set_recv_timeout(int64_t timeout_us)
|
||||
{
|
||||
recv_timeout = timeout_us;
|
||||
}
|
||||
|
||||
int64_t SrsSocket::get_recv_timeout()
|
||||
{
|
||||
return recv_timeout;
|
||||
}
|
||||
|
||||
void SrsSocket::set_send_timeout(int64_t timeout_us)
|
||||
{
|
||||
send_timeout = timeout_us;
|
||||
}
|
||||
|
||||
int64_t SrsSocket::get_send_timeout()
|
||||
{
|
||||
return send_timeout;
|
||||
}
|
||||
|
||||
int64_t SrsSocket::get_recv_bytes()
|
||||
{
|
||||
return recv_bytes;
|
||||
}
|
||||
|
||||
int64_t SrsSocket::get_send_bytes()
|
||||
{
|
||||
return send_bytes;
|
||||
}
|
||||
|
||||
int SrsSocket::get_recv_kbps()
|
||||
{
|
||||
int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
|
||||
|
||||
if (diff_ms <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return recv_bytes * 8 / diff_ms;
|
||||
}
|
||||
|
||||
int SrsSocket::get_send_kbps()
|
||||
{
|
||||
int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
|
||||
|
||||
if (diff_ms <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return send_bytes * 8 / diff_ms;
|
||||
}
|
||||
|
||||
int SrsSocket::read(const void* buf, size_t size, ssize_t* nread)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nread = st_read(stfd, (void*)buf, size, recv_timeout);
|
||||
|
||||
// On success a non-negative integer indicating the number of bytes actually read is returned
|
||||
// (a value of 0 means the network connection is closed or end of file is reached).
|
||||
if (*nread <= 0) {
|
||||
if (errno == ETIME) {
|
||||
return ERROR_SOCKET_TIMEOUT;
|
||||
}
|
||||
|
||||
if (*nread == 0) {
|
||||
errno = ECONNRESET;
|
||||
}
|
||||
|
||||
return ERROR_SOCKET_READ;
|
||||
}
|
||||
|
||||
recv_bytes += *nread;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nread = st_read_fully(stfd, (void*)buf, size, recv_timeout);
|
||||
|
||||
// On success a non-negative integer indicating the number of bytes actually read is returned
|
||||
// (a value less than nbyte means the network connection is closed or end of file is reached)
|
||||
if (*nread != (ssize_t)size) {
|
||||
if (errno == ETIME) {
|
||||
return ERROR_SOCKET_TIMEOUT;
|
||||
}
|
||||
|
||||
if (*nread >= 0) {
|
||||
errno = ECONNRESET;
|
||||
}
|
||||
|
||||
return ERROR_SOCKET_READ_FULLY;
|
||||
}
|
||||
|
||||
recv_bytes += *nread;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSocket::write(const void* buf, size_t size, ssize_t* nwrite)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nwrite = st_write(stfd, (void*)buf, size, send_timeout);
|
||||
|
||||
if (*nwrite <= 0) {
|
||||
if (errno == ETIME) {
|
||||
return ERROR_SOCKET_TIMEOUT;
|
||||
}
|
||||
|
||||
return ERROR_SOCKET_WRITE;
|
||||
}
|
||||
|
||||
send_bytes += *nwrite;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nwrite = st_writev(stfd, iov, iov_size, send_timeout);
|
||||
|
||||
if (*nwrite <= 0) {
|
||||
if (errno == ETIME) {
|
||||
return ERROR_SOCKET_TIMEOUT;
|
||||
}
|
||||
|
||||
return ERROR_SOCKET_WRITE;
|
||||
}
|
||||
|
||||
send_bytes += *nwrite;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_SOCKET_HPP
|
||||
#define SRS_CORE_SOCKET_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_socket.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
/**
|
||||
* the socket provides TCP socket over st,
|
||||
* that is, the sync socket mechanism.
|
||||
*/
|
||||
class SrsSocket
|
||||
{
|
||||
private:
|
||||
int64_t recv_timeout;
|
||||
int64_t send_timeout;
|
||||
int64_t recv_bytes;
|
||||
int64_t send_bytes;
|
||||
int64_t start_time_ms;
|
||||
st_netfd_t stfd;
|
||||
public:
|
||||
SrsSocket(st_netfd_t client_stfd);
|
||||
virtual ~SrsSocket();
|
||||
public:
|
||||
virtual void set_recv_timeout(int64_t timeout_us);
|
||||
virtual int64_t get_recv_timeout();
|
||||
virtual void set_send_timeout(int64_t timeout_us);
|
||||
virtual int64_t get_send_timeout();
|
||||
virtual int64_t get_recv_bytes();
|
||||
virtual int64_t get_send_bytes();
|
||||
virtual int get_recv_kbps();
|
||||
virtual int get_send_kbps();
|
||||
public:
|
||||
virtual int read(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
||||
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,997 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_source.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_protocol.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_core_amf0.hpp>
|
||||
#include <srs_core_codec.hpp>
|
||||
#include <srs_core_hls.hpp>
|
||||
#include <srs_core_forward.hpp>
|
||||
#include <srs_core_config.hpp>
|
||||
#include <srs_core_encoder.hpp>
|
||||
#include <srs_core_rtmp.hpp>
|
||||
|
||||
#define CONST_MAX_JITTER_MS 500
|
||||
#define DEFAULT_FRAME_TIME_MS 40
|
||||
|
||||
SrsRtmpJitter::SrsRtmpJitter()
|
||||
{
|
||||
last_pkt_correct_time = last_pkt_time = 0;
|
||||
}
|
||||
|
||||
SrsRtmpJitter::~SrsRtmpJitter()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsRtmpJitter::correct(SrsSharedPtrMessage* msg, int tba, int tbv)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// set to 0 for metadata.
|
||||
if (!msg->header.is_video() && !msg->header.is_audio()) {
|
||||
msg->header.timestamp = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sample_rate = tba;
|
||||
int frame_rate = tbv;
|
||||
|
||||
/**
|
||||
* we use a very simple time jitter detect/correct algorithm:
|
||||
* 1. delta: ensure the delta is positive and valid,
|
||||
* we set the delta to DEFAULT_FRAME_TIME_MS,
|
||||
* if the delta of time is nagative or greater than CONST_MAX_JITTER_MS.
|
||||
* 2. last_pkt_time: specifies the original packet time,
|
||||
* is used to detect next jitter.
|
||||
* 3. last_pkt_correct_time: simply add the positive delta,
|
||||
* and enforce the time monotonically.
|
||||
*/
|
||||
int64_t time = msg->header.timestamp;
|
||||
int64_t delta = time - last_pkt_time;
|
||||
|
||||
// if jitter detected, reset the delta.
|
||||
if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
|
||||
// calc the right diff by audio sample rate
|
||||
if (msg->header.is_audio() && sample_rate > 0) {
|
||||
delta = (int64_t)(delta * 1000.0 / sample_rate);
|
||||
} else if (msg->header.is_video() && frame_rate > 0) {
|
||||
delta = (int64_t)(delta * 1.0 / frame_rate);
|
||||
} else {
|
||||
delta = DEFAULT_FRAME_TIME_MS;
|
||||
}
|
||||
|
||||
// sometimes, the time is absolute time, so correct it again.
|
||||
if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
|
||||
delta = DEFAULT_FRAME_TIME_MS;
|
||||
}
|
||||
|
||||
srs_info("jitter detected, last_pts=%"PRId64", pts=%"PRId64", diff=%"PRId64", last_time=%"PRId64", time=%"PRId64", diff=%"PRId64"",
|
||||
last_pkt_time, time, time - last_pkt_time, last_pkt_correct_time, last_pkt_correct_time + delta, delta);
|
||||
} else {
|
||||
srs_verbose("timestamp no jitter. time=%"PRId64", last_pkt=%"PRId64", correct_to=%"PRId64"",
|
||||
time, last_pkt_time, last_pkt_correct_time + delta);
|
||||
}
|
||||
|
||||
last_pkt_correct_time = srs_max(0, last_pkt_correct_time + delta);
|
||||
|
||||
msg->header.timestamp = last_pkt_correct_time;
|
||||
last_pkt_time = time;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsRtmpJitter::get_time()
|
||||
{
|
||||
return (int)last_pkt_correct_time;
|
||||
}
|
||||
|
||||
SrsMessageQueue::SrsMessageQueue()
|
||||
{
|
||||
queue_size_ms = 0;
|
||||
av_start_time = av_end_time = -1;
|
||||
}
|
||||
|
||||
SrsMessageQueue::~SrsMessageQueue()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void SrsMessageQueue::set_queue_size(double queue_size)
|
||||
{
|
||||
queue_size_ms = (int)(queue_size * 1000);
|
||||
}
|
||||
|
||||
int SrsMessageQueue::enqueue(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (msg->header.is_video() || msg->header.is_audio()) {
|
||||
if (av_start_time == -1) {
|
||||
av_start_time = msg->header.timestamp;
|
||||
}
|
||||
|
||||
av_end_time = msg->header.timestamp;
|
||||
}
|
||||
|
||||
msgs.push_back(msg);
|
||||
|
||||
while (av_end_time - av_start_time > queue_size_ms) {
|
||||
shrink();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsMessageQueue::get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (msgs.empty()) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (max_count == 0) {
|
||||
count = (int)msgs.size();
|
||||
} else {
|
||||
count = srs_min(max_count, (int)msgs.size());
|
||||
}
|
||||
|
||||
if (count <= 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
pmsgs = new SrsSharedPtrMessage*[count];
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
pmsgs[i] = msgs[i];
|
||||
}
|
||||
|
||||
SrsSharedPtrMessage* last = msgs[count - 1];
|
||||
av_start_time = last->header.timestamp;
|
||||
|
||||
if (count == (int)msgs.size()) {
|
||||
msgs.clear();
|
||||
} else {
|
||||
msgs.erase(msgs.begin(), msgs.begin() + count);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsMessageQueue::shrink()
|
||||
{
|
||||
int iframe_index = -1;
|
||||
|
||||
// issue the first iframe.
|
||||
// skip the first frame, whatever the type of it,
|
||||
// for when we shrinked, the first is the iframe,
|
||||
// we will directly remove the gop next time.
|
||||
for (int i = 1; i < (int)msgs.size(); i++) {
|
||||
SrsSharedPtrMessage* msg = msgs[i];
|
||||
|
||||
if (msg->header.is_video()) {
|
||||
if (SrsCodec::video_is_keyframe(msg->payload, msg->size)) {
|
||||
// the max frame index to remove.
|
||||
iframe_index = i;
|
||||
|
||||
// set the start time, we will remove until this frame.
|
||||
av_start_time = msg->header.timestamp;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// no iframe, clear the queue.
|
||||
if (iframe_index < 0) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
|
||||
srs_trace("shrink the cache queue, size=%d, removed=%d, max=%.2f",
|
||||
(int)msgs.size(), iframe_index, queue_size_ms / 1000.0);
|
||||
|
||||
// remove the first gop from the front
|
||||
for (int i = 0; i < iframe_index; i++) {
|
||||
SrsSharedPtrMessage* msg = msgs[i];
|
||||
srs_freep(msg);
|
||||
}
|
||||
msgs.erase(msgs.begin(), msgs.begin() + iframe_index);
|
||||
}
|
||||
|
||||
void SrsMessageQueue::clear()
|
||||
{
|
||||
std::vector<SrsSharedPtrMessage*>::iterator it;
|
||||
|
||||
for (it = msgs.begin(); it != msgs.end(); ++it) {
|
||||
SrsSharedPtrMessage* msg = *it;
|
||||
srs_freep(msg);
|
||||
}
|
||||
|
||||
msgs.clear();
|
||||
|
||||
av_start_time = av_end_time = -1;
|
||||
}
|
||||
|
||||
SrsConsumer::SrsConsumer(SrsSource* _source)
|
||||
{
|
||||
source = _source;
|
||||
paused = false;
|
||||
jitter = new SrsRtmpJitter();
|
||||
queue = new SrsMessageQueue();
|
||||
}
|
||||
|
||||
SrsConsumer::~SrsConsumer()
|
||||
{
|
||||
source->on_consumer_destroy(this);
|
||||
srs_freep(jitter);
|
||||
srs_freep(queue);
|
||||
}
|
||||
|
||||
void SrsConsumer::set_queue_size(double queue_size)
|
||||
{
|
||||
queue->set_queue_size(queue_size);
|
||||
}
|
||||
|
||||
int SrsConsumer::get_time()
|
||||
{
|
||||
return jitter->get_time();
|
||||
}
|
||||
|
||||
int SrsConsumer::enqueue(SrsSharedPtrMessage* msg, int tba, int tbv)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = jitter->correct(msg, tba, tbv)) != ERROR_SUCCESS) {
|
||||
srs_freep(msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsConsumer::get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count)
|
||||
{
|
||||
// paused, return nothing.
|
||||
if (paused) {
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
return queue->get_packets(max_count, pmsgs, count);
|
||||
}
|
||||
|
||||
int SrsConsumer::on_play_client_pause(bool is_pause)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_trace("stream consumer change pause state %d=>%d", paused, is_pause);
|
||||
paused = is_pause;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SrsGopCache::SrsGopCache()
|
||||
{
|
||||
cached_video_count = 0;
|
||||
enable_gop_cache = true;
|
||||
}
|
||||
|
||||
SrsGopCache::~SrsGopCache()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void SrsGopCache::set(bool enabled)
|
||||
{
|
||||
enable_gop_cache = enabled;
|
||||
|
||||
if (!enabled) {
|
||||
srs_info("disable gop cache, clear %d packets.", (int)gop_cache.size());
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
|
||||
srs_info("enable gop cache");
|
||||
}
|
||||
|
||||
int SrsGopCache::cache(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (!enable_gop_cache) {
|
||||
srs_verbose("gop cache is disabled.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// got video, update the video count if acceptable
|
||||
if (msg->header.is_video()) {
|
||||
cached_video_count++;
|
||||
}
|
||||
|
||||
// no acceptable video or pure audio, disable the cache.
|
||||
if (cached_video_count == 0) {
|
||||
srs_verbose("ignore any frame util got a h264 video frame.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// clear gop cache when got key frame
|
||||
if (msg->header.is_video() && SrsCodec::video_is_keyframe(msg->payload, msg->size)) {
|
||||
srs_info("clear gop cache when got keyframe. vcount=%d, count=%d",
|
||||
cached_video_count, (int)gop_cache.size());
|
||||
|
||||
clear();
|
||||
|
||||
// curent msg is video frame, so we set to 1.
|
||||
cached_video_count = 1;
|
||||
}
|
||||
|
||||
// cache the frame.
|
||||
gop_cache.push_back(msg->copy());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsGopCache::clear()
|
||||
{
|
||||
std::vector<SrsSharedPtrMessage*>::iterator it;
|
||||
for (it = gop_cache.begin(); it != gop_cache.end(); ++it) {
|
||||
SrsSharedPtrMessage* msg = *it;
|
||||
srs_freep(msg);
|
||||
}
|
||||
gop_cache.clear();
|
||||
|
||||
cached_video_count = 0;
|
||||
}
|
||||
|
||||
int SrsGopCache::dump(SrsConsumer* consumer, int tba, int tbv)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
std::vector<SrsSharedPtrMessage*>::iterator it;
|
||||
for (it = gop_cache.begin(); it != gop_cache.end(); ++it) {
|
||||
SrsSharedPtrMessage* msg = *it;
|
||||
if ((ret = consumer->enqueue(msg->copy(), tba, tbv)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch cached gop failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
srs_trace("dispatch cached gop success. count=%d, duration=%d", (int)gop_cache.size(), consumer->get_time());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::map<std::string, SrsSource*> SrsSource::pool;
|
||||
|
||||
SrsSource* SrsSource::find(SrsRequest* req)
|
||||
{
|
||||
string stream_url = req->get_stream_url();
|
||||
string vhost = req->vhost;
|
||||
|
||||
if (pool.find(stream_url) == pool.end()) {
|
||||
pool[stream_url] = new SrsSource(req);
|
||||
srs_verbose("create new source for url=%s, vhost=%s", stream_url.c_str(), vhost.c_str());
|
||||
}
|
||||
|
||||
return pool[stream_url];
|
||||
}
|
||||
|
||||
SrsSource::SrsSource(SrsRequest* _req)
|
||||
{
|
||||
req = _req->copy();
|
||||
|
||||
#ifdef SRS_HLS
|
||||
hls = new SrsHls(this);
|
||||
#endif
|
||||
#ifdef SRS_FFMPEG
|
||||
encoder = new SrsEncoder();
|
||||
#endif
|
||||
|
||||
cache_metadata = cache_sh_video = cache_sh_audio = NULL;
|
||||
|
||||
frame_rate = sample_rate = 0;
|
||||
_can_publish = true;
|
||||
|
||||
gop_cache = new SrsGopCache();
|
||||
|
||||
config->subscribe(this);
|
||||
}
|
||||
|
||||
SrsSource::~SrsSource()
|
||||
{
|
||||
config->unsubscribe(this);
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
for (it = consumers.begin(); it != consumers.end(); ++it) {
|
||||
SrsConsumer* consumer = *it;
|
||||
srs_freep(consumer);
|
||||
}
|
||||
consumers.clear();
|
||||
}
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
srs_freep(forwarder);
|
||||
}
|
||||
forwarders.clear();
|
||||
}
|
||||
|
||||
srs_freep(cache_metadata);
|
||||
srs_freep(cache_sh_video);
|
||||
srs_freep(cache_sh_audio);
|
||||
|
||||
srs_freep(gop_cache);
|
||||
|
||||
#ifdef SRS_HLS
|
||||
srs_freep(hls);
|
||||
#endif
|
||||
#ifdef SRS_FFMPEG
|
||||
srs_freep(encoder);
|
||||
#endif
|
||||
|
||||
srs_freep(req);
|
||||
}
|
||||
|
||||
int SrsSource::on_reload_gop_cache(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// gop cache changed.
|
||||
bool enabled_cache = config->get_gop_cache(vhost);
|
||||
|
||||
srs_trace("vhost %s gop_cache changed to %d, source url=%s",
|
||||
vhost.c_str(), enabled_cache, req->get_stream_url().c_str());
|
||||
|
||||
set_cache(enabled_cache);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_reload_queue_length(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
double queue_size = config->get_queue_length(req->vhost);
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
|
||||
for (it = consumers.begin(); it != consumers.end(); ++it) {
|
||||
SrsConsumer* consumer = *it;
|
||||
consumer->set_queue_size(queue_size);
|
||||
}
|
||||
|
||||
srs_trace("consumers reload queue size success.");
|
||||
}
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
forwarder->set_queue_size(queue_size);
|
||||
}
|
||||
|
||||
srs_trace("forwarders reload queue size success.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_reload_forward(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// forwarders
|
||||
destroy_forwarders();
|
||||
if ((ret = create_forwarders()) != ERROR_SUCCESS) {
|
||||
srs_error("create forwarders failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("vhost %s forwarders reload success", vhost.c_str());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_reload_hls(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef SRS_HLS
|
||||
hls->on_unpublish();
|
||||
if ((ret = hls->on_publish(req)) != ERROR_SUCCESS) {
|
||||
srs_error("hls publish failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("vhost %s hls reload success", vhost.c_str());
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_reload_transcode(string vhost)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (req->vhost != vhost) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef SRS_FFMPEG
|
||||
encoder->on_unpublish();
|
||||
if ((ret = encoder->on_publish(req)) != ERROR_SUCCESS) {
|
||||
srs_error("start encoder failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_trace("vhost %s transcode reload success", vhost.c_str());
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_forwarder_start(SrsForwarder* forwarder)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// feed the forwarder the metadata/sequence header,
|
||||
// when reload to enable the forwarder.
|
||||
if (cache_metadata && (ret = forwarder->on_meta_data(cache_metadata->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process onMetaData message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (cache_sh_video && (ret = forwarder->on_video(cache_sh_video->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process video sequence header message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (cache_sh_audio && (ret = forwarder->on_audio(cache_sh_audio->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process audio sequence header message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_hls_start()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
#ifdef SRS_HLS
|
||||
|
||||
// feed the hls the metadata/sequence header,
|
||||
// when reload to enable the hls.
|
||||
// TODO: maybe need to decode the metadata?
|
||||
if (cache_sh_video && (ret = hls->on_video(cache_sh_video->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("hls process video sequence header message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
if (cache_sh_audio && (ret = hls->on_audio(cache_sh_audio->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("hls process audio sequence header message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SrsSource::can_publish()
|
||||
{
|
||||
return _can_publish;
|
||||
}
|
||||
|
||||
int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
#ifdef SRS_HLS
|
||||
if (metadata && (ret = hls->on_meta_data(metadata->metadata)) != ERROR_SUCCESS) {
|
||||
srs_error("hls process onMetaData message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
metadata->metadata->set("server", new SrsAmf0String(
|
||||
RTMP_SIG_SRS_KEY" "RTMP_SIG_SRS_VERSION" ("RTMP_SIG_SRS_URL_SHORT")"));
|
||||
metadata->metadata->set("contributor",
|
||||
new SrsAmf0String(RTMP_SIG_SRS_PRIMARY_AUTHROS));
|
||||
|
||||
SrsAmf0Any* prop = NULL;
|
||||
if ((prop = metadata->metadata->get_property("audiosamplerate")) != NULL) {
|
||||
if (prop->is_number()) {
|
||||
sample_rate = (int)(srs_amf0_convert<SrsAmf0Number>(prop)->value);
|
||||
}
|
||||
}
|
||||
if ((prop = metadata->metadata->get_property("framerate")) != NULL) {
|
||||
if (prop->is_number()) {
|
||||
frame_rate = (int)(srs_amf0_convert<SrsAmf0Number>(prop)->value);
|
||||
}
|
||||
}
|
||||
|
||||
// encode the metadata to payload
|
||||
int size = metadata->get_payload_length();
|
||||
if (size <= 0) {
|
||||
srs_warn("ignore the invalid metadata. size=%d", size);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get metadata size success.");
|
||||
|
||||
char* payload = NULL;
|
||||
if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) {
|
||||
srs_error("encode metadata error. ret=%d", ret);
|
||||
srs_freepa(payload);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("encode metadata success.");
|
||||
|
||||
// create a shared ptr message.
|
||||
srs_freep(cache_metadata);
|
||||
cache_metadata = new SrsSharedPtrMessage();
|
||||
|
||||
// dump message to shared ptr message.
|
||||
if ((ret = cache_metadata->initialize(msg, payload, size)) != ERROR_SUCCESS) {
|
||||
srs_error("initialize the cache metadata failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("initialize shared ptr metadata success.");
|
||||
|
||||
// copy to all consumer
|
||||
if (true) {
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
for (it = consumers.begin(); it != consumers.end(); ++it) {
|
||||
SrsConsumer* consumer = *it;
|
||||
if ((ret = consumer->enqueue(cache_metadata->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch the metadata failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
srs_trace("dispatch metadata success.");
|
||||
}
|
||||
|
||||
// copy to all forwarders
|
||||
if (true) {
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
if ((ret = forwarder->on_meta_data(cache_metadata->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process onMetaData message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_audio(SrsCommonMessage* audio)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsSharedPtrMessage* msg = new SrsSharedPtrMessage();
|
||||
SrsAutoFree(SrsSharedPtrMessage, msg, false);
|
||||
if ((ret = msg->initialize(audio)) != ERROR_SUCCESS) {
|
||||
srs_error("initialize the audio failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("initialize shared ptr audio success.");
|
||||
|
||||
#ifdef SRS_HLS
|
||||
if ((ret = hls->on_audio(msg->copy())) != ERROR_SUCCESS) {
|
||||
srs_warn("hls process audio message failed, ignore and disable hls. ret=%d", ret);
|
||||
|
||||
// unpublish, ignore ret.
|
||||
hls->on_unpublish();
|
||||
|
||||
// ignore.
|
||||
ret = ERROR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
// copy to all consumer
|
||||
if (true) {
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
for (it = consumers.begin(); it != consumers.end(); ++it) {
|
||||
SrsConsumer* consumer = *it;
|
||||
if ((ret = consumer->enqueue(msg->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch the audio failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
srs_info("dispatch audio success.");
|
||||
}
|
||||
|
||||
// copy to all forwarders.
|
||||
if (true) {
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
if ((ret = forwarder->on_audio(msg->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process audio message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cache the sequence header if h264
|
||||
if (SrsCodec::audio_is_sequence_header(msg->payload, msg->size)) {
|
||||
srs_freep(cache_sh_audio);
|
||||
cache_sh_audio = msg->copy();
|
||||
srs_trace("update audio sequence header success. size=%d", msg->header.payload_length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// cache the last gop packets
|
||||
if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("shrink gop cache failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("cache gop success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_video(SrsCommonMessage* video)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsSharedPtrMessage* msg = new SrsSharedPtrMessage();
|
||||
SrsAutoFree(SrsSharedPtrMessage, msg, false);
|
||||
if ((ret = msg->initialize(video)) != ERROR_SUCCESS) {
|
||||
srs_error("initialize the video failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("initialize shared ptr video success.");
|
||||
|
||||
#ifdef SRS_HLS
|
||||
if ((ret = hls->on_video(msg->copy())) != ERROR_SUCCESS) {
|
||||
srs_warn("hls process video message failed, ignore and disable hls. ret=%d", ret);
|
||||
|
||||
// unpublish, ignore ret.
|
||||
hls->on_unpublish();
|
||||
|
||||
// ignore.
|
||||
ret = ERROR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
// copy to all consumer
|
||||
if (true) {
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
for (it = consumers.begin(); it != consumers.end(); ++it) {
|
||||
SrsConsumer* consumer = *it;
|
||||
if ((ret = consumer->enqueue(msg->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch the video failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
srs_info("dispatch video success.");
|
||||
}
|
||||
|
||||
// copy to all forwarders.
|
||||
if (true) {
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
if ((ret = forwarder->on_video(msg->copy())) != ERROR_SUCCESS) {
|
||||
srs_error("forwarder process video message failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cache the sequence header if h264
|
||||
if (SrsCodec::video_is_sequence_header(msg->payload, msg->size)) {
|
||||
srs_freep(cache_sh_video);
|
||||
cache_sh_video = msg->copy();
|
||||
srs_trace("update video sequence header success. size=%d", msg->header.payload_length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// cache the last gop packets
|
||||
if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) {
|
||||
srs_error("gop cache msg failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("cache gop success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsSource::on_publish(SrsRequest* _req)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
// update the request object.
|
||||
srs_freep(req);
|
||||
req = _req->copy();
|
||||
srs_assert(req);
|
||||
|
||||
_can_publish = false;
|
||||
|
||||
// create forwarders
|
||||
if ((ret = create_forwarders()) != ERROR_SUCCESS) {
|
||||
srs_error("create forwarders failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef SRS_FFMPEG
|
||||
if ((ret = encoder->on_publish(req)) != ERROR_SUCCESS) {
|
||||
srs_error("start encoder failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SRS_HLS
|
||||
if ((ret = hls->on_publish(req)) != ERROR_SUCCESS) {
|
||||
srs_error("start hls failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsSource::on_unpublish()
|
||||
{
|
||||
// destroy all forwarders
|
||||
destroy_forwarders();
|
||||
|
||||
#ifdef SRS_FFMPEG
|
||||
encoder->on_unpublish();
|
||||
#endif
|
||||
|
||||
// TODO: HLS should continue previous sequence and stream.
|
||||
#ifdef SRS_HLS
|
||||
hls->on_unpublish();
|
||||
#endif
|
||||
|
||||
gop_cache->clear();
|
||||
|
||||
srs_freep(cache_metadata);
|
||||
frame_rate = sample_rate = 0;
|
||||
|
||||
srs_freep(cache_sh_video);
|
||||
srs_freep(cache_sh_audio);
|
||||
|
||||
srs_trace("clear cache/metadata/sequence-headers when unpublish.");
|
||||
|
||||
_can_publish = true;
|
||||
}
|
||||
|
||||
int SrsSource::create_consumer(SrsConsumer*& consumer)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
consumer = new SrsConsumer(this);
|
||||
consumers.push_back(consumer);
|
||||
|
||||
double queue_size = config->get_queue_length(req->vhost);
|
||||
consumer->set_queue_size(queue_size);
|
||||
|
||||
if (cache_metadata && (ret = consumer->enqueue(cache_metadata->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch metadata failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("dispatch metadata success");
|
||||
|
||||
if (cache_sh_video && (ret = consumer->enqueue(cache_sh_video->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch video sequence header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("dispatch video sequence header success");
|
||||
|
||||
if (cache_sh_audio && (ret = consumer->enqueue(cache_sh_audio->copy(), sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
srs_error("dispatch audio sequence header failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_info("dispatch audio sequence header success");
|
||||
|
||||
if ((ret = gop_cache->dump(consumer, sample_rate, frame_rate)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("create consumer, queue_size=%.2f, tba=%d, tbv=%d", queue_size, sample_rate, frame_rate);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsSource::on_consumer_destroy(SrsConsumer* consumer)
|
||||
{
|
||||
std::vector<SrsConsumer*>::iterator it;
|
||||
it = std::find(consumers.begin(), consumers.end(), consumer);
|
||||
if (it != consumers.end()) {
|
||||
consumers.erase(it);
|
||||
}
|
||||
srs_info("handle consumer destroy success.");
|
||||
}
|
||||
|
||||
void SrsSource::set_cache(bool enabled)
|
||||
{
|
||||
gop_cache->set(enabled);
|
||||
}
|
||||
|
||||
int SrsSource::create_forwarders()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
SrsConfDirective* conf = config->get_forward(req->vhost);
|
||||
for (int i = 0; conf && i < (int)conf->args.size(); i++) {
|
||||
std::string forward_server = conf->args.at(i);
|
||||
|
||||
SrsForwarder* forwarder = new SrsForwarder(this);
|
||||
forwarders.push_back(forwarder);
|
||||
|
||||
double queue_size = config->get_queue_length(req->vhost);
|
||||
forwarder->set_queue_size(queue_size);
|
||||
|
||||
if ((ret = forwarder->on_publish(req, forward_server)) != ERROR_SUCCESS) {
|
||||
srs_error("start forwarder failed. "
|
||||
"vhost=%s, app=%s, stream=%s, forward-to=%s",
|
||||
req->vhost.c_str(), req->app.c_str(), req->stream.c_str(),
|
||||
forward_server.c_str());
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsSource::destroy_forwarders()
|
||||
{
|
||||
std::vector<SrsForwarder*>::iterator it;
|
||||
for (it = forwarders.begin(); it != forwarders.end(); ++it) {
|
||||
SrsForwarder* forwarder = *it;
|
||||
forwarder->on_unpublish();
|
||||
srs_freep(forwarder);
|
||||
}
|
||||
forwarders.clear();
|
||||
}
|
||||
|
|
@ -1,286 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_SOURCE_HPP
|
||||
#define SRS_CORE_SOURCE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_source.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <srs_core_reload.hpp>
|
||||
|
||||
class SrsSource;
|
||||
class SrsCommonMessage;
|
||||
class SrsOnMetaDataPacket;
|
||||
class SrsSharedPtrMessage;
|
||||
class SrsForwarder;
|
||||
class SrsRequest;
|
||||
#ifdef SRS_HLS
|
||||
class SrsHls;
|
||||
#endif
|
||||
#ifdef SRS_FFMPEG
|
||||
class SrsEncoder;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* time jitter detect and correct,
|
||||
* to ensure the rtmp stream is monotonically.
|
||||
*/
|
||||
class SrsRtmpJitter
|
||||
{
|
||||
private:
|
||||
int64_t last_pkt_time;
|
||||
int64_t last_pkt_correct_time;
|
||||
public:
|
||||
SrsRtmpJitter();
|
||||
virtual ~SrsRtmpJitter();
|
||||
public:
|
||||
/**
|
||||
* detect the time jitter and correct it.
|
||||
*/
|
||||
virtual int correct(SrsSharedPtrMessage* msg, int tba, int tbv);
|
||||
/**
|
||||
* get current client time, the last packet time.
|
||||
*/
|
||||
virtual int get_time();
|
||||
};
|
||||
|
||||
/**
|
||||
* the message queue for the consumer(client), forwarder.
|
||||
* we limit the size in seconds, drop old messages(the whole gop) if full.
|
||||
*/
|
||||
class SrsMessageQueue
|
||||
{
|
||||
private:
|
||||
int64_t av_start_time;
|
||||
int64_t av_end_time;
|
||||
int queue_size_ms;
|
||||
std::vector<SrsSharedPtrMessage*> msgs;
|
||||
public:
|
||||
SrsMessageQueue();
|
||||
virtual ~SrsMessageQueue();
|
||||
public:
|
||||
/**
|
||||
* set the queue size
|
||||
* @param queue_size the queue size in seconds.
|
||||
*/
|
||||
virtual void set_queue_size(double queue_size);
|
||||
public:
|
||||
/**
|
||||
* enqueue the message, the timestamp always monotonically.
|
||||
* @param msg, the msg to enqueue, user never free it whatever the return code.
|
||||
*/
|
||||
virtual int enqueue(SrsSharedPtrMessage* msg);
|
||||
/**
|
||||
* get packets in consumer queue.
|
||||
* @pmsgs SrsMessages*[], output the prt array.
|
||||
* @count the count in array.
|
||||
* @max_count the max count to dequeue, 0 to dequeue all.
|
||||
*/
|
||||
virtual int get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count);
|
||||
private:
|
||||
/**
|
||||
* remove a gop from the front.
|
||||
* if no iframe found, clear it.
|
||||
*/
|
||||
virtual void shrink();
|
||||
virtual void clear();
|
||||
};
|
||||
|
||||
/**
|
||||
* the consumer for SrsSource, that is a play client.
|
||||
*/
|
||||
class SrsConsumer
|
||||
{
|
||||
private:
|
||||
SrsRtmpJitter* jitter;
|
||||
SrsSource* source;
|
||||
SrsMessageQueue* queue;
|
||||
bool paused;
|
||||
public:
|
||||
SrsConsumer(SrsSource* _source);
|
||||
virtual ~SrsConsumer();
|
||||
public:
|
||||
virtual void set_queue_size(double queue_size);
|
||||
public:
|
||||
/**
|
||||
* get current client time, the last packet time.
|
||||
*/
|
||||
virtual int get_time();
|
||||
/**
|
||||
* enqueue an shared ptr message.
|
||||
* @param tba timebase of audio.
|
||||
* used to calc the audio time delta if time-jitter detected.
|
||||
* @param tbv timebase of video.
|
||||
* used to calc the video time delta if time-jitter detected.
|
||||
*/
|
||||
virtual int enqueue(SrsSharedPtrMessage* msg, int tba, int tbv);
|
||||
/**
|
||||
* get packets in consumer queue.
|
||||
* @pmsgs SrsMessages*[], output the prt array.
|
||||
* @count the count in array.
|
||||
* @max_count the max count to dequeue, 0 to dequeue all.
|
||||
*/
|
||||
virtual int get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count);
|
||||
/**
|
||||
* when client send the pause message.
|
||||
*/
|
||||
virtual int on_play_client_pause(bool is_pause);
|
||||
};
|
||||
|
||||
/**
|
||||
* cache a gop of video/audio data,
|
||||
* delivery at the connect of flash player,
|
||||
* to enable it to fast startup.
|
||||
*/
|
||||
class SrsGopCache
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* if disabled the gop cache,
|
||||
* the client will wait for the next keyframe for h264,
|
||||
* and will be black-screen.
|
||||
*/
|
||||
bool enable_gop_cache;
|
||||
/**
|
||||
* the video frame count, avoid cache for pure audio stream.
|
||||
*/
|
||||
int cached_video_count;
|
||||
/**
|
||||
* cached gop.
|
||||
*/
|
||||
std::vector<SrsSharedPtrMessage*> gop_cache;
|
||||
public:
|
||||
SrsGopCache();
|
||||
virtual ~SrsGopCache();
|
||||
public:
|
||||
virtual void set(bool enabled);
|
||||
/**
|
||||
* only for h264 codec
|
||||
* 1. cache the gop when got h264 video packet.
|
||||
* 2. clear gop when got keyframe.
|
||||
*/
|
||||
virtual int cache(SrsSharedPtrMessage* msg);
|
||||
virtual void clear();
|
||||
virtual int dump(SrsConsumer* consumer, int tba, int tbv);
|
||||
};
|
||||
|
||||
/**
|
||||
* live streaming source.
|
||||
*/
|
||||
class SrsSource : public ISrsReloadHandler
|
||||
{
|
||||
private:
|
||||
static std::map<std::string, SrsSource*> pool;
|
||||
public:
|
||||
/**
|
||||
* find stream by vhost/app/stream.
|
||||
* @param req the client request.
|
||||
* @return the matched source, never be NULL.
|
||||
* @remark stream_url should without port and schema.
|
||||
*/
|
||||
static SrsSource* find(SrsRequest* req);
|
||||
private:
|
||||
// deep copy of client request.
|
||||
SrsRequest* req;
|
||||
// to delivery stream to clients.
|
||||
std::vector<SrsConsumer*> consumers;
|
||||
// hls handler.
|
||||
#ifdef SRS_HLS
|
||||
SrsHls* hls;
|
||||
#endif
|
||||
// transcoding handler.
|
||||
#ifdef SRS_FFMPEG
|
||||
SrsEncoder* encoder;
|
||||
#endif
|
||||
// gop cache for client fast startup.
|
||||
SrsGopCache* gop_cache;
|
||||
// to forward stream to other servers
|
||||
std::vector<SrsForwarder*> forwarders;
|
||||
private:
|
||||
/**
|
||||
* the sample rate of audio in metadata.
|
||||
*/
|
||||
int sample_rate;
|
||||
/**
|
||||
* the video frame rate in metadata.
|
||||
*/
|
||||
int frame_rate;
|
||||
/**
|
||||
* can publish, true when is not streaming
|
||||
*/
|
||||
bool _can_publish;
|
||||
private:
|
||||
SrsSharedPtrMessage* cache_metadata;
|
||||
// the cached video sequence header.
|
||||
SrsSharedPtrMessage* cache_sh_video;
|
||||
// the cached audio sequence header.
|
||||
SrsSharedPtrMessage* cache_sh_audio;
|
||||
public:
|
||||
/**
|
||||
* @param _req the client request object,
|
||||
* this object will deep copy it for reload.
|
||||
*/
|
||||
SrsSource(SrsRequest* _req);
|
||||
virtual ~SrsSource();
|
||||
// interface ISrsReloadHandler
|
||||
public:
|
||||
virtual int on_reload_gop_cache(std::string vhost);
|
||||
virtual int on_reload_queue_length(std::string vhost);
|
||||
virtual int on_reload_forward(std::string vhost);
|
||||
virtual int on_reload_hls(std::string vhost);
|
||||
virtual int on_reload_transcode(std::string vhost);
|
||||
public:
|
||||
// for the SrsForwarder to callback to request the sequence headers.
|
||||
virtual int on_forwarder_start(SrsForwarder* forwarder);
|
||||
// for the SrsHls to callback to request the sequence headers.
|
||||
virtual int on_hls_start();
|
||||
public:
|
||||
virtual bool can_publish();
|
||||
virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);
|
||||
virtual int on_audio(SrsCommonMessage* audio);
|
||||
virtual int on_video(SrsCommonMessage* video);
|
||||
/**
|
||||
* publish stream event notify.
|
||||
* @param _req the request from client, the source will deep copy it,
|
||||
* for when reload the request of client maybe invalid.
|
||||
*/
|
||||
virtual int on_publish(SrsRequest* _req);
|
||||
virtual void on_unpublish();
|
||||
public:
|
||||
virtual int create_consumer(SrsConsumer*& consumer);
|
||||
virtual void on_consumer_destroy(SrsConsumer* consumer);
|
||||
virtual void set_cache(bool enabled);
|
||||
private:
|
||||
virtual int create_forwarders();
|
||||
virtual void destroy_forwarders();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,225 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_stream.hpp>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
SrsStream::SrsStream()
|
||||
{
|
||||
p = bytes = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
SrsStream::~SrsStream()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsStream::initialize(char* _bytes, int _size)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (!_bytes) {
|
||||
ret = ERROR_SYSTEM_STREAM_INIT;
|
||||
srs_error("stream param bytes must not be NULL. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (_size <= 0) {
|
||||
ret = ERROR_SYSTEM_STREAM_INIT;
|
||||
srs_error("stream param size must be positive. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
size = _size;
|
||||
p = bytes = _bytes;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsStream::reset()
|
||||
{
|
||||
p = bytes;
|
||||
}
|
||||
|
||||
bool SrsStream::empty()
|
||||
{
|
||||
return !p || !bytes || (p >= bytes + size);
|
||||
}
|
||||
|
||||
bool SrsStream::require(int required_size)
|
||||
{
|
||||
return !empty() && (required_size <= bytes + size - p);
|
||||
}
|
||||
|
||||
void SrsStream::skip(int size)
|
||||
{
|
||||
p += size;
|
||||
}
|
||||
|
||||
int SrsStream::pos()
|
||||
{
|
||||
if (empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return p - bytes;
|
||||
}
|
||||
|
||||
int SrsStream::left()
|
||||
{
|
||||
return size - pos();
|
||||
}
|
||||
|
||||
char* SrsStream::current()
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
int8_t SrsStream::read_1bytes()
|
||||
{
|
||||
srs_assert(require(1));
|
||||
|
||||
return (int8_t)*p++;
|
||||
}
|
||||
|
||||
int16_t SrsStream::read_2bytes()
|
||||
{
|
||||
srs_assert(require(2));
|
||||
|
||||
int16_t value;
|
||||
pp = (char*)&value;
|
||||
pp[1] = *p++;
|
||||
pp[0] = *p++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t SrsStream::read_3bytes()
|
||||
{
|
||||
srs_assert(require(3));
|
||||
|
||||
int32_t value = 0x00;
|
||||
pp = (char*)&value;
|
||||
pp[2] = *p++;
|
||||
pp[1] = *p++;
|
||||
pp[0] = *p++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t SrsStream::read_4bytes()
|
||||
{
|
||||
srs_assert(require(4));
|
||||
|
||||
int32_t value;
|
||||
pp = (char*)&value;
|
||||
pp[3] = *p++;
|
||||
pp[2] = *p++;
|
||||
pp[1] = *p++;
|
||||
pp[0] = *p++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int64_t SrsStream::read_8bytes()
|
||||
{
|
||||
srs_assert(require(8));
|
||||
|
||||
int64_t value;
|
||||
pp = (char*)&value;
|
||||
pp[7] = *p++;
|
||||
pp[6] = *p++;
|
||||
pp[5] = *p++;
|
||||
pp[4] = *p++;
|
||||
pp[3] = *p++;
|
||||
pp[2] = *p++;
|
||||
pp[1] = *p++;
|
||||
pp[0] = *p++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string SrsStream::read_string(int len)
|
||||
{
|
||||
srs_assert(require(len));
|
||||
|
||||
std::string value;
|
||||
value.append(p, len);
|
||||
|
||||
p += len;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void SrsStream::write_1bytes(int8_t value)
|
||||
{
|
||||
srs_assert(require(1));
|
||||
|
||||
*p++ = value;
|
||||
}
|
||||
|
||||
void SrsStream::write_2bytes(int16_t value)
|
||||
{
|
||||
srs_assert(require(2));
|
||||
|
||||
pp = (char*)&value;
|
||||
*p++ = pp[1];
|
||||
*p++ = pp[0];
|
||||
}
|
||||
|
||||
void SrsStream::write_4bytes(int32_t value)
|
||||
{
|
||||
srs_assert(require(4));
|
||||
|
||||
pp = (char*)&value;
|
||||
*p++ = pp[3];
|
||||
*p++ = pp[2];
|
||||
*p++ = pp[1];
|
||||
*p++ = pp[0];
|
||||
}
|
||||
|
||||
void SrsStream::write_8bytes(int64_t value)
|
||||
{
|
||||
srs_assert(require(8));
|
||||
|
||||
pp = (char*)&value;
|
||||
*p++ = pp[7];
|
||||
*p++ = pp[6];
|
||||
*p++ = pp[5];
|
||||
*p++ = pp[4];
|
||||
*p++ = pp[3];
|
||||
*p++ = pp[2];
|
||||
*p++ = pp[1];
|
||||
*p++ = pp[0];
|
||||
}
|
||||
|
||||
void SrsStream::write_string(std::string value)
|
||||
{
|
||||
srs_assert(require(value.length()));
|
||||
|
||||
memcpy(p, value.data(), value.length());
|
||||
p += value.length();
|
||||
}
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_STREAM_HPP
|
||||
#define SRS_CORE_STREAM_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_stream.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string>
|
||||
|
||||
class SrsStream
|
||||
{
|
||||
private:
|
||||
char* p;
|
||||
char* pp;
|
||||
char* bytes;
|
||||
int size;
|
||||
public:
|
||||
SrsStream();
|
||||
virtual ~SrsStream();
|
||||
public:
|
||||
/**
|
||||
* initialize the stream from bytes.
|
||||
* @_bytes, must not be NULL, or return error.
|
||||
* @_size, must be positive, or return error.
|
||||
* @remark, stream never free the _bytes, user must free it.
|
||||
*/
|
||||
virtual int initialize(char* _bytes, int _size);
|
||||
/**
|
||||
* reset the position to beginning.
|
||||
*/
|
||||
virtual void reset();
|
||||
/**
|
||||
* whether stream is empty.
|
||||
* if empty, never read or write.
|
||||
*/
|
||||
virtual bool empty();
|
||||
/**
|
||||
* whether required size is ok.
|
||||
* @return true if stream can read/write specified required_size bytes.
|
||||
*/
|
||||
virtual bool require(int required_size);
|
||||
/**
|
||||
* to skip some size.
|
||||
* @size can be any value. positive to forward; nagetive to backward.
|
||||
*/
|
||||
virtual void skip(int size);
|
||||
/**
|
||||
* tell the current pos.
|
||||
*/
|
||||
virtual int pos();
|
||||
/**
|
||||
* left size of bytes.
|
||||
*/
|
||||
virtual int left();
|
||||
virtual char* current();
|
||||
public:
|
||||
/**
|
||||
* get 1bytes char from stream.
|
||||
*/
|
||||
virtual int8_t read_1bytes();
|
||||
/**
|
||||
* get 2bytes int from stream.
|
||||
*/
|
||||
virtual int16_t read_2bytes();
|
||||
/**
|
||||
* get 3bytes int from stream.
|
||||
*/
|
||||
virtual int32_t read_3bytes();
|
||||
/**
|
||||
* get 4bytes int from stream.
|
||||
*/
|
||||
virtual int32_t read_4bytes();
|
||||
/**
|
||||
* get 8bytes int from stream.
|
||||
*/
|
||||
virtual int64_t read_8bytes();
|
||||
/**
|
||||
* get string from stream, length specifies by param len.
|
||||
*/
|
||||
virtual std::string read_string(int len);
|
||||
public:
|
||||
/**
|
||||
* write 1bytes char to stream.
|
||||
*/
|
||||
virtual void write_1bytes(int8_t value);
|
||||
/**
|
||||
* write 2bytes int to stream.
|
||||
*/
|
||||
virtual void write_2bytes(int16_t value);
|
||||
/**
|
||||
* write 4bytes int to stream.
|
||||
*/
|
||||
virtual void write_4bytes(int32_t value);
|
||||
/**
|
||||
* write 8bytes int to stream.
|
||||
*/
|
||||
virtual void write_8bytes(int64_t value);
|
||||
/**
|
||||
* write string to stream
|
||||
*/
|
||||
virtual void write_string(std::string value);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,161 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_core_thread.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_log.hpp>
|
||||
|
||||
ISrsThreadHandler::ISrsThreadHandler()
|
||||
{
|
||||
}
|
||||
|
||||
ISrsThreadHandler::~ISrsThreadHandler()
|
||||
{
|
||||
}
|
||||
|
||||
void ISrsThreadHandler::on_enter_loop()
|
||||
{
|
||||
}
|
||||
|
||||
int ISrsThreadHandler::on_before_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ISrsThreadHandler::on_end_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ISrsThreadHandler::on_leave_loop()
|
||||
{
|
||||
}
|
||||
|
||||
SrsThread::SrsThread(ISrsThreadHandler* thread_handler, int64_t interval_us)
|
||||
{
|
||||
handler = thread_handler;
|
||||
cycle_interval_us = interval_us;
|
||||
|
||||
tid = NULL;
|
||||
loop = false;
|
||||
}
|
||||
|
||||
SrsThread::~SrsThread()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
int SrsThread::start()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if(tid) {
|
||||
srs_info("thread already running.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if((tid = st_thread_create(thread_fun, this, 1, 0)) == NULL){
|
||||
ret = ERROR_ST_CREATE_CYCLE_THREAD;
|
||||
srs_error("st_thread_create failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SrsThread::stop()
|
||||
{
|
||||
if (tid) {
|
||||
loop = false;
|
||||
|
||||
// the interrupt will cause the socket to read/write error,
|
||||
// which will terminate the cycle thread.
|
||||
st_thread_interrupt(tid);
|
||||
|
||||
// wait the thread to exit.
|
||||
st_thread_join(tid, NULL);
|
||||
|
||||
tid = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool SrsThread::can_loop()
|
||||
{
|
||||
return loop;
|
||||
}
|
||||
|
||||
void SrsThread::thread_cycle()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
srs_assert(handler);
|
||||
|
||||
log_context->generate_id();
|
||||
srs_trace("thread cycle start");
|
||||
|
||||
handler->on_end_cycle();
|
||||
|
||||
loop = true;
|
||||
while (loop) {
|
||||
if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) {
|
||||
srs_warn("thread on before cycle failed, ignored and retry, ret=%d", ret);
|
||||
goto failed;
|
||||
}
|
||||
srs_info("thread on before cycle success");
|
||||
|
||||
if ((ret = handler->cycle()) != ERROR_SUCCESS) {
|
||||
srs_warn("thread cycle failed, ignored and retry, ret=%d", ret);
|
||||
goto failed;
|
||||
}
|
||||
srs_info("thread cycle success");
|
||||
|
||||
if ((ret = handler->on_end_cycle()) != ERROR_SUCCESS) {
|
||||
srs_warn("thread on end cycle failed, ignored and retry, ret=%d", ret);
|
||||
goto failed;
|
||||
}
|
||||
srs_info("thread on end cycle success");
|
||||
|
||||
failed:
|
||||
if (!loop) {
|
||||
break;
|
||||
}
|
||||
|
||||
st_usleep(cycle_interval_us);
|
||||
}
|
||||
|
||||
handler->on_leave_loop();
|
||||
srs_trace("thread cycle finished");
|
||||
}
|
||||
|
||||
void* SrsThread::thread_fun(void* arg)
|
||||
{
|
||||
SrsThread* obj = (SrsThread*)arg;
|
||||
srs_assert(obj);
|
||||
|
||||
obj->thread_cycle();
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
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_CORE_THREAD_HPP
|
||||
#define SRS_CORE_THREAD_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_thread.hpp>
|
||||
*/
|
||||
#include <srs_core.hpp>
|
||||
|
||||
/**
|
||||
* the handler for the thread, callback interface.
|
||||
* the thread model defines as:
|
||||
* handler->on_enter_loop()
|
||||
* while loop:
|
||||
* handler->on_before_cycle()
|
||||
* handler->cycle()
|
||||
* handler->on_end_cycle()
|
||||
* if !loop then break for user stop thread.
|
||||
* sleep(CycleIntervalMilliseconds)
|
||||
* handler->on_leave_loop()
|
||||
* when stop, the thread will interrupt the st_thread,
|
||||
* which will cause the socket to return error and
|
||||
* terminate the cycle thread.
|
||||
*
|
||||
* when thread interrupt, the socket maybe not got EINT,
|
||||
* espectially on st_usleep(), so the cycle must check the loop,
|
||||
* when handler->cycle() has loop itself, for example:
|
||||
* handler->cycle() is:
|
||||
* while (true):
|
||||
* st_usleep(0);
|
||||
* if (read_from_socket(skt) < 0) break;
|
||||
* if thread stop when read_from_socket, it's ok, the loop will break,
|
||||
* but when thread stop interrupt the s_usleep(0), then the loop is
|
||||
* death loop.
|
||||
* in a word, the handler->cycle() must:
|
||||
* handler->cycle() is:
|
||||
* while (pthread->can_loop()):
|
||||
* st_usleep(0);
|
||||
* if (read_from_socket(skt) < 0) break;
|
||||
* check the loop, then it works.
|
||||
*/
|
||||
class ISrsThreadHandler
|
||||
{
|
||||
public:
|
||||
ISrsThreadHandler();
|
||||
virtual ~ISrsThreadHandler();
|
||||
public:
|
||||
virtual void on_enter_loop();
|
||||
virtual int on_before_cycle();
|
||||
virtual int cycle() = 0;
|
||||
virtual int on_end_cycle();
|
||||
virtual void on_leave_loop();
|
||||
};
|
||||
|
||||
/**
|
||||
* provides servies from st_thread_t,
|
||||
* for common thread usage.
|
||||
*/
|
||||
class SrsThread
|
||||
{
|
||||
private:
|
||||
st_thread_t tid;
|
||||
bool loop;
|
||||
private:
|
||||
ISrsThreadHandler* handler;
|
||||
int64_t cycle_interval_us;
|
||||
public:
|
||||
/**
|
||||
* initialize the thread.
|
||||
* @param thread_handler, the cycle handler for the thread.
|
||||
* @param interval_us, the sleep interval when cycle finished.
|
||||
*/
|
||||
SrsThread(ISrsThreadHandler* thread_handler, int64_t interval_us);
|
||||
virtual ~SrsThread();
|
||||
public:
|
||||
/**
|
||||
* start the thread, invoke the cycle of handler util
|
||||
* user stop the thread.
|
||||
* @remark ignore any error of cycle of handler.
|
||||
* @remark user can start multiple times, ignore if already started.
|
||||
*/
|
||||
virtual int start();
|
||||
/**
|
||||
* stop the thread, wait for the thread to terminate.
|
||||
* @remark user can stop multiple times, ignore if already stopped.
|
||||
*/
|
||||
virtual void stop();
|
||||
/**
|
||||
* whether the thread should loop,
|
||||
* used for handler->cycle() which has a loop method,
|
||||
* to check this method, break if false.
|
||||
*/
|
||||
virtual bool can_loop();
|
||||
private:
|
||||
virtual void thread_cycle();
|
||||
static void* thread_fun(void* arg);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue