James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file rtmp.h Real Time Messaging Protocol (RTMP) -- Internal API |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | enum { |
| 9 | RTMP_PROTOCOL_VERSION = 3, |
| 10 | RTMP_DEFAULT_CHUNKSIZE = 128, |
| 11 | RTMP_HANDSHAKE_SIZE = 1536, |
| 12 | RTMP_MESSAGE_LEN_MAX = 524288, |
| 13 | }; |
| 14 | |
| 15 | /* Chunk IDs */ |
| 16 | enum { |
| 17 | RTMP_CHUNK_ID_CONTROL = 2, |
| 18 | RTMP_CHUNK_ID_CONN = 3, |
| 19 | }; |
| 20 | |
| 21 | /** Defines the RTMP Handshake State */ |
| 22 | enum rtmp_handshake_state { |
| 23 | RTMP_STATE_UNINITIALIZED = 0, |
| 24 | RTMP_STATE_VERSION_SENT, |
| 25 | RTMP_STATE_ACK_SENT, |
| 26 | RTMP_STATE_HANDSHAKE_DONE |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * Defines an RTMP Connection |
| 31 | */ |
| 32 | struct rtmp_conn { |
| 33 | struct list streaml; |
| 34 | struct rtmp_dechunker *dechunk; |
| 35 | struct tcp_conn *tc; |
| 36 | struct mbuf *mb; /* TCP reassembly buffer */ |
| 37 | enum rtmp_handshake_state state; |
| 38 | size_t total_bytes; |
| 39 | size_t last_ack; |
| 40 | uint32_t window_ack_size; |
| 41 | uint32_t send_chunk_size; |
| 42 | unsigned chunk_id_counter; |
| 43 | bool is_client; |
| 44 | bool connected; |
| 45 | rtmp_estab_h *estabh; |
| 46 | rtmp_command_h *cmdh; |
| 47 | rtmp_close_h *closeh; |
| 48 | void *arg; |
| 49 | |
| 50 | /* client specific: */ |
| 51 | struct dnsc *dnsc; |
| 52 | struct dns_query *dnsq4; |
| 53 | struct dns_query *dnsq6; |
| 54 | struct list ctransl; |
| 55 | struct sa srvv[16]; |
| 56 | unsigned srvc; |
| 57 | uint64_t tid_counter; |
| 58 | uint16_t port; |
| 59 | char *app; |
| 60 | char *uri; |
| 61 | char *stream; |
| 62 | char *host; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Defines an RTMP Stream |
| 67 | */ |
| 68 | struct rtmp_stream { |
| 69 | struct le le; |
| 70 | const struct rtmp_conn *conn; /**< Pointer to parent connection */ |
| 71 | bool created; |
| 72 | uint32_t stream_id; |
| 73 | unsigned chunk_id_audio; |
| 74 | unsigned chunk_id_video; |
| 75 | unsigned chunk_id_data; |
| 76 | rtmp_audio_h *auh; |
| 77 | rtmp_video_h *vidh; |
| 78 | rtmp_command_h *datah; |
| 79 | rtmp_command_h *cmdh; |
| 80 | rtmp_resp_h *resph; |
| 81 | rtmp_control_h *ctrlh; |
| 82 | void *arg; |
| 83 | }; |
| 84 | |
| 85 | struct rtmp_header { |
| 86 | unsigned format:2; /* type 0-3 */ |
| 87 | uint32_t chunk_id; /* from 3-65599 */ |
| 88 | |
| 89 | uint32_t timestamp; /* 24-bit or 32-bit */ |
| 90 | uint32_t timestamp_delta; /* 24-bit */ |
| 91 | uint32_t timestamp_ext; |
| 92 | uint32_t length; /* 24-bit */ |
| 93 | uint8_t type_id; /* enum rtmp_packet_type */ |
| 94 | uint32_t stream_id; |
| 95 | bool ext_ts; |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | /* Command */ |
| 100 | |
| 101 | int rtmp_command_header_encode(struct mbuf *mb, const char *name, |
| 102 | uint64_t tid); |
| 103 | |
| 104 | /* Connection */ |
| 105 | |
| 106 | int rtmp_conn_send_msg(const struct rtmp_conn *conn, unsigned format, |
| 107 | uint32_t chunk_id, uint32_t timestamp, |
| 108 | uint32_t timestamp_delta, uint8_t msg_type_id, |
| 109 | uint32_t msg_stream_id, |
| 110 | const uint8_t *payload, size_t payload_len); |
| 111 | int rtmp_send_amf_command(const struct rtmp_conn *conn, |
| 112 | unsigned format, uint32_t chunk_id, |
| 113 | uint8_t type_id, |
| 114 | uint32_t msg_stream_id, |
| 115 | const uint8_t *cmd, size_t len); |
| 116 | unsigned rtmp_conn_assign_chunkid(struct rtmp_conn *conn); |
| 117 | uint64_t rtmp_conn_assign_tid(struct rtmp_conn *conn); |
| 118 | |
| 119 | |
| 120 | /* Client Transaction */ |
| 121 | |
| 122 | |
| 123 | struct rtmp_ctrans; |
| 124 | |
| 125 | int rtmp_ctrans_response(const struct list *ctransl, |
| 126 | const struct odict *msg); |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * RTMP Chunk |
| 131 | */ |
| 132 | |
| 133 | int rtmp_chunker(unsigned format, uint32_t chunk_id, |
| 134 | uint32_t timestamp, uint32_t timestamp_delta, |
| 135 | uint8_t msg_type_id, uint32_t msg_stream_id, |
| 136 | const uint8_t *payload, size_t payload_len, |
| 137 | size_t max_chunk_sz, struct tcp_conn *tc); |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * RTMP Header |
| 142 | */ |
| 143 | |
| 144 | int rtmp_header_encode(struct mbuf *mb, struct rtmp_header *hdr); |
| 145 | int rtmp_header_decode(struct rtmp_header *hdr, struct mbuf *mb); |
| 146 | int rtmp_header_print(struct re_printf *pf, const struct rtmp_header *hdr); |
| 147 | const char *rtmp_packet_type_name(enum rtmp_packet_type type); |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * RTMP De-chunker |
| 152 | */ |
| 153 | |
| 154 | struct rtmp_dechunker; |
| 155 | |
| 156 | typedef int (rtmp_dechunk_h)(const struct rtmp_header *hdr, |
| 157 | struct mbuf *mb, void *arg); |
| 158 | |
| 159 | int rtmp_dechunker_alloc(struct rtmp_dechunker **rdp, size_t chunk_sz, |
| 160 | rtmp_dechunk_h *chunkh, void *arg); |
| 161 | int rtmp_dechunker_receive(struct rtmp_dechunker *rd, struct mbuf *mb); |
| 162 | void rtmp_dechunker_set_chunksize(struct rtmp_dechunker *rd, size_t chunk_sz); |
| 163 | int rtmp_dechunker_debug(struct re_printf *pf, |
| 164 | const struct rtmp_dechunker *rd); |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * AMF (Action Message Format) |
| 169 | */ |
| 170 | |
| 171 | int rtmp_amf_encode_number(struct mbuf *mb, double val); |
| 172 | int rtmp_amf_encode_boolean(struct mbuf *mb, bool boolean); |
| 173 | int rtmp_amf_encode_string(struct mbuf *mb, const char *str); |
| 174 | int rtmp_amf_encode_null(struct mbuf *mb); |
| 175 | int rtmp_amf_vencode_object(struct mbuf *mb, enum rtmp_amf_type container, |
| 176 | unsigned propc, va_list *ap); |
| 177 | |
| 178 | int rtmp_amf_decode(struct odict **msgp, struct mbuf *mb); |