1/* 2 * Copyright (C) 2010 Julien BLACHE <jb@jblache.org> 3 * Based on evhttp from libevent 1.4.x 4 * 5 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#ifndef _EVRTSP_H_ 31#define _EVRTSP_H_ 32 33#include <event.h> 34 35#ifdef __cplusplus 36extern "C" { 37#endif 38 39#ifdef WIN32 40#define WIN32_LEAN_AND_MEAN 41#include <winsock2.h> 42#include <windows.h> 43#undef WIN32_LEAN_AND_MEAN 44#endif 45 46/* Response codes */ 47#define RTSP_OK 200 48#define RTSP_UNAUTHORIZED 401 49 50struct evrtsp_connection; 51 52/* 53 * Interfaces for making requests 54 */ 55enum evrtsp_cmd_type { 56 EVRTSP_REQ_ANNOUNCE, 57 EVRTSP_REQ_OPTIONS, 58 EVRTSP_REQ_SETUP, 59 EVRTSP_REQ_RECORD, 60 EVRTSP_REQ_PAUSE, 61 EVRTSP_REQ_GET_PARAMETER, 62 EVRTSP_REQ_SET_PARAMETER, 63 EVRTSP_REQ_FLUSH, 64 EVRTSP_REQ_TEARDOWN, 65}; 66 67enum evrtsp_request_kind { EVRTSP_REQUEST, EVRTSP_RESPONSE }; 68 69struct evrtsp_request { 70#if defined(TAILQ_ENTRY) 71 TAILQ_ENTRY(evrtsp_request) next; 72#else 73struct { 74 struct evrtsp_request *tqe_next; 75 struct evrtsp_request **tqe_prev; 76} next; 77#endif 78 79 /* the connection object that this request belongs to */ 80 struct evrtsp_connection *evcon; 81 int flags; 82#define EVRTSP_REQ_OWN_CONNECTION 0x0001 83 84 struct evkeyvalq *input_headers; 85 struct evkeyvalq *output_headers; 86 87 enum evrtsp_request_kind kind; 88 enum evrtsp_cmd_type type; 89 90 char *uri; /* uri after RTSP request was parsed */ 91 92 char major; /* RTSP Major number */ 93 char minor; /* RTSP Minor number */ 94 95 int response_code; /* RTSP Response code */ 96 char *response_code_line; /* Readable response */ 97 98 struct evbuffer *input_buffer; /* read data */ 99 ev_int64_t ntoread; 100 101 struct evbuffer *output_buffer; /* outgoing post or data */ 102 103 /* Callback */ 104 void (*cb)(struct evrtsp_request *, void *); 105 void *cb_arg; 106}; 107 108/** 109 * Creates a new request object that needs to be filled in with the request 110 * parameters. The callback is executed when the request completed or an 111 * error occurred. 112 */ 113struct evrtsp_request *evrtsp_request_new( 114 void (*cb)(struct evrtsp_request *, void *), void *arg); 115 116/** Frees the request object and removes associated events. */ 117void evrtsp_request_free(struct evrtsp_request *req); 118 119/** 120 * A connection object that can be used to for making RTSP requests. The 121 * connection object tries to establish the connection when it is given an 122 * rtsp request object. 123 */ 124struct evrtsp_connection *evrtsp_connection_new( 125 const char *address, unsigned short port); 126 127/** Frees an rtsp connection */ 128void evrtsp_connection_free(struct evrtsp_connection *evcon); 129 130/** Set a callback for connection close. */ 131void evrtsp_connection_set_closecb(struct evrtsp_connection *evcon, 132 void (*)(struct evrtsp_connection *, void *), void *); 133 134/** 135 * Associates an event base with the connection - can only be called 136 * on a freshly created connection object that has not been used yet. 137 */ 138void evrtsp_connection_set_base(struct evrtsp_connection *evcon, 139 struct event_base *base); 140 141/** Get the remote address and port associated with this connection. */ 142void evrtsp_connection_get_peer(struct evrtsp_connection *evcon, 143 char **address, u_short *port); 144 145/** Get the local address and port associated with this connection. */ 146void 147evrtsp_connection_get_local_address(struct evrtsp_connection *evcon, 148 char **address, u_short *port); 149 150/** The connection gets ownership of the request */ 151int evrtsp_make_request(struct evrtsp_connection *evcon, 152 struct evrtsp_request *req, 153 enum evrtsp_cmd_type type, const char *uri); 154 155const char *evrtsp_request_uri(struct evrtsp_request *req); 156 157/* Interfaces for dealing with headers */ 158 159const char *evrtsp_find_header(const struct evkeyvalq *, const char *); 160int evrtsp_remove_header(struct evkeyvalq *, const char *); 161int evrtsp_add_header(struct evkeyvalq *, const char *, const char *); 162void evrtsp_clear_headers(struct evkeyvalq *); 163 164const char *evrtsp_method(enum evrtsp_cmd_type type); 165 166#ifdef __cplusplus 167} 168#endif 169 170#endif /* !_EVRTSP_H_ */ 171