Layer1Pin.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "stdafx.h"
- #include ".layer1pin.h"
- Layer1Pin::Layer1Pin(NPLayer1::Layer1* pLayer1, bool bAudio) :
- m_pLayer1(pLayer1),
- m_bAudio(bAudio),
- m_needmediatype(true)
- {
- if(bAudio)
- m_maxSize = 200*1024; //音频数据缓冲区按200K计算
- else
- m_maxSize = 1000*1024; //视频缓冲按1000K计算
- }
- Layer1Pin::~Layer1Pin(void)
- {
- }
- void Layer1Pin::Start()
- {
- m_needmediatype = true;
- }
- P2P_RETURN_TYPE Layer1Pin::GetDataEx(
- SampleHeader& header, // out, 数据头
- PBYTE& pData, // out, 存储数据的缓冲区
- const UINT maxSize, // in, 缓冲区的长度
- const bool bKeySample, // in, 是否寻找关键帧
- bool bSaveSample //in, 是否缓冲当前的sample到本地
- )
- {
- P2P_RETURN_TYPE ret = m_pLayer1->GetData(header, pData, maxSize, m_bAudio, bKeySample);
- if(ret != PRT_OK)
- return ret;
- if(header.start == 0 && header.length == 0) //这是媒体类型的头
- {
- m_mediatype.SaveMediaTypeData(header, pData, 256); //保存媒体类型数据,存开头的256个byte也应该够了
- }
- else if(bSaveSample)
- {
- SampleBuffer::Clean(); //先清理当前的缓冲数据
- SampleBuffer::SaveData(header, pData, maxSize); //保存被缓冲的sample
- }
- return ret;
- }
- P2P_RETURN_TYPE Layer1Pin::PeerData(
- SampleHeader& header, // out, 数据头
- PBYTE& pData, // out, 存储数据的缓冲区
- const UINT maxSize, // in, 缓冲区的长度
- const bool bKeySample // in, 是否寻找关键帧
- )
- {
- if(IsAnyDataSaved()) //如果已经缓冲了数据,只显示缓冲的那块数据
- {
- SampleBuffer::PeerData(header, pData, maxSize);
- return PRT_OK;
- }
- return GetDataEx(header, pData, maxSize, bKeySample, true);
- }
- P2P_RETURN_TYPE Layer1Pin::GetData(
- SampleHeader& header, // out, 数据头
- PBYTE& pData, // out, 存储数据的缓冲区
- const UINT maxSize, // in, 缓冲区的长度
- const bool bKeySample // in, 是否寻找关键帧
- )
- {
- if(m_needmediatype)
- {
- int m_errcount = 0;
- while(!m_mediatype.IsInited())
- {
- if(m_errcount > 3)
- {
- //我ft! 类型数据不知道到哪里去了。只有放弃了
- MessageBox(NULL, "获取媒体类型失败了。播放将会停止", "", MB_OK);
- return PRT_SYS;
- }
- P2P_RETURN_TYPE ret = GetDataEx(header, pData, maxSize, bKeySample, true);
- if(ret != PRT_OK) //得不到数据头,再试几次
- {
- m_errcount++;
- Sleep(50);
- }
- else
- break;
- }
- //得到了数据头,可能是刚刚peerdata得到的,也可能是之前存下来的,恢复它
- m_mediatype.RestoreMediaTypeData(header, pData, maxSize);
- m_needmediatype = false;
- return PRT_OK;
- }
- if(IsAnyDataSaved() && SampleBuffer::GetData(header, pData, maxSize)) //有被缓冲的数据, 将缓冲的数据恢复
- {
- if(!(bKeySample && !header.bSyncPoint)) //要求关键帧,但我们缓冲的不是,就只能放弃,从layer1中获得,否则,可以返回
- return PRT_OK;
- }
- return GetDataEx(header, pData, maxSize, bKeySample, false);
- }