2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "DVDPlayerCodec.h"
22 #include "cores/AudioEngine/Utils/AEUtil.h"
24 #include "cores/dvdplayer/DVDInputStreams/DVDFactoryInputStream.h"
25 #include "cores/dvdplayer/DVDDemuxers/DVDFactoryDemuxer.h"
26 #include "cores/dvdplayer/DVDDemuxers/DVDDemuxUtils.h"
27 #include "cores/dvdplayer/DVDStreamInfo.h"
28 #include "cores/dvdplayer/DVDCodecs/DVDFactoryCodec.h"
29 #include "utils/log.h"
30 #include "settings/Settings.h"
33 #include "AudioDecoder.h"
35 DVDPlayerCodec::DVDPlayerCodec()
37 m_CodecName = "DVDPlayer";
39 m_pInputStream = NULL;
50 DVDPlayerCodec::~DVDPlayerCodec()
55 void DVDPlayerCodec::SetContentType(const CStdString &strContent)
57 m_strContentType = strContent;
60 bool DVDPlayerCodec::Init(const CStdString &strFile, unsigned int filecache)
62 // take precaution if Init()ialized earlier
65 // keep things as is if Init() was done with known strFile
66 if (m_strFileName == strFile)
69 // got differing filename, so cleanup before starting over
76 CStdString strFileToOpen = strFile;
78 CURL urlFile(strFile);
79 if (urlFile.GetProtocol() == "shout" )
80 strFileToOpen.Replace("shout://","http://");
82 m_pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, strFileToOpen, m_strContentType);
85 CLog::Log(LOGERROR, "%s: Error creating input stream for %s", __FUNCTION__, strFileToOpen.c_str());
89 if (!m_pInputStream->Open(strFileToOpen.c_str(), m_strContentType))
91 CLog::Log(LOGERROR, "%s: Error opening file %s", __FUNCTION__, strFileToOpen.c_str());
93 delete m_pInputStream;
94 m_pInputStream = NULL;
102 m_pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream);
105 delete m_pInputStream;
106 m_pInputStream = NULL;
107 CLog::Log(LOGERROR, "%s: Error creating demuxer", __FUNCTION__);
113 CLog::Log(LOGERROR, "%s: Exception thrown when opening demuxer", __FUNCTION__);
119 delete m_pInputStream;
120 m_pInputStream = NULL;
124 CDemuxStream* pStream = NULL;
126 for (int i = 0; i < m_pDemuxer->GetNrOfStreams(); i++)
128 pStream = m_pDemuxer->GetStream(i);
129 if (pStream && pStream->type == STREAM_AUDIO)
136 if (m_nAudioStream == -1)
138 CLog::Log(LOGERROR, "%s: Could not find audio stream", __FUNCTION__);
141 delete m_pInputStream;
142 m_pInputStream = NULL;
146 CDVDStreamInfo hint(*pStream, true);
148 m_pAudioCodec = CDVDFactoryCodec::CreateAudioCodec(hint);
151 CLog::Log(LOGERROR, "%s: Could not create audio codec", __FUNCTION__);
154 delete m_pInputStream;
155 m_pInputStream = NULL;
159 // we have to decode initial data in order to get channels/samplerate
160 // for sanity - we read no more than 10 packets
162 for (int nPacket=0; nPacket < 10 && (m_Channels == 0 || m_SampleRate == 0); nPacket++)
166 if (ReadPCM(dummy, nSize, &nSize) == READ_ERROR)
169 m_DataFormat = m_pAudioCodec->GetDataFormat();
170 m_BitsPerSample = CAEUtil::DataFormatToBits(m_DataFormat);
171 m_SampleRate = m_pAudioCodec->GetSampleRate();
172 m_EncodedSampleRate = m_pAudioCodec->GetEncodedSampleRate();
173 m_Channels = m_pAudioCodec->GetChannels();
174 m_ChannelInfo = m_pAudioCodec->GetChannelMap();
179 CLog::Log(LOGDEBUG, "%s: Could not decode data", __FUNCTION__);
185 if (m_Channels == 0) // no data - just guess and hope for the best
188 if (m_SampleRate == 0)
189 m_SampleRate = 44100;
191 m_TotalTime = m_pDemuxer->GetStreamLength();
192 m_Bitrate = m_pAudioCodec->GetBitRate();
193 m_pDemuxer->GetStreamCodecName(m_nAudioStream,m_CodecName);
195 m_strFileName = strFile;
201 void DVDPlayerCodec::DeInit()
204 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
207 if (m_pDemuxer != NULL)
213 if (m_pInputStream != NULL)
215 delete m_pInputStream;
216 m_pInputStream = NULL;
219 if (m_pAudioCodec != NULL)
221 delete m_pAudioCodec;
222 m_pAudioCodec = NULL;
225 // cleanup format information
228 m_EncodedSampleRate = 0;
230 m_DataFormat = AE_FMT_INVALID;
242 int64_t DVDPlayerCodec::Seek(int64_t iSeekTime)
245 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
248 m_pDemuxer->SeekTime((int)iSeekTime, false);
249 m_pAudioCodec->Reset();
257 int DVDPlayerCodec::ReadPCM(BYTE *pBuffer, int size, int *actualsize)
259 if (m_decoded && m_nDecodedLen > 0)
261 int nLen = (size<m_nDecodedLen)?size:m_nDecodedLen;
263 memcpy(pBuffer, m_decoded, *actualsize);
264 m_nDecodedLen -= nLen;
265 m_decoded += (*actualsize);
272 // dvdplayer returns a read error on a single invalid packet, while
273 // in paplayer READ_ERROR is a fatal error.
274 // Therefore skip over invalid packets here.
276 for (int tries = 0; decodeLen < 0 && tries < 2; ++tries)
278 if (m_pPacket && m_audioPos >= m_pPacket->iSize)
280 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
285 if (m_pPacket == NULL)
289 m_pPacket = m_pDemuxer->Read();
290 } while (m_pPacket && m_pPacket->iStreamId != m_nAudioStream);
299 decodeLen = m_pAudioCodec->Decode(m_pPacket->pData + m_audioPos, m_pPacket->iSize - m_audioPos);
302 m_audioPos = m_pPacket->iSize; // skip packet
307 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
313 m_audioPos += decodeLen;
315 m_nDecodedLen = m_pAudioCodec->GetData(&m_decoded);
317 *actualsize = (m_nDecodedLen <= size) ? m_nDecodedLen : size;
320 memcpy(pBuffer, m_decoded, *actualsize);
321 m_nDecodedLen -= *actualsize;
322 m_decoded += (*actualsize);
328 bool DVDPlayerCodec::CanInit()
333 bool DVDPlayerCodec::CanSeek()