1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for #133, create rtsp framework.

This commit is contained in:
winlin 2015-02-16 14:05:01 +08:00
parent e81e090239
commit c0e50265bd
11 changed files with 348 additions and 26 deletions

View file

@ -23,7 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_rtsp.hpp>
#include <algorithm>
using namespace std;
#include <srs_app_config.hpp>
#include <srs_kernel_error.hpp>
#include <srs_rtsp_stack.hpp>
#include <srs_app_st_socket.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_utility.hpp>
#ifdef SRS_AUTO_STREAM_CASTER
@ -35,13 +43,115 @@ ISrsRtspHandler::~ISrsRtspHandler()
{
}
SrsRtspConn::SrsRtspConn(SrsConfDirective* c)
SrsRtspConn::SrsRtspConn(SrsRtspCaster* c, st_netfd_t fd, std::string o)
{
output = _srs_config->get_stream_caster_output(c);
output = o;
caster = c;
stfd = fd;
skt = new SrsStSocket(fd);
rtsp = new SrsRtspStack(skt);
trd = new SrsThread("rtsp", this, 0, false);
}
SrsRtspConn::~SrsRtspConn()
{
srs_close_stfd(stfd);
trd->stop();
srs_freep(trd);
srs_freep(skt);
srs_freep(rtsp);
}
int SrsRtspConn::serve()
{
return trd->start();
}
int SrsRtspConn::do_cycle()
{
int ret = ERROR_SUCCESS;
// retrieve ip of client.
std::string ip = srs_get_peer_ip(st_netfd_fileno(stfd));
srs_trace("rtsp: serve %s", ip.c_str());
return ret;
}
int SrsRtspConn::cycle()
{
// serve the rtsp client.
int ret = do_cycle();
// if socket io error, set to closed.
if (srs_is_client_gracefully_close(ret)) {
ret = ERROR_SOCKET_CLOSED;
}
// success.
if (ret == ERROR_SUCCESS) {
srs_trace("client finished.");
}
// client close peer.
if (ret == ERROR_SOCKET_CLOSED) {
srs_warn("client disconnect peer. ret=%d", ret);
}
// terminate thread in the thread cycle itself.
trd->stop_loop();
return ERROR_SUCCESS;
}
void SrsRtspConn::on_thread_stop()
{
caster->remove(this);
}
SrsRtspCaster::SrsRtspCaster(SrsConfDirective* c)
{
// TODO: FIXME: support reload.
output = _srs_config->get_stream_caster_output(c);
}
SrsRtspCaster::~SrsRtspCaster()
{
std::vector<SrsRtspConn*>::iterator it;
for (it = clients.begin(); it != clients.end(); ++it) {
SrsRtspConn* conn = *it;
srs_freep(conn);
}
clients.clear();
}
int SrsRtspCaster::serve_client(st_netfd_t stfd)
{
int ret = ERROR_SUCCESS;
SrsRtspConn* conn = new SrsRtspConn(this, stfd, output);
if ((ret = conn->serve()) != ERROR_SUCCESS) {
srs_error("rtsp: serve client failed. ret=%d", ret);
srs_freep(conn);
return ret;
}
clients.push_back(conn);
srs_info("rtsp: start thread to serve client.");
return ret;
}
void SrsRtspCaster::remove(SrsRtspConn* conn)
{
std::vector<SrsRtspConn*>::iterator it = find(clients.begin(), clients.end(), conn);
if (it != clients.end()) {
clients.erase(it);
}
srs_info("rtsp: remove connection from caster.");
srs_freep(conn);
}
#endif