Merge pull request #3455 from FernetMenta/audiopage
[vuplus_xbmc] / xbmc / cores / paplayer / DVDPlayerCodec.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "DVDPlayerCodec.h"
22 #include "cores/AudioEngine/Utils/AEUtil.h"
23
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"
31 #include "URL.h"
32
33 #include "AudioDecoder.h"
34
35 DVDPlayerCodec::DVDPlayerCodec()
36 {
37   m_CodecName = "DVDPlayer";
38   m_pDemuxer = NULL;
39   m_pInputStream = NULL;
40   m_pAudioCodec = NULL;
41   m_nAudioStream = -1;
42   m_audioPos = 0;
43   m_pPacket = NULL;
44   m_decoded = NULL;
45   m_nDecodedLen = 0;
46   m_strFileName = "";
47   m_bInited = false;
48 }
49
50 DVDPlayerCodec::~DVDPlayerCodec()
51 {
52   DeInit();
53 }
54
55 void DVDPlayerCodec::SetContentType(const CStdString &strContent)
56 {
57   m_strContentType = strContent;
58 }
59
60 bool DVDPlayerCodec::Init(const CStdString &strFile, unsigned int filecache)
61 {
62   // take precaution if Init()ialized earlier
63   if (m_bInited)
64   {
65     // keep things as is if Init() was done with known strFile
66     if (m_strFileName == strFile)
67       return true;
68
69     // got differing filename, so cleanup before starting over
70     DeInit();
71   }
72
73   m_decoded = NULL;
74   m_nDecodedLen = 0;
75
76   CStdString strFileToOpen = strFile;
77
78   CURL urlFile(strFile);
79   if (urlFile.GetProtocol() == "shout" )
80     strFileToOpen.Replace("shout://","http://");
81
82   m_pInputStream = CDVDFactoryInputStream::CreateInputStream(NULL, strFileToOpen, m_strContentType);
83   if (!m_pInputStream)
84   {
85     CLog::Log(LOGERROR, "%s: Error creating input stream for %s", __FUNCTION__, strFileToOpen.c_str());
86     return false;
87   }
88
89   if (!m_pInputStream->Open(strFileToOpen.c_str(), m_strContentType))
90   {
91     CLog::Log(LOGERROR, "%s: Error opening file %s", __FUNCTION__, strFileToOpen.c_str());
92     if (m_pInputStream)
93       delete m_pInputStream;
94     m_pInputStream = NULL;
95     return false;
96   }
97
98   m_pDemuxer = NULL;
99
100   try
101   {
102     m_pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream);
103     if (!m_pDemuxer)
104     {
105       delete m_pInputStream;
106       m_pInputStream = NULL;
107       CLog::Log(LOGERROR, "%s: Error creating demuxer", __FUNCTION__);
108       return false;
109     }
110   }
111   catch(...)
112   {
113     CLog::Log(LOGERROR, "%s: Exception thrown when opening demuxer", __FUNCTION__);
114     if (m_pDemuxer)
115     {
116       delete m_pDemuxer;
117       m_pDemuxer = NULL;
118     }
119     delete m_pInputStream;
120     m_pInputStream = NULL;
121     return false;
122   }
123
124   CDemuxStream* pStream = NULL;
125   m_nAudioStream = -1;
126   for (int i = 0; i < m_pDemuxer->GetNrOfStreams(); i++)
127   {
128     pStream = m_pDemuxer->GetStream(i);
129     if (pStream && pStream->type == STREAM_AUDIO)
130     {
131       m_nAudioStream = i;
132       break;
133     }
134   }
135
136   if (m_nAudioStream == -1)
137   {
138     CLog::Log(LOGERROR, "%s: Could not find audio stream", __FUNCTION__);
139     delete m_pDemuxer;
140     m_pDemuxer = NULL;
141     delete m_pInputStream;
142     m_pInputStream = NULL;
143     return false;
144   }
145
146   CDVDStreamInfo hint(*pStream, true);
147
148   m_pAudioCodec = CDVDFactoryCodec::CreateAudioCodec(hint);
149   if (!m_pAudioCodec)
150   {
151     CLog::Log(LOGERROR, "%s: Could not create audio codec", __FUNCTION__);
152     delete m_pDemuxer;
153     m_pDemuxer = NULL;
154     delete m_pInputStream;
155     m_pInputStream = NULL;
156     return false;
157   }
158
159   // we have to decode initial data in order to get channels/samplerate
160   // for sanity - we read no more than 10 packets
161   int nErrors = 0;
162   for (int nPacket=0; nPacket < 10 && (m_Channels == 0 || m_SampleRate == 0); nPacket++)
163   {
164     BYTE dummy[256];
165     int nSize = 256;
166     if (ReadPCM(dummy, nSize, &nSize) == READ_ERROR)
167       ++nErrors;
168
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();
175
176   }
177   if (nErrors >= 10)
178   {
179     CLog::Log(LOGDEBUG, "%s: Could not decode data", __FUNCTION__);
180     return false;
181   }
182
183   m_nDecodedLen = 0;
184
185   if (m_Channels == 0) // no data - just guess and hope for the best
186     m_Channels = 2;
187
188   if (m_SampleRate == 0)
189     m_SampleRate = 44100;
190
191   m_TotalTime = m_pDemuxer->GetStreamLength();
192   m_Bitrate = m_pAudioCodec->GetBitRate();
193   m_pDemuxer->GetStreamCodecName(m_nAudioStream,m_CodecName);
194
195   m_strFileName = strFile;
196   m_bInited = true;
197
198   return true;
199 }
200
201 void DVDPlayerCodec::DeInit()
202 {
203   if (m_pPacket)
204     CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
205   m_pPacket = NULL;
206
207   if (m_pDemuxer != NULL)
208   {
209     delete m_pDemuxer;
210     m_pDemuxer = NULL;
211   }
212
213   if (m_pInputStream != NULL)
214   {
215     delete m_pInputStream;
216     m_pInputStream = NULL;
217   }
218
219   if (m_pAudioCodec != NULL)
220   {
221     delete m_pAudioCodec;
222     m_pAudioCodec = NULL;
223   }
224
225   // cleanup format information
226   m_TotalTime = 0;
227   m_SampleRate = 0;
228   m_EncodedSampleRate = 0;
229   m_BitsPerSample = 0;
230   m_DataFormat = AE_FMT_INVALID;
231   m_Channels = 0;
232   m_Bitrate = 0;
233
234   m_audioPos = 0;
235   m_decoded = NULL;
236   m_nDecodedLen = 0;
237
238   m_strFileName = "";
239   m_bInited = false;
240 }
241
242 int64_t DVDPlayerCodec::Seek(int64_t iSeekTime)
243 {
244   if (m_pPacket)
245     CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
246   m_pPacket = NULL;
247
248   m_pDemuxer->SeekTime((int)iSeekTime, false);
249   m_pAudioCodec->Reset();
250
251   m_decoded = NULL;
252   m_nDecodedLen = 0;
253
254   return iSeekTime;
255 }
256
257 int DVDPlayerCodec::ReadPCM(BYTE *pBuffer, int size, int *actualsize)
258 {
259   if (m_decoded && m_nDecodedLen > 0)
260   {
261     int nLen = (size<m_nDecodedLen)?size:m_nDecodedLen;
262     *actualsize = nLen;
263     memcpy(pBuffer, m_decoded, *actualsize);
264     m_nDecodedLen -= nLen;
265     m_decoded += (*actualsize);
266     return READ_SUCCESS;
267   }
268
269   m_decoded = NULL;
270   m_nDecodedLen = 0;
271
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.
275   int decodeLen = -1;
276   for (int tries = 0; decodeLen < 0 && tries < 2; ++tries)
277   {
278     if (m_pPacket && m_audioPos >= m_pPacket->iSize)
279     {
280       CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
281       m_audioPos = 0;
282       m_pPacket = NULL;
283     }
284
285     if (m_pPacket == NULL)
286     {
287       do
288       {
289         m_pPacket = m_pDemuxer->Read();
290       } while (m_pPacket && m_pPacket->iStreamId != m_nAudioStream);
291
292       if (!m_pPacket)
293       {
294         return READ_EOF;
295       }
296       m_audioPos = 0;
297     }
298
299     decodeLen = m_pAudioCodec->Decode(m_pPacket->pData + m_audioPos, m_pPacket->iSize - m_audioPos);
300
301     if (decodeLen < 0)
302       m_audioPos = m_pPacket->iSize; // skip packet
303   }
304
305   if (decodeLen < 0)
306   {
307     CDVDDemuxUtils::FreeDemuxPacket(m_pPacket);
308     m_pPacket = NULL;
309     m_audioPos = 0;
310     return READ_ERROR;
311   }
312
313   m_audioPos += decodeLen;
314
315   m_nDecodedLen = m_pAudioCodec->GetData(&m_decoded);
316
317   *actualsize = (m_nDecodedLen <= size) ? m_nDecodedLen : size;
318   if (*actualsize > 0)
319   {
320     memcpy(pBuffer, m_decoded, *actualsize);
321     m_nDecodedLen -= *actualsize;
322     m_decoded += (*actualsize);
323   }
324
325   return READ_SUCCESS;
326 }
327
328 bool DVDPlayerCodec::CanInit()
329 {
330   return true;
331 }
332
333 bool DVDPlayerCodec::CanSeek()
334 {
335   return true;
336 }