]> git.vomp.tv Git - vompclient-marten.git/blob - audiovpe.cc
h264 allmost done but still not working
[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
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
131   //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
132   return 1;
133 }
134
135 int AudioVPE::mute()
136 {
137   if (!initted) return 0;
138
139 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
140   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
141
142   muted = 1;
143   return 1;
144 }
145
146 int AudioVPE::unMute()
147 {
148   if (!initted) return 0;
149
150 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
151   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
152
153   muted = 0;
154   return 1;
155 }
156
157 int AudioVPE::pause()
158 {
159   if (!initted) return 0;
160
161  // if (ioctl(fdAudio, AV_SET_AUD_PAUSE, 1) != 0) return 0;
162   return 1;
163 }
164
165 int AudioVPE::unPause()
166 {
167   if (!initted) return 0;
168
169  // if (ioctl(fdAudio, AV_SET_AUD_UNPAUSE, 1) != 0) return 0;
170   return 1;
171 }
172
173 int AudioVPE::reset()
174 {
175   if (!initted) return 0;
176 //test();
177   Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
178
179
180 //  if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
181 //  Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
182  // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
183
184   doMuting();
185   return 1;
186 }
187
188 int AudioVPE::setVolume(int tvolume)
189 {
190   // parameter: 0 for silence, 20 for full
191   if ((tvolume < 0) || (tvolume > 20)) return 0;
192
193 // volume = 2 * (20 - volume);
194 // Right, that one was rubbish... 0-10 were almost
195 // inaudible, 11-20 did what should have been done
196 // over the whole 0-20 range
197
198   tvolume = 20 - tvolume;
199
200   audio_volume Avolume;
201   Avolume.frontleft = tvolume + Aoffset.frontleft;
202   Avolume.frontright = tvolume + Aoffset.frontright;
203   Avolume.rearleft = tvolume + Aoffset.rearleft;
204   Avolume.rearright = tvolume + Aoffset.rearright;
205   Avolume.center = tvolume + Aoffset.center;
206   Avolume.lfe = tvolume + Aoffset.lfe;
207
208 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
209
210 //  unsigned long vol = (tvolume << 24) | (tvolume << 16);
211 //
212 //  Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
213 //  Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
214
215 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
216   return 1;
217 }
218
219 #ifdef DEV
220 int AudioVPE::test()
221 {
222 //  ULLONG stc = 0;
223 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
224
225 /*  aud_sync_parms_t a;
226   a.parm1 = 0;
227   a.parm2 = 0;
228 */
229 //  int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
230
231
232   /*OK*/ //printf("Audio sync disable = %i\n", b);
233
234   return 1;
235
236
237 }
238 #endif
239
240 void AudioVPE::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
241 {
242   packet = mplist.front();
243 }
244
245 UINT AudioVPE::DeliverMediaSample(UCHAR* buffer, UINT* samplepos)
246 {/*
247         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
248         //Log::getInstance()->log("Audio", Log::DEBUG, "lastpacket %d lastbuddypacket %d currentpacket %d",lastpacketnum,video->getLastPacketNum(),packet.index);
249         currentpacketnum=packet.index;
250   if (lastpacketnum!=-1 && packet.index-1!=lastpacketnum && packet.index-1>video->getLastPacketNum()) return 0; //Not in order
251
252   //format pes
253   switch (packet.type) {
254           case MPTYPE_MPEG_AUDIO:
255                  buffer[packet.pos_buffer + 3]=0xc0; break;
256           case MPTYPE_AC3:
257                   buffer[packet.pos_buffer +buffer[packet.pos_buffer+8]+9]=0x80;
258                   buffer[packet.pos_buffer + 3]=0xbd; break;
259                   break;
260           default:
261           case MPTYPE_AC3_PRE13://Not tested no recording availiable
262                   buffer[packet.pos_buffer + 3]=0xbd; break;
263                   break;
264   };
265
266    if (packet.type!=lastAType && lastpacketnum!=-1){//Format Change //Push data out !
267       video->deinitMuxer();
268           lastAType=packet.type;
269           video->initMuxer();
270   }
271   lastAType=packet.type;
272
273
274  /* Mutex *mutex=video->getMuxMutex();
275   mutex->Lock();
276   FILE *muxout=video->getMuxFile();
277   if (!muxout) {mutex->Unlock(); return 0;}*
278   int written=0;
279   //written=fwrite(buffer + packet.pos_buffer,1,packet.length,muxout);
280   written=video->WriteOutTS(buffer + packet.pos_buffer,packet.length,packet.type);
281   lastpacketnum=packet.index;
282   
283   //mutex->Unlock();
284
285    //Log::getInstance()->log("Audio", Log::DEBUG, "wrote %d bytes to mux",written);
286   
287   if (written == (int)packet.length) { *samplepos = 0; return 1;}
288   if (written <= 0) {
289           return 0;
290   }
291   *samplepos = written;*/
292   // Handle a partial write. Is this possible? Should we bother? No!
293  
294   *samplepos=packet.length;
295   return 1;
296 }
297
298 void AudioVPE::ResetTimeOffsets()
299 {
300 }