diff --git a/lib/Makefile.am b/lib/Makefile.am index 9d29bc18..55364d05 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES=libmist-1.0.la -libmist_1_0_la_SOURCES=amf.h amf.cpp auth.h auth.cpp base64.h base64.cpp config.h config.cpp crypto.h crypto.cpp dtsc.h dtsc.cpp flv_tag.h flv_tag.cpp http_parser.h http_parser.cpp json.h json.cpp procs.h procs.cpp rtmpchunks.h rtmpchunks.cpp socket.h socket.cpp mp4.h mp4.cpp +libmist_1_0_la_SOURCES=amf.h amf.cpp auth.h auth.cpp base64.h base64.cpp config.h config.cpp crypto.h crypto.cpp dtsc.h dtsc.cpp flv_tag.h flv_tag.cpp http_parser.h http_parser.cpp json.h json.cpp procs.h procs.cpp rtmpchunks.h rtmpchunks.cpp socket.h socket.cpp mp4.h mp4.cpp stream.h stream.cpp libmist_1_0_la_LIBADD=-lssl -lcrypto libmist_1_0_la_LDFLAGS = -version-info 1:0:0 @@ -7,4 +7,4 @@ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = mist-1.0.pc library_includedir=$(includedir)/mist-1.0/mist -library_include_HEADERS = amf.h auth.h base64.h config.h crypto.h dtsc.h flv_tag.h http_parser.h json.h procs.h rtmpchunks.h socket.h mp4.h +library_include_HEADERS = amf.h auth.h base64.h config.h crypto.h dtsc.h flv_tag.h http_parser.h json.h procs.h rtmpchunks.h socket.h mp4.h stream.h diff --git a/lib/socket.cpp b/lib/socket.cpp index 424460d4..55c60d5b 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -609,43 +609,3 @@ bool Socket::Server::connected() const{ /// Returns internal socket number. int Socket::Server::getSocket(){return sock;} - -/// Connect to a stream on the system. -/// Filters the streamname, removing invalid characters and -/// converting all letters to lowercase. -/// If a '?' character is found, everything following that character is deleted. -Socket::Connection Socket::getStream(std::string streamname){ - //strip anything that isn't a number, alpha or underscore - for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){ - if (*i == '?'){streamname.erase(i, streamname.end()); break;} - if (!isalpha(*i) && !isdigit(*i) && *i != '_'){ - streamname.erase(i); - }else{ - *i=tolower(*i); - } - } - return Socket::Connection("/tmp/mist/stream_"+streamname); -} - -/// Create a stream on the system. -/// Filters the streamname, removing invalid characters and -/// converting all letters to lowercase. -/// If a '?' character is found, everything following that character is deleted. -/// If the /tmp/mist directory doesn't exist yet, this will create it. -Socket::Server Socket::makeStream(std::string streamname){ - //strip anything that isn't numbers, digits or underscores - for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){ - if (*i == '?'){streamname.erase(i, streamname.end()); break;} - if (!isalpha(*i) && !isdigit(*i) && *i != '_'){ - streamname.erase(i); - }else{ - *i=tolower(*i); - } - } - std::string loc = "/tmp/mist/stream_"+streamname; - //attempt to create the /tmp/mist directory if it doesn't exist already. - //ignore errors - we catch all problems in the Socket::Server creation already - mkdir("/tmp/mist", S_IRWXU | S_IRWXG | S_IRWXO); - //create and return the Socket::Server - return Socket::Server(loc); -} diff --git a/lib/socket.h b/lib/socket.h index e4f2e69a..bef03fca 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -85,11 +85,5 @@ namespace Socket{ void close(); ///< Close connection. int getSocket(); ///< Returns internal socket number. }; - - /// Connect to a stream on the system. - Connection getStream(std::string streamname); - - /// Create a stream on the system. - Server makeStream(std::string streamname); }; diff --git a/lib/stream.cpp b/lib/stream.cpp new file mode 100644 index 00000000..2e5072a3 --- /dev/null +++ b/lib/stream.cpp @@ -0,0 +1,62 @@ +/// \file stream.cpp +/// Utilities for handling streams. + +#include "stream.h" +#include "procs.h" +#include "socket.h" + +/// Filters the streamname, removing invalid characters and converting all +/// letters to lowercase. If a '?' character is found, everything following +/// that character is deleted. The original string is modified. +void Util::Stream::sanitizeName(std::string & streamname){ + //strip anything that isn't numbers, digits or underscores + for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){ + if (*i == '?'){streamname.erase(i, streamname.end()); break;} + if (!isalpha(*i) && !isdigit(*i) && *i != '_'){ + streamname.erase(i); + }else{ + *i=tolower(*i); + } + } +} + +Socket::Connection Util::Stream::getLive(std::string streamname){ + sanitizeName(streamname); + return Socket::Connection("/tmp/mist/stream_"+streamname); +} + +/// Starts a process for the VoD stream. +Socket::Connection Util::Stream::getVod(std::string streamname){ + sanitizeName(streamname); + std::string filename = "/tmp/mist/vod_" + streamname; + /// \todo Is the name unique enough? + std::string name = "MistPlayer " + filename; + const char *argv[] = { "MistPlayer", filename.c_str(), NULL }; + int fdin = -1, fdout = -1; + Util::Procs::StartPiped(name, (char **)argv, &fdin, &fdout, 0); + // if StartPiped fails then fdin and fdout will be unmodified (-1) + return Socket::Connection(fdin, fdout); +} + +/// Probe for available streams. Currently first VoD, then Live. +Socket::Connection Util::Stream::getStream(std::string streamname){ + Socket::Connection vod = getVod(streamname); + if (vod.connected()){ + return vod; + } + return getLive(streamname); +} +/// Create a stream on the system. +/// Filters the streamname, removing invalid characters and +/// converting all letters to lowercase. +/// If a '?' character is found, everything following that character is deleted. +/// If the /tmp/mist directory doesn't exist yet, this will create it. +Socket::Server Util::Stream::makeLive(std::string streamname){ + sanitizeName(streamname); + std::string loc = "/tmp/mist/stream_"+streamname; + //attempt to create the /tmp/mist directory if it doesn't exist already. + //ignore errors - we catch all problems in the Socket::Server creation already + mkdir("/tmp/mist", S_IRWXU | S_IRWXG | S_IRWXO); + //create and return the Socket::Server + return Socket::Server(loc); +} diff --git a/lib/stream.h b/lib/stream.h new file mode 100644 index 00000000..e970c848 --- /dev/null +++ b/lib/stream.h @@ -0,0 +1,23 @@ +/// \file stream.h +/// Utilities for handling streams. + +#pragma once +#include +#include + +namespace Util{ + class Stream{ + /// Sanitize a streamname. + void sanitizeName(std::string & streamname); + public: + /// Get a connection to a Live stream. + static Socket::Connection getLive(std::string streamname); + /// Get a connection to a VoD stream. + static Socket::Connection getVod(std::string streamname); + /// Probe for available streams. Currently first VoD, then Live. + static Socket::Connection getStream(std::string streamname); + + /// Create a Live stream on the system. + static Socket::Server makeLive(std::string streamname); + }; +}