直接上代码

// demo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
extern "C"
{
#include "../../common/common.h"
#include "../../common/cpu.h""
#include "../../x264.h"
#include "../../encoder/set.h"
}
#include "librtmp/rtmp_sys.h"
#include "librtmp/log.h"
#include "librtmp/amf.h"
#include "CameraDS.h"
void ConvertYCbCr2BGR(unsigned char *pYUV,unsigned char *pBGR,int iWidth,int iHeight);
void ConvertRGB2YUV(int w,int h,unsigned char *bmp,unsigned char *yuv);
int InitSockets()
{
#ifdef WIN32
    WORD version;
    WSADATA wsaData;
    version = MAKEWORD(1, 1);
    return (WSAStartup(version, &wsaData) == 0);
#else
    return TRUE;
#endif
}
inline void CleanupSockets()
{
#ifdef WIN32
    WSACleanup();
#endif
}
#define HEX2BIN(a)      (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
int hex2bin(char *str, char **hex)
{
    char *ptr;
    int i, l = strlen(str);
    if (l & 1)
        return 0;
    *hex = (char *)malloc(l/2);
    ptr = *hex;
    if (!ptr)
        return 0;
    for (i=0; i
        *ptr++ = (HEX2BIN(str[i]) << 4) | HEX2BIN(str[i+1]);
    return l/2;
}
char * put_byte( char *output, uint8_t nVal )
{
    output[0] = nVal;
    return output+1;
}
char * put_be16(char *output, uint16_t nVal )
{
    output[1] = nVal & 0xff;
    output[0] = nVal >> 8;
    return output+2;
}
char * put_be24(char *output,uint32_t nVal )
{
    output[2] = nVal & 0xff;
    output[1] = nVal >> 8;
    output[0] = nVal >> 16;
    return output+3;
}
char * put_be32(char *output, uint32_t nVal )
{
    output[3] = nVal & 0xff;
    output[2] = nVal >> 8;
    output[1] = nVal >> 16;
    output[0] = nVal >> 24;
    return output+4;
}
char *  put_be64( char *output, uint64_t nVal )
{
    output=put_be32( output, nVal >> 32 );
    output=put_be32( output, nVal );
    return output;
}
char * put_amf_string( char *c, const char *str )
{
    uint16_t len = strlen( str );
    c=put_be16( c, len );
    memcpy(c,str,len);
    return c+len;
}
char * put_amf_double( char *c, double d )
{
    *c++ = AMF_NUMBER;    
    {
        unsigned char *ci, *co;
        ci = (unsigned char *)&d;
        co = (unsigned char *)c;
        co[0] = ci[7];
        co[1] = ci[6];
        co[2] = ci[5];
        co[3] = ci[4];
        co[4] = ci[3];
        co[5] = ci[2];
        co[6] = ci[1];
        co[7] = ci[0];
    }
    return c+8;
}
int main(int argc, char * argv[])
{
    if (argc<2)
    {
        RTMP_LogPrintf("RTMP_URL IS NULL!!!/n");
        //return -1;
    }
    if (!InitSockets())
    {
        RTMP_LogPrintf("InitSockets Error!/n");
        return -1;
    }
    RTMP_LogPrintf("InitSockets!/n");
    CoInitialize(NULL);
    CCameraDS camera;
    if (!camera.OpenCamera(0,320,240,false))
    {
        RTMP_LogPrintf("Open Camera Error/n");
        return -1;
    }
    int nHeight=camera.GetHeight();
    int nWidth=camera.GetWidth();
    unsigned char * szRGBBuffer=new unsigned char[nHeight*nWidth * 3];
    RTMP_LogPrintf("Camera Open Scuess!  Picture Size[-x%d]/n",nWidth,nHeight);
    RTMP_debuglevel = RTMP_LOGINFO;
    RTMP *r;
    //char uri[]="rtmp://127.0.0.1/live/test";
    //char uri[]="rtmp://192.199.15.223/live/test";
    //char uri[]="rtmp://221.9.244.4/live/jltv";
    //char uri[]="rtmp://192.199.15.223/oflaDemo/red5StreamDemo";
    //char uri[]="rtmp://192.199.15.151/live/test";
    char uri[]="rtmp://127.0.0.1/live/zzj";
    r= RTMP_Alloc();
    RTMP_Init(r);
    RTMP_SetupURL(r, (char*)uri);
    RTMP_EnableWrite(r);
    RTMP_Connect(r, NULL);
    RTMP_ConnectStream(r,0);
    unsigned char szNalBuffer[1024*32];
    unsigned char szBodyBuffer[1024*32];
    x264_nal_t    *p264Nal;
    int            i264Nal;
    x264_param_t * p264Param;
    x264_picture_t * p264Pic;
    x264_t *p264Handle;    
    p264Param = new x264_param_t();
    p264Pic     = new x264_picture_t();
    memset(p264Pic,0,sizeof(x264_picture_t));
    x264_param_default(p264Param);    //set default param
    p264Param->i_threads=2;
    p264Param->i_width    = nWidth;    //set frame width
    p264Param->i_height    = nHeight;    //set frame height
    
    p264Param->b_cabac =0;
    p264Param->i_bframe =0;
    p264Param->b_interlaced=0;
    p264Param->rc.i_rc_method=X264_RC_ABR;//X264_RC_CQP
    p264Param->i_level_idc=21;
    p264Param->rc.i_bitrate=200;
    p264Param->i_fps_num=30;
    p264Param->i_keyint_max=p264Param->i_fps_num*3;
    if((p264Handle = x264_encoder_open(p264Param)) == NULL)
    {
        fprintf( stderr, "x264_encoder_open failed/n" );
        return -2;
    }
    bs_t bs={0};
    x264_picture_alloc(p264Pic, X264_CSP_YV12, p264Param->i_width, p264Param->i_height);
    p264Pic->i_type = X264_TYPE_AUTO;
    x264_picture_t pic_out;
    RTMPPacket packet={0};
    memset(&packet,0,sizeof(RTMPPacket));
    packet.m_nChannel = 0x04;
    packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
    packet.m_nTimeStamp = 0;
    packet.m_nInfoField2 = r->m_stream_id;
    packet.m_hasAbsTimestamp = 0;
    packet.m_body =(char *) szBodyBuffer;
    char * szTmp=(char *)szBodyBuffer;
    packet.m_packetType = RTMP_PACKET_TYPE_INFO;
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string(szTmp, "@setDataFrame" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string(szTmp, "onMetaData" );
    szTmp=put_byte(szTmp, AMF_OBJECT );
    szTmp=put_amf_string( szTmp, "author" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_amf_string( szTmp, "copyright" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_amf_string( szTmp, "description" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_amf_string( szTmp, "keywords" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_amf_string( szTmp, "rating" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_amf_string( szTmp, "presetname" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "Custom" );
    szTmp=put_amf_string( szTmp, "width" );
    szTmp=put_amf_double( szTmp, p264Param->i_width );
    szTmp=put_amf_string( szTmp, "width" );
    szTmp=put_amf_double( szTmp, p264Param->i_width );
    szTmp=put_amf_string( szTmp, "height" );
    szTmp=put_amf_double( szTmp, p264Param->i_height );
    szTmp=put_amf_string( szTmp, "framerate" );
    szTmp=put_amf_double( szTmp, (double)p264Param->i_fps_num / p264Param->i_fps_den );
    szTmp=put_amf_string( szTmp, "videocodecid" );
    szTmp=put_byte(szTmp, AMF_STRING );
    szTmp=put_amf_string( szTmp, "avc1" );
    szTmp=put_amf_string( szTmp, "videodatarate" );
    szTmp=put_amf_double( szTmp, p264Param->rc.i_bitrate );
    szTmp=put_amf_string( szTmp, "avclevel" );
    szTmp=put_amf_double( szTmp, p264Param->i_level_idc );
    szTmp=put_amf_string( szTmp, "avcprofile" );
    szTmp=put_amf_double( szTmp, 0x42 );
    szTmp=put_amf_string( szTmp, "videokeyframe_frequency" );
    szTmp=put_amf_double( szTmp, 3 );
    szTmp=put_amf_string( szTmp, "" );
    szTmp=put_byte( szTmp, AMF_OBJECT_END );
    packet.m_nBodySize=szTmp-(char *)szBodyBuffer;
    RTMP_SendPacket(r,&packet,1);
    packet.m_packetType = RTMP_PACKET_TYPE_VIDEO;    
    szBodyBuffer[ 0]=0x17;
    szBodyBuffer[ 1]=0x00;
    szBodyBuffer[ 2]=0x00;
    szBodyBuffer[ 3]=0x00;
    szBodyBuffer[ 4]=0x00;
    szBodyBuffer[ 5]=0x01;
    szBodyBuffer[ 6]=0x42;
    szBodyBuffer[ 7]=0xC0;
    szBodyBuffer[ 8]=0x15;
    szBodyBuffer[ 9]=0x03;
    szBodyBuffer[10]=0x01;
    szTmp=(char *)szBodyBuffer+11;
    short slen=0;
    bs_init(&bs,szNalBuffer,16);//初始话bs
    x264_sps_write(&bs, p264Handle->sps);//读取编码器的SPS
    slen=bs.p-bs.p_start+1;//spslen(short)
    slen=htons(slen);
    memcpy(szTmp,&slen,sizeof(short));
    szTmp+=sizeof(short);
    *szTmp=0x67;
    szTmp+=1;
    memcpy(szTmp,bs.p_start,bs.p-bs.p_start);
    szTmp+=bs.p-bs.p_start;
    *szTmp=0x01;
    szTmp+=1;
    bs_init(&bs,szNalBuffer,16);//初始话bs
    x264_pps_write(&bs, p264Handle->pps);//读取编码器的PPS
    slen=bs.p-bs.p_start+1;//spslen(short)
    slen=htons(slen);
    memcpy(szTmp,&slen,sizeof(short));
    szTmp+=sizeof(short);
    *szTmp=0x68;
    szTmp+=1;
    memcpy(szTmp,bs.p_start,bs.p-bs.p_start);
    szTmp+=bs.p-bs.p_start;
    packet.m_nBodySize=szTmp-(char *)szBodyBuffer;
    RTMP_SendPacket(r,&packet,0);
    unsigned int nTimes=0;
    unsigned int oldTick=GetTickCount();
    unsigned int newTick=0;
    packet.m_nTimeStamp=0;
    
    while(true)
    {
        szBodyBuffer[ 0]=0x17;
        szBodyBuffer[ 1]=0x01;
        szBodyBuffer[ 2]=0x00;
        szBodyBuffer[ 3]=0x00;
        szBodyBuffer[ 4]=0x42;
        unsigned char * szTmp=szBodyBuffer+5;
        unsigned  char * pNal=szNalBuffer;
        nTimes++;
        int nFramsInPack=0;
        while(true)
        {
            nFramsInPack++;
            unsigned char * pCameraBuf = camera.QueryFrame();
            if (!pCameraBuf)
            {
                return -1;
            }
            for(int ii=0;ii
            {
                memcpy(szRGBBuffer+(nWidth*3)*(nHeight-ii-1),pCameraBuf+(nWidth*3)*ii,nWidth*3);
                //memcpy(pCameraBuf+nWidth*(nHeight-ii-1),pCameraBuf+nWidth*ii,nWidth*3);
                //memcpy(szLineBuffer,pCameraBuf+nWidth*(nHeight-ii-1),nWidth*3);
            }
            ConvertRGB2YUV(nWidth,nHeight,szRGBBuffer,p264Pic->img.plane[0]);
            //memcpy(p264Pic->img.plane[0],szNalBuffer,nWidth*nHeight);
            //memcpy(p264Pic->img.plane[1],szNalBuffer+nWidth*nHeight,nWidth*nHeight/4);
            //memcpy(p264Pic->img.plane[2],szNalBuffer+nWidth*nHeight*5/4,nWidth*nHeight/4);
            
            if( x264_encoder_encode( p264Handle, &p264Nal, &i264Nal, p264Pic ,&pic_out) < 0 )
            {
                fprintf( stderr, "x264_encoder_encode failed/n" );
            }
            for( int i = 0; i < i264Nal; i++ )
            {
                int i_size;
                int i_data;
                i_data = 1024*32;
                if( ( i_size = x264_nal_encode( pNal, &i_data, 1, &p264Nal[i] ) ) > 0 )
                {
                    if ((pNal[4]&0x60)==0)
                    {
                        continue;
                    }
                    if (pNal[4]==0x67)
                    {
                        continue;
                    }
                    if (pNal[4]==0x68)
                    {
                        continue;
                    }
                    memmove(pNal,pNal+4,i_size-4);
                    pNal+=i_size-4;
                }
                else if( i_size < 0 )
                {
                    fprintf( stderr,"need to increase buffer size (size=%d)/n", -i_size );
                }
            }
            unsigned int nSize=pNal-szNalBuffer;
            packet.m_nBodySize=nSize+9;
            if (i264Nal>1)
            {
                szBodyBuffer[ 0]=0x17;
            }
            else
            {
                szBodyBuffer[ 0]=0x27;
            }
            put_be32((char *)szBodyBuffer+5,nSize);
            memcpy(szBodyBuffer+9,szNalBuffer,pNal-szNalBuffer);
            RTMP_SendPacket(r,&packet,0);
            Sleep(20);
            newTick=GetTickCount();
            //RTMP_LogStatus("/rInfo NAUL Type:0xx size: ] Tick:d d",szNalBuffer[0], nSize,33-nSleep,GetTickCount()-oldTick+nSleep);
            packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
            packet.m_nTimeStamp+=newTick-oldTick;
            oldTick=newTick;
            break;
        }
    }
    return 0;
}

(转)H264通过RTMP发布 V2.0 (Red5 Wowza 测试通过)的更多相关文章

  1. 怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块

     因为项目的需要,所以研究了一下自动生成测试代码.将经验记录下来,总会有用的.我个人认为,好记性不如多做笔记多反思总结. 1.    前提条件 开发环境已正确配置 工程已解决JUnit依赖关系(pom ...

  2. 25.怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块

    转自:https://blog.csdn.net/wangyj1992/article/details/78387728 因为项目的需要,所以研究了一下自动生成测试代码.将经验记录下来,总会有用的.我 ...

  3. D盾 v2.0.6.42 测试记录

    0x01 前言 之前发了一篇博客<Bypass D盾_IIS防火墙SQL注入防御(多姿势)>,D哥第一时间联系我,对问题进行修复.这段时间与D哥聊了挺多关于D盾这款产品的话题,实在是很佩服 ...

  4. Restful.Data v2.0发布,谢谢你们的支持和鼓励

    v1.0发布后,承蒙各位博友们的热心关注,也给我不少意见和建议,在此我真诚的感谢 @冰麟轻武 等朋友,你们的支持和鼓励,是这个开源项目最大的推动力. v2.0在除了细枝末节外,在功能上主要做了一下更新 ...

  5. iBox v2.0 发布,Web化仿iOS7界面/交互的JavaScirpt库

    iBox2 是一个仿 iOS 7 界面/交互的 JavaScirpt 库,它运行在 webkit 内核的移动浏览器之上,依赖 iScroll5,帮助开发者构建更接近 iOS 体验的 WebApp. 伴 ...

  6. QzzmServer v2.0正式版发布

                                 V2.1升级程序已发布,具体见下文                                首先,感谢网友的热情的测评及反馈,现Qzzm ...

  7. PCMM(人力资源能力成熟度模型)V2.0中英对照版发布

    PCMM中英版终于发布 时光荏苒,从当初的回眸到如今的回头,这才发现:坚守一份承诺是多么的不易! 一年多了,这份承载殷切期待的作品--<PCMM(人力资源能力成熟度模型)V2.0 (中英文对照版 ...

  8. go-wingui 2018 全新 v2.0 版本发布,包含重大更新!

    go-wingui 2018 全新 v2.0 版本发布,包含重大更新!使用新版CEF内核Chromium 63.0.3239.109,页面可以使用最新的css3,html5技术.使用delphi7重写 ...

  9. 【Beta】“北航社团帮”发布声明——小程序v2.0与网页端v1.0

    目录 Beta版本新功能 小程序v2.0新功能 新功能列表 功能详情图 新功能动图展示 网页端v1.0功能 登录方式 社团信息的修改 新闻的录入和修改 活动的录入和修改 这一版修复的缺陷 Beta版本 ...

随机推荐

  1. shell视频

    本帖最后由 Shell_HAT 于 2014-04-18 16:51 编辑 尚观全套RHCE视频http://pan.baidu.com/s/1pJvzVR1 马哥网络班-中级视频内容http://p ...

  2. Java显式锁学习总结之三:AbstractQueuedSynchronizer的实现原理

    概述 上一篇我们讲了AQS的使用,这一篇讲AQS的内部实现原理. 我们前面介绍了,AQS使用一个int变量state表示同步状态,使用一个隐式的FIFO同步队列(隐式队列就是并没有声明这样一个队列,只 ...

  3. 以太坊go-ethereum客户端docker安装(一)

    最近一段时间忙于工作,就没来得及发表博客,但一直没有停止对区块链的研究.周末抽时间分享一下近期比较重大的收获之一--使用docker来搭建和使用以太坊的节点.本人已经顺利搭建出,开发环境,测试环境,F ...

  4. LeetCode765. Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  5. 远程连接 mysql 数据库连接不上的解决方案

    今天用Navicat访问虚拟机上的mysql,无法访问报cannot connect(10038). 首先看是否可以telnet,本机cmd,telnet 10.10.10.10 3306,结果是连接 ...

  6. MVC – 7.Razor 语法

    7.1 Razor视图引擎语法 Razor通过理解标记的结构来实现代码和标记之间的顺畅切换. @核心转换字符,用来 标记-代码 的转换字符串. 语境A: @{ string rootName=&quo ...

  7. 语音性别识别 - 使用R提取特征

    步骤 1)安装R.windows操作系统安装包的链接:https://cran.r-project.org/bin/windows/base/ 2)切换当前路径为脚本所在路径 点击 文件 > 改 ...

  8. NIO-3网络通信(非阻塞)

    import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import ja ...

  9. Model Binder

    上面需检测id是否为null 如果未提供id值或id值类型不匹配,则使用默认值.但仍需校验值是否超出范围    

  10. js 正则验证多个邮箱,用;隔开的那种

    var r = /^((([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6}\;))*(([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\. ...