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