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/GUISettings.h"
33 #include "AudioDecoder.h"
35 DVDPlayerCodec::DVDPlayerCodec()
37 m_CodecName = "DVDPlayer";
39 m_pInputStream = NULL;
48 DVDPlayerCodec::~DVDPlayerCodec()
53 void DVDPlayerCodec::SetContentType(const CStdString &strContent)
55 m_strContentType = strContent;
58 bool DVDPlayerCodec::Init(const CStdString &strFile, unsigned int filecache)
63 CStdString strFileToOpen = strFile;
65 CURL urlFile(strFile);
66 if (urlFile.GetProtocol() == "shout" )
67 strFileToOpen.Replace("shout://","http://");
69 m_pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, strFileToOpen, m_strContentType);
72 CLog::Log(LOGERROR, "%s: Error creating input stream for %s", __FUNCTION__, strFileToOpen.c_str());
76 if (!m_pInputStream->Open(strFileToOpen.c_str(), m_strContentType))
78 CLog::Log(LOGERROR, "%s: Error opening file %s", __FUNCTION__, strFileToOpen.c_str());
80 delete m_pInputStream;
81 m_pInputStream = NULL;
89 m_pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream);
92 delete m_pInputStream;
93 m_pInputStream = NULL;
94 CLog::Log(LOGERROR, "%s: Error creating demuxer", __FUNCTION__);
100 CLog::Log(LOGERROR, "%s: Exception thrown when opeing demuxer", __FUNCTION__);
106 delete m_pInputStream;
107 m_pInputStream = NULL;
111 CDemuxStream* pStream = NULL;
113 for (int i = 0; i < m_pDemuxer->GetNrOfStreams(); i++)
115 pStream = m_pDemuxer->GetStream(i);
116 if (pStream && pStream->type == STREAM_AUDIO)
123 if (m_nAudioStream == -1)
125 CLog::Log(LOGERROR, "%s: Could not find audio stream", __FUNCTION__);
128 delete m_pInputStream;
129 m_pInputStream = NULL;
133 CDVDStreamInfo hint(*pStream, true);
135 bool passthrough = AUDIO_IS_BITSTREAM(g_guiSettings.GetInt("audiooutput.mode"));
136 m_pAudioCodec = CDVDFactoryCodec::CreateAudioCodec(hint, passthrough);
139 CLog::Log(LOGERROR, "%s: Could not create audio codec", __FUNCTION__);
142 delete m_pInputStream;
143 m_pInputStream = NULL;
147 // we have to decode initial data in order to get channels/samplerate
148 // for sanity - we read no more than 10 packets
150 for (int nPacket=0; nPacket < 10 && (m_Channels == 0 || m_SampleRate == 0); nPacket++)
154 if (ReadPCM(dummy, nSize, &nSize) == READ_ERROR)
157 m_DataFormat = m_pAudioCodec->GetDataFormat();
158 m_BitsPerSample = CAEUtil::DataFormatToBits(m_DataFormat);
159 m_SampleRate = m_pAudioCodec->GetSampleRate();
160 m_EncodedSampleRate = m_pAudioCodec->GetEncodedSampleRate();
161 m_Channels = m_pAudioCodec->GetChannels();
162 m_ChannelInfo = m_pAudioCodec->GetChannelMap();
167 CLog::Log(LOGDEBUG, "%s: Could not decode data", __FUNCTION__);
173 if (m_Channels == 0) // no data - just guess and hope for the best
176 if (m_SampleRate == 0)
177 m_SampleRate = 44100;
179 m_TotalTime = m_pDemuxer->GetStreamLength();
180 m_Bitrate = m_pAudioCodec->GetBitRate();
181 m_pDemuxer->GetStreamCodecName(m_nAudioStream,m_CodecName);
186 void DVDPlayerCodec::DeInit()
189 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
192 if (m_pDemuxer != NULL)
198 if (m_pInputStream != NULL)
200 delete m_pInputStream;
201 m_pInputStream = NULL;
204 if (m_pAudioCodec != NULL)
206 delete m_pAudioCodec;
207 m_pAudioCodec = NULL;
215 int64_t DVDPlayerCodec::Seek(int64_t iSeekTime)
218 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
221 m_pDemuxer->SeekTime((int)iSeekTime, false);
222 m_pAudioCodec->Reset();
230 int DVDPlayerCodec::ReadPCM(BYTE *pBuffer, int size, int *actualsize)
232 if (m_decoded && m_nDecodedLen > 0)
234 int nLen = (size<m_nDecodedLen)?size:m_nDecodedLen;
236 memcpy(pBuffer, m_decoded, *actualsize);
237 m_nDecodedLen -= nLen;
238 m_decoded += (*actualsize);
245 // dvdplayer returns a read error on a single invalid packet, while
246 // in paplayer READ_ERROR is a fatal error.
247 // Therefore skip over invalid packets here.
249 for (int tries = 0; decodeLen < 0 && tries < 2; ++tries)
251 if (m_pPacket && m_audioPos >= m_pPacket->iSize)
253 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
258 if (m_pPacket == NULL)
262 m_pPacket = m_pDemuxer->Read();
263 } while (m_pPacket && m_pPacket->iStreamId != m_nAudioStream);
272 decodeLen = m_pAudioCodec->Decode(m_pPacket->pData + m_audioPos, m_pPacket->iSize - m_audioPos);
275 m_audioPos = m_pPacket->iSize; // skip packet
280 CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
286 m_audioPos += decodeLen;
288 m_nDecodedLen = m_pAudioCodec->GetData(&m_decoded);
290 *actualsize = (m_nDecodedLen <= size) ? m_nDecodedLen : size;
293 memcpy(pBuffer, m_decoded, *actualsize);
294 m_nDecodedLen -= *actualsize;
295 m_decoded += (*actualsize);
301 bool DVDPlayerCodec::CanInit()
306 bool DVDPlayerCodec::CanSeek()