#include "com/amazonaws/kinesis/video/webrtcclient/Include.h"
#include "amazon_webrtc_interface.h"
#define AMAZON_WEBRTC_CONNECTION_INVALID_RET(pstConnect, ret) \
if((PTR_NULL == pstConnect)) \
{ \
printf("%s|%d ERROR connection valid check failed \n",__FUNCTION__,__LINE__); \
return ret; \
}
typedef struct
{
void *pCbPrvAddr;
PRtcPeerConnection peer;
PRtcDataChannel datachannel;
PRtcRtpTransceiver videoTransceiver;
PRtcRtpTransceiver audioTransceiver;
RtcSessionDescriptionInit offerSessionDescriptionInit;
RtcSessionDescriptionInit answerSessionDescriptionInit;
WEBRTCONFRAME RtcOnFrame;
WEBRTCONICECANDIDATE RtcOnIceCandidateCb;
WEBRTCONCONNECTIONSTATECHANGE RtcOnConnectionStateChangeCb;
WEBRTCONMESSAGE RtcOnMessage;
}WEBRTC_AMAZON_CONNECTION;
int WebRtc_AmazonInit()
{
initKvsWebRtc();
return _RET_OK;
}
int WebRtc_AmazonDestory()
{
deinitKvsWebRtc();
return _RET_OK;
}
static RTC_CODEC AudioCodec2RtcCodec(WEBRTC_AUDIO_TYPE enAudio)
{
switch (enAudio)
{
case WEBRTC_OPUS:
return RTC_CODEC_OPUS;
case WEBRTC_VP8:
return RTC_CODEC_VP8;
case WEBRTC_MULAW:
return RTC_CODEC_MULAW;
case WEBRTC_ALAW:
return RTC_CODEC_ALAW;
default :
return RTC_CODEC_MULAW;
}
}
void WebRtcOnFrame(UINT64 prvData, PFrame pInFrame)
{
WEBRTC_FRAME frame;
WEBRTC_AMAZON_CONNECTION *pstConnect = (WEBRTC_AMAZON_CONNECTION *)prvData;
AMAZON_WEBRTC_CONNECTION_INVALID_RET(pstConnect, ITRD_RET_NONE);
if (pstConnect->RtcOnFrame)
{
frame.index = pInFrame->index;
frame.decodingTs = pInFrame->decodingTs;
frame.presentationTs = pInFrame->presentationTs;
frame.size = pInFrame->size;
frame.frameData = pInFrame->frameData;
pstConnect->RtcOnFrame(pstConnect->pCbPrvAddr, &frame);
}
return ;
}
void WebRtcOnIceCandidate(UINT64 prvData, PCHAR pucJson)
{
WEBRTC_AMAZON_CONNECTION *pstConnect = (WEBRTC_AMAZON_CONNECTION *)prvData;
// printf("WEBRTC_CLIENT Connection:[%p] receive candidate:%s \n", pstConnect, pucJson );
AMAZON_WEBRTC_CONNECTION_INVALID_RET(pstConnect, ITRD_RET_NONE);
if(pstConnect->RtcOnIceCandidateCb)
pstConnect->RtcOnIceCandidateCb(pstConnect->pCbPrvAddr, pucJson);
return;
}
void WebRtcOnConnectionStateChange(UINT64 prvData, RTC_PEER_CONNECTION_STATE eConnectStatus)
{
WEBRTC_AMAZON_CONNECTION *pstConnect = (WEBRTC_AMAZON_CONNECTION *)prvData;
AMAZON_WEBRTC_CONNECTION_INVALID_RET(pstConnect, ITRD_RET_NONE);
if(pstConnect->RtcOnConnectionStateChangeCb)
pstConnect->RtcOnConnectionStateChangeCb(pstConnect->pCbPrvAddr, eConnectStatus);
printf("WEBRTC_CLIENT Connection:[%p] prv:[%p] StateChange \n",pstConnect,pstConnect->pCbPrvAddr);
return;
}
int WebRtc_AmazonCreateConnection(void **ppInst, void *pCbPrvAddr,int bVideo, WEBRTC_AUDIO_TYPE enAudio,
WEBRTCONICECANDIDATE RtcOnIceCandidateCb,WEBRTCONCONNECTIONSTATECHANGE RtcOnStateChangeCb,WEBRTCONFRAME RtcOnFrame)
{
int iRet = 0;
WEBRTC_AMAZON_CONNECTION *pstConnect = PTR_NULL;
RtcConfiguration conf;
UINT64 customData = 0;
if (PTR_NULL == ppInst)
return -1;
pstConnect = (WEBRTC_AMAZON_CONNECTION *)malloc(sizeof(WEBRTC_AMAZON_CONNECTION));
if (PTR_NULL == pstConnect)
return -2;
memset(&conf, 0, sizeof(RtcConfiguration));
memset(pstConnect, 0, sizeof(WEBRTC_AMAZON_CONNECTION));
conf.iceTransportPolicy = ICE_TRANSPORT_POLICY_ALL;
strcpy(conf.iceServers[0].urls, "stun:77.72.169.213:3478");
// strcpy(conf_.iceServers[1].urls, "stun:stun.l.google.com:19302"); // ubuntu gethosename error
strcpy(conf.iceServers[1].urls, "stun:172.217.211.127:19302");
if (createPeerConnection(&conf, &pstConnect->peer))
{
iRet = -3;
goto CLEANUP;
}
customData = (UINT64)pstConnect;
if (peerConnectionOnIceCandidate(pstConnect->peer, customData, WebRtcOnIceCandidate))
{
iRet = -4;
goto CLEANUP;
}
pstConnect->RtcOnIceCandidateCb = RtcOnIceCandidateCb;
if (peerConnectionOnConnectionStateChange(pstConnect->peer, customData, WebRtcOnConnectionStateChange))
{
iRet = -5;
goto CLEANUP;
}
pstConnect->RtcOnConnectionStateChangeCb = RtcOnStateChangeCb;
if (bVideo)
{
RtcMediaStreamTrack videoTrack ;
RtcRtpTransceiverInit videoTransceiverInit;
memset(&videoTrack, 0, sizeof(RtcMediaStreamTrack));
videoTrack.codec = RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE;
videoTrack.kind = MEDIA_STREAM_TRACK_KIND_VIDEO;
strcpy(videoTrack.streamId, "videoStream");
strcpy(videoTrack.trackId, "videoTrack");
videoTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
addSupportedCodec(pstConnect->peer, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE);
addTransceiver(pstConnect->peer, &videoTrack, &videoTransceiverInit, &pstConnect->videoTransceiver);
}
if (enAudio)
{
RtcMediaStreamTrack audioTrack;
RtcRtpTransceiverInit audioTransceiverInit;
RTC_CODEC codec = AudioCodec2RtcCodec(enAudio);
memset(&audioTrack, 0, sizeof(RtcMediaStreamTrack));
audioTrack.codec = codec;
audioTrack.kind = MEDIA_STREAM_TRACK_KIND_AUDIO;
strcpy(audioTrack.streamId, "audioStream");
strcpy(audioTrack.trackId, "audioTrack");
if (RtcOnFrame)
audioTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV;
else
audioTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
addSupportedCodec(pstConnect->peer, codec);
addTransceiver(pstConnect->peer, &audioTrack, &audioTransceiverInit, &pstConnect->audioTransceiver);
if (RtcOnFrame)
{
pstConnect->RtcOnFrame = RtcOnFrame;
transceiverOnFrame(pstConnect->audioTransceiver, customData, WebRtcOnFrame);
}
}
pstConnect->pCbPrvAddr = pCbPrvAddr;
*ppInst = pstConnect;
printf("WEBRTC_CLIENT Connection:[%p] prv:[%p] videoTransCeiver:%p audioTranseiver:%p\n",pstConnect,pCbPrvAddr, pstConnect->videoTransceiver,pstConnect->audioTransceiver);
return 0;
CLEANUP:
if (pstConnect)
{
if (pstConnect->peer)
{
closePeerConnection(pstConnect->peer);
if (pstConnect->videoTransceiver)
{
freeTransceiver(&pstConnect->videoTransceiver);
}
if (pstConnect->audioTransceiver)
{
freeTransceiver(&pstConnect->audioTransceiver);
}
freePeerConnection(&pstConnect->peer);
}
free(pstConnect);
pstConnect = PTR_NULL;
}
return iRet;
}
int WebRtc_AmazonDeleteConnection(void *pInst)
{
WEBRTC_AMAZON_CONNECTION *pstConnect = (WEBRTC_AMAZON_CONNECTION *)pInst;
AMAZON_WEBRTC_CONNECTION_INVALID_RET(pstConnect, -1);
if (pstConnect->peer)
{
closePeerConnection(pstConnect->peer);
if (pstConnect->videoTransceiver)
{
freeTransceiver(&pstConnect->videoTransceiver);
}
if (pstConnect->audioTransceiver)
{
freeTransceiver(&pstConnect->audioTransceiver);
}
freePeerConnection(&pstConnect->peer);
}
free(pstConnec
评论0