ios实现H264裸流封装为FLV格式

62 篇文章 0 订阅

http://blog.csdn.net/tiberx/article/details/42025907


公司最近想承接一个通过智能手机实现视频双向通讯的功能。提前开始了技术预研究。为保证较小的延迟,和优质的视频功能,我们对手机采集的音频和视频都利用手机硬件提供的硬编码功能直接实现H264+AAC编码。封包采用目前视频网站普遍使用的FLV格式。然后通过开源的RtmpLib库,以RTMP协议发送给音视频分发服务器。从而实现延迟很小的高质量视频通讯。

作为这个实施方案的第一步,我们需要分别实现AndroidIOS手机上的H264裸流封装为FLV格式。我主要负责iOS部分的实现,经过2天的努力,终于实现了目标。看到VLC播放成功我封装的视频,实在非常有成就感。下面我把这几天的努力,总结下。

FLV是一个二进制文件,简单来说,其是由一个文件头(FLV header)和很多tag组成(FLV body)。tag又可以分成三类:audio,video,script,分别代表音频流,视频流,脚本流,而每个tag又由tag header和tag data组成。用通俗语言介绍下我的理解。FLV是音视频的容器。有固定长度的FLV Head和FLV Body组成。FLV Body由里面不同的TAG块组成。主要有3种,META TAG,在最前面,告诉后面视频或音频的头信息,从而告诉播放器播放码率和视频尺寸。这些信息都在H264裸流或AAC裸流的头部,可以直接提取封装,不需要我们解码。后面就是Video TAG视频TAG块和Audio TAG音频TAG块组成。本文只涉及视频部分,音频部分类似,不在本文述说了。

知道这些基本知识,要封装还是不容易的。我们现在通过截图来具体讲解。下面是H264裸流文件构成。

h264是一个个NALU单元组成的,每个单元以00 00 01 或者 00 00 00 01分隔开来,每2个00 00 00 01之间就是一个NALU单元。我们实际上就是将一个个NALU单元封装进FLV文件。头两个NAL块封装的数据描述了该视频的码率和版本都信息。后面就是按照时间顺序的视频数据块了。后面我们需要分割后,循环拼装到FLV格式中的。在IOS中可以用二维数组来分割和保存这些NAL块。代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(void)initH264File:(NSString *)path{  
  2.   
  3.     NSInteger fuck = 0;  
  4.       
  5.     NSData * reader = [NSData dataWithContentsOfFile:path];//H264裸数据  
  6.       
  7.     [reader getBytes:&fuck length:sizeof(fuck)];  
  8.       
  9.     Byte *contentByte = (Byte *)[reader bytes];  
  10.       
  11.     NSLog(@"fuck:%d byte len:%d",fuck,[reader length]);  
  12.       
  13.     int count_i=-1;  
  14.       
  15.        Byte kk;  
  16.       
  17.       
  18.     for(int i=0;i<[reader length];i++){  
  19.           
  20.           
  21.           
  22.           
  23.         if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00&&contentByte[i+3]==0x01){  
  24.               
  25.             i=i+3;  
  26.               
  27.             count_i++;  
  28.               
  29.             [self.VideoListArray addObject:[[NSMutableData alloc] init]];  
  30.               
  31.              
  32.               
  33.               
  34.         }  
  35.         else if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00){  
  36.           
  37.             i=i+2;  
  38.               
  39.             count_i++;  
  40.               
  41.             [self.VideoListArray addObject:[[NSMutableData alloc] init]];  
  42.               
  43.   
  44.           
  45.         }  
  46.         else{  
  47.               
  48.             if(count_i>-1){  
  49.                   
  50.                  kk=contentByte[i];  
  51.                   
  52.                 [[self.VideoListArray objectAtIndex:count_i] appendBytes:&kk length:sizeof(kk)];  
  53.                   
  54.             }  
  55.               
  56.               
  57.         }  
  58.           
  59.          
  60.           
  61.     }  
  62.       
  63.       
  64.       
  65.   
  66.   
  67.   
  68. }  



后面我们看下FLV格式,首先看下图:

绿色标注的前9个Byte是FLVHead头信息。前三个是固定的'F','L','V'。前3个bytes是文件类型,总是“FLV”,也就是(0x46 0x4C 0x56)。第4btye是版本号,目前一般是0x01。第5byte是流的信息,倒数第一bit是1表示有视频(0x01),倒数第三bit是1表示有音频(0x4),有视频又有音频就是0x01 | 0x04(0x05),其他都应该是0。最后4bytes表示FLV 头的长度,3+1+1+4 = 9。 IOS代码可以如下实现:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSMutableData *writer = [[NSMutableData alloc] init];  
  2.       
  3.       
  4.     
  5.       
  6.     Byte i1 = 0x46;  
  7.       
  8.     [writer appendBytes:&i1 length:sizeof(i1)];//1  
  9.       
  10.      i1 = 0x4C;  
  11.       
  12.      [writer appendBytes:&i1 length:sizeof(i1)];//2  
  13.       
  14.       
  15.     i1 = 0x56;  
  16.       
  17.     [writer appendBytes:&i1 length:sizeof(i1)];//3  
  18.       
  19.       
  20.     i1 = 0x01;  
  21.       
  22.     [writer appendBytes:&i1 length:sizeof(i1)];//4  
  23.       
  24.      i1 = 0x01;//0x01--代表只有视频,0x05--音频和视频混合,0x04--只有音频  
  25.       
  26.     [writer appendBytes:&i1 length:sizeof(i1)];//5  
  27.       
  28.     i1 = 0x00;  
  29.       
  30.     [writer appendBytes:&i1 length:sizeof(i1)];//6  
  31.       
  32.     i1 = 0x00;  
  33.       
  34.     [writer appendBytes:&i1 length:sizeof(i1)];//7  
  35.       
  36.     i1 = 0x00;  
  37.       
  38.     [writer appendBytes:&i1 length:sizeof(i1)];//8  
  39.       
  40.     i1 = 0x09;  
  41.       
  42.     [writer appendBytes:&i1 length:sizeof(i1)];//9  
  43.       
  44.       


然后讲解下META TAG,如下图:

首先把音视频的配置信息封进metadata之后的tag,然后就可以封数据了。再说下元数据,flv header之后就是它了,再之后就是音视频配置信息,再后面就是音视频数据。

0x12前面的00 00 00 00 就是刚刚说的记录着上一个tag的长度的4bytes,这里因为前面没有tag,所以为0。

如果是视频数据,第一个byte记录video信息:

前4bits表示类型:(·1-- keyframe·2 -- inner frame·3 -- disposable inner frame (h.263 only)·4 --generated keyframe)

后4bits表示解码器ID:(·6 -- Screen video version ·7 -- AVC(h.264))。

这部分就是把H264文件的都两个块封装进来,前面加上2byte的数据长度。具体代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //Meta Tag data  
  2.   
  3.    i1 = 0x17;  
  4.      
  5.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  6.      
  7.    i1 = 0x00;  
  8.      
  9.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  10.      
  11.    i1 = 0x00;  
  12.      
  13.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  14.      
  15.    i1 = 0x00;  
  16.      
  17.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  18.      
  19.    i1 = 0x00;  
  20.      
  21.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  22.      
  23.    //------------  
  24.      
  25.    i1 = 0x01;  
  26.      
  27.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  28.      
  29.    i1 = 0x42;  
  30.      
  31.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  32.      
  33.    i1 = 0x80;  
  34.      
  35.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  36.      
  37.    i1 = 0x0D;  
  38.      
  39.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  40.      
  41.    i1 = 0xFF;  
  42.      
  43.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  44.      
  45.    i1 = 0xE1;  
  46.      
  47.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  48.      
  49.      
  50.     
  51.      
  52.    i1 = (Byte)(([[self.VideoListArray objectAtIndex:0] length]&0x0000FF00)>>8);  
  53.      
  54.    [writer appendBytes:&i1 length:sizeof(i1)];//17  
  55.      
  56.    i1 = (Byte)( [[self.VideoListArray objectAtIndex:0] length]&0x000000FF);  
  57.      
  58.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  59.      
  60.      
  61.    [writer appendData:[self.VideoListArray objectAtIndex:0]];  
  62.      
  63.    i1 = 0x01;  
  64.      
  65.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  66.      
  67.      
  68.    i1 = (Byte)(([[self.VideoListArray objectAtIndex:1] length]&0x0000FF00)>>8);  
  69.      
  70.    [writer appendBytes:&i1 length:sizeof(i1)];//17  
  71.      
  72.    i1 = (Byte)([[self.VideoListArray objectAtIndex:1] length]&0x000000FF);  
  73.      
  74.    [writer appendBytes:&i1 length:sizeof(i1)];//18  
  75.   
  76.    [writer appendData:[self.VideoListArray objectAtIndex:1]];  
  77.   
  78.      


每个TAG的前4个Byte是上一个TAG的总长度,如下图:

视频TAG由固定的Head头,20个Byte和不固定的Body组成。数据是:一个byte的video信息+一个byte的AVCPacket type+3个bytes的无用数据(composition time,当AVC时无用,全是0)+ 4个bytes的NALU单元长度 + N个bytes的NALU数据,所以包头数据长度信息是刚才提到的信息的总和长度。要强调下,当音视频配置信息tag的时候,是没有4个bytes的NALU单元长度的。在这儿我们需要说明下,除了PreTAGHEAD之外,里面所有涉及到描述数据长度的位置,都是后面的数据区域。上图的0x06 0xf2的描述的长度是包括后续的9个描述字节的,NALU的单位长度的描述的是后面数据区的长度,这儿是0x06 0xe9.这儿尤其要注意。17 -- 高4bits:1,keyframe。低4bits:7,代表AVC。后面一个byte 0x00,AVCPacket type,代表AVC sequence header。后3个bytes无意义,之后就是decoder configuration record的内容了。 图中绿色后面 00 00 00 28就是前面tag的总长度。当NALU第一个byte xx& 0x1f == 5的时候,说明该单元是一个I frame,关键帧。否则就是0x27.

后面讲下时间戳的设定,这是依据摄像头采样率考虑的,一般采样是11500,播放速度是25毫秒,1000/25=40.所以依次增加40就可以。上这部分代码:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int time_h=0;//初始时间戳  
  2.   
  3. for(int j=2;j<[self.VideoListArray count];j++){  
  4.       
  5.     if(j==2){  
  6.       
  7.         fuck=metaFixLen+[[self.VideoListArray objectAtIndex:0] length]+[[self.VideoListArray objectAtIndex:1] length];  
  8.           
  9.         //  NSLog(@"len:%d",fuck);  
  10.           
  11.         i1 = (Byte)((fuck&0x00FF0000)>>24);  
  12.           
  13.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  14.           
  15.           
  16.         i1 = (Byte)((fuck&0x00FF0000)>>16);  
  17.           
  18.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  19.           
  20.         i1 = (Byte)((fuck&0x0000FF00)>>8);  
  21.           
  22.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  23.           
  24.         i1 = (Byte)(fuck&0x000000FF);  
  25.           
  26.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  27.   
  28.       
  29.     }else{  
  30.       
  31.       fuck=videoTagFixLen+[[self.VideoListArray objectAtIndex:j-1] length];  
  32.           
  33.         //  NSLog(@"len:%d",fuck);  
  34.           
  35.         i1 = (Byte)((fuck&0x00FF0000)>>24);  
  36.           
  37.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  38.           
  39.           
  40.         i1 = (Byte)((fuck&0x00FF0000)>>16);  
  41.           
  42.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  43.           
  44.         i1 = (Byte)((fuck&0x0000FF00)>>8);  
  45.           
  46.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  47.           
  48.         i1 = (Byte)(fuck&0x000000FF);  
  49.           
  50.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  51.   
  52.       
  53.       
  54.     }  
  55.   
  56.       
  57.     i1 = 0x09;  
  58.       
  59.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  60.       
  61.   
  62.     fuck=[[self.VideoListArray objectAtIndex:j] length]+9;  
  63.       
  64.     i1 = (Byte)((fuck&0x00FF0000)>>16);  
  65.       
  66.     [writer appendBytes:&i1 length:sizeof(i1)];//16  
  67.       
  68.     i1 = (Byte)((fuck&0x0000FF00)>>8);  
  69.       
  70.     [writer appendBytes:&i1 length:sizeof(i1)];//17  
  71.       
  72.     i1 = (Byte)(fuck&0x000000FF);  
  73.       
  74.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  75.       
  76.       
  77.     //----  
  78.     //定义3个时间戳  
  79.     i1 = (Byte)((time_h&0x00FF0000)>>16);  
  80.       
  81.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  82.       
  83.     i1 = (Byte)((time_h&0x0000FF00)>>8);  
  84.       
  85.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  86.       
  87.     i1 =(Byte)(time_h&0x000000FF);  
  88.       
  89.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  90.       
  91.     //备份时间戳  
  92.     i1 = 0x00;  
  93.       
  94.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  95.       
  96.       
  97.       
  98.     //----  
  99.       
  100.     //定义3个3bytes是streamID  
  101.     i1 = 0x00;  
  102.       
  103.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  104.       
  105.     i1 = 0x00;  
  106.       
  107.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  108.       
  109.     i1 = 0x00;  
  110.       
  111.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  112.       
  113.     //---------  
  114.       
  115.      Byte *contentByte = (Byte *)[[self.VideoListArray objectAtIndex:j] bytes];  
  116.       
  117.     if((contentByte[0]& 0x1f) == 5){  
  118.       
  119.         i1 = 0x17;  
  120.           
  121.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  122.       
  123.     }else{  
  124.       
  125.       
  126.         i1 = 0x27;  
  127.           
  128.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  129.           
  130.     }  
  131.       
  132.       
  133.     i1 = 0x01;  
  134.       
  135.     [writer appendBytes:&i1 length:sizeof(i1)];//  
  136.       
  137.     //-------  
  138.       
  139.     i1 = 0x00;  
  140.       
  141.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  142.       
  143.     i1 = 0x00;  
  144.       
  145.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  146.       
  147.     i1 = 0x00;  
  148.       
  149.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  150.       
  151.     //--------  
  152.       
  153.     fuck=[[self.VideoListArray objectAtIndex:j] length];  
  154.       
  155.       NSLog(@"len:%d",fuck);  
  156.       
  157.     i1 = (Byte)((fuck&0x00FF0000)>>24);  
  158.       
  159.     [writer appendBytes:&i1 length:sizeof(i1)];//16  
  160.       
  161.       
  162.     i1 = (Byte)((fuck&0x00FF0000)>>16);  
  163.       
  164.     [writer appendBytes:&i1 length:sizeof(i1)];//16  
  165.       
  166.     i1 = (Byte)((fuck&0x0000FF00)>>8);  
  167.       
  168.     [writer appendBytes:&i1 length:sizeof(i1)];//17  
  169.       
  170.     i1 = (Byte)(fuck&0x000000FF);  
  171.       
  172.     [writer appendBytes:&i1 length:sizeof(i1)];//18  
  173.       
  174.       
  175.       
  176.    [writer appendData:[self.VideoListArray objectAtIndex:j]];  
  177.   
  178.       
  179.     //---------  
  180.     time_h=time_h+40;//采样率是11500时候,是40递增,加倍就是80递增  
  181.   
  182. }//for  


到此为止,我们就完成了所有封装了。本文全部代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  xukangwenViewController.m  
  3. //  H264ToFlv  
  4. //  
  5. //  Created by zhengyu xu on 14-2-26.  
  6. //  Copyright (c) 2014年 smg. All rights reserved.  
  7. //  
  8.   
  9. #import "xukangwenViewController.h"  
  10. //#import "rtmp.h"  
  11. //#import "log.h"  
  12.   
  13.   
  14.   
  15. //#import "Rtmpdump.h"  
  16.   
  17. #include <stdio.h>  
  18. #include <stdlib.h>  
  19. #include <time.h>  
  20.   
  21.   
  22. //网络与本地字节转换  
  23. #define HTON16(x)  ((x>>8&0xff)|(x<<8&0xff00))  
  24. #define HTON24(x)  ((x>>16&0xff)|(x<<16&0xff0000)|x&0xff00)  
  25. #define HTON32(x)  ((x>>24&0xff)|(x>>8&0xff00)|\  
  26. (x<<8&0xff0000)|(x<<24&0xff000000))  
  27.   
  28. /** 
  29. class RtmpSend { 
  30. private: 
  31.     id flv_route;  // holds an NSString 
  32.      
  33.     //RTMP_XXX()返回0表示失败,1表示成功 
  34.     RTMP*rtmp=NULL;//rtmp应用指针 
  35.     RTMPPacket*packet=NULL;//rtmp包结构 
  36.      
  37.     //id rtmpurl="rtmp://172.28.125.74:1935/ios/test";//连接的URL 
  38.      
  39.    // id flvfile="test.flv";//读取的flv文件 
  40.      
  41.      
  42. public: 
  43.      
  44.     RtmpSend() { 
  45.         flv_route = @"Test 11123"; 
  46.     } 
  47.      
  48.     RtmpSend(const char* initial_greeting_text) { 
  49.         flv_route = [[NSString alloc] initWithUTF8String:initial_greeting_text]; 
  50.          
  51.     } 
  52.      
  53.     void say_flv_route2(){ 
  54.          
  55.      printf("test 111234\n"); 
  56.          
  57.        
  58.          
  59.     } 
  60.      
  61.     int say_flv_route(char* rtmpurl,char* flvfile) { 
  62.          
  63.         
  64.          
  65.         if (!ZINIT()) 
  66.         { 
  67.             printf("init socket err\n"); 
  68.             return -1; 
  69.         } 
  70.          
  71.         /初始化// 
  72.         RTMP_LogLevel lvl=RTMP_LOGINFO; 
  73.         RTMP_LogSetLevel(lvl);//设置信息等级 
  74.         //RTMP_LogSetOutput(FILE*fp);//设置信息输出文件 
  75.         rtmp=RTMP_Alloc();//申请rtmp空间 
  76.         RTMP_Init(rtmp);//初始化rtmp设置 
  77.         rtmp->Link.timeout=5;//设置连接超时,单位秒,默认30秒 
  78.         packet=new RTMPPacket();//创建包 
  79.         RTMPPacket_Alloc(packet,1024*64);//给packet分配数据空间,要满足最长的帧,不知道可设大些 
  80.         RTMPPacket_Reset(packet);//重置packet状态 
  81.          
  82.         RTMPPacket_Reset(packet);//重置packet状态 
  83.         连接// 
  84.         RTMP_SetupURL(rtmp,rtmpurl);//设置url 
  85.          
  86.         RTMP_EnableWrite(rtmp);//设置可写状态 
  87.         if (!RTMP_Connect(rtmp,NULL))//连接服务器 
  88.         { 
  89.             printf("connect err\n"); 
  90.             ZCLEAR(); 
  91.             return -1; 
  92.         } 
  93.         if (!RTMP_ConnectStream(rtmp,0))//创建并发布流(取决于rtmp->Link.lFlags) 
  94.         { 
  95.             printf("ConnectStreamerr\n"); 
  96.             ZCLEAR(); 
  97.             return -1; 
  98.         } 
  99.         packet->m_hasAbsTimestamp = 0; //绝对时间戳 
  100.         packet->m_nChannel = 0x04; //通道 
  101.         packet->m_nInfoField2 = rtmp->m_stream_id; 
  102.          
  103.         FILE*fp=fopen(flvfile,"rb"); 
  104.         if (fp==NULL) 
  105.         { 
  106.             printf("open file:%s err\n",flvfile); 
  107.             ZCLEAR(); 
  108.             return -1; 
  109.         } 
  110.          
  111.         printf("rtmpurl:%s\nflvfile:%s\nsend data ...\n",rtmpurl,flvfile); 
  112.         发送数据// 
  113.         fseek(fp,9,SEEK_SET);//跳过前9个字节 
  114.         fseek(fp,4,SEEK_CUR);//跳过4字节长度 
  115.         long start=clock()-1000; 
  116.         long perframetime=0;//上一帧时间戳 
  117.         while(TRUE) 
  118.         { 
  119.             if((clock()-start)<perframetime)//发的太快就等一下 
  120.             { 
  121.                 sleep(500); 
  122.                 continue; 
  123.             } 
  124.             int type=0;//类型 
  125.             int datalength=0;//数据长度 
  126.             int time=0;//时间戳 
  127.             int streamid=0;//流ID 
  128.             if(!Read8(type,fp)) 
  129.                 break; 
  130.             if(!Read24(datalength,fp)) 
  131.                 break; 
  132.             if(!ReadTime(time,fp)) 
  133.                 break; 
  134.             if(!Read24(streamid,fp)) 
  135.                 break; 
  136.              
  137.             if (type!=0x08&&type!=0x09) 
  138.             { 
  139.                 fseek(fp,datalength+4,SEEK_CUR); 
  140.                 continue; 
  141.             } 
  142.              
  143.             if(fread(packet->m_body,1,datalength,fp)!=datalength) 
  144.                 break; 
  145.             packet->m_headerType = RTMP_PACKET_SIZE_MEDIUM; 
  146.             packet->m_nTimeStamp = time; 
  147.             packet->m_packetType=type; 
  148.             packet->m_nBodySize=datalength; 
  149.              
  150.             if (!RTMP_IsConnected(rtmp)) 
  151.             { 
  152.                 printf("rtmp is not connect\n"); 
  153.                 break; 
  154.             } 
  155.             if (!RTMP_SendPacket(rtmp,packet,0)) 
  156.             { 
  157.                 printf("send err\n"); 
  158.                 break; 
  159.             } 
  160.             int alldatalength=0;//该帧总长度 
  161.             if(!Read32(alldatalength,fp)) 
  162.                 break; 
  163.             perframetime=time; 
  164.         } 
  165.         printf("send data over\n"); 
  166.         fclose(fp); 
  167.         ZCLEAR(); 
  168.  
  169.          
  170.         return 0; 
  171.     } 
  172.      
  173.     bool ZINIT() 
  174.     { 
  175.        // WORD version; 
  176.        // WSADATA wsaData; 
  177.        // version=MAKEWORD(2,2); 
  178.   
  179.         return TRUE; 
  180.     } 
  181.     void ZCLEAR() 
  182.     { 
  183.         //释放/ 
  184.         if (rtmp!=NULL) 
  185.         { 
  186.             RTMP_Close(rtmp);//断开连接 
  187.             RTMP_Free(rtmp);//释放内存 
  188.             rtmp=NULL; 
  189.         } 
  190.         if (packet!=NULL) 
  191.         { 
  192.             RTMPPacket_Free(packet);//释放内存 
  193.             delete packet; 
  194.             packet=NULL; 
  195.         } 
  196.         /// 
  197.        // WSACleanup(); 
  198.     } 
  199.      
  200.      
  201.      
  202.     bool Read8(int &i8,FILE*fp) 
  203.     { 
  204.         if(fread(&i8,1,1,fp)!=1) 
  205.             return false; 
  206.         return true; 
  207.     } 
  208.      
  209.     bool Read16(int &i16,FILE*fp) 
  210.     { 
  211.         if(fread(&i16,2,1,fp)!=1) 
  212.             return false; 
  213.         i16=HTON16(i16); 
  214.         return true; 
  215.     } 
  216.     bool Read24(int &i24,FILE*fp) 
  217.     { 
  218.         if(fread(&i24,3,1,fp)!=1) 
  219.             return false; 
  220.         i24=HTON24(i24); 
  221.         return true; 
  222.     } 
  223.     bool Read32(int &i32,FILE*fp) 
  224.     { 
  225.         if(fread(&i32,4,1,fp)!=1) 
  226.             return false; 
  227.         i32=HTON32(i32); 
  228.         return true; 
  229.     } 
  230.     bool Peek8(int &i8,FILE*fp) 
  231.     { 
  232.         if(fread(&i8,1,1,fp)!=1) 
  233.             return false; 
  234.         fseek(fp,-1,SEEK_CUR); 
  235.         return true; 
  236.     } 
  237.     bool ReadTime(int &itime,FILE*fp) 
  238.     { 
  239.         int temp=0; 
  240.         if(fread(&temp,4,1,fp)!=1) 
  241.             return false; 
  242.         itime=HTON24(temp); 
  243.         itime|=(temp&0xff000000); 
  244.         return true; 
  245.     } 
  246.  
  247.      
  248.      
  249. }; 
  250.   
  251. */  
  252.   
  253.   
  254. @interface xukangwenViewController (){  
  255.   
  256. //@private RtmpSend *RtmpSend1;  
  257.     
  258.       
  259.   
  260. }  
  261.   
  262. @end  
  263.   
  264. @implementation xukangwenViewController  
  265. @synthesize VideoListArray;  
  266. @synthesize rPublish;  
  267.   
  268. int first1=0;  
  269.   
  270. int topTagLen=16;  
  271.   
  272. int metaFixLen=27;  
  273.   
  274. int first2=0;  
  275.   
  276. int videoLen=0;  
  277.   
  278. int videoTagFixLen=20;  
  279.   
  280. - (id)init {  
  281.     if (self = [super init]) {  
  282.           
  283.      //  RtmpSend1= new RtmpSend();  
  284.           
  285.     }  
  286.     return self;  
  287. }  
  288.   
  289. - (void)viewDidLoad  
  290. {  
  291.     [super viewDidLoad];  
  292.       
  293.       
  294.     if(NULL==self.VideoListArray){  
  295.           
  296.         self.VideoListArray=[[NSMutableArray alloc] init];  
  297.           
  298.     }else{  
  299.           
  300.         if([self.VideoListArray count]>0){  
  301.               
  302.             [self.VideoListArray removeAllObjects];  
  303.         }  
  304.           
  305.     }  
  306.   
  307.       
  308.       
  309.       
  310.     // Do any additional setup after loading the view, typically from a nib.  
  311.     [self TestBitFile];  
  312.     NSLog(@"test");  
  313.       
  314.     //[self setRTMP];  
  315.       
  316.     rPublish=[[RtmpLib alloc] init];  
  317.       
  318. }  
  319.   
  320.   
  321. -(void)setRTMP{  
  322.       
  323.       
  324.     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  325.        
  326.       
  327.   NSString *realPath = [documentPath stringByAppendingPathComponent:@"IOSencoder.flv"];  
  328.       
  329.       
  330.     NSFileManager *fileManager=[NSFileManager defaultManager];  
  331.       
  332.       
  333.     if ([fileManager fileExistsAtPath:realPath]){//if 2  
  334.           
  335.      //  NSString *string_content = @"rtmp://172.28.125.74:1935/ios/test";  
  336.           
  337.       //  NSString *color_string = @"0xff0000";  
  338.           
  339.       //  char *color_char = NULL;//"rtmp://172.28.125.74:1935/ios/test";//[color_string cStringUsingEncoding:NSASCIIStringEncoding];  
  340.   
  341.        // char *color_char2 = NULL;  
  342.           
  343.        //  char *inStrg = (char *)[[string_content dataUsingEncoding : NSASCIIStringEncoding ] bytes];  
  344.           
  345.        //   char *inStrg2 = (char *)[[realPath dataUsingEncoding : NSASCIIStringEncoding ] bytes];  
  346.   
  347.     //   RtmpSend1->say_flv_route(inStrg, inStrg2);  
  348.           
  349.      //  RtmpSend1->say_flv_route2();  
  350.           
  351.           
  352.     }//if 2  
  353.       
  354. }  
  355.   
  356.   
  357.   
  358. -(void)initH264File:(NSString *)path{  
  359.   
  360.     NSInteger fuck = 0;  
  361.       
  362.     NSData * reader = [NSData dataWithContentsOfFile:path];//H264裸数据  
  363.       
  364.     [reader getBytes:&fuck length:sizeof(fuck)];  
  365.       
  366.     Byte *contentByte = (Byte *)[reader bytes];  
  367.       
  368.     NSLog(@"fuck:%d byte len:%d",fuck,[reader length]);  
  369.       
  370.     int count_i=-1;  
  371.       
  372.        Byte kk;  
  373.       
  374.       
  375.     for(int i=0;i<[reader length];i++){  
  376.           
  377.           
  378.           
  379.           
  380.         if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00&&contentByte[i+3]==0x01){  
  381.               
  382.             i=i+3;  
  383.               
  384.             count_i++;  
  385.               
  386.             [self.VideoListArray addObject:[[NSMutableData alloc] init]];  
  387.               
  388.              
  389.               
  390.               
  391.         }  
  392.         else if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00){  
  393.           
  394.             i=i+2;  
  395.               
  396.             count_i++;  
  397.               
  398.             [self.VideoListArray addObject:[[NSMutableData alloc] init]];  
  399.               
  400.   
  401.           
  402.         }  
  403.         else{  
  404.               
  405.             if(count_i>-1){  
  406.                   
  407.                  kk=contentByte[i];  
  408.                   
  409.                 [[self.VideoListArray objectAtIndex:count_i] appendBytes:&kk length:sizeof(kk)];  
  410.                   
  411.             }  
  412.               
  413.               
  414.         }  
  415.           
  416.          
  417.           
  418.     }  
  419.       
  420.       
  421.       
  422.   
  423.   
  424.   
  425. }  
  426.   
  427.   
  428. -(NSMutableData *)getSecondFile:(NSString *)path{  
  429.       
  430.     NSMutableData *writer = [[NSMutableData alloc] init];  
  431.       
  432.       
  433.     NSInteger fuck = 0;  
  434.       
  435.     NSData * reader = [NSData dataWithContentsOfFile:path];//H264裸数据  
  436.       
  437.     [reader getBytes:&fuck length:sizeof(fuck)];  
  438.       
  439.     Byte *contentByte = (Byte *)[reader bytes];  
  440.       
  441.       
  442.       
  443.     int count_i=0;  
  444.       
  445.     Byte kk;  
  446.       
  447.     for(int i=0;i<[reader length];i++){  
  448.           
  449.         if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00&&contentByte[i+3]==0x01){  
  450.               
  451.             i=i+3;  
  452.               
  453.             count_i++;  
  454.               
  455.             if(count_i==3){  
  456.                 break;  
  457.             }  
  458.               
  459.               
  460.         }else{  
  461.               
  462.             if(count_i<3&&count_i>1){  
  463.                   
  464.                 kk=contentByte[i];  
  465.                   
  466.                   
  467.                 [writer appendBytes:&kk length:sizeof(kk)];//7  
  468.                   
  469.                 first2++;  
  470.                   
  471.             }  
  472.               
  473.               
  474.         }  
  475.           
  476.     }  
  477.       
  478.       
  479.       
  480.     return writer;  
  481. }  
  482.   
  483.   
  484.   
  485. -(NSMutableData *)getFirstFile:(NSString *)path{  
  486.   
  487.      NSMutableData *writer = [[NSMutableData alloc] init];  
  488.   
  489.   
  490.     NSInteger fuck = 0;  
  491.       
  492.     NSData * reader = [NSData dataWithContentsOfFile:path];//H264裸数据  
  493.       
  494.     [reader getBytes:&fuck length:sizeof(fuck)];  
  495.       
  496.     Byte *contentByte = (Byte *)[reader bytes];  
  497.       
  498.       
  499.       
  500.     int count_i=0;  
  501.       
  502.     Byte kk;  
  503.       
  504.     for(int i=0;i<[reader length];i++){  
  505.           
  506.         if(contentByte[i+0]==0x00&&contentByte[i+1]==0x00&&contentByte[i+2]==0x00&&contentByte[i+3]==0x01){  
  507.               
  508.             i=i+3;  
  509.               
  510.             count_i++;  
  511.               
  512.             if(count_i==2){  
  513.                 break;  
  514.             }  
  515.               
  516.           
  517.         }else{  
  518.           
  519.             if(count_i<2){  
  520.               
  521.              kk=contentByte[i];  
  522.               
  523.               
  524.             [writer appendBytes:&kk length:sizeof(kk)];//7  
  525.                   
  526.                 first1++;  
  527.                   
  528.             }  
  529.   
  530.           
  531.         }  
  532.           
  533.     }  
  534.   
  535.       
  536.       
  537.     return writer;  
  538. }  
  539.   
  540. -(void)TestBitFile{  
  541.   
  542.      
  543.       
  544.     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  545.     NSString *realPath = [documentPath stringByAppendingPathComponent:@"IOSencoder.flv"];  
  546.       
  547.       
  548.     NSString *realPath2 = [documentPath stringByAppendingPathComponent:@"encoder.h264"];  
  549.   
  550.       
  551.     NSFileManager *fileManager=[NSFileManager defaultManager];  
  552.       
  553.       
  554.     if (![fileManager fileExistsAtPath:realPath]){  
  555.   
  556.          
  557.           
  558.      [self initH264File:realPath2];  
  559.           
  560.           
  561.           
  562.           
  563.         NSMutableData *writer = [[NSMutableData alloc] init];  
  564.           
  565.           
  566.         
  567.           
  568.         Byte i1 = 0x46;  
  569.           
  570.         [writer appendBytes:&i1 length:sizeof(i1)];//1  
  571.           
  572.          i1 = 0x4C;  
  573.           
  574.          [writer appendBytes:&i1 length:sizeof(i1)];//2  
  575.           
  576.           
  577.         i1 = 0x56;  
  578.           
  579.         [writer appendBytes:&i1 length:sizeof(i1)];//3  
  580.           
  581.           
  582.         i1 = 0x01;  
  583.           
  584.         [writer appendBytes:&i1 length:sizeof(i1)];//4  
  585.           
  586.          i1 = 0x01;//0x01--代表只有视频,0x05--音频和视频混合,0x04--只有音频  
  587.           
  588.         [writer appendBytes:&i1 length:sizeof(i1)];//5  
  589.           
  590.         i1 = 0x00;  
  591.           
  592.         [writer appendBytes:&i1 length:sizeof(i1)];//6  
  593.           
  594.         i1 = 0x00;  
  595.           
  596.         [writer appendBytes:&i1 length:sizeof(i1)];//7  
  597.           
  598.         i1 = 0x00;  
  599.           
  600.         [writer appendBytes:&i1 length:sizeof(i1)];//8  
  601.           
  602.         i1 = 0x09;  
  603.           
  604.         [writer appendBytes:&i1 length:sizeof(i1)];//9  
  605.           
  606.           
  607.           
  608.         //-----------  
  609.         //LAst TAG Head 4  
  610.           
  611.         i1 = 0x00;  
  612.           
  613.         [writer appendBytes:&i1 length:sizeof(i1)];//10  
  614.           
  615.         i1 = 0x00;  
  616.           
  617.         [writer appendBytes:&i1 length:sizeof(i1)];//11  
  618.           
  619.         i1 = 0x00;  
  620.           
  621.         [writer appendBytes:&i1 length:sizeof(i1)];//12  
  622.           
  623.         i1 = 0x00;  
  624.           
  625.         [writer appendBytes:&i1 length:sizeof(i1)];//13  
  626.           
  627.         /** 
  628.          0x09前面的00 00 00 00 就是刚刚说的记录着上一个tag的长度的4bytes,这里因为前面没有tag,所以为0 
  629.           
  630.          */  
  631.           
  632.         //TAG Head 11  
  633.          
  634.         /** 
  635.         第1个byte为记录着tag的类型,音频(0x8),视频(0x9),脚本(0x12) 
  636.          
  637.         */  
  638.           
  639.          i1 = 0x09;  
  640.           
  641.         [writer appendBytes:&i1 length:sizeof(i1)];//14  
  642.           
  643.          
  644.           
  645.           
  646.              //----  
  647.         //读取H264文件  
  648.         int fuck=topTagLen+[[self.VideoListArray objectAtIndex:0] length]+[[self.VideoListArray objectAtIndex:1] length];  
  649.           
  650.           
  651.        // NSLog(@"len:%d",fuck);  
  652.   
  653.           
  654.         i1 = (Byte)((fuck&0x00FF0000)>>16);  
  655.           
  656.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  657.           
  658.         i1 = (Byte)((fuck&0x0000FF00)>>8);  
  659.           
  660.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  661.           
  662.         i1 = (Byte)(fuck&0x000000FF);  
  663.           
  664.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  665.           
  666.           
  667.        //----  
  668.        //定义3个时间戳  
  669.         i1 = 0x00;  
  670.           
  671.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  672.           
  673.         i1 = 0x00;  
  674.           
  675.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  676.           
  677.         i1 = 0x00;  
  678.           
  679.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  680.           
  681.         //备份时间戳  
  682.         i1 = 0x00;  
  683.           
  684.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  685.   
  686.   
  687.   
  688.         //----  
  689.           
  690.         //定义3个3bytes是streamID  
  691.         i1 = 0x00;  
  692.           
  693.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  694.           
  695.         i1 = 0x00;  
  696.           
  697.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  698.           
  699.         i1 = 0x00;  
  700.           
  701.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  702.           
  703.         //Meta Tag data  
  704.   
  705.         i1 = 0x17;  
  706.           
  707.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  708.           
  709.         i1 = 0x00;  
  710.           
  711.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  712.           
  713.         i1 = 0x00;  
  714.           
  715.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  716.           
  717.         i1 = 0x00;  
  718.           
  719.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  720.           
  721.         i1 = 0x00;  
  722.           
  723.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  724.           
  725.         //------------  
  726.           
  727.         i1 = 0x01;  
  728.           
  729.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  730.           
  731.         i1 = 0x42;  
  732.           
  733.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  734.           
  735.         i1 = 0x80;  
  736.           
  737.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  738.           
  739.         i1 = 0x0D;  
  740.           
  741.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  742.           
  743.         i1 = 0xFF;  
  744.           
  745.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  746.           
  747.         i1 = 0xE1;  
  748.           
  749.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  750.           
  751.           
  752.          
  753.           
  754.         i1 = (Byte)(([[self.VideoListArray objectAtIndex:0] length]&0x0000FF00)>>8);  
  755.           
  756.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  757.           
  758.         i1 = (Byte)( [[self.VideoListArray objectAtIndex:0] length]&0x000000FF);  
  759.           
  760.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  761.           
  762.           
  763.         [writer appendData:[self.VideoListArray objectAtIndex:0]];  
  764.           
  765.         i1 = 0x01;  
  766.           
  767.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  768.           
  769.           
  770.         i1 = (Byte)(([[self.VideoListArray objectAtIndex:1] length]&0x0000FF00)>>8);  
  771.           
  772.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  773.           
  774.         i1 = (Byte)([[self.VideoListArray objectAtIndex:1] length]&0x000000FF);  
  775.           
  776.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  777.   
  778.         [writer appendData:[self.VideoListArray objectAtIndex:1]];  
  779.   
  780.           
  781.           
  782.        
  783.         //----------------  
  784.           
  785.         int time_h=0;//初始时间戳  
  786.           
  787.         for(int j=2;j<[self.VideoListArray count];j++){  
  788.               
  789.             if(j==2){  
  790.               
  791.                 fuck=metaFixLen+[[self.VideoListArray objectAtIndex:0] length]+[[self.VideoListArray objectAtIndex:1] length];  
  792.                   
  793.                 //  NSLog(@"len:%d",fuck);  
  794.                   
  795.                 i1 = (Byte)((fuck&0x00FF0000)>>24);  
  796.                   
  797.                 [writer appendBytes:&i1 length:sizeof(i1)];//16  
  798.                   
  799.                   
  800.                 i1 = (Byte)((fuck&0x00FF0000)>>16);  
  801.                   
  802.                 [writer appendBytes:&i1 length:sizeof(i1)];//16  
  803.                   
  804.                 i1 = (Byte)((fuck&0x0000FF00)>>8);  
  805.                   
  806.                 [writer appendBytes:&i1 length:sizeof(i1)];//17  
  807.                   
  808.                 i1 = (Byte)(fuck&0x000000FF);  
  809.                   
  810.                 [writer appendBytes:&i1 length:sizeof(i1)];//18  
  811.   
  812.               
  813.             }else{  
  814.               
  815.               fuck=videoTagFixLen+[[self.VideoListArray objectAtIndex:j-1] length];  
  816.                   
  817.                 //  NSLog(@"len:%d",fuck);  
  818.                   
  819.                 i1 = (Byte)((fuck&0x00FF0000)>>24);  
  820.                   
  821.                 [writer appendBytes:&i1 length:sizeof(i1)];//16  
  822.                   
  823.                   
  824.                 i1 = (Byte)((fuck&0x00FF0000)>>16);  
  825.                   
  826.                 [writer appendBytes:&i1 length:sizeof(i1)];//16  
  827.                   
  828.                 i1 = (Byte)((fuck&0x0000FF00)>>8);  
  829.                   
  830.                 [writer appendBytes:&i1 length:sizeof(i1)];//17  
  831.                   
  832.                 i1 = (Byte)(fuck&0x000000FF);  
  833.                   
  834.                 [writer appendBytes:&i1 length:sizeof(i1)];//18  
  835.   
  836.               
  837.               
  838.             }  
  839.           
  840.               
  841.             i1 = 0x09;  
  842.               
  843.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  844.               
  845.           
  846.             fuck=[[self.VideoListArray objectAtIndex:j] length]+9;  
  847.               
  848.             i1 = (Byte)((fuck&0x00FF0000)>>16);  
  849.               
  850.             [writer appendBytes:&i1 length:sizeof(i1)];//16  
  851.               
  852.             i1 = (Byte)((fuck&0x0000FF00)>>8);  
  853.               
  854.             [writer appendBytes:&i1 length:sizeof(i1)];//17  
  855.               
  856.             i1 = (Byte)(fuck&0x000000FF);  
  857.               
  858.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  859.               
  860.               
  861.             //----  
  862.             //定义3个时间戳  
  863.             i1 = (Byte)((time_h&0x00FF0000)>>16);  
  864.               
  865.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  866.               
  867.             i1 = (Byte)((time_h&0x0000FF00)>>8);  
  868.               
  869.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  870.               
  871.             i1 =(Byte)(time_h&0x000000FF);  
  872.               
  873.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  874.               
  875.             //备份时间戳  
  876.             i1 = 0x00;  
  877.               
  878.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  879.               
  880.               
  881.               
  882.             //----  
  883.               
  884.             //定义3个3bytes是streamID  
  885.             i1 = 0x00;  
  886.               
  887.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  888.               
  889.             i1 = 0x00;  
  890.               
  891.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  892.               
  893.             i1 = 0x00;  
  894.               
  895.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  896.               
  897.             //---------  
  898.               
  899.              Byte *contentByte = (Byte *)[[self.VideoListArray objectAtIndex:j] bytes];  
  900.               
  901.             if((contentByte[0]& 0x1f) == 5){  
  902.               
  903.                 i1 = 0x17;  
  904.                   
  905.                 [writer appendBytes:&i1 length:sizeof(i1)];//18  
  906.               
  907.             }else{  
  908.               
  909.               
  910.                 i1 = 0x27;  
  911.                   
  912.                 [writer appendBytes:&i1 length:sizeof(i1)];//18  
  913.                   
  914.             }  
  915.               
  916.               
  917.             i1 = 0x01;  
  918.               
  919.             [writer appendBytes:&i1 length:sizeof(i1)];//  
  920.               
  921.             //-------  
  922.               
  923.             i1 = 0x00;  
  924.               
  925.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  926.               
  927.             i1 = 0x00;  
  928.               
  929.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  930.               
  931.             i1 = 0x00;  
  932.               
  933.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  934.               
  935.             //--------  
  936.               
  937.             fuck=[[self.VideoListArray objectAtIndex:j] length];  
  938.               
  939.               NSLog(@"len:%d",fuck);  
  940.               
  941.             i1 = (Byte)((fuck&0x00FF0000)>>24);  
  942.               
  943.             [writer appendBytes:&i1 length:sizeof(i1)];//16  
  944.               
  945.               
  946.             i1 = (Byte)((fuck&0x00FF0000)>>16);  
  947.               
  948.             [writer appendBytes:&i1 length:sizeof(i1)];//16  
  949.               
  950.             i1 = (Byte)((fuck&0x0000FF00)>>8);  
  951.               
  952.             [writer appendBytes:&i1 length:sizeof(i1)];//17  
  953.               
  954.             i1 = (Byte)(fuck&0x000000FF);  
  955.               
  956.             [writer appendBytes:&i1 length:sizeof(i1)];//18  
  957.               
  958.               
  959.               
  960.            [writer appendData:[self.VideoListArray objectAtIndex:j]];  
  961.   
  962.               
  963.             //---------  
  964.             time_h=time_h+40;//采样率是11500时候,是40递增,加倍就是80递增  
  965.           
  966.         }//for  
  967.         //-----------------------  
  968.           
  969.           
  970.           
  971.         fuck=videoTagFixLen+[[self.VideoListArray objectAtIndex:([self.VideoListArray count]-1)] length];  
  972.           
  973.         //  NSLog(@"len:%d",fuck);  
  974.           
  975.         i1 = (Byte)((fuck&0x00FF0000)>>24);  
  976.           
  977.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  978.           
  979.           
  980.         i1 = (Byte)((fuck&0x00FF0000)>>16);  
  981.           
  982.         [writer appendBytes:&i1 length:sizeof(i1)];//16  
  983.           
  984.         i1 = (Byte)((fuck&0x0000FF00)>>8);  
  985.           
  986.         [writer appendBytes:&i1 length:sizeof(i1)];//17  
  987.           
  988.         i1 = (Byte)(fuck&0x000000FF);  
  989.           
  990.         [writer appendBytes:&i1 length:sizeof(i1)];//18  
  991.           
  992.           
  993. [writer writeToFile:realPath atomically:YES];  
  994.           
  995.           
  996.     }  
  997.   
  998.       
  999.       
  1000.   
  1001.   
  1002. }  
  1003.   
  1004.   
  1005.   
  1006.   
  1007. - (void)didReceiveMemoryWarning  
  1008. {  
  1009.     [super didReceiveMemoryWarning];  
  1010.     // Dispose of any resources that can be recreated.  
  1011. }  
  1012.   
  1013. @end  




1
0
 
 
我的同类文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C++ 中将 H.264 裸流转换成 DHAV 帧结构体,可以使用以下代码作为参考: ```c++ #include <iostream> #include <fstream> #include <cstring> using namespace std; // DHAV帧头结构体 typedef struct _DHAV_HEADER { unsigned char cTag[4]; // 帧头标签,固定为"DHAV" unsigned int nLength; // 帧长度,包括帧头和数据部分 unsigned char cType; // 帧类型,0表示视频帧,1表示音频帧 unsigned char cReserve1; // 保留字段1 unsigned char cReserve2; // 保留字段2 unsigned char cReserve3; // 保留字段3 unsigned int nFrameNum; // 帧序号 unsigned int nTimeStamp; // 时间戳 unsigned int nFrameRate; // 帧率 unsigned int nWidth; // 视频宽度 unsigned int nHeight; // 视频高度 unsigned char cReserve4[16]; // 保留字段4 } DHAV_HEADER; // 读取H.264裸流文件 int readH264File(const char* filePath, unsigned char* buffer, int bufferSize) { ifstream fileStream(filePath, ios::in | ios::binary); if (!fileStream.is_open()) { cout << "Failed to open file: " << filePath << endl; return -1; } fileStream.read((char*)buffer, bufferSize); int readSize = fileStream.gcount(); fileStream.close(); return readSize; } // 将H.264裸流转换成DHAV帧结构体 int h264ToDHAV(unsigned char* h264Buffer, int h264Size, DHAV_HEADER* dhavHeader, unsigned char* dhavBuffer, int dhavBufferSize) { // 填写DHAV帧头部分 memset(dhavHeader, 0, sizeof(DHAV_HEADER)); memcpy(dhavHeader->cTag, "DHAV", 4); dhavHeader->nLength = sizeof(DHAV_HEADER) + h264Size; dhavHeader->cType = 0; // 视频帧 dhavHeader->nFrameNum = 1; // 帧序号,可以根据需要进行修改 dhavHeader->nTimeStamp = time(NULL); // 时间戳,可以根据需要进行修改 dhavHeader->nFrameRate = 25; // 帧率,可以根据需要进行修改 dhavHeader->nWidth = 1920; // 视频宽度,可以根据需要进行修改 dhavHeader->nHeight = 1080; // 视频高度,可以根据需要进行修改 // 填写DHAV帧数据部分 memcpy(dhavBuffer, dhavHeader, sizeof(DHAV_HEADER)); memcpy(dhavBuffer + sizeof(DHAV_HEADER), h264Buffer, h264Size); return dhavHeader->nLength; } int main(int argc, char* argv[]) { const char* h264FilePath = "test.h264"; const char* dhavFilePath = "test.dhav"; const int h264BufferSize = 1024 * 1024; const int dhavBufferSize = 1024 * 1024; unsigned char h264Buffer[h264BufferSize]; DHAV_HEADER dhavHeader; unsigned char dhavBuffer[dhavBufferSize]; // 读取H.264裸流文件 int h264Size = readH264File(h264FilePath, h264Buffer, h264BufferSize); if (h264Size < 0) { return -1; } // 将H.264裸流转换成DHAV帧结构体 int dhavSize = h264ToDHAV(h264Buffer, h264Size, &dhavHeader, dhavBuffer, dhavBufferSize); if (dhavSize < 0) { return -1; } // 将DHAV帧数据写入文件 ofstream fileStream(dhavFilePath, ios::out | ios::binary); if (!fileStream.is_open()) { cout << "Failed to open file: " << dhavFilePath << endl; return -1; } fileStream.write((char*)dhavBuffer, dhavSize); fileStream.close(); return 0; } ``` 需要注意的是,以上代码仅供参考,实际应用中需要根据协议文档进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值