]> git.vomp.tv Git - vompclient.git/blob - audiomvp.cc
Updates to new streaming protocol and live tv
[vompclient.git] / audiomvp.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "audiomvp.h"
22
23 #include "log.h"
24
25 AudioMVP::AudioMVP()
26 {
27   if (instance) return;
28   initted = 0;
29   fdAudio = 0;
30   streamType = 0;
31   volume = 20;
32   muted = 0;
33 }
34
35 AudioMVP::~AudioMVP()
36 {
37 }
38
39 int AudioMVP::init(UCHAR tstreamType)
40 {
41   if (initted) return 0;
42   initted = 1;
43
44 //  if ((fdAudio = open("/dev/adec_mpg", O_RDWR | O_NONBLOCK)) < 0) return 0;
45   if ((fdAudio = open("/dev/adec_mpg", O_WRONLY)) < 0) return 0;
46
47   streamType = tstreamType;
48
49   if (!initAllParams())
50   {
51     shutdown();
52     return 0;
53   }
54
55   unMute();
56
57   // Set the volume variable to what the hardware is set at now
58   int hwvol = -1;
59   int hwvolFail = ioctl(fdAudio, AV_GET_AUD_VOLUME, &hwvol);
60   if (!hwvolFail)
61   {
62     volume = 20 - ((hwvol >> 16) & 0xFF);
63     if ((volume < 0) || (volume > 20)) volume = 20;
64   }
65
66   return 1;
67 }
68
69 int AudioMVP::initAllParams()
70 {
71   return (setStreamType(streamType) && setChannel() && setSource());
72 }
73
74 int AudioMVP::shutdown()
75 {
76   if (!initted) return 0;
77   initted = 0;
78   close(fdAudio);
79   return 1;
80 }
81
82 int AudioMVP::setStreamType(UCHAR type)
83 {
84   if (!initted) return 0;
85
86   if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
87   return 1;
88 }
89
90 int AudioMVP::setChannel()
91 {
92   if (!initted) return 0;
93
94   if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
95   return 1;
96 }
97
98 int AudioMVP::setSource()
99 {
100   if (!initted) return 0;
101
102   if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
103   return 1;
104 }
105
106 int AudioMVP::sync()
107 {
108   if (!initted) return 0;
109
110   if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
111   return 1;
112 }
113
114 int AudioMVP::play()
115 {
116   if (!initted) return 0;
117
118   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
119   return 1;
120 }
121
122 int AudioMVP::stop()
123 {
124   if (!initted) return 0;
125
126   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
127   return 1;
128 }
129
130 int AudioMVP::mute()
131 {
132   if (!initted) return 0;
133
134   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
135   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
136
137   muted = 1;
138   return 1;
139 }
140
141 int AudioMVP::unMute()
142 {
143   if (!initted) return 0;
144
145   if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
146   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
147
148   muted = 0;
149   return 1;
150 }
151
152 int AudioMVP::pause()
153 {
154   if (!initted) return 0;
155
156   if (ioctl(fdAudio, AV_SET_AUD_PAUSE, 1) != 0) return 0;
157   return 1;
158 }
159
160 int AudioMVP::unPause()
161 {
162   if (!initted) return 0;
163
164   if (ioctl(fdAudio, AV_SET_AUD_UNPAUSE, 1) != 0) return 0;
165   return 1;
166 }
167
168 int AudioMVP::reset()
169 {
170   if (!initted) return 0;
171 //test();
172   if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
173   if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
174
175   doMuting();
176   return 1;
177 }
178
179 int AudioMVP::setVolume(int tvolume)
180 {
181   // parameter: 0 for silence, 20 for full
182   if ((tvolume < 0) || (tvolume > 20)) return 0;
183
184 // volume = 2 * (20 - volume);
185 // Right, that one was rubbish... 0-10 were almost
186 // inaudible, 11-20 did what should have been done
187 // over the whole 0-20 range
188
189   tvolume = 20 - tvolume;
190
191   audio_volume Avolume;
192   Avolume.frontleft = tvolume + Aoffset.frontleft;
193   Avolume.frontright = tvolume + Aoffset.frontright;
194   Avolume.rearleft = tvolume + Aoffset.rearleft;
195   Avolume.rearright = tvolume + Aoffset.rearright;
196   Avolume.center = tvolume + Aoffset.center;
197   Avolume.lfe = tvolume + Aoffset.lfe;
198
199   if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
200
201 //  unsigned long vol = (tvolume << 24) | (tvolume << 16);
202 //
203 //  Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
204 //  Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
205
206 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
207   return 1;
208 }
209
210 #ifdef DEV
211 int AudioMVP::test()
212 {
213 //  ULLONG stc = 0;
214 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
215
216   aud_sync_parms_t a;
217   a.parm1 = 0;
218   a.parm2 = 0;
219
220   int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
221
222
223   /*OK*/ printf("Audio sync disable = %i\n", b);
224
225   return 1;
226
227
228 }
229 #endif
230
231 void AudioMVP::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
232 {
233   packet = mplist.front();
234 }
235
236 UINT AudioMVP::DeliverMediaSample(const UCHAR* buffer, UINT* samplepos)
237 {
238   int written = ::write(fdAudio, buffer + packet.pos_buffer + *samplepos,
239                                  packet.length - *samplepos);
240   if (written == (int)(packet.length - *samplepos)) {*samplepos = 0; return 1;}
241   if (written > 0) *samplepos += written;
242   return 0;
243 }
244
245 void AudioMVP::ResetTimeOffsets()
246 {
247 }