xuejianhui

导航

MFC 发送数据到SDK服务器

//====HWPuSDK.h=================================================================

/* 接口文件中 : 结构体及变量的定义 */

//连接信息
typedef struct {
    CHAR szDecVIP[IP6_ADDR_LEN];    //视频IP地址,以"/n" 做为结束符
    ULONG ulDecVPort;               //视频端口
    CHAR szDecAIP[IP6_ADDR_LEN];    //音频IP地址,以"/n" 做为结束符
    ULONG ulDecAPort;               //音频端口
    CHAR chTransProtocol;           //传输协议: 1:RTP(TCP Client) 2:RTP(UDP) ...
            // 音频视频解码需相同,当TCP时,以上IP、PORT为入参,当UDP时,IP、PORT为出参
    CHAR chDecFlags;                //解码标志:0 仅解视频; 1 仅解音频; 2 同时解,同时解时使用VIP及VPORT即可
    CHAR chKeepaliveFlags;          //保活标志,在VIP/VPort 发送RTSP保活信令: 0 不保活,1 保活 
    CHAR szRes[6];                  //预留,主动解码:szRes[0]=1;默认被动:szRes[0]=0;被动解码音视频Ip和Port可不填写
} PU_DECLINK_INFO_S, *LPPU_DECLINK_INFO_S;

//解码信息结构体
typedef struct DEC_INFO {
    ULONG ulChannelId;              //通道ID
    PU_DECLINK_INFO_S stDecLinkInfo;//连接信息
} PU_DEC_INFO_S, *LPPU_DEC_INFO_S;

//====DecSendData.h=============================================================

public:
    LONG    m_threadID;         // 
    bool    m_bThreadFlage;     // 

    CString DecSendData::GetDirectoryPathA();
    void DecSendData::SendData();
    int DecSendData::getOneFrame(char *&pstrbuff, int &iLength, char *pOneFrame, 
                            int &iOneLength, char &cType, bool &bIsFindStartCode);

//====DecSendData.cpp===========================================================

/* 窗体函数中 : 结构体及变量的定义 */

typedef struct
{
    //byte 0
    unsigned char TYPE : 5;
    unsigned char NRI  : 2;
    unsigned char F    : 1;
} NALU_HEADER;

public static int const H264_START_CODE_LENGTH = 0x04;
public static int const H264_START_CODE = 0x00000001;
public static int const H264_NALU_TYPE_SILCE = 1;
public static int const H264_NALU_TYPE_IDR = 5;
public static int const EIVS_MAX_IPADDR_LEN = 32;

-------------------------------------------------------------------------------------------------------------------

// 线程声明
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
    // 创建DecSendData窗体的对象
    DecSendData *pDlg = (DecSendData *)lpParam;
    ASSERT(pDlg);

    // 调用发送数据函数
    pDlg->SendData();

    return 1;
}

-------------------------------------------------------------------------------------------------------------------

// 发送数据按钮事件
void DecSendData::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码
    UpdateData(TRUE);
    g_pMainDlg->fedDecInfo.ulChannelId = g_pMainDlg->m_pDecodeMainInfo->ulChannelId;

    m_bThreadFlage = true;
    CreateThread( NULL, 0,ThreadProc,(void*)this,0,NULL);

    OnOK();
}

-------------------------------------------------------------------------------------------------------------------

// 发送数据
void DecSendData::SendData()
{
    FILE *pFileHandle = NULL;

    // 打开需要解码的文件
    pFileHandle = fopen("OneStdFrame3.264", "rb");

    char *pBuff = new char[1024 * 1024];
    char *pOneFrame = new char[1024 * 1024];

    int iOneLength = 0;
    int iReadLength = 1;

    ULONG ulthreadID = m_threadID;

    bool bIsFindStartCode = false;
    char cType = 0;;


    while ((0 < iReadLength) && m_bThreadFlage)
    {
        iReadLength = fread(pBuff, 1, 1024 * 1024, pFileHandle);
        if (iReadLength < 0)
        {
            printf("Error : Read the decodeing file Error! \n");
            break;
        }
        char *pTempBuff = pBuff;
        int iLeftLength = iReadLength;

        while ((iLeftLength > 0) && m_bThreadFlage)
        {
            // 获得一帧数据
            if (1 == getOneFrame(pTempBuff, iLeftLength, pOneFrame, iOneLength, cType, bIsFindStartCode))
            {
                memcpy(pOneFrame, pTempBuff, iLeftLength);
                iOneLength = iLeftLength;
                break;
            }
            else
            {
                //fwrite(pOneFrame, 1, iOneLength, pnFileHandle);
                IVS_PU_MatrixSendData(ulthreadID, pOneFrame, iOneLength, ENC_H264);
                iOneLength = 0;

                if ((H264_NALU_TYPE_SILCE == cType)
                    || (H264_NALU_TYPE_IDR == cType))
                {
                    //fwrite("Hello world.", 1,sizeof("Hello world."), pnFileHandle);
                    Sleep(40);
                }

                //fflush(pnFileHandle);
            }
        }
    }

    Sleep(1000);
    delete [] pBuff;
    pBuff = NULL;

    delete [] pOneFrame;
    pOneFrame = NULL;

    fclose(pFileHandle);
}

-------------------------------------------------------------------------------------------------------------------

// 获取一帧数据
int DecSendData::getOneFrame(char *&pstrbuff, int &iLength, char *pOneFrame, int &iOneLength, char &cType, bool &bIsFindStartCode)
{
    int iCopyLength = 0;

    while (H264_START_CODE_LENGTH <= iLength)
    {
        if (H264_START_CODE == ntohl((unsigned long)*(int *)pstrbuff))  //lint !e826
        {
            if (!bIsFindStartCode)
            {
                bIsFindStartCode = true;
                pstrbuff += H264_START_CODE_LENGTH;
                iLength -= H264_START_CODE_LENGTH;
                iCopyLength += H264_START_CODE_LENGTH;
                NALU_HEADER naluheader = *(NALU_HEADER*)pstrbuff;
                cType = naluheader.TYPE;
            }
            else
            {
                if (iOneLength)
                {
                    memcpy(pOneFrame + iOneLength, (pstrbuff - iCopyLength), iCopyLength);
                }
                else
                {
                    memcpy(pOneFrame, (pstrbuff - iCopyLength), iCopyLength);
                }

                iOneLength += iCopyLength;
                bIsFindStartCode = false;
                return 0;
            }
        }

        if (bIsFindStartCode)
        {
            ++iCopyLength;
        }

        ++pstrbuff;
        --iLength;
    }

    pstrbuff -= iCopyLength;
    iLength += iCopyLength;
    return 1;
}

结尾。。。。。。待完善!

posted on 2012-10-26 15:24  xuejianhui  阅读(402)  评论(0编辑  收藏  举报