]> git.vomp.tv Git - vompclient.git/blob - audiowin.cc
Windows port. New sync code. Various other bug fixes.
[vompclient.git] / audiowin.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 "audiowin.h"
22 #include "videowin.h"
23
24
25
26
27
28 AudioWin::AudioWin()
29 {
30   initted = 0;
31   firstsynched=false;
32
33
34 }
35
36 AudioWin::~AudioWin()
37 {
38
39 }
40
41 int AudioWin::init(UCHAR tstreamType)
42 {
43   if (initted) return 0;
44   initted = 1;
45   return 1;
46 }
47
48 int AudioWin::shutdown()
49 {
50   if (!initted) return 0;
51   initted = 0;
52   return 1;
53 }
54
55 int AudioWin::getFD()
56 {
57   return 0;
58 }
59
60 int AudioWin::write(char *buf, int len)
61 {
62   return 0; //write(fdAudio, buf, len);
63 }
64
65 int AudioWin::setStreamType(UCHAR type)
66 {
67   if (!initted) return 0;
68   return 1;
69 }
70
71 int AudioWin::setChannel()
72 {
73   if (!initted) return 0;
74   return 1;
75 }
76
77 int AudioWin::setSource()
78 {
79   if (!initted) return 0;
80   return 1;
81 }
82
83 int AudioWin::sync()
84 {
85   if (!initted) return 0;
86   return 1;
87 }
88
89 int AudioWin::play()
90 {
91   if (!initted) return 0;
92   firstsynched=false;
93
94   return 1;
95 }
96
97 int AudioWin::stop()
98 {
99   if (!initted) return 0;
100   return 1;
101 }
102
103 int AudioWin::pause()
104 {
105   if (!initted) return 0;
106   return 1;
107 }
108
109 int AudioWin::unPause()
110 {
111   if (!initted) return 0;
112   return 1;
113 }
114
115 int AudioWin::reset()
116 {
117   if (!initted) return 0;
118   return 1;
119 }
120
121 int AudioWin::setVolume(int tvolume)
122 {
123   // parameter: 0 for silence, 20 for full
124   if ((tvolume < 0) || (tvolume > 20)) return 0;
125   return 1;
126 }
127
128 int AudioWin::mute()
129 {
130   if (!initted) return 0;
131   ((VideoWin*)Video::getInstance())->SetAudioState(false);
132   return 1;
133 }
134
135 int AudioWin::unMute()
136 {
137   if (!initted) return 0;
138   ((VideoWin*)Video::getInstance())->SetAudioState(true);
139   return 1;
140 }
141
142 UINT AudioWin::DeliverMediaSample(MediaPacket packet,
143      UCHAR* buffer,
144      UINT *samplepos)
145 {
146   /*First Check, if we have an audio sample*/
147   VideoWin *vw=(VideoWin*)Video::getInstance();
148   IMediaSample* ms=NULL;
149   REFERENCE_TIME reftime1=0;
150   REFERENCE_TIME reftime2=0;
151
152   UINT headerstrip=0;
153   if (packet.disconti) {
154     firstsynched=false;
155     vw->DeliverVideoMediaSample();
156   }
157
158
159
160   /*Inspect PES-Header */
161 /*  UINT header_length=buffer[(packet.pos_buffer+8)%bufferlength]+8/*is this right*;
162 */
163   if (*samplepos==0) {//stripheader
164     headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
165     *samplepos+=headerstrip;
166     if ( packet.synched ) {
167       vw->DeliverAudioMediaSample();//write out old data
168       if (packet.presentation_time<0) { //Preroll?
169         *samplepos=packet.length;//if we have not processed at least one
170         return packet.length;//synched packet ignore it!
171       }
172
173       reftime1=packet.presentation_time;
174       reftime2=reftime1+1;
175       firstsynched=true;
176     } else {
177       if (!firstsynched) {//
178         *samplepos=packet.length;//if we have not processed at least one
179         return packet.length;//synched packet ignore it!
180       }
181     }
182   }
183   BYTE *ms_buf;
184   UINT ms_length;
185   UINT ms_pos;
186   UINT haveToCopy;
187   if (!vw->getCurrentAudioMediaSample(&ms) || ms==NULL) {// get the current sample
188     samplepos=0;
189     MILLISLEEP(10);
190     return 0;
191   }
192   ms_pos=ms->GetActualDataLength();
193   ms_length=ms->GetSize();
194   haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
195   if ((ms_length-ms_pos)<1) {
196     vw->DeliverAudioMediaSample(); //we are full!
197     if (!vw->getCurrentAudioMediaSample(&ms) || ms==NULL) {// get the current sample
198       samplepos=0;
199       MILLISLEEP(10);
200       return 0;
201     }
202     ms_pos=ms->GetActualDataLength();
203     ms_length=ms->GetSize();
204     haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
205   }
206   ms->GetPointer(&ms_buf);
207
208   if (ms_pos==0) {//will only be changed on first packet
209     if (packet.disconti) {
210       ms->SetDiscontinuity(TRUE);
211     } else {
212       ms->SetDiscontinuity(FALSE);
213     }
214     if (packet.synched) {
215       ms->SetSyncPoint(TRUE);
216       ms->SetTime(&reftime1,&reftime2);
217       //ms->SetTime(NULL,NULL);
218       ms->SetMediaTime(NULL, NULL);
219     }else {
220       ms->SetSyncPoint(FALSE);
221       ms->SetTime(NULL,NULL);
222       ms->SetMediaTime(NULL, NULL);
223       ms->SetSyncPoint(TRUE);
224     }
225   }
226
227
228   memcpy(ms_buf+ms_pos,buffer+packet.pos_buffer+*samplepos,haveToCopy);
229     ms->SetActualDataLength(haveToCopy+ms_pos);
230
231   *samplepos+=haveToCopy;
232
233   return haveToCopy+headerstrip;
234
235 }
236
237 long long AudioWin::SetStartOffset(long long curreftime, bool *rsync){
238   VideoWin *vw=(VideoWin*)Video::getInstance();
239   return vw->SetStartAudioOffset(curreftime,rsync);
240 }
241
242 #ifdef DEV
243 int AudioWin::test()
244 {
245   return 0;
246 }
247 #endif