On Mon, 9 Apr 2012, Samuel Pitoiset wrote:

This allows using connection parameters in order to append arbitrary
AMF data like "B:1 S:authMe O:1 NN:code:1.23 NS:flag:ok O:0" to the
Connect message.
---
libavformat/rtmpproto.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)

diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index 491453e..823587e 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -66,6 +66,7 @@ typedef struct RTMPContext {
    int           is_input;                   ///< input/output flag
    char          playpath[256];              ///< path to filename to play (with 
possible "mp4:" prefix)
    char          *app;                       ///< name of application
+    char          *conn;                      ///< arbitrary AMF data
    ClientState   state;                      ///< current state
    int           main_channel_id;            ///< an additional channel ID 
which is used for some invocations
    uint8_t*      flv_data;                   ///< buffer with data for demuxer
@@ -105,6 +106,65 @@ static const uint8_t rtmp_server_key[] = {
    0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
};

+static int rtmp_write_amf_data(char *param, uint8_t *p)
+{
+    char *field, *value, *saveptr;
+    char type;
+
+    /* The type must be B for Boolean, N for number, S for string, O for
+     * object, or Z for null. For Booleans the data must be either 0 or 1 for
+     * FALSE or TRUE, respectively. Likewise for Objects the data must be
+     * 0 or 1 to end or begin an object, respectively. Data items in subobjects
+     * may be named, by prefixing the type with 'N' and specifying the name
+     * before the value (ie. NB:myFlag:1). This option may be used multiple 
times
+     * to construct arbitrary AMF sequences. */
+    if (param[1] == ':') {
+        type = param[0];
+        value = param + 2;
+    } else if (param[0] == 'N' && param[2] == ':') {
+        type = param[1];
+        field = strtok_r(param + 3, ":", &saveptr);
+        value = strtok_r(NULL, ":", &saveptr);
+
+        if (!field || !value)
+            goto fail;
+
+        ff_amf_write_field_name(&p, field);
+    } else {
+        goto fail;
+    }
+
+    switch (type) {
+        case 'B':
+            ff_amf_write_bool(&p, value[0] != '0');
+            break;
+        case 'S':
+            ff_amf_write_string(&p, value);
+            break;
+        case 'N':
+            ff_amf_write_number(&p, strtod(value, NULL));
+            break;
+        case 'Z':
+            ff_amf_write_null(&p);
+            break;
+        case 'O':
+            if (value[0] != '0')
+                ff_amf_write_object_start(&p);
+            else
+                ff_amf_write_object_end(&p);
+            break;
+        default:
+            goto fail;
+            break;
+    }
+
+    return 0;
+
+fail:
+    av_log(NULL, AV_LOG_ERROR, "Invalid AMF parameter: %s\n", param);
+    return -1;
+}
+
/**
 * Generate 'connect' call and send it to the server.
 */
@@ -114,6 +174,7 @@ static void gen_connect(URLContext *s, RTMPContext *rt, 
const char *proto,
    RTMPPacket pkt;
    uint8_t ver[64], *p;
    char tcurl[512];
+    char *param, *saveptr;

    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 4096);
    p = pkt.data;
@@ -155,6 +216,19 @@ static void gen_connect(URLContext *s, RTMPContext *rt, 
const char *proto,
    }
    ff_amf_write_object_end(&p);

+    if (rt->conn) {
+        // Write arbitraty AMF data to the Connect message.
+        param = strtok_r(rt->conn, " ", &saveptr);
+        while (param != NULL) {
+            if (rtmp_write_amf_data(param, p) < 0) {
+                // Invalid AMF parameter.
+                break;
+            }
+
+            param = strtok_r(NULL, " ", &saveptr);
+        }
+    }
+
    pkt.data_size = p - pkt.data;

    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
@@ -1040,6 +1114,7 @@ static int rtmp_write(URLContext *s, const uint8_t *buf, 
int size)

static const AVOption rtmp_options[] = {
    {"app", "Name of application to connect to on the RTMP server", 
OFFSET(app), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
+    {"conn", "Append arbitrary AMF data to the Connect message", OFFSET(conn), 
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
    { NULL },
};

--
1.7.10

Looks ok to me, although I didn't test it. Luca?

// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to