在使用flash cs/builder开发rtmp视频应用时,我们时常会在nc.connect的时候再传递一个对象作为连接信息发送给服务端。用于验证,确认连接用户,获取用户信息等等操作。
当我们使用ffmpeg来开发android,ios,pc版的rtmp应用时,已经为我们提供了非常方便的传参来实现。
先来看源代码:定位到libavformat/rtmpproto.c文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
static int rtmp_write_amf_data(URLContext *s, char *param, uint8_t **p) { char *field, *value; 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[0] && param[1] == ':') { type = param[0]; value = param + 2; } else if (param[0] == 'N' && param[1] && param[2] == ':') { type = param[1]; field = param + 3; value = strchr(field, ':'); if (!value) goto fail; *value = ' '; value++; 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(s, AV_LOG_ERROR, "Invalid AMF parameter: %sn", param); return AVERROR(EINVAL); } |
可以看出,支持基本的String,Boolean,Number,Null,Object
好,我们先用flash创建包含一段信息的连接
1 2 3 4 5 6 |
var myObj = new Object(); myObj.myId = 21; myObj.myName = 'ALiang'; myObj.isVIP = true; nc.connect(StringUtil.trim(uriInput.text),myObj); |
在ffmpeg中,按规则,书写为:
1 |
"O:1 NN:myId:22 NS:myName:YHLiang NB:isVIIP:0 O:0" |
碉堡了:D
原创文章,转载请注明: 转载自贝壳博客
本文链接地址: 使用FFmpeg连接rtmp时发送自定义AMF DATA