轻量便携流媒体播放器框架设计-2

http://blog.csdn.net/tteaonly/article/details/7378162

前面讨论最基础的设计思路,下面说明详细的设计实现。
  • Access 模块
     Access模块完成数据获得,设计两个接口:Open()/Close(),提供三个回调函数即onStreamBegin(bool), onStreamEnd(), onStreamData(void *,int);  看意思就清楚了,无需多余的说明,代码如下:
[cpp] view plain copy
 
  1. class Access {  
  2. public:      
  3.     virtual ~TeaAccess(){};  
  4.     //interfaces  
  5.     virtual bool Open() = 0;  
  6.     virtual void Close() = 0;  
  7.     //callbacks  
  8.     sigslot::signal1<bool> signalBeginOfStream;  
  9.     sigslot::signal0<> signalEndOfStream;  
  10.     sigslot::signal2<const unsigned char*, size_t> signalData;  
  11. };  
    Access为独立线程,因此signalData回调需要进行数据复制之后进行线程切换。
 
  • Demux 模块
 Demux模块完成数据解析,提供的接口以及回调如下:
[cpp] view plain copy
 
  1. class Demux{  
  2. public:          
  3.     virtual ~TeaDemux(){};  
  4.     //interfaces      
  5.     virtual bool Open() = 0;  
  6.     virtual void Close() = 0;  
  7.     virtual bool PushNewData(const unsigned char *data, size_t length) = 0;                                    //callbacks  
  8.     sigslot::signal1<bool> signalProbed;  
  9.     sigslot::signal1<MediaPacket *> signalMediaPacket;  
  10.       
  11.     std::map<unsigned int, Decoder *> decoders;  
  12. };  

主要Open/Close如文字定义,负责Demuxer资源初始化和释放,其中PushNewData完成数据接收(即接收Access的数据)。
回调主要有两个,signalProbed(bool)回调主要完成是否正确解析多媒体文件,signalMediaPacket即解析出来的媒体包。
Demux为一独立线程,根据Access的数据不断产生MediaPacket数据,送给Decode模块解码。
此外,Demux在正确解析多媒体文件之后,维护Decoder对象,Decoder对象将MediaPacket解码为YUV和PCM数据。
 
  • DecodeTask 模块
该模块是整个播放器的核心之一,设计如下:
[cpp] view plain copy
 
  1. class DecodeTask {  
  2. public:          
  3.     DecodeTask(Demux *dm);  
  4.     virtual ~DecodeTask(){};  
  5.     //interfaces      
  6.     void PushMediaPacket(MediaPacket *pkt);                                                                  
  7.     void DecodeVideo(MediaTime target);  
  8.     void DecodeAudio(MediaTime target);  
  9.     MediaTime BufferedVideoLength();  
  10.     unsigned int BufferedPictures();  
  11.     MediaTime FirstPictureTime();  
  12.     MediaTime LastPictureTime();  
  13.     MediaTime BufferedAudioLength();  
  14.     MediaTime FirstAudioTime();  
  15.     MediaTime LastAudioTime();  
  16.     //callbacks  
  17.     sigslot::signal1<VideoPicture *> signalVideoPicture;  
  18.     sigslot::signal1<AudioPCM *> signalVideoPCM;  
  19. };  

DecodeTask 模块独立线程,但是通过DecodeVideo/DecodeAudio驱动,生成的结果通过两个回调函数返回,并且提供内部音视频数据缓冲状况。Player对象通过定期查询DecodeTask的缓冲状况,驱动解码起进行解码,并将解码结果送交AudioOut模块以及VideoOut模块。
[cpp] view plain copy
 
  1. <pre name="code" class="cpp"><pre name="code" class="cpp" style="background-color: rgb(255, 255, 255); text-align: left; "><pre name="code" class="cpp" style="background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-size: 14px; line-height: 26px; text-align: left; "><pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12. <pre></pre>  
  13. <pre></pre>  
  14. <pre></pre>  
  15. <pre></pre>  
  16. <pre></pre>  
  17. <pre></pre>  
  18. <pre></pre>  
  19. <pre></pre>  
  20. <pre></pre>  
  21. <pre></pre>  
  22. <pre></pre>  
  23. </pre></pre></pre>  
原文地址:https://www.cnblogs.com/stnlcd/p/7202442.html