Skip to content

Commit

Permalink
add rtsp-server-option/rtsp-server-get/set-pararmter
Browse files Browse the repository at this point in the history
  • Loading branch information
ireader committed Dec 22, 2018
1 parent 6f09d20 commit cd5e87d
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 68 deletions.
30 changes: 30 additions & 0 deletions librtsp/include/rtsp-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ struct rtsp_handler_t
/// @param[in] scale request scale, NULL if don't have Scale parameter
/// @return 0-ok, other-error code
int (*onrecord)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const int64_t *npt, const double *scale);

/// RTSP OPTIONS request
/// @param[in] uri default is '*'
/// @return 0-ok, other-error code
int (*onoptions)(void* ptr, rtsp_server_t* rtsp, const char* uri);

/// RTSP GET_PARAMETER/SET_PARAMETER request
/// use rtsp_server_get_header("content-type") to if need
/// @param[in] uri the presentation/stream uri
/// @param[in] session RTSP session
/// @param[in] content paramter(s), NULL for test client or server liveness ("ping")
/// @return 0-ok, other-error code
int (*ongetparameter)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const void* content, int bytes);
int (*onsetparameter)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const void* content, int bytes);
};

/// create (reuse-able) rtsp server
Expand Down Expand Up @@ -147,6 +161,22 @@ int rtsp_server_reply_announce(rtsp_server_t* rtsp, int code);
/// @return 0-ok, other-error code
int rtsp_server_reply_record(rtsp_server_t* rtsp, int code, const int64_t *nptstart, const int64_t *nptend);

/// RTSP OPTIONS reply
/// @return 0-ok, other-error code
int rtsp_server_reply_options(rtsp_server_t* rtsp, int code);

/// RTSP GET_PARAMETER reply(content-type/content-encoding/content-language copy from request header)
/// @param[in] rtsp request handle
/// @param[in] code RTSP status-code(200-OK...)
/// @return 0-ok, other-error code
int rtsp_server_reply_get_parameter(rtsp_server_t* rtsp, int code, const void* content, int bytes);

/// RTSP SET_PARAMETER reply
/// @param[in] rtsp request handle
/// @param[in] code RTSP status-code(200-OK...)
/// @return 0-ok, other-error code
int rtsp_server_reply_set_parameter(rtsp_server_t* rtsp, int code);

/// RTSP send Embedded (Interleaved) Binary Data
/// @param[in] rtsp request handle
/// @param[in] data interleaved binary data, start with 1-byte $ + 1-byte CHANNEL + 2-bytes LEN + RTP/RTCP HEADER + PAYLOAD
Expand Down
1 change: 1 addition & 0 deletions librtsp/librtsp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<ClCompile Include="source\server\rtsp-server-describe.c" />
<ClCompile Include="source\server\rtsp-server-handler.c" />
<ClCompile Include="source\server\rtsp-server-options.c" />
<ClCompile Include="source\server\rtsp-server-parameter.c" />
<ClCompile Include="source\server\rtsp-server-pause.c" />
<ClCompile Include="source\server\rtsp-server-play.c" />
<ClCompile Include="source\server\rtsp-server-record.c" />
Expand Down
3 changes: 3 additions & 0 deletions librtsp/librtsp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<ClCompile Include="source\rtsp-media.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\server\rtsp-server-parameter.c">
<Filter>Source Files\server</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\rtsp-header-range.h">
Expand Down
15 changes: 15 additions & 0 deletions librtsp/source/server/rtsp-server-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ int rtsp_server_handle(struct rtsp_server_t *rtsp)
int major, minor;
const char* uri;
const char* method;
const char* session;

http_get_version(rtsp->parser, protocol, &major, &minor);
if (1 != major && 0 != minor)
Expand All @@ -20,6 +21,12 @@ int rtsp_server_handle(struct rtsp_server_t *rtsp)
return rtsp_server_reply(rtsp, 400);
}

// parse session
rtsp->session.session[0] = 0; // clear session value
session = http_get_header_by_name(rtsp->parser, "Session");
if (session)
rtsp_header_session(session, &rtsp->session);

uri = http_get_request_uri(rtsp->parser);
method = http_get_request_method(rtsp->parser);

Expand All @@ -37,10 +44,18 @@ int rtsp_server_handle(struct rtsp_server_t *rtsp)
return rtsp_server_describe(rtsp, uri);
break;

case 'g':
case 'G':
if (0 == strcasecmp("GET_PARAMETER", method) && rtsp->handler.ongetparameter)
return rtsp_server_get_parameter(rtsp, uri);
break;

case 's':
case 'S':
if (0 == strcasecmp("SETUP", method) && rtsp->handler.onsetup)
return rtsp_server_setup(rtsp, uri);
else if (0 == strcasecmp("SET_PARAMETER", method) && rtsp->handler.onsetparameter)
return rtsp_server_set_parameter(rtsp, uri);
break;

case 'p':
Expand Down
4 changes: 3 additions & 1 deletion librtsp/source/server/rtsp-server-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ int rtsp_server_setup(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_play(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_pause(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_teardown(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_get_parameter(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_set_parameter(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_record(struct rtsp_server_t *rtsp, const char* uri);
int rtsp_server_reply(struct rtsp_server_t *rtsp, int code);
int rtsp_server_reply2(struct rtsp_server_t *rtsp, int code, const char* header);
int rtsp_server_reply2(struct rtsp_server_t *rtsp, int code, const char* header, const void* data, int bytes);

#endif /* !_rtsp_server_internal_h_ */
16 changes: 14 additions & 2 deletions librtsp/source/server/rtsp-server-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
// RFC 2326 10.1 OPTIONS (p30)
int rtsp_server_options(struct rtsp_server_t* rtsp, const char* uri)
{
(void)uri;
return rtsp_server_reply2(rtsp, 200, "Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE,ANNOUNCE,RECORD\r\n");
http_get_header_by_name(rtsp->parser, "Connection");
http_get_header_by_name(rtsp->parser, "Require");
http_get_header_by_name(rtsp->parser, "Proxy-Require");
http_get_header_by_name(rtsp->parser, "Proxy-Authenticate");

if (rtsp->handler.onoptions)
return rtsp->handler.onoptions(rtsp->param, rtsp, uri);
else
return rtsp_server_reply2(rtsp, 200, "Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE,ANNOUNCE,RECORD,GET_PARAMETER,SET_PARAMETER\r\n", NULL, 0);
}

int rtsp_server_reply_options(rtsp_server_t* rtsp, int code)
{
return rtsp_server_reply(rtsp, code);
}
49 changes: 49 additions & 0 deletions librtsp/source/server/rtsp-server-parameter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "rtsp-server-internal.h"

int rtsp_server_get_parameter(struct rtsp_server_t *rtsp, const char* uri)
{
int bytes;
const void *content;
content = http_get_content(rtsp->parser);
bytes = http_get_content_length(rtsp->parser);
return rtsp->handler.ongetparameter(rtsp->param, rtsp, uri, rtsp->session.session[0] ? rtsp->session.session : NULL, content, bytes);
}

int rtsp_server_set_parameter(struct rtsp_server_t *rtsp, const char* uri)
{
int bytes;
const void *content;
content = http_get_content(rtsp->parser);
bytes = http_get_content_length(rtsp->parser);
return rtsp->handler.onsetparameter(rtsp->param, rtsp, uri, rtsp->session.session[0] ? rtsp->session.session : NULL, content, bytes);
}

int rtsp_server_reply_get_parameter(struct rtsp_server_t *rtsp, int code, const void* content, int bytes)
{
static const char* headers[] = { "Content-Type", "Content-Encoding", "Content-Language" };

int i, len;
char buffer[1024];
const char* value;

// copy headers
for (len = i = 0; i < sizeof(headers) / sizeof(headers[0]); i++)
{
value = http_get_header_by_name(rtsp->parser, headers[i]);
if (value)
len += snprintf(buffer + len, sizeof(buffer) - len, "%s: %s\r\n", headers[i], value);
}

if (len < 0 || len >= sizeof(buffer))
{
assert(0); // headers too long
return -1;
}

return rtsp_server_reply2(rtsp, code, buffer, content, bytes);
}

int rtsp_server_reply_set_parameter(struct rtsp_server_t *rtsp, int code)
{
return rtsp_server_reply(rtsp, code);
}
14 changes: 4 additions & 10 deletions librtsp/source/server/rtsp-server-pause.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
int rtsp_server_pause(struct rtsp_server_t* rtsp, const char* uri)
{
int64_t npt = -1L;
const char *prange, *psession;
const char *prange;
struct rtsp_header_range_t range;

prange = http_get_header_by_name(rtsp->parser, "range");
psession = http_get_header_by_name(rtsp->parser, "Session");

rtsp->session.session[0] = 0; // clear session value
if (!psession || 0 != rtsp_header_session(psession, &rtsp->session))
if (0 == rtsp->session.session[0])
{
// 454 Session Not Found
return rtsp_server_reply(rtsp, 454);
}

prange = http_get_header_by_name(rtsp->parser, "range");
if (prange && 0 == rtsp_header_range(prange, &range))
{
npt = range.from;
Expand All @@ -38,8 +35,5 @@ int rtsp_server_pause(struct rtsp_server_t* rtsp, const char* uri)

int rtsp_server_reply_pause(struct rtsp_server_t *rtsp, int code)
{
char header[256] = { 0 };
if(rtsp->session.session[0])
snprintf(header, sizeof(header), "Session: %s\r\n", rtsp->session.session);
return rtsp_server_reply2(rtsp, code, header);
return rtsp_server_reply(rtsp, code);
}
25 changes: 12 additions & 13 deletions librtsp/source/server/rtsp-server-play.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ int rtsp_server_play(struct rtsp_server_t *rtsp, const char* uri)
{
int64_t npt = -1L;
double scale = 0.0f;
const char *pscale, *prange, *psession;
const char *pscale, *prange;
struct rtsp_header_range_t range;

pscale = http_get_header_by_name(rtsp->parser, "scale");
prange = http_get_header_by_name(rtsp->parser, "range");
psession = http_get_header_by_name(rtsp->parser, "Session");

rtsp->session.session[0] = 0; // clear session value
if (!psession || 0 != rtsp_header_session(psession, &rtsp->session))
if (0 == rtsp->session.session[0])
{
// 454 (Session Not Found)
return rtsp_server_reply(rtsp, 454);
Expand All @@ -35,26 +33,27 @@ int rtsp_server_play(struct rtsp_server_t *rtsp, const char* uri)

int rtsp_server_reply_play(struct rtsp_server_t *rtsp, int code, const int64_t *nptstart, const int64_t *nptend, const char* rtp)
{
int len = 0;
int n = 0;
char header[1024] = { 0 };

if (nptstart)
if (n >= 0 && nptstart)
{
if (nptend)
len += snprintf(header + len, sizeof(header) - len, "Range: npt=%.3f-%.3f\r\n", (float)(*nptstart / 1000.0f), (float)(*nptend / 1000.0f));
n += snprintf(header + n, sizeof(header) - n, "Range: npt=%.3f-%.3f\r\n", (float)(*nptstart / 1000.0f), (float)(*nptend / 1000.0f));
else
len += snprintf(header + len, sizeof(header) - len, "Range: npt=%.3f-\r\n", (float)(*nptstart / 1000.0f));
n += snprintf(header + n, sizeof(header) - n, "Range: npt=%.3f-\r\n", (float)(*nptstart / 1000.0f));
}

if (rtsp->session.session[0])
if (n >= 0 && rtp)
{
len += snprintf(header + len, sizeof(header) - len, "Session: %s\r\n", rtsp->session.session);
n += snprintf(header + n, sizeof(header) - n, "RTP-Info: %s\r\n", rtp);
}

if (rtp)
if (n < 0 || n >= sizeof(header))
{
len += snprintf(header + len, sizeof(header) - len, "RTP-Info: %s\r\n", rtp);
assert(0); // rtp-info too long
return -1;
}

return rtsp_server_reply2(rtsp, code, header);
return rtsp_server_reply2(rtsp, code, header, NULL, 0);
}
17 changes: 5 additions & 12 deletions librtsp/source/server/rtsp-server-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ int rtsp_server_record(struct rtsp_server_t *rtsp, const char* uri)
{
int64_t npt = -1L;
double scale = 0.0f;
const char *pscale, *prange, *psession;
const char *pscale, *prange;
struct rtsp_header_range_t range;

pscale = http_get_header_by_name(rtsp->parser, "scale");
prange = http_get_header_by_name(rtsp->parser, "range");
psession = http_get_header_by_name(rtsp->parser, "Session");

rtsp->session.session[0] = 0; // clear session value
if (!psession || 0 != rtsp_header_session(psession, &rtsp->session))

if (0 == rtsp->session.session[0])
{
// 454 (Session Not Found)
return rtsp_server_reply(rtsp, 454);
Expand All @@ -36,7 +34,7 @@ int rtsp_server_record(struct rtsp_server_t *rtsp, const char* uri)
int rtsp_server_reply_record(struct rtsp_server_t *rtsp, int code, const int64_t *nptstart, const int64_t *nptend)
{
int len = 0;
char header[1024] = { 0 };
char header[128] = { 0 };

if (nptstart)
{
Expand All @@ -46,10 +44,5 @@ int rtsp_server_reply_record(struct rtsp_server_t *rtsp, int code, const int64_t
len += snprintf(header + len, sizeof(header) - len, "Range: npt=%.3f-\r\n", (float)(*nptstart / 1000.0f));
}

if (rtsp->session.session[0])
{
len += snprintf(header + len, sizeof(header) - len, "Session: %s\r\n", rtsp->session.session);
}

return rtsp_server_reply2(rtsp, code, header);
return rtsp_server_reply2(rtsp, code, header, NULL, 0);
}
34 changes: 17 additions & 17 deletions librtsp/source/server/rtsp-server-setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@ static int rtsp_header_transport_ex(const char* value, struct rtsp_header_transp
int rtsp_server_setup(struct rtsp_server_t* rtsp, const char* uri)
{
size_t n;
const char *psession, *ptransport;
struct rtsp_header_session_t session;
const char *ptransport;
struct rtsp_header_transport_t transport[16];

psession = http_get_header_by_name(rtsp->parser, "Session");
ptransport = http_get_header_by_name(rtsp->parser, "Transport");

memset(transport, 0, sizeof(transport));
n = sizeof(transport) / sizeof(transport[0]);

ptransport = http_get_header_by_name(rtsp->parser, "Transport");
if (!ptransport || 0 != rtsp_header_transport_ex(ptransport, transport, &n) || 0 == n)
{
// 461 Unsupported Transport
return rtsp_server_reply(rtsp, 461);
}

assert(n > 0);
if (psession && 0 == rtsp_header_session(psession, &session))
{
return rtsp->handler.onsetup(rtsp->param, rtsp, uri, session.session, transport, n);
}
else
{
return rtsp->handler.onsetup(rtsp->param, rtsp, uri, NULL, transport, n);
}
return rtsp->handler.onsetup(rtsp->param, rtsp, uri, rtsp->session.session[0] ? rtsp->session.session : NULL, transport, n);
}

int rtsp_server_reply_setup(struct rtsp_server_t *rtsp, int code, const char* sessionid, const char* transport)
{
int len;
int n;
char header[1024];

// save session-id
n = snprintf(rtsp->session.session, sizeof(rtsp->session.session), "%s", sessionid ? sessionid : "");
if (n < 0 || n >= sizeof(rtsp->session.session))
{
assert(0); // sessionid too long
return -1;
}

// RTP/AVP;unicast;client_port=4588-4589;server_port=6256-6257
len = snprintf(header, sizeof(header), "Transport: %s\r\nSession: %s\r\n", transport ? transport : "", sessionid ? sessionid : "");
if (len < 0 || len == sizeof(header))
n = snprintf(header, sizeof(header), "Transport: %s\r\n", transport ? transport : "");
if (n < 0 || n >= sizeof(header))
{
assert(0); // transport or sessionid too long
return -1;
}
return rtsp_server_reply2(rtsp, code, header);

return rtsp_server_reply2(rtsp, code, header, NULL, 0);
}
8 changes: 2 additions & 6 deletions librtsp/source/server/rtsp-server-teardown.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

int rtsp_server_teardown(struct rtsp_server_t *rtsp, const char* uri)
{
const char *psession;
struct rtsp_header_session_t session;

psession = http_get_header_by_name(rtsp->parser, "Session");
if (!psession || 0 != rtsp_header_session(psession, &session))
if (0 == rtsp->session.session[0])
{
// 454 (Session Not Found)
return rtsp_server_reply(rtsp, 454);
}

return rtsp->handler.onteardown(rtsp->param, rtsp, uri, session.session);
return rtsp->handler.onteardown(rtsp->param, rtsp, uri, rtsp->session.session);
}

int rtsp_server_reply_teardown(struct rtsp_server_t *rtsp, int code)
Expand Down
Loading

0 comments on commit cd5e87d

Please sign in to comment.