changed: Add logic to properly handle subtitles for stacked files
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / ActiveAE / ActiveAESound.cpp
1 /*
2  *      Copyright (C) 2010-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 "Interfaces/AESound.h"
22
23 #include "AEFactory.h"
24 #include "Utils/AEAudioFormat.h"
25 #include "ActiveAE.h"
26 #include "ActiveAESound.h"
27 #include "utils/log.h"
28 #include "DllAvUtil.h"
29
30 using namespace ActiveAE;
31 using namespace XFILE;
32
33 /* typecast AE to CActiveAE */
34 #define AE (*((CActiveAE*)CAEFactory::GetEngine()))
35
36 CActiveAESound::CActiveAESound(const std::string &filename) :
37   IAESound         (filename),
38   m_filename       (filename),
39   m_volume         (1.0f    )
40 {
41   m_orig_sound = NULL;
42   m_dst_sound = NULL;
43   m_pFile = NULL;
44 }
45
46 CActiveAESound::~CActiveAESound()
47 {
48   delete m_orig_sound;
49   delete m_dst_sound;
50   Finish();
51 }
52
53 void CActiveAESound::Play()
54 {
55   AE.PlaySound(this);
56 }
57
58 void CActiveAESound::Stop()
59 {
60   AE.StopSound(this);
61 }
62
63 bool CActiveAESound::IsPlaying()
64 {
65   // TODO
66   return false;
67 }
68
69 uint8_t** CActiveAESound::InitSound(bool orig, SampleConfig config, int nb_samples)
70 {
71   CSoundPacket **info;
72   if (orig)
73     info = &m_orig_sound;
74   else
75     info = &m_dst_sound;
76
77   delete *info;
78   *info = new CSoundPacket(config, nb_samples);
79
80   (*info)->nb_samples = 0;
81   m_isConverted = false;
82   return (*info)->data;
83 }
84
85 bool CActiveAESound::StoreSound(bool orig, uint8_t **buffer, int samples, int linesize)
86 {
87   CSoundPacket **info;
88   if (orig)
89     info = &m_orig_sound;
90   else
91     info = &m_dst_sound;
92
93   if ((*info)->nb_samples + samples > (*info)->max_nb_samples)
94   {
95     CLog::Log(LOGERROR, "CActiveAESound::StoreSound - exceeded max samples");
96     return false;
97   }
98
99   int bytes_to_copy = samples * (*info)->bytes_per_sample * (*info)->config.channels;
100   bytes_to_copy /= (*info)->planes;
101   int start = (*info)->nb_samples * (*info)->bytes_per_sample * (*info)->config.channels;
102   start /= (*info)->planes;
103
104   for (int i=0; i<(*info)->planes; i++)
105   {
106     memcpy((*info)->data[i]+start, buffer[i], bytes_to_copy);
107   }
108   (*info)->nb_samples += samples;
109
110   return true;
111 }
112
113 CSoundPacket *CActiveAESound::GetSound(bool orig)
114 {
115   if (orig)
116     return m_orig_sound;
117   else
118     return m_dst_sound;
119 }
120
121 bool CActiveAESound::Prepare()
122 {
123   unsigned int flags = READ_TRUNCATED | READ_CHUNKED;
124   m_pFile = new CFile();
125
126   if (!m_pFile->Open(m_filename, flags))
127   {
128     delete m_pFile;
129     m_pFile = NULL;
130     return false;
131   }
132   m_isSeekPosible = m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL) != 0;
133   m_fileSize = m_pFile->GetLength();
134   return true;
135 }
136
137 void CActiveAESound::Finish()
138 {
139   delete m_pFile;
140   m_pFile = NULL;
141 }
142
143 int CActiveAESound::GetChunkSize()
144 {
145   return m_pFile->GetChunkSize();
146 }
147
148 int CActiveAESound::Read(void *h, uint8_t* buf, int size)
149 {
150   CFile *pFile = static_cast<CActiveAESound*>(h)->m_pFile;
151   return pFile->Read(buf, size);
152 }
153
154 offset_t CActiveAESound::Seek(void *h, offset_t pos, int whence)
155 {
156   CFile* pFile = static_cast<CActiveAESound*>(h)->m_pFile;
157   if(whence == AVSEEK_SIZE)
158     return pFile->GetLength();
159   else
160     return pFile->Seek(pos, whence & ~AVSEEK_FORCE);
161 }