James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_rtmp.h Interface to Real Time Messaging Protocol (RTMP) |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | /** RTMP Protocol values */ |
| 9 | enum { |
| 10 | RTMP_PORT = 1935, |
| 11 | }; |
| 12 | |
| 13 | /** RTMP Stream IDs */ |
| 14 | enum { |
| 15 | |
| 16 | /* User Control messages SHOULD use message stream ID 0 |
| 17 | (known as the control stream) */ |
| 18 | RTMP_CONTROL_STREAM_ID = 0 |
| 19 | }; |
| 20 | |
| 21 | /** RTMP Packet types */ |
| 22 | enum rtmp_packet_type { |
| 23 | RTMP_TYPE_SET_CHUNK_SIZE = 1, /**< Set Chunk Size */ |
| 24 | RTMP_TYPE_ACKNOWLEDGEMENT = 3, /**< Acknowledgement */ |
| 25 | RTMP_TYPE_USER_CONTROL_MSG = 4, /**< User Control Messages */ |
| 26 | RTMP_TYPE_WINDOW_ACK_SIZE = 5, /**< Window Acknowledgement Size */ |
| 27 | RTMP_TYPE_SET_PEER_BANDWIDTH = 6, /**< Set Peer Bandwidth */ |
| 28 | RTMP_TYPE_AUDIO = 8, /**< Audio Message */ |
| 29 | RTMP_TYPE_VIDEO = 9, /**< Video Message */ |
| 30 | RTMP_TYPE_DATA = 18, /**< Data Message */ |
| 31 | RTMP_TYPE_AMF0 = 20, /**< Action Message Format (AMF) */ |
| 32 | }; |
| 33 | |
| 34 | /** RTMP AMF types */ |
| 35 | enum rtmp_amf_type { |
| 36 | RTMP_AMF_TYPE_ROOT = -1, /**< Special internal type */ |
| 37 | RTMP_AMF_TYPE_NUMBER = 0x00, /**< Number Type */ |
| 38 | RTMP_AMF_TYPE_BOOLEAN = 0x01, /**< Boolean Type */ |
| 39 | RTMP_AMF_TYPE_STRING = 0x02, /**< String Type */ |
| 40 | RTMP_AMF_TYPE_OBJECT = 0x03, /**< Object Type */ |
| 41 | RTMP_AMF_TYPE_NULL = 0x05, /**< Null type */ |
| 42 | RTMP_AMF_TYPE_ECMA_ARRAY = 0x08, /**< ECMA 'associative' Array */ |
| 43 | RTMP_AMF_TYPE_OBJECT_END = 0x09, /**< Object End Type */ |
| 44 | RTMP_AMF_TYPE_STRICT_ARRAY = 0x0a, /**< Array with ordinal indices */ |
| 45 | }; |
| 46 | |
| 47 | /** RTMP Event types */ |
| 48 | enum rtmp_event_type { |
| 49 | RTMP_EVENT_STREAM_BEGIN = 0, /**< Stream begin */ |
| 50 | RTMP_EVENT_STREAM_EOF = 1, /**< Stream End-Of-File */ |
| 51 | RTMP_EVENT_STREAM_DRY = 2, /**< No more data on the stream */ |
| 52 | RTMP_EVENT_SET_BUFFER_LENGTH = 3, /**< Set buffer size in [ms] */ |
| 53 | RTMP_EVENT_STREAM_IS_RECORDED = 4, /**< Stream is recorded */ |
| 54 | RTMP_EVENT_PING_REQUEST = 6, /**< Ping Request from server */ |
| 55 | RTMP_EVENT_PING_RESPONSE = 7, /**< Ping Response to server */ |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | /* forward declarations */ |
| 60 | struct dnsc; |
| 61 | struct odict; |
| 62 | struct tcp_sock; |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * RTMP High-level API (connection, stream) |
| 67 | */ |
| 68 | |
| 69 | |
| 70 | /* conn */ |
| 71 | struct rtmp_conn; |
| 72 | |
| 73 | typedef void (rtmp_estab_h)(void *arg); |
| 74 | typedef void (rtmp_command_h)(const struct odict *msg, void *arg); |
| 75 | typedef void (rtmp_close_h)(int err, void *arg); |
| 76 | |
| 77 | int rtmp_connect(struct rtmp_conn **connp, struct dnsc *dnsc, const char *uri, |
| 78 | rtmp_estab_h *estabh, rtmp_command_h *cmdh, |
| 79 | rtmp_close_h *closeh, void *arg); |
| 80 | int rtmp_accept(struct rtmp_conn **connp, struct tcp_sock *ts, |
| 81 | rtmp_command_h *cmdh, rtmp_close_h *closeh, void *arg); |
| 82 | int rtmp_control(const struct rtmp_conn *conn, |
| 83 | enum rtmp_packet_type type, ...); |
| 84 | void rtmp_set_handlers(struct rtmp_conn *conn, rtmp_command_h *cmdh, |
| 85 | rtmp_close_h *closeh, void *arg); |
| 86 | struct tcp_conn *rtmp_conn_tcpconn(const struct rtmp_conn *conn); |
| 87 | const char *rtmp_conn_stream(const struct rtmp_conn *conn); |
| 88 | int rtmp_conn_debug(struct re_printf *pf, const struct rtmp_conn *conn); |
| 89 | |
| 90 | |
| 91 | typedef void (rtmp_resp_h)(bool success, const struct odict *msg, |
| 92 | void *arg); |
| 93 | |
| 94 | /* amf */ |
| 95 | int rtmp_amf_command(const struct rtmp_conn *conn, uint32_t stream_id, |
| 96 | const char *command, |
| 97 | unsigned body_propc, ...); |
| 98 | int rtmp_amf_request(struct rtmp_conn *conn, uint32_t stream_id, |
| 99 | const char *command, |
| 100 | rtmp_resp_h *resph, void *arg, unsigned body_propc, ...); |
| 101 | int rtmp_amf_reply(struct rtmp_conn *conn, uint32_t stream_id, bool success, |
| 102 | const struct odict *req, |
| 103 | unsigned body_propc, ...); |
| 104 | int rtmp_amf_data(const struct rtmp_conn *conn, uint32_t stream_id, |
| 105 | const char *command, unsigned body_propc, ...); |
| 106 | |
| 107 | |
| 108 | /* stream */ |
| 109 | struct rtmp_stream; |
| 110 | |
| 111 | typedef void (rtmp_control_h)(enum rtmp_event_type event, struct mbuf *mb, |
| 112 | void *arg); |
| 113 | typedef void (rtmp_audio_h)(uint32_t timestamp, |
| 114 | const uint8_t *pld, size_t len, void *arg); |
| 115 | typedef void (rtmp_video_h)(uint32_t timestamp, |
| 116 | const uint8_t *pld, size_t len, void *arg); |
| 117 | |
| 118 | int rtmp_stream_alloc(struct rtmp_stream **strmp, struct rtmp_conn *conn, |
| 119 | uint32_t stream_id, rtmp_command_h *cmdh, |
| 120 | rtmp_control_h *ctrlh, rtmp_audio_h *auh, |
| 121 | rtmp_video_h *vidh, rtmp_command_h *datah, |
| 122 | void *arg); |
| 123 | int rtmp_stream_create(struct rtmp_stream **strmp, struct rtmp_conn *conn, |
| 124 | rtmp_resp_h *resph, rtmp_command_h *cmdh, |
| 125 | rtmp_control_h *ctrlh, rtmp_audio_h *auh, |
| 126 | rtmp_video_h *vidh, rtmp_command_h *datah, |
| 127 | void *arg); |
| 128 | int rtmp_play(struct rtmp_stream *strm, const char *name); |
| 129 | int rtmp_publish(struct rtmp_stream *strm, const char *name); |
| 130 | int rtmp_meta(struct rtmp_stream *strm); |
| 131 | int rtmp_send_audio(struct rtmp_stream *strm, uint32_t timestamp, |
| 132 | const uint8_t *pld, size_t len); |
| 133 | int rtmp_send_video(struct rtmp_stream *strm, uint32_t timestamp, |
| 134 | const uint8_t *pld, size_t len); |
| 135 | struct rtmp_stream *rtmp_stream_find(const struct rtmp_conn *conn, |
| 136 | uint32_t stream_id); |
| 137 | |
| 138 | |
| 139 | const char *rtmp_event_name(enum rtmp_event_type event); |