mirror of
https://github.com/albfan/miraclecast.git
synced 2025-03-09 23:38:56 +00:00
rtsp: add rtsp bus
WifiDisplay uses RTSP for stream-setups. This adds a basic rtsp-bus implementations that we can use for sinks and sources. Note that the implementation is optimized for usability, not speed. RTSP is used for control-data, not streaming-data so there's no need to over-optimize it. In case inlined RTP is used, we still provide proper speed, even though that's usually not used by WifiDisplay implementations (due to the TCP requirement). Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
This commit is contained in:
parent
0d5b431367
commit
f6e434e86a
5 changed files with 4224 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -29,6 +29,7 @@ miraclectl
|
|||
miracled
|
||||
stamp-h1
|
||||
test-suite.log
|
||||
test_rtsp
|
||||
test_valgrind
|
||||
test_wpas
|
||||
wpa_cli
|
||||
|
|
|
@ -72,6 +72,8 @@ AM_LIBADD = \
|
|||
noinst_LTLIBRARIES += libmiracle-shared.la
|
||||
|
||||
libmiracle_shared_la_SOURCES = \
|
||||
src/shared/rtsp.h \
|
||||
src/shared/rtsp.c \
|
||||
src/shared/shl_dlist.h \
|
||||
src/shared/shl_htable.h \
|
||||
src/shared/shl_htable.c \
|
||||
|
@ -178,6 +180,7 @@ miracled_LDFLAGS = $(AM_LDFLAGS)
|
|||
#
|
||||
|
||||
tests = \
|
||||
test_rtsp \
|
||||
test_wpas
|
||||
|
||||
if BUILD_HAVE_CHECK
|
||||
|
|
3247
src/shared/rtsp.c
Normal file
3247
src/shared/rtsp.c
Normal file
File diff suppressed because it is too large
Load diff
261
src/shared/rtsp.h
Normal file
261
src/shared/rtsp.h
Normal file
|
@ -0,0 +1,261 @@
|
|||
/*
|
||||
* MiracleCast - Wifi-Display/Miracast Implementation
|
||||
*
|
||||
* Copyright (c) 2013-2014 David Herrmann <dh.herrmann@gmail.com>
|
||||
*
|
||||
* MiracleCast is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* MiracleCast is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with MiracleCast; If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef MIRACLE_RTSP_H
|
||||
#define MIRACLE_RTSP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <systemd/sd-event.h>
|
||||
|
||||
/* types */
|
||||
|
||||
struct rtsp;
|
||||
struct rtsp_message;
|
||||
|
||||
#define RTSP_ANY_CODE (~0U)
|
||||
#define RTSP_ANY_CHANNEL (~0U)
|
||||
|
||||
enum {
|
||||
RTSP_MESSAGE_UNKNOWN,
|
||||
RTSP_MESSAGE_REQUEST,
|
||||
RTSP_MESSAGE_REPLY,
|
||||
RTSP_MESSAGE_DATA,
|
||||
RTSP_MESSAGE_CNT,
|
||||
};
|
||||
|
||||
#define RTSP_TYPE_STRING 's'
|
||||
#define RTSP_TYPE_INT32 'i'
|
||||
#define RTSP_TYPE_UINT32 'u'
|
||||
#define RTSP_TYPE_RAW '&'
|
||||
#define RTSP_TYPE_HEADER_START '<'
|
||||
#define RTSP_TYPE_HEADER_END '>'
|
||||
#define RTSP_TYPE_BODY_START '{'
|
||||
#define RTSP_TYPE_BODY_END '}'
|
||||
|
||||
enum {
|
||||
RTSP_CODE_CONTINUE = 100,
|
||||
|
||||
RTSP_CODE_OK = 200,
|
||||
RTSP_CODE_CREATED,
|
||||
|
||||
RTSP_CODE_LOW_ON_STORAGE_SPACE = 250,
|
||||
|
||||
RTSP_CODE_MULTIPLE_CHOICES = 300,
|
||||
RTSP_CODE_MOVED_PERMANENTLY,
|
||||
RTSP_CODE_MOVED_TEMPORARILY,
|
||||
RTSP_CODE_SEE_OTHER,
|
||||
RTSP_CODE_NOT_MODIFIED,
|
||||
RTSP_CODE_USE_PROXY,
|
||||
|
||||
RTSP_CODE_BAD_REQUEST = 400,
|
||||
RTSP_CODE_UNAUTHORIZED,
|
||||
RTSP_CODE_PAYMENT_REQUIRED,
|
||||
RTSP_CODE_FORBIDDEN,
|
||||
RTSP_CODE_NOT_FOUND,
|
||||
RTSP_CODE_METHOD_NOT_ALLOWED,
|
||||
RTSP_CODE_NOT_ACCEPTABLE,
|
||||
RTSP_CODE_PROXY_AUTHENTICATION_REQUIRED,
|
||||
RTSP_CODE_REQUEST_TIMEOUT,
|
||||
RTSP_CODE__PLACEHOLDER__1,
|
||||
RTSP_CODE_GONE,
|
||||
RTSP_CODE_LENGTH_REQUIRED,
|
||||
RTSP_CODE_PRECONDITION_FAILED,
|
||||
RTSP_CODE_REQUEST_ENTITY_TOO_LARGE,
|
||||
RTSP_CODE_REQUEST_URI_TOO_LARGE,
|
||||
RTSP_CODE_UNSUPPORTED_MEDIA_TYPE,
|
||||
|
||||
RTSP_CODE_PARAMETER_NOT_UNDERSTOOD = 451,
|
||||
RTSP_CODE_CONFERENCE_NOT_FOUND,
|
||||
RTSP_CODE_NOT_ENOUGH_BANDWIDTH,
|
||||
RTSP_CODE_SESSION_NOT_FOUND,
|
||||
RTSP_CODE_METHOD_NOT_VALID_IN_THIS_STATE,
|
||||
RTSP_CODE_HEADER_FIELD_NOT_VALID_FOR_RESOURCE,
|
||||
RTSP_CODE_INVALID_RANGE,
|
||||
RTSP_CODE_PARAMETER_IS_READ_ONLY,
|
||||
RTSP_CODE_AGGREGATE_OPERATION_NOT_ALLOWED,
|
||||
RTSP_CODE_ONLY_AGGREGATE_OPERATION_ALLOWED,
|
||||
RTSP_CODE_UNSUPPORTED_TRANSPORT,
|
||||
RTSP_CODE_DESTINATION_UNREACHABLE,
|
||||
|
||||
RTSP_CODE_INTERNAL_SERVER_ERROR = 500,
|
||||
RTSP_CODE_NOT_IMPLEMENTED,
|
||||
RTSP_CODE_BAD_GATEWAY,
|
||||
RTSP_CODE_SERVICE_UNAVAILABLE,
|
||||
RTSP_CODE_GATEWAY_TIMEOUT,
|
||||
RTSP_CODE_RTSP_VERSION_NOT_SUPPORTED,
|
||||
|
||||
RTSP_CODE_OPTION_NOT_SUPPORTED = 551,
|
||||
|
||||
RTSP_CODE_CNT
|
||||
};
|
||||
|
||||
typedef int (*rtsp_callback_fn) (struct rtsp *bus,
|
||||
struct rtsp_message *m,
|
||||
void *data);
|
||||
|
||||
/*
|
||||
* Bus
|
||||
*/
|
||||
|
||||
int rtsp_open(struct rtsp **out, int fd);
|
||||
void rtsp_ref(struct rtsp *bus);
|
||||
void rtsp_unref(struct rtsp *bus);
|
||||
|
||||
static inline void rtsp_unref_p(struct rtsp **bus)
|
||||
{
|
||||
rtsp_unref(*bus);
|
||||
}
|
||||
|
||||
#define _rtsp_unref_ __attribute__((__cleanup__(rtsp_unref_p)))
|
||||
|
||||
bool rtsp_is_dead(struct rtsp *bus);
|
||||
|
||||
int rtsp_attach_event(struct rtsp *bus, sd_event *event, int priority);
|
||||
void rtsp_detach_event(struct rtsp *bus);
|
||||
|
||||
int rtsp_add_match(struct rtsp *bus, rtsp_callback_fn cb_fn, void *data);
|
||||
void rtsp_remove_match(struct rtsp *bus, rtsp_callback_fn cb_fn, void *data);
|
||||
|
||||
int rtsp_send(struct rtsp *bus, struct rtsp_message *m);
|
||||
int rtsp_call_async(struct rtsp *bus,
|
||||
struct rtsp_message *m,
|
||||
rtsp_callback_fn cb_fn,
|
||||
void *data,
|
||||
uint64_t timeout,
|
||||
uint64_t *cookie);
|
||||
void rtsp_call_async_cancel(struct rtsp *bus, uint64_t cookie);
|
||||
|
||||
/*
|
||||
* Messages
|
||||
*/
|
||||
|
||||
int rtsp_message_new_request(struct rtsp *bus,
|
||||
struct rtsp_message **out,
|
||||
const char *method,
|
||||
const char *uri);
|
||||
int rtsp_message_new_reply(struct rtsp *bus,
|
||||
struct rtsp_message **out,
|
||||
uint64_t cookie,
|
||||
unsigned int code,
|
||||
const char *phrase);
|
||||
int rtsp_message_new_reply_for(struct rtsp_message *orig,
|
||||
struct rtsp_message **out,
|
||||
unsigned int code,
|
||||
const char *phrase);
|
||||
int rtsp_message_new_data(struct rtsp *bus,
|
||||
struct rtsp_message **out,
|
||||
unsigned int channel,
|
||||
const void *payload,
|
||||
size_t size);
|
||||
int rtsp_message_new_from_raw(struct rtsp *bus,
|
||||
struct rtsp_message **out,
|
||||
const void *data,
|
||||
size_t len);
|
||||
void rtsp_message_ref(struct rtsp_message *m);
|
||||
void rtsp_message_unref(struct rtsp_message *m);
|
||||
|
||||
static inline void rtsp_message_unref_p(struct rtsp_message **m)
|
||||
{
|
||||
rtsp_message_unref(*m);
|
||||
}
|
||||
|
||||
#define _rtsp_message_unref_ __attribute__((__cleanup__(rtsp_message_unref_p)))
|
||||
|
||||
bool rtsp_message_is_request(struct rtsp_message *m,
|
||||
const char *method,
|
||||
const char *uri);
|
||||
bool rtsp_message_is_reply(struct rtsp_message *m,
|
||||
unsigned int code,
|
||||
const char *phrase);
|
||||
bool rtsp_message_is_data(struct rtsp_message *m,
|
||||
unsigned int channel);
|
||||
|
||||
/* message attributes */
|
||||
|
||||
unsigned int rtsp_message_get_type(struct rtsp_message *m);
|
||||
const char *rtsp_message_get_method(struct rtsp_message *m);
|
||||
const char *rtsp_message_get_uri(struct rtsp_message *m);
|
||||
unsigned int rtsp_message_get_code(struct rtsp_message *m);
|
||||
const char *rtsp_message_get_phrase(struct rtsp_message *m);
|
||||
unsigned int rtsp_message_get_channel(struct rtsp_message *m);
|
||||
const void *rtsp_message_get_payload(struct rtsp_message *m);
|
||||
size_t rtsp_message_get_payload_size(struct rtsp_message *m);
|
||||
|
||||
struct rtsp *rtsp_message_get_bus(struct rtsp_message *m);
|
||||
uint64_t rtsp_message_get_cookie(struct rtsp_message *m);
|
||||
bool rtsp_message_is_sealed(struct rtsp_message *m);
|
||||
|
||||
/* appending arguments */
|
||||
|
||||
int rtsp_message_append_line(struct rtsp_message *m, const char *line);
|
||||
int rtsp_message_open_header(struct rtsp_message *m, const char *name);
|
||||
int rtsp_message_close_header(struct rtsp_message *m);
|
||||
int rtsp_message_open_body(struct rtsp_message *m);
|
||||
int rtsp_message_close_body(struct rtsp_message *m);
|
||||
|
||||
int rtsp_message_append_basic(struct rtsp_message *m,
|
||||
char type,
|
||||
...);
|
||||
int rtsp_message_appendv_basic(struct rtsp_message *m,
|
||||
char type,
|
||||
va_list args);
|
||||
int rtsp_message_append(struct rtsp_message *m,
|
||||
const char *types,
|
||||
...);
|
||||
int rtsp_message_appendv(struct rtsp_message *m,
|
||||
const char *types,
|
||||
va_list args);
|
||||
|
||||
int rtsp_message_set_cookie(struct rtsp_message *m, uint64_t cookie);
|
||||
int rtsp_message_seal(struct rtsp_message *m);
|
||||
|
||||
/* parsing arguments */
|
||||
|
||||
int rtsp_message_enter_header(struct rtsp_message *m, const char *name);
|
||||
void rtsp_message_exit_header(struct rtsp_message *m);
|
||||
int rtsp_message_enter_body(struct rtsp_message *m);
|
||||
void rtsp_message_exit_body(struct rtsp_message *m);
|
||||
|
||||
int rtsp_message_read_basic(struct rtsp_message *m,
|
||||
char type,
|
||||
...);
|
||||
int rtsp_message_readv_basic(struct rtsp_message *m,
|
||||
char type,
|
||||
va_list args);
|
||||
int rtsp_message_read(struct rtsp_message *m,
|
||||
const char *types,
|
||||
...);
|
||||
int rtsp_message_readv(struct rtsp_message *m,
|
||||
const char *types,
|
||||
va_list args);
|
||||
|
||||
int rtsp_message_skip_basic(struct rtsp_message *m, char type);
|
||||
int rtsp_message_skip(struct rtsp_message *m, const char *types);
|
||||
|
||||
int rtsp_message_rewind(struct rtsp_message *m, bool complete);
|
||||
|
||||
void *rtsp_message_get_body(struct rtsp_message *m);
|
||||
size_t rtsp_message_get_body_size(struct rtsp_message *m);
|
||||
void *rtsp_message_get_raw(struct rtsp_message *m);
|
||||
size_t rtsp_message_get_raw_size(struct rtsp_message *m);
|
||||
|
||||
#endif /* MIRACLE_RTSP_H */
|
712
test/test_rtsp.c
Normal file
712
test/test_rtsp.c
Normal file
|
@ -0,0 +1,712 @@
|
|||
/*
|
||||
* MiracleCast - Wifi-Display/Miracast Implementation
|
||||
*
|
||||
* Copyright (c) 2013-2014 David Herrmann <dh.herrmann@gmail.com>
|
||||
*
|
||||
* MiracleCast is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* MiracleCast is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with MiracleCast; If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "test_common.h"
|
||||
#include "rtsp.h"
|
||||
|
||||
static bool debug = false;
|
||||
|
||||
START_TEST(bus_invalid_ops)
|
||||
{
|
||||
struct rtsp *bus;
|
||||
int r;
|
||||
|
||||
r = rtsp_open(NULL, -1);
|
||||
ck_assert_int_lt(r, 0);
|
||||
r = rtsp_open(&bus, -1);
|
||||
ck_assert_int_lt(r, 0);
|
||||
r = rtsp_open(NULL, 0);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
rtsp_ref(NULL);
|
||||
rtsp_unref(NULL);
|
||||
|
||||
ck_assert(rtsp_is_dead(NULL));
|
||||
|
||||
r = rtsp_attach_event(NULL, NULL, 0);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
rtsp_detach_event(NULL);
|
||||
|
||||
r = rtsp_add_match(NULL, NULL, NULL);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
rtsp_remove_match(NULL, NULL, NULL);
|
||||
|
||||
r = rtsp_send(NULL, NULL);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
r = rtsp_call_async(NULL, NULL, NULL, NULL, 0, NULL);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
rtsp_call_async_cancel(NULL, 0);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
static int bus_open_match_fn(struct rtsp *bus,
|
||||
struct rtsp_message *m,
|
||||
void *data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
START_TEST(bus_open)
|
||||
{
|
||||
struct rtsp *bus;
|
||||
int r, fd;
|
||||
|
||||
fd = dup(0);
|
||||
ck_assert_int_ge(fd, 0);
|
||||
|
||||
r = rtsp_open(&bus, fd);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
ck_assert(!rtsp_is_dead(bus));
|
||||
|
||||
rtsp_ref(bus);
|
||||
rtsp_unref(bus);
|
||||
|
||||
ck_assert(!rtsp_is_dead(bus));
|
||||
|
||||
r = rtsp_add_match(bus, NULL, NULL);
|
||||
ck_assert_int_lt(r, 0);
|
||||
|
||||
rtsp_remove_match(bus, NULL, NULL);
|
||||
|
||||
r = rtsp_add_match(bus, bus_open_match_fn, NULL);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
rtsp_remove_match(bus, bus_open_match_fn, NULL);
|
||||
|
||||
r = rtsp_attach_event(bus, NULL, 0);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
rtsp_detach_event(bus);
|
||||
|
||||
rtsp_unref(bus);
|
||||
|
||||
/* rtsp takes ownership over @fd, verify that */
|
||||
|
||||
ck_assert_int_lt(dup(fd), 0);
|
||||
|
||||
/* try again with implicit detach during destruction */
|
||||
|
||||
fd = dup(0);
|
||||
ck_assert_int_ge(fd, 0);
|
||||
|
||||
r = rtsp_open(&bus, fd);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = rtsp_add_match(bus, bus_open_match_fn, NULL);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = rtsp_attach_event(bus, NULL, 0);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
rtsp_unref(bus);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
TEST_DEFINE_CASE(bus)
|
||||
TEST(bus_invalid_ops)
|
||||
TEST(bus_open)
|
||||
TEST_END_CASE
|
||||
|
||||
START_TEST(msg_new_invalid)
|
||||
{
|
||||
char data[128] = { };
|
||||
struct rtsp *bus;
|
||||
struct rtsp_message *m;
|
||||
int r, fd;
|
||||
|
||||
fd = dup(0);
|
||||
ck_assert_int_ge(fd, 0);
|
||||
r = rtsp_open(&bus, fd);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
/* request messages */
|
||||
|
||||
m = TEST_INVALID_PTR;
|
||||
r = rtsp_message_new_request(NULL, &m, "method", "uri");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_request(bus, NULL, "method", "uri");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_request(bus, &m, "", "uri");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_request(bus, &m, NULL, "uri");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_request(bus, &m, "method", "");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_request(bus, &m, "method", NULL);
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
|
||||
r = rtsp_message_new_request(bus, &m, "method", "uri");
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert(m != TEST_INVALID_PTR);
|
||||
rtsp_message_unref(m);
|
||||
|
||||
/* reply-for messages */
|
||||
|
||||
m = TEST_INVALID_PTR;
|
||||
r = rtsp_message_new_reply(NULL, &m, 1, 200, "OK");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_reply(bus, NULL, 1, 200, "OK");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_reply(bus, &m, 0, 200, "OK");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_reply(bus, &m, 1, RTSP_ANY_CODE, "OK");
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
|
||||
r = rtsp_message_new_reply(bus, &m, 1, 200, "OK");
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert(m != TEST_INVALID_PTR);
|
||||
rtsp_message_unref(m);
|
||||
|
||||
/* data messages */
|
||||
|
||||
m = TEST_INVALID_PTR;
|
||||
r = rtsp_message_new_data(NULL, &m, 0, data, sizeof(data));
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_data(bus, NULL, 0, data, sizeof(data));
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_data(bus, &m, RTSP_ANY_CHANNEL, data, sizeof(data));
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
r = rtsp_message_new_data(bus, &m, 0, NULL, sizeof(data));
|
||||
ck_assert_int_lt(r, 0);
|
||||
ck_assert(m == TEST_INVALID_PTR);
|
||||
|
||||
r = rtsp_message_new_data(bus, &m, 0, data, sizeof(data));
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert(m != TEST_INVALID_PTR);
|
||||
rtsp_message_unref(m);
|
||||
|
||||
/* invalid ops */
|
||||
|
||||
rtsp_message_ref(NULL);
|
||||
rtsp_message_unref(NULL);
|
||||
|
||||
ck_assert_int_eq(rtsp_message_get_type(NULL), 0);
|
||||
ck_assert(!rtsp_message_get_method(NULL));
|
||||
ck_assert(!rtsp_message_get_uri(NULL));
|
||||
ck_assert_int_eq(rtsp_message_get_code(NULL), RTSP_ANY_CODE);
|
||||
ck_assert(!rtsp_message_get_phrase(NULL));
|
||||
ck_assert_int_eq(rtsp_message_get_channel(NULL), RTSP_ANY_CHANNEL);
|
||||
ck_assert(!rtsp_message_get_payload(NULL));
|
||||
ck_assert_int_eq(rtsp_message_get_payload_size(NULL), 0);
|
||||
|
||||
ck_assert(!rtsp_message_is_request(NULL, NULL, NULL));
|
||||
ck_assert(!rtsp_message_is_reply(NULL, RTSP_ANY_CODE, NULL));
|
||||
ck_assert(!rtsp_message_is_data(NULL, RTSP_ANY_CHANNEL));
|
||||
|
||||
ck_assert(!rtsp_message_get_bus(NULL));
|
||||
ck_assert(!rtsp_message_get_cookie(NULL));
|
||||
ck_assert(!rtsp_message_is_sealed(NULL));
|
||||
|
||||
rtsp_unref(bus);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
struct recipe {
|
||||
unsigned int type;
|
||||
|
||||
union {
|
||||
struct {
|
||||
const char *method;
|
||||
const char *uri;
|
||||
} request;
|
||||
|
||||
struct {
|
||||
uint64_t cookie;
|
||||
unsigned int code;
|
||||
const char *phrase;
|
||||
} reply;
|
||||
|
||||
struct {
|
||||
unsigned int channel;
|
||||
void *payload;
|
||||
size_t size;
|
||||
} data;
|
||||
};
|
||||
|
||||
const char *types;
|
||||
union {
|
||||
void *ptr;
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
} args[128];
|
||||
|
||||
const char *raw;
|
||||
size_t rawlen;
|
||||
const void *equivalents[128];
|
||||
size_t eq_sizes[128];
|
||||
};
|
||||
|
||||
static void verify_recipe(struct recipe *rec, struct rtsp_message *m)
|
||||
{
|
||||
size_t i;
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
const char *str;
|
||||
char t;
|
||||
int r;
|
||||
bool in_header = false;
|
||||
|
||||
ck_assert_int_eq(rtsp_message_get_type(m), rec->type);
|
||||
|
||||
switch (rec->type) {
|
||||
case RTSP_MESSAGE_REQUEST:
|
||||
ck_assert_str_eq(rtsp_message_get_method(m),
|
||||
rec->request.method);
|
||||
ck_assert_str_eq(rtsp_message_get_uri(m),
|
||||
rec->request.uri);
|
||||
break;
|
||||
case RTSP_MESSAGE_REPLY:
|
||||
ck_assert_int_eq(rtsp_message_get_code(m), rec->reply.code);
|
||||
ck_assert_str_eq(rtsp_message_get_phrase(m), rec->reply.phrase);
|
||||
break;
|
||||
case RTSP_MESSAGE_DATA:
|
||||
ck_assert_int_eq(rtsp_message_get_channel(m),
|
||||
rec->data.channel);
|
||||
ck_assert_int_eq(rtsp_message_get_payload_size(m),
|
||||
rec->data.size);
|
||||
ck_assert(!memcmp(rtsp_message_get_payload(m),
|
||||
rec->data.payload,
|
||||
rec->data.size));
|
||||
break;
|
||||
defualt:
|
||||
ck_assert(false);
|
||||
abort();
|
||||
}
|
||||
|
||||
for (i = 0; rec->types && rec->types[i]; ++i) {
|
||||
t = rec->types[i];
|
||||
switch (t) {
|
||||
case RTSP_TYPE_STRING:
|
||||
r = rtsp_message_read_basic(m, t, &str);
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert(!!str);
|
||||
ck_assert_str_eq(str, rec->args[i].ptr);
|
||||
break;
|
||||
case RTSP_TYPE_INT32:
|
||||
r = rtsp_message_read_basic(m, t, &i32);
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert_int_eq(i32, rec->args[i].i32);
|
||||
break;
|
||||
case RTSP_TYPE_UINT32:
|
||||
r = rtsp_message_read_basic(m, t, &u32);
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert_int_eq(u32, rec->args[i].u32);
|
||||
break;
|
||||
case RTSP_TYPE_RAW:
|
||||
/* we cannot read TYPE_RAW outside headers */
|
||||
if (in_header) {
|
||||
r = rtsp_message_read_basic(m, t, &str);
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert(!!str);
|
||||
ck_assert_str_eq(str, rec->args[i].ptr);
|
||||
}
|
||||
|
||||
break;
|
||||
case RTSP_TYPE_HEADER_START:
|
||||
in_header = true;
|
||||
r = rtsp_message_read_basic(m, t, rec->args[i].ptr);
|
||||
ck_assert_int_ge(r, 0);
|
||||
break;
|
||||
case RTSP_TYPE_HEADER_END:
|
||||
in_header = false;
|
||||
/* fallthrough */
|
||||
case RTSP_TYPE_BODY_START:
|
||||
case RTSP_TYPE_BODY_END:
|
||||
r = rtsp_message_read_basic(m, t, NULL);
|
||||
ck_assert_int_ge(r, 0);
|
||||
break;
|
||||
default:
|
||||
ck_assert(false);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static struct rtsp_message *create_from_recipe(struct rtsp *bus,
|
||||
struct recipe *rec)
|
||||
{
|
||||
struct rtsp_message *m;
|
||||
void *raw;
|
||||
size_t i, rawlen;
|
||||
char t;
|
||||
int r;
|
||||
|
||||
ck_assert(!!rec);
|
||||
|
||||
switch (rec->type) {
|
||||
case RTSP_MESSAGE_REQUEST:
|
||||
r = rtsp_message_new_request(bus,
|
||||
&m,
|
||||
rec->request.method,
|
||||
rec->request.uri);
|
||||
ck_assert_int_ge(r, 0);
|
||||
break;
|
||||
case RTSP_MESSAGE_REPLY:
|
||||
r = rtsp_message_new_reply(bus,
|
||||
&m,
|
||||
rec->reply.cookie ? : 1,
|
||||
rec->reply.code,
|
||||
rec->reply.phrase);
|
||||
ck_assert_int_ge(r, 0);
|
||||
break;
|
||||
case RTSP_MESSAGE_DATA:
|
||||
r = rtsp_message_new_data(bus,
|
||||
&m,
|
||||
rec->data.channel,
|
||||
rec->data.payload,
|
||||
rec->data.size);
|
||||
ck_assert_int_ge(r, 0);
|
||||
break;
|
||||
default:
|
||||
ck_assert(false);
|
||||
abort();
|
||||
}
|
||||
|
||||
for (i = 0; rec->types && rec->types[i]; ++i) {
|
||||
t = rec->types[i];
|
||||
switch (t) {
|
||||
case RTSP_TYPE_INT32:
|
||||
r = rtsp_message_append_basic(m,
|
||||
t,
|
||||
rec->args[i].i32);
|
||||
break;
|
||||
case RTSP_TYPE_UINT32:
|
||||
r = rtsp_message_append_basic(m,
|
||||
t,
|
||||
rec->args[i].u32);
|
||||
break;
|
||||
default:
|
||||
r = rtsp_message_append_basic(m,
|
||||
t,
|
||||
rec->args[i].ptr);
|
||||
break;
|
||||
}
|
||||
|
||||
ck_assert_int_ge(r, 0);
|
||||
}
|
||||
|
||||
r = rtsp_message_set_cookie(m, 1);
|
||||
ck_assert_int_ge(r, 0);
|
||||
r = rtsp_message_seal(m);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
/* compare to @raw */
|
||||
|
||||
raw = rtsp_message_get_raw(m);
|
||||
ck_assert(!!raw);
|
||||
rawlen = rtsp_message_get_raw_size(m);
|
||||
|
||||
if (debug)
|
||||
fprintf(stderr, "---------EXPECT---------\n%s\n----------GOT-----------\n%s\n-----------END----------\n", (char*)rec->raw, (char*)raw);
|
||||
|
||||
ck_assert_int_eq(rawlen, rec->rawlen ? : strlen(rec->raw));
|
||||
ck_assert(!memcmp(raw, rec->raw, rawlen));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static struct rtsp_message *create_from_recipe_and_verify(struct rtsp *bus,
|
||||
struct recipe *rec)
|
||||
{
|
||||
struct rtsp_message *m, *eq;
|
||||
const void *e;
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
m = create_from_recipe(bus, rec);
|
||||
|
||||
for (i = 0; i < SHL_ARRAY_LENGTH(rec->equivalents); ++i) {
|
||||
e = rec->equivalents[i];
|
||||
if (!e)
|
||||
break;
|
||||
|
||||
r = rtsp_message_new_from_raw(bus,
|
||||
&eq,
|
||||
e,
|
||||
rec->eq_sizes[i] ? : strlen(e));
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
verify_recipe(rec, eq);
|
||||
|
||||
rtsp_message_unref(eq);
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static struct recipe recipes[] = {
|
||||
{
|
||||
.type = RTSP_MESSAGE_REQUEST,
|
||||
.request = { .method = "METHOD", .uri = "http://URI" },
|
||||
|
||||
.raw = "METHOD http://URI RTSP/1.0\r\n"
|
||||
"CSeq: 1\r\n"
|
||||
"\r\n",
|
||||
|
||||
.equivalents = {
|
||||
" METHOD http://URI RTSP/1.0 \r\n\r\n",
|
||||
" METHOD http://URI RTSP/1.0 \r\r",
|
||||
" METHOD http://URI RTSP/1.0 \n\n",
|
||||
" METHOD http://URI RTSP/1.0 \n\r\n",
|
||||
" METHOD http://URI RTSP/1.0 \n\r",
|
||||
},
|
||||
},
|
||||
{
|
||||
.type = RTSP_MESSAGE_REPLY,
|
||||
.reply = { .code = 200, .phrase = "OK" },
|
||||
|
||||
.raw = "RTSP/1.0 200 OK\r\n"
|
||||
"CSeq: 1\r\n"
|
||||
"\r\n",
|
||||
|
||||
.equivalents = {
|
||||
" RTSP/1.0 200 OK \r\n",
|
||||
" RTSP/1.0 200 OK ",
|
||||
" RTSP/1.0 200 OK \r",
|
||||
},
|
||||
},
|
||||
{
|
||||
.type = RTSP_MESSAGE_DATA,
|
||||
.data = { .channel = 5, .payload = "asdf", .size = 4 },
|
||||
|
||||
.raw = "$\005\000\004asdf",
|
||||
.rawlen = 8,
|
||||
},
|
||||
{
|
||||
.type = RTSP_MESSAGE_REQUEST,
|
||||
.request = { .method = "METHOD", .uri = "http://URI" },
|
||||
.types = "<sui><&>&{<sui><&>&}",
|
||||
.args = {
|
||||
{ .ptr = "header1" },
|
||||
{ .ptr = "string" },
|
||||
{ .u32 = 10 },
|
||||
{ .i32 = -5 },
|
||||
{ },
|
||||
{ .ptr = "header2" },
|
||||
{ .ptr = "raw value" },
|
||||
{ },
|
||||
{ .ptr = "raw header 3 :as full line" },
|
||||
{ },
|
||||
{ .ptr = "body-header1" },
|
||||
{ .ptr = "body string" },
|
||||
{ .u32 = 10 },
|
||||
{ .i32 = -5 },
|
||||
{ },
|
||||
{ .ptr = "body-header2" },
|
||||
{ .ptr = "body raw value" },
|
||||
{ },
|
||||
{ .ptr = "body raw header 3 :as full line" },
|
||||
{ },
|
||||
},
|
||||
|
||||
.raw = "METHOD http://URI RTSP/1.0\r\n"
|
||||
"header1: string 10 -5\r\n"
|
||||
"header2: raw value\r\n"
|
||||
"raw header 3 :as full line\r\n"
|
||||
"Content-Length: 98\r\n"
|
||||
"Content-Type: text/parameters\r\n"
|
||||
"CSeq: 1\r\n"
|
||||
"\r\n"
|
||||
"body-header1: \"body string\" 10 -5\r\n"
|
||||
"body-header2: body raw value\r\n"
|
||||
"body raw header 3 :as full line\r\n",
|
||||
|
||||
.equivalents = {
|
||||
"METHOD http://URI RTSP/1.0\r\n"
|
||||
"header1: string 10 -5\r\n"
|
||||
"header2: raw value\r\n"
|
||||
"raw header 3 :as full line\r\n"
|
||||
"Content-Length: 98\r"
|
||||
"Content-Type: text/parameters\r\n"
|
||||
"\r"
|
||||
"body-header1: \"body string\" 10 -5\r\n"
|
||||
"body-header2: body raw value\r\n"
|
||||
"body raw header 3 :as full line\r\n",
|
||||
|
||||
"METHOD http://URI RTSP/1.0\r\n"
|
||||
"header1: string 10 -5\r\n"
|
||||
"header2: raw value\r\n"
|
||||
"raw header 3 :as full line\r\n"
|
||||
"Content-Length: 98\n"
|
||||
"Content-Type: text/parameters\r\n"
|
||||
"\n"
|
||||
"body-header1: \"body string\" 10 -5\r\n"
|
||||
"body-header2: body raw value\r\n"
|
||||
"\n"
|
||||
"body raw header 3 :as full line\r\n",
|
||||
|
||||
"METHOD http://URI RTSP/1.0\r\n"
|
||||
" header1 : string 10 -5\r\n"
|
||||
"header2: raw value\r\n"
|
||||
"raw header 3 :as full line\r\n"
|
||||
" Content-Length : 98 \r"
|
||||
"Content-Type: text/parameters\r\n"
|
||||
"\r\n"
|
||||
"body-header1: \"body string\" 10 -5\r\n"
|
||||
"\n\r"
|
||||
" body-header2 : body raw value \r\n"
|
||||
"body raw header 3 :as full line\r\n",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
START_TEST(msg_new)
|
||||
{
|
||||
struct rtsp *bus;
|
||||
struct rtsp_message *m;
|
||||
size_t i;
|
||||
int r, fd;
|
||||
|
||||
fd = dup(0);
|
||||
ck_assert_int_ge(fd, 0);
|
||||
r = rtsp_open(&bus, fd);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
for (i = 0; i < SHL_ARRAY_LENGTH(recipes); ++i) {
|
||||
m = create_from_recipe_and_verify(bus, &recipes[i]);
|
||||
rtsp_message_unref(m);
|
||||
}
|
||||
|
||||
rtsp_unref(bus);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
TEST_DEFINE_CASE(msg)
|
||||
TEST(msg_new_invalid)
|
||||
TEST(msg_new)
|
||||
TEST_END_CASE
|
||||
|
||||
static struct rtsp *server, *client;
|
||||
static sd_event *event;
|
||||
|
||||
static struct rtsp *start_test_client(void)
|
||||
{
|
||||
int r, fds[2] = { };
|
||||
|
||||
r = sd_event_default(&event);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
|
||||
ck_assert_int_ge(r, 0);
|
||||
ck_assert_int_ge(fds[0], 0);
|
||||
ck_assert_int_ge(fds[1], 0);
|
||||
|
||||
r = rtsp_open(&server, fds[0]);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = rtsp_attach_event(server, event, 0);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = rtsp_open(&client, fds[1]);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
r = rtsp_attach_event(client, event, 0);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
static void stop_test_client(void)
|
||||
{
|
||||
rtsp_unref(client);
|
||||
client = NULL;
|
||||
rtsp_unref(server);
|
||||
server = NULL;
|
||||
sd_event_unref(event);
|
||||
event = NULL;
|
||||
}
|
||||
|
||||
static int match_recipe(struct rtsp *bus,
|
||||
struct rtsp_message *m,
|
||||
void *data)
|
||||
{
|
||||
struct recipe **rec = data;
|
||||
|
||||
ck_assert(!!rec);
|
||||
ck_assert(!!*rec);
|
||||
ck_assert(!!m);
|
||||
verify_recipe(*rec, m);
|
||||
*rec = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
START_TEST(run_all)
|
||||
{
|
||||
struct recipe *rec;
|
||||
struct rtsp_message *m;
|
||||
int r, i;
|
||||
|
||||
start_test_client();
|
||||
|
||||
r = rtsp_add_match(server, match_recipe, &rec);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
for (i = 0; i < SHL_ARRAY_LENGTH(recipes); ++i) {
|
||||
rec = &recipes[i];
|
||||
if (rec->type == RTSP_MESSAGE_REPLY)
|
||||
continue;
|
||||
|
||||
m = create_from_recipe(client, rec);
|
||||
r = rtsp_send(client, m);
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
do {
|
||||
r = sd_event_run(event, (uint64_t)-1);
|
||||
} while (rec);
|
||||
|
||||
ck_assert_int_ge(r, 0);
|
||||
|
||||
rtsp_message_unref(m);
|
||||
}
|
||||
|
||||
stop_test_client();
|
||||
}
|
||||
END_TEST
|
||||
|
||||
TEST_DEFINE_CASE(run)
|
||||
TEST(run_all)
|
||||
TEST_END_CASE
|
||||
|
||||
TEST_DEFINE(
|
||||
TEST_SUITE(rtsp,
|
||||
TEST_CASE(bus),
|
||||
TEST_CASE(msg),
|
||||
TEST_CASE(run),
|
||||
TEST_END
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue