]> git.vomp.tv Git - vompclient-marten.git/blob - audiovpe.cc
Add files specific for raspberry pi
[vompclient-marten.git] / audiovpe.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "audiovpe.h"
22 #include "videovpeogl.h"
23 #include "log.h"
24
25 AudioVPE::AudioVPE()
26 {
27   if (instance) return;
28   initted = 0;
29   lastpacketnum=-1;
30   currentpacketnum=-1;
31   streamType = 0;
32   volume = 20;
33   muted = 0;
34   lastAType = MPTYPE_MPEG_AUDIO;
35 }
36
37 AudioVPE::~AudioVPE()
38 {
39 }
40
41 int AudioVPE::init(UCHAR tstreamType)
42 {
43   if (initted) return 0;
44   initted = 1;
45
46 // // if ((fdAudio = open("/dev/adec_mpg", O_RDWR | O_NONBLOCK)) < 0) return 0;
47  // if ((fdAudio = open("/dev/adec_mpg", O_WRONLY)) < 0) return 0;
48
49   streamType = tstreamType;
50
51   if (!initAllParams())
52   {
53     shutdown();
54     return 0;
55   }
56
57   unMute();
58
59   // Set the volume variable to what the hardware is set at now
60   int hwvol = -1;
61 /*  int hwvolFail = ioctl(fdAudio, AV_GET_AUD_VOLUME, &hwvol);
62   if (!hwvolFail)
63   {
64     volume = 20 - ((hwvol >> 16) & 0xFF);
65     if ((volume < 0) || (volume > 20)) volume = 20;
66   }*/
67
68   return 1;
69 }
70
71 int AudioVPE::initAllParams()
72 {
73   return (setStreamType(streamType) && setChannel() && setSource());
74 }
75
76 int AudioVPE::shutdown()
77 {
78   if (!initted) return 0;
79   initted = 0;
80   //close(fdAudio);
81   return 1;
82 }
83
84 int AudioVPE::setStreamType(UCHAR type)
85 {
86   if (!initted) return 0;
87
88  // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
89   return 1;
90 }
91
92 int AudioVPE::setChannel()
93 {
94   if (!initted) return 0;
95
96  // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
97   return 1;
98 }
99
100 int AudioVPE::setSource()
101 {
102   if (!initted) return 0;
103
104  // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
105   return 1;
106 }
107
108 int AudioVPE::sync()
109 {
110   if (!initted) return 0;
111
112  // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
113   return 1;
114 }
115
116 int AudioVPE::play()
117 {
118   if (!initted) return 0;
119   lastpacketnum=-1;
120   currentpacketnum=-1;
121   ((VideoVPEOGL*) Video::getInstance())->initMuxer();
122
123   //if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
124   return 1;
125 }
126
127 int AudioVPE::stop()
128 {
129   if (!initted) return 0;
130   ((VideoVPEOGL*) Video::getInstance())->deinitMuxer();
131
132   //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
133   return 1;
134 }
135
136 int AudioVPE::mute()
137 {
138   if (!initted) return 0;
139
140 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
141   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
142
143   muted = 1;
144   return 1;
145 }
146
147 int AudioVPE::unMute()
148 {
149   if (!initted) return 0;
150
151 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
152   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
153
154   muted = 0;
155   return 1;
156 }
157
158 int AudioVPE::pause()
159 {
160   if (!initted) return 0;
161
162  // if (ioctl(fdAudio, AV_SET_AUD_PAUSE, 1) != 0) return 0;
163   return 1;
164 }
165
166 int AudioVPE::unPause()
167 {
168   if (!initted) return 0;
169
170  // if (ioctl(fdAudio, AV_SET_AUD_UNPAUSE, 1) != 0) return 0;
171   return 1;
172 }
173
174 int AudioVPE::reset()
175 {
176   if (!initted) return 0;
177 //test();
178   Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
179   ((VideoVPEOGL*) Video::getInstance())->deinitMuxer();
180
181 //  if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
182 //  Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
183  // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
184
185   doMuting();
186   return 1;
187 }
188
189 int AudioVPE::setVolume(int tvolume)
190 {
191   // parameter: 0 for silence, 20 for full
192   if ((tvolume < 0) || (tvolume > 20)) return 0;
193
194 // volume = 2 * (20 - volume);
195 // Right, that one was rubbish... 0-10 were almost
196 // inaudible, 11-20 did what should have been done
197 // over the whole 0-20 range
198
199   tvolume = 20 - tvolume;
200
201   audio_volume Avolume;
202   Avolume.frontleft = tvolume + Aoffset.frontleft;
203   Avolume.frontright = tvolume + Aoffset.frontright;
204   Avolume.rearleft = tvolume + Aoffset.rearleft;
205   Avolume.rearright = tvolume + Aoffset.rearright;
206   Avolume.center = tvolume + Aoffset.center;
207   Avolume.lfe = tvolume + Aoffset.lfe;
208
209 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
210
211 //  unsigned long vol = (tvolume << 24) | (tvolume << 16);
212 //
213 //  Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
214 //  Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
215
216 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
217   return 1;
218 }
219
220 #ifdef DEV
221 int AudioVPE::test()
222 {
223 //  ULLONG stc = 0;
224 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
225
226 /*  aud_sync_parms_t a;
227   a.parm1 = 0;
228   a.parm2 = 0;
229 */
230 //  int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
231
232
233   /*OK*/ //printf("Audio sync disable = %i\n", b);
234
235   return 1;
236
237
238 }
239 #endif
240
241 void AudioVPE::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
242 {
243   packet = mplist.front();
244 }
245
246 UINT AudioVPE::DeliverMediaSample(UCHAR* buffer, UINT* samplepos)
247 {/*
248         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
249         //Log::getInstance()->log("Audio", Log::DEBUG, "lastpacket %d lastbuddypacket %d currentpacket %d",lastpacketnum,video->getLastPacketNum(),packet.index);
250         currentpacketnum=packet.index;
251   if (lastpacketnum!=-1 && packet.index-1!=lastpacketnum && packet.index-1>video->getLastPacketNum()) return 0; //Not in order
252
253   //format pes
254   switch (packet.type) {
255           case MPTYPE_MPEG_AUDIO:
256                  buffer[packet.pos_buffer + 3]=0xc0; break;
257           case MPTYPE_AC3:
258                   buffer[packet.pos_buffer +buffer[packet.pos_buffer+8]+9]=0x80;
259                   buffer[packet.pos_buffer + 3]=0xbd; break;
260                   break;
261           default:
262           case MPTYPE_AC3_PRE13://Not tested no recording availiable
263                   buffer[packet.pos_buffer + 3]=0xbd; break;
264                   break;
265   };
266
267    if (packet.type!=lastAType && lastpacketnum!=-1){//Format Change //Push data out !
268       video->deinitMuxer();
269           lastAType=packet.type;
270           video->initMuxer();
271   }
272   lastAType=packet.type;
273
274
275  /* Mutex *mutex=video->getMuxMutex();
276   mutex->Lock();
277   FILE *muxout=video->getMuxFile();
278   if (!muxout) {mutex->Unlock(); return 0;}*
279   int written=0;
280   //written=fwrite(buffer + packet.pos_buffer,1,packet.length,muxout);
281   written=video->WriteOutTS(buffer + packet.pos_buffer,packet.length,packet.type);
282   lastpacketnum=packet.index;
283   
284   //mutex->Unlock();
285
286    //Log::getInstance()->log("Audio", Log::DEBUG, "wrote %d bytes to mux",written);
287   
288   if (written == (int)packet.length) { *samplepos = 0; return 1;}
289   if (written <= 0) {
290           return 0;
291   }
292   *samplepos = written;*/
293   // Handle a partial write. Is this possible? Should we bother? No!
294  
295   return 1;
296 }
297
298 void AudioVPE::ResetTimeOffsets()
299 {
300 }