Ported AVMATCH - not yet tested

This commit is contained in:
saxon 2018-08-14 12:45:03 +09:30
parent 5a3383c27d
commit ceb954a328
1 changed files with 75 additions and 58 deletions

View File

@ -1875,7 +1875,7 @@ func C_HandleChangeChunkSize(r *C.RTMP, packet *C.RTMPPacket) {
//r.m_inChunkSize = C.int(C.AMF_DecodeInt32((*C.char)(unsafe.Pointer(packet.m_body))))
r.m_inChunkSize = C.int(C_AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body))))
// TODO use new logger here
// RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, r->m_inChunkSize);
// RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, r.m_inChunkSize);
}
}
@ -1886,7 +1886,7 @@ func C_HandlServerBW(r *C.RTMP, packet *C.RTMPPacket) {
r.m_nServerBW = C.int(C_AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body))))
//r.m_nServerBW = C.int(C.AMF_DecodeInt32((*C.char)(unsafe.Pointer(packet.m_body))))
// TODO use new logger here
// RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r->m_nServerBW);
// RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r.m_nServerBW);
}
// void HandleClientBW(RTMP* r, const RTMPPacket* packet);
@ -1902,43 +1902,51 @@ func C_HandleClientBW(r *C.RTMP, packet *C.RTMPPacket) {
r.m_nClientBW2 = 0
}
// TODO use new logger here
// RTMP_Log(RTMP_LOGDEBUG, "%s: client BW = %d %d", __FUNCTION__, r->m_nClientBW,
//r->m_nClientBW2);
// RTMP_Log(RTMP_LOGDEBUG, "%s: client BW = %d %d", __FUNCTION__, r.m_nClientBW,
//r.m_nClientBW2);
}
// int HandleInvoke(RTMP* r, const char* body, unsigned int nBodySize);
// rtmp.c +2912
func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
AMFObject obj;
AVal method;
double txn;
int ret = 0, nRes;
if (body[0] != 0x02){
var obj C.AMFObject
var method C.AVal
var txn float32
var ret int32 = 0
var nRes int32
if *body != 0x02{
// TODO use new logger here
//RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet",
//__FUNCTION__);
return 0;
return 0
}
nRes = AMF_Decode(&obj, body, nBodySize, FALSE);
if (nRes < 0){
RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__);
return 0;
// nRes = C.AMF_Decode(&obj, body, nBodySize, 0)
nRes = C_AMF_Decode(&obj, body, nBodySize, 0);
if nRes < 0 {
// TODO use new logger here
//RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__);
return 0
}
AMF_Dump(&obj);
AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method);
txn = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1));
RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val);
// TODO port this func
C.AMF_Dump(&obj);
// TODO port these two c funcs
C.AMFProp_GetString(C.AMF_GetProp(&obj, NULL, 0), &method);
// TODO port this func
txn = C.AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1));
// TODO use new logger here
// RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val);
switch{
case AVMATCH(&method, &av__result):
AVal methodInvoked = {0};
int i;
for i=0; i<r->m_numCalls; i++ {
if (r->m_methodCalls[i].num == (int)txn) {
methodInvoked = r->m_methodCalls[i].name;
AV_erase(r->m_methodCalls, &r->m_numCalls, i, FALSE);
for i=0; i<r.m_numCalls; i++ {
if (r.m_methodCalls[i].num == (int)txn) {
methodInvoked = r.m_methodCalls[i].name;
AV_erase(r.m_methodCalls, &r.m_numCalls, i, FALSE);
break;
}
}
@ -1952,14 +1960,14 @@ func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
methodInvoked.av_val);
switch {
case AVMATCH(&methodInvoked, &av_connect):
if (r->Link.token.av_len){
if (r.Link.token.av_len){
AMFObjectProperty p;
if (RTMP_FindFirstMatchingProperty(&obj, &av_secureToken, &p)){
DecodeTEA(&r->Link.token, &p.p_vu.p_aval);
DecodeTEA(&r.Link.token, &p.p_vu.p_aval);
SendSecureTokenResponse(r, &p.p_vu.p_aval);
}
}
if (r->Link.protocol & RTMP_FEATURE_WRITE){
if (r.Link.protocol & RTMP_FEATURE_WRITE){
SendReleaseStream(r);
SendFCPublish(r);
}else{
@ -1968,38 +1976,38 @@ func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
}
RTMP_SendCreateStream(r);
if (!(r->Link.protocol & RTMP_FEATURE_WRITE)){
if (!(r.Link.protocol & RTMP_FEATURE_WRITE)){
/* Authenticate on Justin.tv legacy servers before sending FCSubscribe */
if (r->Link.usherToken.av_len){
SendUsherToken(r, &r->Link.usherToken);
if (r.Link.usherToken.av_len){
SendUsherToken(r, &r.Link.usherToken);
}
/* Send the FCSubscribe if live stream or if subscribepath is set */
switch{
case r->Link.subscribepath.av_len:
SendFCSubscribe(r, &r->Link.subscribepath);
case r->Link.lFlags & RTMP_LF_LIV:
SendFCSubscribe(r, &r->Link.playpath);
case r.Link.subscribepath.av_len:
SendFCSubscribe(r, &r.Link.subscribepath);
case r.Link.lFlags & RTMP_LF_LIV:
SendFCSubscribe(r, &r.Link.playpath);
}
}
case AVMATCH(&methodInvoked, &av_createStream):
r->m_stream_id = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 3));
r.m_stream_id = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 3));
if (r->Link.protocol & RTMP_FEATURE_WRITE){
if (r.Link.protocol & RTMP_FEATURE_WRITE){
SendPublish(r);
} else {
if (r->Link.lFlags & RTMP_LF_PLST){
if (r.Link.lFlags & RTMP_LF_PLST){
SendPlaylist(r);
}
SendPlay(r);
RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS);
RTMP_SendCtrl(r, 3, r.m_stream_id, r.m_nBufferMS);
}
case AVMATCH(&methodInvoked, &av_play) ||
AVMATCH(&methodInvoked, &av_publish):
r->m_bPlaying = TRUE;
r.m_bPlaying = TRUE;
}
free(methodInvoked.av_val);
case (AVMATCH(&method, &av_onBWDone)):
if (!r->m_nBWCheckCounter){
if (!r.m_nBWCheckCounter){
SendCheckBW(r);
}
case AVMATCH(&method, &av_onFCSubscribe):
@ -2013,9 +2021,9 @@ func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
SendCheckBWResult(r, txn);
case AVMATCH(&method, &av__onbwdone):
int i;
for (i = 0; i < r->m_numCalls; i++){
if (AVMATCH(&r->m_methodCalls[i].name, &av__checkbw)){
AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE);
for (i = 0; i < r.m_numCalls; i++){
if (AVMATCH(&r.m_methodCalls[i].name, &av__checkbw)){
AV_erase(r.m_methodCalls, &r.m_numCalls, i, TRUE);
break;
}
}
@ -2035,25 +2043,25 @@ func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
|| AVMATCH(&code, &av_NetStream_Play_Failed)
|| AVMATCH(&code, &av_NetStream_Play_StreamNotFound)
|| AVMATCH(&code, &av_NetConnection_Connect_InvalidApp):
r->m_stream_id = -1;
r.m_stream_id = -1;
RTMP_Close(r);
RTMP_Log(RTMP_LOGERROR, "Closing connection: %s", code.av_val);
case AVMATCH(&code, &av_NetStream_Play_Start)
|| AVMATCH(&code, &av_NetStream_Play_PublishNotify):
int i;
r->m_bPlaying = TRUE;
for (i = 0; i < r->m_numCalls; i++){
if (AVMATCH(&r->m_methodCalls[i].name, &av_play)){
AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE);
r.m_bPlaying = TRUE;
for (i = 0; i < r.m_numCalls; i++){
if (AVMATCH(&r.m_methodCalls[i].name, &av_play)){
AV_erase(r.m_methodCalls, &r.m_numCalls, i, TRUE);
break;
}
}
case AVMATCH(&code, &av_NetStream_Publish_Start):
int i;
r->m_bPlaying = TRUE;
for (i = 0; i < r->m_numCalls; i++){
if (AVMATCH(&r->m_methodCalls[i].name, &av_publish)){
AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE);
r.m_bPlaying = TRUE;
for (i = 0; i < r.m_numCalls; i++){
if (AVMATCH(&r.m_methodCalls[i].name, &av_publish)){
AV_erase(r.m_methodCalls, &r.m_numCalls, i, TRUE);
break;
}
}
@ -2063,18 +2071,18 @@ func C_HandleInvoke(r *C.RTMP, body *byte, nBodySize uint32) int32 {
RTMP_Close(r);
ret = 1;
case AVMATCH(&code, &av_NetStream_Seek_Notify):
r->m_read.flags &= ~RTMP_READ_SEEKING;
r.m_read.flags &= ~RTMP_READ_SEEKING;
case AVMATCH(&code, &av_NetStream_Pause_Notify):
if (r->m_pausing == 1 || r->m_pausing == 2){
RTMP_SendPause(r, FALSE, r->m_pauseStamp);
r->m_pausing = 3;
if (r.m_pausing == 1 || r.m_pausing == 2){
RTMP_SendPause(r, FALSE, r.m_pauseStamp);
r.m_pausing = 3;
}
}
case AVMATCH(&method, &av_playlist_ready):
int i;
for (i = 0; i < r->m_numCalls; i++){
if (AVMATCH(&r->m_methodCalls[i].name, &av_set_playlist)){
AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE);
for (i = 0; i < r.m_numCalls; i++){
if (AVMATCH(&r.m_methodCalls[i].name, &av_set_playlist)){
AV_erase(r.m_methodCalls, &r.m_numCalls, i, TRUE);
break;
}
}
@ -2084,6 +2092,15 @@ leave:
AMF_Reset(&obj);
return ret;
}
// #define AVMATCH(a1,a2)
// amf.h +63
func C_AVMATCH(a1, a2 C.AVal) int32 {
if a1.av_len == a2.av_len && memcmp(unsafe.Pointer(a1.av_val), unsafe.Pointer(a2.av_val), a1.av_len) {
return 1
} else {
return 0
}
}
// void RTMPPacket_Free(RTMPPacket* p);