rtmp: reduce CAV/AVC conversions

This commit is contained in:
Dan Kortschak 2018-09-06 18:31:14 +09:30
parent 42f7316f09
commit b1c904ee22
1 changed files with 94 additions and 91 deletions

View File

@ -66,59 +66,63 @@ const (
length = 512
)
// av_setDataFrame is a static const global in rtmp.c
// TODO(kortschak): Reduce this list.
var (
AV_empty = AVC("")
setDataFrame = AVC("@setDataFrame")
av_connect = AVC("connect")
av_app = AVC("app")
av_type = AVC("type")
av_nonprivate = AVC("nonprivate")
av_flashVer = AVC("flashVer")
av_swfUrl = AVC("swfUrl")
av_tcUrl = AVC("tcUrl")
av_fpad = AVC("fpad")
av_capabilities = AVC("capabilities")
av_audioCodecs = AVC("audioCodecs")
av_videoCodecs = AVC("videoCodecs")
av_videoFunction = AVC("videoFunction")
av_pageUrl = AVC("pageUrl")
av_objectEncoding = AVC("objectEncoding")
av__result = AVC("_result")
av_secureToken = AVC("secureToken")
av_createStream = AVC("createStream")
av_releaseStream = AVC("releaseStream")
av_play = AVC("play")
av_publish = AVC("publish")
av_onBWDone = AVC("onBWDone")
av_onFCSubscribe = AVC("onFCSubscribe")
av_onFCUnsubscribe = AVC("onFCUnsubscribe")
av__onbwcheck = AVC("_onbwcheck")
av__onbwdone = AVC("_onbwdone")
av_ping = AVC("ping")
av__checkbw = AVC("_checkbw")
av_close = AVC("close")
av_onStatus = AVC("onStatus")
av_code = AVC("code")
av_level = AVC("level")
av_NetStream_Failed = AVC("NetStream.Failed")
av_NetStream_Play_Failed = AVC("NetStream.Play.Failed")
av_NetConnection_Connect_InvalidApp = AVC("NetConnection.Connect.InvalidApp")
av_NetStream_Play_StreamNotFound = AVC("NetStream.Play.StreamNotFound")
av_NetStream_Play_Start = AVC("NetStream.Play.Start")
av_NetStream_Play_PublishNotify = AVC("NetStream.Play.PublishNotify")
av_NetStream_Publish_Start = AVC("NetStream.Publish.Start")
av_NetStream_Play_Complete = AVC("NetStream.Play.Complete")
av_NetStream_Play_Stop = AVC("NetStream.Play.Stop")
av_NetStream_Play_UnpublishNotify = AVC("NetStream.Play.UnpublishNotify")
av_NetStream_Seek_Notify = AVC("NetStream.Seek.Notify")
av_NetStream_Pause_Notify = AVC("NetStream.Pause.Notify")
av_playlist_ready = AVC("playlist_ready")
av_secureToken = AVC("secureToken")
av_set_playlist = AVC("set_playlist")
av_FCPublish = AVC("FCPublish")
av_FCUnpublish = AVC("FCUnpublish")
av_live = AVC("live")
av_deleteStream = AVC("deleteStream")
)
const (
// av_setDataFrame is a static const global in rtmp.c
setDataFrame = "@setDataFrame"
av__checkbw = "_checkbw"
av__onbwcheck = "_onbwcheck"
av__onbwdone = "_onbwdone"
av__result = "_result"
av_app = "app"
av_audioCodecs = "audioCodecs"
av_capabilities = "capabilities"
av_close = "close"
av_code = "code"
av_deleteStream = "deleteStream"
av_FCPublish = "FCPublish"
av_FCUnpublish = "FCUnpublish"
av_flashVer = "flashVer"
av_fpad = "fpad"
av_level = "level"
av_live = "live"
av_NetConnection_Connect_InvalidApp = "NetConnection.Connect.InvalidApp"
av_NetStream_Failed = "NetStream.Failed"
av_NetStream_Pause_Notify = "NetStream.Pause.Notify"
av_NetStream_Play_Complete = "NetStream.Play.Complete"
av_NetStream_Play_Failed = "NetStream.Play.Failed"
av_NetStream_Play_PublishNotify = "NetStream.Play.PublishNotify"
av_NetStream_Play_Start = "NetStream.Play.Start"
av_NetStream_Play_Stop = "NetStream.Play.Stop"
av_NetStream_Play_StreamNotFound = "NetStream.Play.StreamNotFound"
av_NetStream_Play_UnpublishNotify = "NetStream.Play.UnpublishNotify"
av_NetStream_Publish_Start = "NetStream.Publish.Start"
av_NetStream_Seek_Notify = "NetStream.Seek.Notify"
av_nonprivate = "nonprivate"
av_objectEncoding = "objectEncoding"
av_onBWDone = "onBWDone"
av_onFCSubscribe = "onFCSubscribe"
av_onFCUnsubscribe = "onFCUnsubscribe"
av_onStatus = "onStatus"
av_pageUrl = "pageUrl"
av_ping = "ping"
av_playlist_ready = "playlist_ready"
av_releaseStream = "releaseStream"
av_swfUrl = "swfUrl"
av_tcUrl = "tcUrl"
av_type = "type"
av_videoCodecs = "videoCodecs"
av_videoFunction = "videoFunction"
)
var RTMPT_cmds = []string{
@ -746,60 +750,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1)))
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_app), CAV(&r.Link.app))
enc = C_AMF_EncodeNamedString(enc, pend, av_app, CAV(&r.Link.app))
if enc == nil {
return false
}
if r.Link.protocol&RTMP_FEATURE_WRITE != 0 {
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_type), CAV(&av_nonprivate))
enc = C_AMF_EncodeNamedString(enc, pend, av_type, av_nonprivate)
if enc == nil {
return false
}
}
if r.Link.flashVer.av_len != 0 {
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_flashVer), CAV(&r.Link.flashVer))
enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, CAV(&r.Link.flashVer))
if enc == nil {
return false
}
}
if r.Link.swfUrl.av_len != 0 {
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_swfUrl), CAV(&r.Link.swfUrl))
enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, CAV(&r.Link.swfUrl))
if enc == nil {
return false
}
}
if r.Link.tcUrl.av_len != 0 {
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_tcUrl), CAV(&r.Link.tcUrl))
enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, CAV(&r.Link.tcUrl))
if enc == nil {
return false
}
}
if r.Link.protocol&RTMP_FEATURE_WRITE == 0 {
enc = C_AMF_EncodeNamedBoolean(enc, pend, CAV(&av_fpad), false)
enc = C_AMF_EncodeNamedBoolean(enc, pend, av_fpad, false)
if enc == nil {
return false
}
enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_capabilities), 15)
enc = C_AMF_EncodeNamedNumber(enc, pend, av_capabilities, 15)
if enc == nil {
return false
}
enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_audioCodecs), float64(r.m_fAudioCodecs))
enc = C_AMF_EncodeNamedNumber(enc, pend, av_audioCodecs, float64(r.m_fAudioCodecs))
if enc == nil {
return false
}
enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoCodecs), float64(r.m_fVideoCodecs))
enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoCodecs, float64(r.m_fVideoCodecs))
if enc == nil {
return false
}
enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoFunction), 1)
enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoFunction, 1)
if enc == nil {
return false
}
if r.Link.pageUrl.av_len != 0 {
enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_pageUrl), CAV(&r.Link.pageUrl))
enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, CAV(&r.Link.pageUrl))
if enc == nil {
return false
}
@ -807,7 +811,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
}
if r.m_fEncoding != 0.0 || r.m_bSendEncoding != 0 {
enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_objectEncoding), float64(r.m_fEncoding))
enc = C_AMF_EncodeNamedNumber(enc, pend, av_objectEncoding, float64(r.m_fEncoding))
if enc == nil {
return false
}
@ -903,7 +907,7 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) {
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE]
enc = (*byte)(unsafe.Pointer(packet.m_body))
enc = C_AMF_EncodeString(enc, pend, CAV(&av_releaseStream))
enc = C_AMF_EncodeString(enc, pend, av_releaseStream)
r.m_numInvokes++
enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes))
*enc = AMF_NULL
@ -936,7 +940,7 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) {
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE]
enc = (*byte)(unsafe.Pointer(packet.m_body))
enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCPublish))
enc = C_AMF_EncodeString(enc, pend, av_FCPublish)
r.m_numInvokes++
enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes))
*enc = AMF_NULL
@ -969,7 +973,7 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) {
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE]
enc = (*byte)(unsafe.Pointer(packet.m_body))
enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCUnpublish))
enc = C_AMF_EncodeString(enc, pend, av_FCUnpublish)
r.m_numInvokes++
enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes))
*enc = AMF_NULL
@ -1015,7 +1019,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) {
return false
}
enc = C_AMF_EncodeString(enc, pend, CAV(&av_live))
enc = C_AMF_EncodeString(enc, pend, av_live)
if enc == nil {
return false
}
@ -1044,7 +1048,7 @@ func C_SendDeleteStream(r *C_RTMP, dStreamId float64) (ok bool) {
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE]
enc = (*byte)(unsafe.Pointer(packet.m_body))
enc = C_AMF_EncodeString(enc, pend, CAV(&av_deleteStream))
enc = C_AMF_EncodeString(enc, pend, av_deleteStream)
r.m_numInvokes++
enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes))
*enc = AMF_NULL
@ -1100,7 +1104,7 @@ func C_SendCheckBW(r *C_RTMP) (ok bool) {
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE]
enc = (*byte)(unsafe.Pointer(packet.m_body))
enc = C_AMF_EncodeString(enc, pend, CAV(&av__checkbw))
enc = C_AMF_EncodeString(enc, pend, av__checkbw)
r.m_numInvokes++
enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes))
*enc = AMF_NULL
@ -1180,7 +1184,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) {
// RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val);
switch method {
case CAV(&av__result):
case av__result:
var methodInvoked C_AVal
for i := int32(0); i < r.m_numCalls; i++ {
if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i),
@ -1233,49 +1237,48 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) {
}
//C.free(unsafe.Pointer(methodInvoked.av_val))
case CAV(&av_onBWDone):
case av_onBWDone:
if r.m_nBWCheckCounter == 0 {
C_SendCheckBW(r)
}
case CAV(&av_onFCUnsubscribe), CAV(&av_onFCSubscribe):
case av_onFCUnsubscribe, av_onFCSubscribe:
panic("Unsupported method av_onFCUnsubscribe/av_onFCSubscribe")
case CAV(&av_ping):
case av_ping:
panic("Unsupported method av_ping")
case CAV(&av__onbwcheck):
case av__onbwcheck:
panic("Unsupported method av_onbwcheck")
case CAV(&av__onbwdone):
case av__onbwdone:
panic("Unsupported method av_onbwdone")
case CAV(&av_close):
case av_close:
panic("Unsupported method av_close")
case CAV(&av_onStatus):
case av_onStatus:
var obj2 C_AMFObject
C_AMFProp_GetObject(C_AMF_GetProp(&obj, "", 3), &obj2)
code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_code), -1))
code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_code, -1))
level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_level), -1)) // Not used.
level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_level, -1)) // Not used.
_ = level
// TODO use new logger
// RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val);
switch code {
case CAV(&av_NetStream_Failed), CAV(&av_NetStream_Play_Failed),
CAV(&av_NetStream_Play_StreamNotFound), CAV(&av_NetConnection_Connect_InvalidApp):
case av_NetStream_Failed, av_NetStream_Play_Failed,
av_NetStream_Play_StreamNotFound, av_NetConnection_Connect_InvalidApp:
panic("Unsupported method av_NetStream/av_NetStream_Play_Failed/av_netSTream_Play_StreamNotFound/av_netConnection_Connect_invalidApp")
case CAV(&av_NetStream_Play_Start), CAV(&av_NetStream_Play_PublishNotify):
case av_NetStream_Play_Start, av_NetStream_Play_PublishNotify:
panic("Unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify")
case CAV(&av_NetStream_Publish_Start):
var i int32
case av_NetStream_Publish_Start:
r.m_bPlaying = true
for i = 0; i < int32(r.m_numCalls); i++ {
for i := int32(0); i < r.m_numCalls; i++ {
if C_AVMATCH(&(*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i),
int(unsafe.Sizeof(*r.m_methodCalls))))).name, &av_publish) != 0 {
C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 1)
@ -1283,17 +1286,17 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) {
}
}
case CAV(&av_NetStream_Play_Complete), CAV(&av_NetStream_Play_Stop), CAV(&av_NetStream_Play_UnpublishNotify):
case av_NetStream_Play_Complete, av_NetStream_Play_Stop, av_NetStream_Play_UnpublishNotify:
panic("Unsupported method av_NetStream_Play_Complete/av_NetStream_Play_Stop/av_NetStream_Play_UnpublishNotify")
case CAV(&av_NetStream_Seek_Notify):
case av_NetStream_Seek_Notify:
panic("Unsupported method av_netStream_Seek_Notify")
case CAV(&av_NetStream_Pause_Notify):
case av_NetStream_Pause_Notify:
panic("Unsupported method av_NetStream_Pause_Notify")
}
case CAV(&av_playlist_ready):
case av_playlist_ready:
panic("Unsupported method av_playlist_ready")
default:
@ -2065,7 +2068,7 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int {
if pkt.m_packetType == RTMP_PACKET_TYPE_INFO {
enc = (*[_Gi]byte)(unsafe.Pointer(C_AMF_EncodeString((*byte)(unsafe.Pointer(&enc[0])),
(*byte)(unsafe.Pointer(&pend[0])), CAV(&setDataFrame))))[:pkt.m_nBodySize]
(*byte)(unsafe.Pointer(&pend[0])), setDataFrame)))[:pkt.m_nBodySize]
// TODO: work out what to do with this
pkt.m_nBytesRead = uint32(float64(uintptr(unsafe.Pointer(&enc[0])) -