@ -2,11 +2,12 @@
# include <ctime>
# include <nlohmann/json.hpp>
# include <memory>
# include <sstream>
namespace mtx {
# include <json/json.h> // JsonCpp of dr_frucht (lib/include/json, lib/data/json)
using json = nlohmann : : json ;
namespace mtx {
namespace {
@ -24,70 +25,104 @@ std::string isoTime(int64_t us)
return tB ;
}
json pathConfJson ( const PathConf & p )
std : : string toText ( const Json : : Value & v )
{
Json : : StreamWriterBuilder tB ;
tB [ " indentation " ] = " " ;
return Json : : writeString ( tB , v ) ;
}
bool parseJson ( const std : : string & text , Json : : Value & out , std : : string & err )
{
return json { { " name " , p . name } , { " source " , p . source } , { " record " , p . record } , { " rtspTransport " , p . transport = = RtspTransport : : Udp ? " udp " : " tcp " } } ;
Json : : CharReaderBuilder tB ;
std : : unique_ptr < Json : : CharReader > tR ( tB . newCharReader ( ) ) ;
return tR - > parse ( text . data ( ) , text . data ( ) + text . size ( ) , & out , & err ) ;
}
json pathInfoJson ( const PathInfo & i )
Json : : Value pathConfJson ( const PathConf & p )
{
json j = { { " name " , i . name } ,
{ " confName " , i . name } ,
{ " source " , { { " type " , " rtspSource " } , { " id " , " " } } } ,
{ " ready " , i . ready } ,
{ " readyTime " , i . ready ? json ( isoTime ( i . readySinceUs ) ) : json ( nullptr ) } ,
{ " tracks " , i . ready ? json : : array ( { " H264 " } ) : json : : array ( ) } ,
{ " bytesReceived " , i . bytesReceived } ,
{ " record " , i . record } ,
{ " readers " , i . readers } } ;
Json : : Value j ( Json : : objectValue ) ;
j [ " name " ] = p . name ;
j [ " source " ] = p . source ;
j [ " record " ] = p . record ;
j [ " rtspTransport " ] = p . transport = = RtspTransport : : Udp ? " udp " : " tcp " ;
return j ;
}
Json : : Value pathInfoJson ( const PathInfo & i )
{
Json : : Value j ( Json : : objectValue ) ;
j [ " name " ] = i . name ;
j [ " confName " ] = i . name ;
j [ " source " ] [ " type " ] = " rtspSource " ;
j [ " source " ] [ " id " ] = " " ;
j [ " ready " ] = i . ready ;
j [ " readyTime " ] = i . ready ? Json : : Value ( isoTime ( i . readySinceUs ) ) : Json : : Value ( Json : : nullValue ) ;
j [ " tracks " ] = Json : : Value ( Json : : arrayValue ) ;
if ( i . ready )
{
j [ " tracks " ] . append ( " H264 " ) ;
j [ " width " ] = i . desc . width ;
j [ " height " ] = i . desc . height ;
}
j [ " bytesReceived " ] = ( Json : : UInt64 ) i . bytesReceived ;
j [ " record " ] = i . record ;
j [ " readers " ] = ( Json : : UInt64 ) i . readers ;
return j ;
}
Json : : Value listJson ( const Json : : Value & items )
{
Json : : Value j ( Json : : objectValue ) ;
j [ " itemCount " ] = items . size ( ) ;
j [ " pageCount " ] = 1 ;
j [ " items " ] = items ;
return j ;
}
void error ( HttpResponse & res , int status , const std : : string & msg )
{
Json : : Value j ( Json : : objectValue ) ;
j [ " error " ] = msg ;
res . status = status ;
res . setHeader ( " Content-Type " , " application/json " ) ;
res . body = json { { " error " , msg } } . dump ( ) ;
res . body = toText ( j ) ;
}
void ok ( HttpResponse & res , const json & body )
void ok ( HttpResponse & res , const Json : : Value & body )
{
res . status = 200 ;
res . setHeader ( " Content-Type " , " application/json " ) ;
res . body = body . dump ( ) ;
res . body = toText ( body ) ;
}
// fields of a path configuration from JSON into pc
bool applyFields ( const json & j , PathConf & pc , std : : string & err )
bool applyFields ( const Json : : Value & j , PathConf & pc , std : : string & err )
{
if ( ! j . is_o bject ( ) )
if ( ! j . isO bject ( ) )
{
err = " body must be a JSON object " ;
return false ;
}
for ( auto it = j . begin ( ) ; it ! = j . end ( ) ; + + it )
for ( const std : : string & k : j . getMemberNames ( ) )
{
if ( it . key ( ) = = " source " & & it - > is_string ( ) )
const Json : : Value & v = j [ k ] ;
if ( k = = " source " & & v . isString ( ) )
{
pc . source = it - > get < std : : string > ( ) ;
pc . source = v . asString ( ) ;
}
else if ( it . key ( ) = = " record " & & it - > is_boolean ( ) )
else if ( k = = " record " & & v . isBool ( ) )
{
pc . record = it - > get < bool > ( ) ;
pc . record = v . asBool ( ) ;
}
else if ( it . key ( ) = = " rtspTransport " & & it - > is_s tring( ) )
else if ( k = = " rtspTransport " & & v . isS tring( ) )
{
std : : string v = it - > get < std : : string > ( ) ;
if ( v = = " tcp " | | v = = " automatic " )
std : : string t = v . asString ( ) ;
if ( t = = " tcp " | | t = = " automatic " )
{
pc . transport = RtspTransport : : Tcp ;
}
else if ( v = = " udp " )
else if ( t = = " udp " )
{
pc . transport = RtspTransport : : Udp ;
}
@ -97,13 +132,13 @@ bool applyFields(const json & j, PathConf & pc, std::string & err)
return false ;
}
}
else if ( it . key ( ) = = " name " )
else if ( k = = " name " )
{
continue ;
}
else
{
err = " unsupported field ' " + it . key ( ) + " ' " ;
err = " unsupported field ' " + k + " ' " ;
return false ;
}
}
@ -146,12 +181,12 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
if ( req . method = = " GET " & & p = = " /v3/paths/list " )
{
json tItems = json : : array ( ) ;
Json : : Value tItems ( Json : : arrayValue ) ;
for ( const PathInfo & i : m_pPaths - > list ( ) )
{
tItems . push_back ( pathInfoJson ( i ) ) ;
tItems . append ( pathInfoJson ( i ) ) ;
}
ok ( res , json { { " itemCount " , tItems . size ( ) } , { " pageCount " , 1 } , { " items " , tItems } } ) ;
ok ( res , listJson ( tItems ) ) ;
return ;
}
if ( req . method = = " GET " & & starts ( " /v3/paths/get/ " ) )
@ -167,12 +202,12 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
}
if ( req . method = = " GET " & & p = = " /v3/config/paths/list " )
{
json tItems = json : : array ( ) ;
Json : : Value tItems ( Json : : arrayValue ) ;
for ( const PathConf & c : m_pPaths - > confs ( ) )
{
tItems . push_back ( pathConfJson ( c ) ) ;
tItems . append ( pathConfJson ( c ) ) ;
}
ok ( res , json { { " itemCount " , tItems . size ( ) } , { " pageCount " , 1 } , { " items " , tItems } } ) ;
ok ( res , listJson ( tItems ) ) ;
return ;
}
if ( req . method = = " GET " & & starts ( " /v3/config/paths/get/ " ) )
@ -222,16 +257,13 @@ void Api::handle(const HttpRequest & req, HttpResponse & res)
}
std : : vector < PathConf > tConfs = m_pPaths - > confs ( ) ;
auto tIt = std : : find_if ( tConfs . begin ( ) , tConfs . end ( ) , [ & tName ] ( const PathConf & c ) { return c . name = = tName ; } ) ;
json tBody ;
if ( tOp ! = DEL )
Json : : Value tBody ( Json : : objectValue ) ;
if ( tOp ! = DEL & & ! req . body . empty ( ) )
{
try
{
tBody = json : : parse ( req . body . empty ( ) ? std : : string ( " {} " ) : req . body ) ;
}
catch ( const std : : exception & e )
std : : string tParseErr ;
if ( ! parseJson ( req . body , tBody , tParseErr ) )
{
error ( res , 400 , std : : string ( " invalid JSON: " ) + e . what ( ) ) ;
error ( res , 400 , " invalid JSON: " + tParseErr ) ;
return ;
}
}