lightblue

lightblue

0个粉丝

7

问答

0

专栏

0

资料

lightblue  发布于  2016-09-11 20:57:49
采纳率 0%
7个问答
8606

rtmp推流到服务器

 
大家好,我用rtmp推送H.264+AAC数据到服务器,然后登录客服端看视频的时候发现没声音,然后视频播放延时了十几秒吧,播放速度加快了,就好像快进一样。不知道什么问题。下面是程序



#define AAC_ADTS_HEADER_SIZE 7
static uint32_t find_start_code(uint8_t *buf, uint32_t zeros_in_startcode)   
{   
  uint32_t info;   
  uint32_t i;   
   
  info = 1;   
  if ((info = (buf[zeros_in_startcode] != 1)? 0: 1) == 0)   
      return 0;   
      
  for (i = 0; i < zeros_in_startcode; i++)   
    if (buf != 0)   
    {
        info = 0;
        break;
    };   
     
  return info;   
}   
uint8_t * get_nal(uint32_t *len, uint8_t **offset, uint8_t *start, uint32_t total)
{
    uint32_t info;
    uint8_t *q ;
    uint8_t *p  =  *offset;
    *len = 0;

    while(1) {
        info =  find_start_code(p, 3);
        if (info == 1)
            break;
        p++;
        if ((p - start) >= total)
            return NULL;
    }
    q = p + 4;
    p = q;
    while(1) {
        info =  find_start_code(p, 3);
        if (info == 1)
            break;
        p++;
        if ((p - start) >= total)
            return NULL;
    }
   
    *len = (p - q);
    *offset = p;
    return q;
}
uint8_t *get_adts(uint32_t *len, uint8_t **offset, uint8_t *start, uint32_t total)
{
    uint8_t *p  =  *offset;
    uint32_t frame_len_1;
    uint32_t frame_len_2;
    uint32_t frame_len_3;
    uint32_t frame_length;
   
    if (total < AAC_ADTS_HEADER_SIZE) {
        return NULL;
    }
    if ((p - start) >= total) {
        return NULL;
    }
   
    if (p[0] != 0xff) {
        return NULL;
    }
    if ((p[1] & 0xf0) != 0xf0) {
        return NULL;
    }
    frame_len_1 = p[3] & 0x03;
    frame_len_2 = p[4];
    frame_len_3 = (p[5] & 0xe0) >> 5;
    frame_length = (frame_len_1 << 11) | (frame_len_2 << 3) | frame_len_3;
    *offset = p + frame_length;
    *len = frame_length;
    return p;
}
typedef unsigned long ULONG;
typedef unsigned int UINT;
typedef unsigned char BYTE;
int main(int argc, char *argv[])
{
        char serverStrBuf[100];
        sprintf(serverStrBuf, "rtmp://%s:%s/%s/livestream_chn_%s",argv[1]/*server*/,argv[2]/*port*/,argv[3],/*myapp*/argv[4]/*channel*/);
        printf("serverStrBuf is %s\n",serverStrBuf);
        char vpipe[15];
        char apipe[15];
        sprintf(vpipe,"/tmp/vfifoChn%s",argv[4]);       
        sprintf(apipe,"/tmp/afifoChn%s",argv[4]);
       
        printf("vpipe is %s\n",vpipe);       
        printf("apipe is %s\n",apipe);
    void*prtmp = rtmp_sender_alloc(serverStrBuf/*"rtmp://192.168.2.102:1936/myapp/livestream_chn_1"*/); //return handle
    rtmp_sender_start_publish(prtmp, 0, 0);
    uint8_t *audio_buf_offset ;
    uint32_t audio_len;
    uint8_t *p_audio;
    int ret = 0;
    int vpipeFD = open(vpipe/*"/tmp/vfifoChn1"*/, O_RDONLY|O_NONBLOCK);
        if (vpipeFD < 0)
        {
                printf("open FOR READ failed\n");
        }
    int apipeFD = open(apipe/*"/tmp/afifoChn1"*/, O_RDONLY|O_NONBLOCK);
        if (apipeFD < 0)
        {
                printf("open FOR aREAD failed\n");
        }

    ULONG nSampleRate = 8000;  // 采样率
    UINT nChannels = 1;         // 声道数
    UINT nBit = 16;             // 单样本位数
    ULONG nInputSamples = 0;        //输入样本数
    ULONG nMaxOutputBytes = 0;        //输出所需最大空间
        ULONG nMaxInputBytes=0;     //输入最大字节
    faacEncHandle hEncoder;                //aac句柄
    faacEncConfigurationPtr pConfiguration;//aac设置指针
    char* abuffer;
        char* vbuffer;

    BYTE* pbAACBuffer;
  // (1) Open FAAC engine
    hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);//初始化aac句柄,同时获取最大输入样本,及编码所需最小字节
        nMaxInputBytes=nInputSamples*nBit/8;//计算最大输入字节,跟据最大输入样本数
        printf("nInputSamples:%d nMaxInputBytes:%d nMaxOutputBytes:%d\n", nInputSamples, nMaxInputBytes,nMaxOutputBytes);
    if(hEncoder == NULL)
    {
        printf("[ERROR] Failed to call faacEncOpen()\n");
        return -1;
    }
    pbAACBuffer = (char*)malloc(nMaxOutputBytes);
    // (2.1) Get current encoding configuration
    pConfiguration = faacEncGetCurrentConfiguration(hEncoder);//获取配置结构指针
    pConfiguration->inputFormat = FAAC_INPUT_16BIT;
        pConfiguration->outputFormat=1;
        pConfiguration->useTns=true;
        pConfiguration->useLfe=false;
        pConfiguration->aacObjectType=LOW;
        pConfiguration->mpegVersion = MPEG4;
        pConfiguration->shortctl=SHORTCTL_NORMAL;
        pConfiguration->quantqual=60;
        pConfiguration->bandWidth=80000;
        pConfiguration->bitRate=0;
        printf("!!!!!!!!!!!!!pConfiguration->outputFormat is %d\n",pConfiguration->outputFormat);
    // (2.2) Set encoding configuration
    ret = faacEncSetConfiguration(hEncoder, pConfiguration);//设置配置,根据不同设置,耗时不一样

        abuffer = (char*)malloc(nMaxInputBytes);
        if(abuffer == NULL)
                exit(0);
        vbuffer = (char*)malloc(25*1024);
        if(vbuffer == NULL)
                exit(0);       
        char* output = (char*)malloc(10*1024);  
        static unsigned int audioCount = 0;
        uint32_t start_time = RTMP_GetTime();
        uint32_t timeCount = 0;
        timeCount = 0;//RTMP_GetTime();
    while (1) {
        //        timeCount += 0.01;
        //        start_time += timeCount;
                ret = read(vpipeFD, vbuffer, 25 * 1024);
                if(ret > 0){     
                        rtmp_sender_write_video_frame(prtmp, vbuffer, ret, timeCount, 0,start_time);
                        timeCount += 2;
                }
                ret = read(apipeFD, abuffer, nMaxInputBytes - audioCount);
                if(ret >0){
                        if(audioCount < 1*2048)        {
                                memcpy(output + audioCount,abuffer,ret);
                                audioCount += ret;
                                ret = 0;
                                if(audioCount >= 1*2048){
                                        ret = audioCount;
                                        audioCount = 0;
                                }
                        }else{
                                ret = audioCount;
                                audioCount = 0;
                        }
                }
                if(ret > 0){
                        nInputSamples = ret/ (nBit / 8);
                        // (3) Encode
                        ret = faacEncEncode(hEncoder, (int*) output, nInputSamples, pbAACBuffer, nMaxOutputBytes);
                        audio_buf_offset = pbAACBuffer;
                        if (ret > 0){
                                p_audio = get_adts(&audio_len, &audio_buf_offset, pbAACBuffer, ret);
                                if (p_audio == NULL){
                                        printf("p_audio is null,this should not happen!!!!\n");
                                        continue;
                                }
                                rtmp_sender_write_audio_frame(prtmp, p_audio, audio_len, timeCount,start_time);
                        }                       
                }
                usleep(1);
    }
}
我来回答
回答6个
时间排序
认可量排序

lightblue

0个粉丝

7

问答

0

专栏

0

资料

lightblue 2016-09-12 14:28:36
认可0
自顶一个,还没解决,大神们快来帮帮忙

2272943826

0个粉丝

24

问答

0

专栏

8

资料

2272943826 2016-09-14 11:32:30
认可0
RTMP是Real Time Messaging Protocol(实时消息传输协议)嘛?
可以实时上传视频流的方法通过192.168.1.1这样的类似的实时更新吗?

qqq306922360

0个粉丝

16

问答

0

专栏

0

资料

qqq306922360 2016-11-07 14:22:39
认可0
大神,你这是在海思上面使用FAAC,CPU的资源怎么?你这个没声音,会不会是跑FAAC       cpu使用率太高造成的?

3wnae

0个粉丝

9

问答

0

专栏

0

资料

3wnae 2016-11-08 10:52:22
认可0
楼主用libfaac。应该来不及转吧。每秒25个音频包估计转不过来。 在3516a上试过。 不知道3520d行不行。

qn1547782507

0个粉丝

1

问答

0

专栏

0

资料

qn1547782507 2019-01-18 13:36:30
认可0
遇到类似的问题,请问怎么解决?

qn1516784304

0个粉丝

5

问答

0

专栏

0

资料

qn1516784304 2019-03-12 15:52:35
认可0
楼主解决了吗?可否指点一二?
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区