live555增强版--支持串流mp4格式

live555目前最新版(0.75)的mediaServer只支持".m4e"格式的Elementary Stream fie,但并不支持串流mp4封装格式的文件,要串流mp4格式的文件一般都是结合FFmpeg进行,但是代码量稍大,这里使用一种较为简单的方法实现对mp4封装格式文件的串流。

其具体思路如下图:

live555增强版--支持串流mp4格式_第1张图片

即我们预先对mp4文件进行处理,解析出各个流,并单独存储它们(解析工具用'mp4creator -extract='),然后将解析出来的H.264文件再进行串流处理(最新版的live555支持H.264的串流),客户端可用VLC或者FFmpeg的ffplay进行串流播放。

具体实现为只需在DynamicRTSPServer.cpp文件中添加如下实现代码:

[cpp]  view plain copy
  1. else if((strcmp(extension, ".mp4") == 0) || (strcmp(extension, ".m4v") == 0))  
  2.   {  
  3.         NEW_SMS("MPEG-4 Video/Audio");  
  4.         // get the info about file  
  5.         char * command = (char *) malloc(150 * sizeof (char));  
  6.         memset(command,0,150*sizeof(char));  
  7.         command = strcat(command, "mp4info  ");  
  8.         command = strcat(command, fileName);  
  9.         command = strcat(command, " >temp ");  
  10.         puts(command);  
  11.         system(command);  
  12.         /////////////////////////////parsing code/////////////////////////////////////////////  
  13.         FILE * fTemp = fopen("temp""r"),*temp = NULL;  
  14.         if (fTemp != NULL)  
  15.         {  
  16.             char c, ext[4];  
  17.             ext[0] = '.';  
  18.             ext[1] = 't';  
  19.             ext[3] = '\0';  
  20.             char * Word = (char *) malloc(100 * sizeof (char));  
  21.             memset(Word,0,100 * sizeof(char));  
  22.             int flagLine = 0, lineCount = 0, flagWord = 0, wordCount = 0, i = 0, flagCodec = 0, streamCount = 1;  
  23.             while (!feof(fTemp))  
  24.             {  
  25.                 if (lineCount != 3)  
  26.                 {  
  27.                     c = getc(fTemp);  
  28.                 }  
  29.                 if (flagLine == 1)  
  30.                 {  
  31.                     flagLine = 0;  
  32.                     if ((c > 48) && (c < 59)) {// get inside the stream numbers only ...  
  33.                         flagWord = 1;  
  34.                         while ((c != '\n') && (!feof(fTemp)))  
  35.                         {  
  36.                             c = getc(fTemp);  
  37.                             if (flagWord == 1)  
  38.                             {  
  39.                                 i = 0;  
  40.                                 while ((c != ' ') && (c != '\t') && (!feof(fTemp)))  
  41.                                 {  
  42.                                     Word[i] = tolower(c);  
  43.                                     i++;  
  44.                                     c = getc(fTemp);  
  45.                                 }  
  46.                                 Word[i] = '\0';  
  47.   
  48.                                 if ((strcmp("video", Word) == 0) || (strcmp("audio", Word) == 0))   
  49.                                 {  
  50.                                     flagCodec = 1;  
  51.                                     wordCount = 0;  
  52.                                     int i;  
  53.                                     for (i = 0; i < 100; i++)  
  54.                                     {  
  55.                                         Word[i] = '\n';  
  56.                                     }  
  57.                                     ext[2] = '0' + streamCount;  
  58.                                     strcpy(Word, "mp4creator -extract=");     
  59.                                     Word[20] = streamCount + 48;  
  60.                                     Word[21] = ' ';  
  61.                                     Word[22] = '\0';  
  62.                                     Word = strcat(Word,fileName);  
  63.                                     puts(Word);  
  64.   
  65.                                     command = strcpy(command, fileName);  
  66.                                     command = strcat(command, ext);  
  67.                                     temp = fopen(command,"r");  
  68.                                     if(temp == NULL)  
  69.                                     {  
  70.                                         env<< "creating files";  
  71.                                         system(Word);  
  72.                                     }  
  73.                                     puts(command);  
  74.                                     streamCount++;  
  75.                                 }  
  76.                                 if ((flagCodec == 1) && (wordCount == 1))  
  77.                                 {  
  78.                                     if (strcmp("h264", Word) == 0)  
  79.                                     {  
  80.                                     //    printf("error cant play H.264 files.");  
  81.                                     //    return 0;  
  82.                                            NEW_SMS("H.264 Video");  
  83.                                            OutPacketBuffer::maxSize = 100000; // allow for some possibly large H.264 frames  
  84.                                            sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(env, command, reuseSource));  
  85.                                     }  
  86.                                 }  
  87.                                 if ((flagCodec == 1) && (wordCount == 2))  
  88.                                 {  
  89.                                     flagCodec = 0;  
  90.                                     printf("Flagged - %s  line: %d\n", Word, lineCount);  
  91.                                     ///////////////////////////////enter the code here////////////////////////////////  
  92.                                     if (strcmp("aac", Word) == 0)  
  93.                                     {  
  94.                                         puts("aac found");  
  95.                                         sms->addSubsession(ADTSAudioFileServerMediaSubsession  
  96.                                                 ::createNew(env, command, reuseSource));  
  97.                                         puts(ext);  
  98.                                     } else if (strcmp("simple", Word) == 0)  
  99.                                     {  
  100.                                         puts("m4e found");  
  101.                                         sms->addSubsession(MPEG4VideoFileServerMediaSubsession  
  102.                                                 ::createNew(env,command, reuseSource));  
  103.                                         puts(ext);  
  104.                                     } else if (strcmp("h264", Word) == 0)   
  105.                                     {  
  106.                                         puts("m4e found");  
  107.                                         puts(ext);  
  108.                                     } else if (strcmp("amr", Word) == 0)  
  109.                                     {  
  110.                                         puts("amr found");  
  111.                                         puts(ext);  
  112.                                     }  
  113.                                 }  
  114.                                 flagWord = 0; ////////// the word flag is reset  
  115.                             }  
  116.                             if ((c == '\t') || (c == ' '))  
  117.                             {  
  118.                                 wordCount++;  
  119.                                 flagWord = 1; // the word flag set for getting next word.  
  120.                             }  
  121.                         }  
  122.                         flagWord = 0;  
  123.                         wordCount = 0;  
  124.                         goto out;  
  125.                     }  
  126.                 }  
  127. out:  
  128.                 if (c == '\n')   
  129.                 {  
  130.                     lineCount++;  
  131.                     if (lineCount > 2)  
  132.                     {  
  133.                         flagLine = 1;  
  134.                     }  
  135.                 }  
  136.   
  137.             }  
  138.   
  139.         } else  
  140.         {  
  141.             printf("the file not found");  
  142.             return 0;  
  143.         }  
  144.     }  


 

参考代码:http://sourceforge.net/projects/live555mp4/

附:

1.上述代码中用到两个工具程序mp4info.exe和mp4creator.exe,下载地址为:

http://download.csdn.net/detail/c_m_deng/4625514http://download.csdn.net/detail/c_m_deng/4625522

2.关于live555最新版(2012.10.04)的下载地址及VC++6.0编译参考前一篇文章

你可能感兴趣的:(live555增强版--支持串流mp4格式)