]> git.vomp.tv Git - vompclient.git/blob - audiovpe.cc
Add basic audio playback (stereo), fast forward, ThreadP bugfix and Stream interface...
[vompclient.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 #include "vdr.h"
25 #include "woptionpane.h"
26
27 AudioVPE::AudioVPE()
28 {
29   initted = 0;
30   streamType = 0;
31   volume = 20;
32   muted = 0;
33   lastAType = MPTYPE_MPEG_AUDIO;
34
35   canpass_ac3=false;
36   canpass_mp2=false;
37   canpass_mp3=false;
38   canpass_pcm_mch=false;
39
40   prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
41   prefered_mp2=0;
42   prefered_mp3=0;
43   hdmi=true;
44
45   omx_running=false;
46
47   omx_aud_rend/*dec*/=0;
48   cur_input_buf_omx=NULL;
49
50   ac3codec_libav=NULL;
51   ac3codec_context_libav=NULL;
52
53   mp23codec_libav=NULL;
54   mp23codec_context_libav=NULL;
55
56 }
57
58 AudioVPE::~AudioVPE()
59 {
60 }
61
62 int AudioVPE::init(UCHAR tstreamType) {
63         if (initted)
64                 return 0;
65         initted = 1;
66
67         streamType = tstreamType;
68
69         if (!initAllParams()) {
70                 shutdown();
71                 return 0;
72         }
73
74         unMute();
75
76
77
78         av_register_all();
79         av_log_set_flags(AV_LOG_SKIP_REPEATED);
80
81         ac3codec_libav = avcodec_find_decoder(CODEC_ID_AC3);
82         if (ac3codec_libav == NULL) {
83                 Log::getInstance()->log("Audio", Log::DEBUG,
84                                 "Find libav ac3 decoder failed");
85                 return 0;
86         }
87
88         mp23codec_libav = avcodec_find_decoder(CODEC_ID_MP3);
89         if (mp23codec_libav == NULL) {
90                 Log::getInstance()->log("Audio", Log::DEBUG,
91                                 "Find libav mpeg audio decoder failed");
92                 return 0;
93         }
94
95         return 1;
96 }
97
98 int AudioVPE::initAllParams()
99 {
100   return (setStreamType(streamType) && setChannel() && setSource());
101 }
102
103 int AudioVPE::shutdown()
104 {
105   if (!initted) return 0;
106   initted = 0;
107
108   Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
109   DeAllocateCodecsOMX();
110
111   return 1;
112 }
113
114 bool AudioVPE::loadOptionsfromServer(VDR* vdr)
115 {
116           Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
117     char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
118
119     if (name != NULL) {
120                 if (STRCASECMP(name, "PCM") == 0) {
121                         prefered_ac3 = 0;
122                 } else if (STRCASECMP(name, "Passthrough") == 0) {
123                         prefered_ac3 = 1;
124                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
125                         prefered_ac3 = 2;
126                 }
127         }
128
129     name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
130
131         if (name != NULL) {
132                 if (STRCASECMP(name, "PCM") == 0) {
133                         prefered_mp2 = 0;
134                 } else if (STRCASECMP(name, "Passthrough") == 0) {
135                         prefered_mp2 = 1;
136                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
137                         prefered_mp2 = 2;
138                 }
139         }
140
141         name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
142
143         if (name != NULL) {
144                 if (STRCASECMP(name, "PCM") == 0) {
145                         prefered_mp3 = 0;
146                 } else if (STRCASECMP(name, "Passthrough") == 0) {
147                         prefered_mp3 = 1;
148                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
149                         prefered_mp3 = 2;
150                 }
151         }
152
153         name = vdr->configLoad("AudioOMX", "AudioOutput");
154
155         if (name != NULL) {
156                 if (STRCASECMP(name, "analog") == 0) {
157                         hdmi = false;
158                 } else if (STRCASECMP(name, "HDMI") == 0) {
159                         hdmi = true;
160                 }
161         }
162
163
164    return true;
165
166 }
167
168 bool AudioVPE::handleOptionChanges(Option* option)
169 {
170     if (Audio::handleOptionChanges(option))
171                 return true;
172         switch (option->id) {
173         case 4: {
174                 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
175                         hdmi = false;
176                 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
177                                 == 0) {
178                         hdmi = true;
179                 }
180                 return true;
181         }
182                 break;
183         case 1: {
184                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
185                         prefered_ac3 = 0;
186                 } else if (STRCASECMP(option->options[option->userSetChoice],
187                                 "Passthrough") == 0) {
188                         prefered_ac3 = 1;
189                 } else if (STRCASECMP(option->options[option->userSetChoice],
190                                 "PCMMultichannel") == 0) {
191                         prefered_ac3 = 2;
192                 }
193         }
194                 break;
195         case 2: {
196                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
197                         prefered_mp2 = 0;
198                 } else if (STRCASECMP(option->options[option->userSetChoice],
199                                 "Passthrough") == 0) {
200                         prefered_mp2 = 1;
201                 } else if (STRCASECMP(option->options[option->userSetChoice],
202                                 "PCMMultichannel") == 0) {
203                         prefered_mp2 = 2;
204                 }
205         }
206                 break;
207         case 3: {
208                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
209                         prefered_mp3 = 0;
210                 } else if (STRCASECMP(option->options[option->userSetChoice],
211                                 "Passthrough") == 0) {
212                         prefered_mp3 = 1;
213                 } else if (STRCASECMP(option->options[option->userSetChoice],
214                                 "PCMMultichannel") == 0) {
215                         prefered_mp3 = 2;
216                 }
217         }
218                 break;
219         };
220         return false;
221
222 }
223
224 bool AudioVPE::saveOptionstoServer()
225 {
226
227     switch (prefered_ac3) {
228         case 0:
229                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
230                 break;
231         case 1:
232                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
233                                 "Passthrough");
234                 break;
235         case 2:
236                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
237                                 "PCMMultichannel");
238                 break;
239         };
240
241         switch (prefered_mp2) {
242         case 0:
243                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
244                 break;
245         case 1:
246                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
247                                 "Passthrough");
248                 break;
249         case 2:
250                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
251                                 "PCMMultichannel");
252                 break;
253         };
254
255         switch (prefered_mp3) {
256         case 0:
257                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
258                 break;
259         case 1:
260                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
261                                 "Passthrough");
262                 break;
263         case 2:
264                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
265                                 "PCMMultichannel");
266                 break;
267         };
268
269         if (!hdmi)
270                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
271         else
272                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
273
274
275     return true;
276 }
277
278 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
279            UINT numChoices, UINT defaultChoice, UINT startInt,
280            const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
281
282 bool AudioVPE::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
283 {
284     if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
285
286
287     Option* option;
288     if (panenumber == 2)
289     {
290
291         static const char* audioopts[]={"analog","HDMI"};
292         option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
293         options->push_back(option);
294         pane->addOptionLine(option);
295
296
297         char **ac3opts=new char *[3];
298         int i=0;
299         ac3opts[i]=new char[strlen("PCM")+1];
300         strcpy(ac3opts[i],"PCM");
301         i++;
302         if (canpass_ac3) {
303                 ac3opts[i]=new char[strlen("Passthrough")+1];
304             strcpy(ac3opts[i],"PassThrough");
305             i++;
306         }
307         if (canpass_pcm_mch) {
308                 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
309             strcpy(ac3opts[i],"PCMMultichannel");
310             i++;
311         }
312         option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
313         options->push_back(option);
314         pane->addOptionLine(option);
315
316         char **mp2opts = new char *[3];
317                 i = 0;
318                 mp2opts[i] = new char[strlen("PCM") + 1];
319                 strcpy(mp2opts[i], "PCM");
320                 i++;
321                 if (canpass_mp2) {
322                         mp2opts[i] = new char[strlen("Passthrough") + 1];
323                         strcpy(mp2opts[i], "PassThrough");
324                         i++;
325                 }
326                 if (canpass_pcm_mch) {
327                         mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
328                         strcpy(mp2opts[i], "PCMMultichannel");
329                         i++;
330                 }
331                 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
332                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
333                                 mp2opts, NULL, true, this);
334                 options->push_back(option);
335                 pane->addOptionLine(option);
336
337                 char **mp3opts = new char *[3];
338                 i = 0;
339                 mp3opts[i] = new char[strlen("PCM") + 1];
340                 strcpy(mp3opts[i], "PCM");
341                 i++;
342                 if (canpass_mp3) {
343                         mp3opts[i] = new char[strlen("Passthrough") + 1];
344                         strcpy(mp3opts[i], "PassThrough");
345                         i++;
346                 }
347                 if (canpass_pcm_mch) {
348                         mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
349                         strcpy(mp3opts[i], "PCMMultichannel");
350                         i++;
351                 }
352                 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
353                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
354                                 NULL, true, this);
355                 options->push_back(option);
356                 pane->addOptionLine(option);
357
358
359     }
360
361     return true;
362 }
363
364
365
366
367
368
369 OMX_ERRORTYPE AudioVPE::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
370
371         //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
372         AudioVPE *audio=(AudioVPE *)getInstance();
373         audio->ReturnEmptyOMXBuffer(buffer);
374         return OMX_ErrorNone;
375
376 }
377
378 void AudioVPE::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
379         input_bufs_omx_mutex.Lock();
380         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
381         input_bufs_omx_free.push_back(buffer);
382         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
383         input_bufs_omx_mutex.Unlock();
384 }
385
386  OMX_ERRORTYPE AudioVPE::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
387          Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
388         return OMX_ErrorNone;
389 }
390
391
392
393 int AudioVPE::setStreamType(UCHAR type)
394 {
395   if (!initted) return 0;
396
397  // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
398   return 1;
399 }
400
401 int AudioVPE::setChannel()
402 {
403   if (!initted) return 0;
404
405  // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
406   return 1;
407 }
408
409 int AudioVPE::setSource()
410 {
411   if (!initted) return 0;
412
413  // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
414   return 1;
415 }
416
417 int AudioVPE::sync()
418 {
419   if (!initted) return 0;
420
421  // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
422   return 1;
423 }
424
425 int AudioVPE::play() {
426         if (!initted)
427                 return 0;
428         lastAType=MPTYPE_MPEG_AUDIO;
429         Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
430
431         if (!AllocateCodecsOMX()) {
432                 return 0;
433         }
434         return 1;
435 }
436
437
438 int AudioVPE::ChangeAudioDestination() //clock aka omx mutex needs to be locked
439 {
440         OMX_ERRORTYPE error;
441         const char * destinations[]={"local","hdmi"};
442         int dest=0;
443         if (hdmi) dest=1;
444         else dest=0;
445
446         OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
447         memset(&auddest,0,sizeof(auddest));
448         auddest.nSize=sizeof(auddest);
449         auddest.nVersion.nVersion=OMX_VERSION;
450         strcpy((char *)auddest.sName, destinations[dest]);
451
452         Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
453         error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
454         if (error!=OMX_ErrorNone){
455                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
456                 DeAllocateCodecsOMX();
457                 return 0;
458         }
459         return 1;
460
461
462 }
463
464 int AudioVPE::ChangeAudioPortConfig() //clock aka omx mutex needs to be locked
465 {
466         OMX_ERRORTYPE error;
467         //Ok first fidle a working configuration
468         Log::getInstance()->log("Audio", Log::DEBUG,
469                                                         "ChangeAudioPortConfig");
470
471         if (hdmi) {
472                 switch (lastAType) {
473                 case MPTYPE_MPEG_AUDIO: {
474                         if (prefered_mp2 == 3 && false) { //not supported yet
475
476                         } else {
477                                 if (prefered_mp2 == 2 && canpass_mp2) {
478                                         passthrough = true;
479                                 } else
480                                         passthrough = false;
481                         }
482                 }
483                         break;
484                 case MPTYPE_AC3_PRE13:
485                 case MPTYPE_AC3: {
486                         if (prefered_ac3 == 3 && false) { //not supported yet
487
488                         } else {
489                                 if (prefered_ac3 == 2 && canpass_ac3) {
490                                         passthrough = true;
491                                 } else
492                                         passthrough = false;
493                         }
494                 }
495                         break;
496                 case MPTYPE_MPEG_AUDIO_LAYER3: {
497                         if (prefered_mp3 == 3 && false) { //not supported yet
498
499                         } else {
500                                 if (prefered_mp3 == 2 && canpass_mp2) {
501                                         passthrough = true;
502                                 } else
503                                         passthrough = false;
504                         }
505                 }
506                         break;
507                 };
508         } else {
509                  passthrough=false;
510                  //mch=false; // multichannel also false
511         }
512
513
514
515         /*OMX_CONFIG_BOOLEANTYPE booly;
516         memset(&booly, 0, sizeof(booly));
517         booly.nSize = sizeof(booly);
518         booly.nVersion.nVersion = OMX_VERSION;
519         if (passthrough)
520                 booly.bEnabled = OMX_TRUE;
521         else
522                 booly.bEnabled = OMX_FALSE;
523
524         error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
525                         &booly);
526         if (error != OMX_ErrorNone) {
527                 Log::getInstance()->log("Audio", Log::DEBUG,
528                                 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
529                 DeAllocateCodecsOMX();
530                 return 0;
531         }*/
532         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
533
534
535         if (!omx_running) {
536                 if (passthrough) {
537                         // TODO coding
538                 } else {
539
540                         OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
541                         memset(&audio_pcm, 0, sizeof(audio_pcm));
542                         audio_pcm.nSize = sizeof(audio_pcm);
543                         audio_pcm.nVersion.nVersion = OMX_VERSION;
544                         audio_pcm.nChannels = 2;
545                         audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
546                         audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
547                         //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
548                         audio_pcm.eNumData = OMX_NumericalDataSigned;
549                         audio_pcm.eEndian = OMX_EndianLittle;
550                         audio_pcm.bInterleaved = OMX_TRUE;
551                         audio_pcm.nBitPerSample = 16;
552                         audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
553                         audio_pcm.nChannels = 2;
554                         audio_pcm.nSamplingRate = 48000;
555                         audio_pcm.nPortIndex = omx_rend_input_port;
556                         error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
557                                         &audio_pcm);
558                         if (error != OMX_ErrorNone) {
559                                 Log::getInstance()->log("Audio", Log::DEBUG,
560                                                 "Init OMX_IndexParamAudioPcm failed %x %d", error,
561                                                 omx_rend_input_port);
562                                 DeAllocateCodecsOMX();
563                                 return 0;
564                         }
565
566                 }
567         }
568
569
570
571         return 1;
572
573
574
575 }
576 int AudioVPE::InitDecoderLibAV()
577 {
578         ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
579         if (!ac3codec_context_libav) {
580                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
581                 return 0;
582         }
583
584         ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
585         ac3codec_context_libav->request_channels=2;
586
587         int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
588         if (avc_ret < 0) {
589                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
590                 return 0;
591         }
592
593         mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
594         if (!ac3codec_context_libav) {
595                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
596                 return 0;
597         }
598
599         mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
600         mp23codec_context_libav->request_channels=2;
601
602         avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
603         if (avc_ret < 0) {
604                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
605                 return 0;
606         }
607
608          av_init_packet(&incoming_paket_libav);
609          decode_frame_libav=avcodec_alloc_frame();
610
611
612
613         return 1;
614 }
615
616 void AudioVPE::DeinitDecoderLibAV() {
617
618
619         if (ac3codec_context_libav) {
620                 avcodec_close(ac3codec_context_libav);
621                 av_free(ac3codec_context_libav);
622                 ac3codec_context_libav = NULL;
623                 av_free(decode_frame_libav);
624
625         }
626
627 }
628
629
630 int AudioVPE::AllocateCodecsOMX()
631 {
632         OMX_ERRORTYPE error;
633         static OMX_CALLBACKTYPE callbacks= {&VideoVPEOGL::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
634
635         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
636         //Clock, move later to audio
637         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
638
639         if (!InitDecoderLibAV()) return 0;;
640
641
642         OMX_PORT_PARAM_TYPE p_param;
643         memset(&p_param,0,sizeof(p_param));
644         p_param.nSize=sizeof(p_param);
645         p_param.nVersion.nVersion=OMX_VERSION;
646
647
648         if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
649                 return 0;// get the clock and init it if necessary
650         }
651
652         /* TODO end */
653         if (!video->idleClock()) {
654                 return 0;
655         }
656         video->LockClock();
657
658         error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
659         if (error != OMX_ErrorNone) {
660                 Log::getInstance()->log("Audio", Log::DEBUG,
661                                 "Init OMX audio rend failed %x", error);
662                 video->UnlockClock();
663                 DeAllocateCodecsOMX();
664                 return 0;
665         }
666
667         if (!ChangeAudioDestination()) {
668                 video->UnlockClock();
669                 DeAllocateCodecsOMX();
670                 return 0;
671         }
672
673         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
674         if (error != OMX_ErrorNone) {
675                 Log::getInstance()->log("Audio", Log::DEBUG,
676                                 "Init OMX audio rend OMX_GetParameter failed %x", error);
677                 video->UnlockClock();
678                 DeAllocateCodecsOMX();
679                 return 0;
680         }
681         omx_rend_input_port = p_param.nStartPortNumber;
682
683         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
684         if (error != OMX_ErrorNone) {
685                 Log::getInstance()->log("Audio", Log::DEBUG,
686                                 "Init OMX aud rend OMX_GetParameter failed %x", error);
687                 video->UnlockClock();
688                 DeAllocateCodecsOMX();
689                 return 0;
690         }
691         // buggy return value
692         omx_rend_clock_port = p_param.nStartPortNumber;
693
694
695 /*      error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
696
697         if (error!=OMX_ErrorNone){
698                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
699                 video->UnlockClock();
700                 DeAllocateCodecsOMX();
701                 return 0;
702         }
703
704         error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
705         if (error!=OMX_ErrorNone){
706                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
707                 video->UnlockClock();
708                 DeAllocateCodecsOMX();
709             return 0;
710         }
711         omx_codec_input_port=p_param.nStartPortNumber;
712         omx_codec_output_port=p_param.nStartPortNumber+1;
713
714         if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
715                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
716                 video->UnlockClock();
717                 DeAllocateCodecsOMX();
718                 return 0;
719         }*/
720
721
722
723
724         if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
725                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
726                 video->UnlockClock();
727                 DeAllocateCodecsOMX();
728                 return 0;
729         }
730
731         if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
732                 Log::getInstance()->log("Audio", Log::DEBUG,
733                                 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
734                 video->UnlockClock();
735                 DeAllocateCodecsOMX();
736                 return 0;
737         }
738
739
740
741
742         //Setuo chain
743
744
745         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
746         if (error!=OMX_ErrorNone){
747                 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel clock to rend failed %x %d %d", error,omx_clock_output_port,omx_rend_clock_port);
748                 video->UnlockClock();
749                 DeAllocateCodecsOMX();
750                 return 0;
751         }
752
753         if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
754                                         ) {
755                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
756                 video->UnlockClock();
757                 DeAllocateCodecsOMX();
758                 return 0;
759         }
760
761         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
762                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
763                 video->UnlockClock();
764                 DeAllocateCodecsOMX();
765                 return 0;
766         }
767
768
769
770
771         if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
772                 video->UnlockClock();
773                 DeAllocateCodecsOMX();
774                 return 0;
775         }
776
777         if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
778                 video->UnlockClock();
779                 DeAllocateCodecsOMX();
780                 return 0;
781         }
782
783
784
785         if (!ChangeAudioPortConfig()){
786                     Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
787                     video->UnlockClock();
788                     DeAllocateCodecsOMX();
789                         return 0;
790         }
791
792 /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
793                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
794                 DeAllocateCodecsOMX();
795                 return 0;
796         }*/
797
798
799
800         if (!PrepareInputBufsOMX()) {
801                 video->UnlockClock();
802                 DeAllocateCodecsOMX();
803                 return 0;
804         }
805
806
807
808 /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
809         if (error!=OMX_ErrorNone){
810                 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
811                  video->UnlockClock();
812                 DeAllocateCodecsOMX();
813                 return 0;
814         }*/
815
816
817
818 /*      if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
819                                                 ) {
820                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
821                  video->UnlockClock();
822                 DeAllocateCodecsOMX();
823                 return 0;
824         }*/
825
826 /*      if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
827                         || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
828                          video->UnlockClock();
829                 DeAllocateCodecsOMX();
830                 return 0;
831         }*/
832
833         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
834                 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
835                  video->UnlockClock();
836                 DeAllocateCodecsOMX();
837                 return 0;
838         }
839
840
841         video->UnlockClock();
842         paused=false;
843         omx_running=true;
844
845
846         if (!video->setClockExecutingandRunning()) return 0;
847
848         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
849
850         return 1;
851 }
852
853
854
855
856 int AudioVPE::PrepareInputBufsOMX() //needs to be called with locvke omx clock mutex
857 {
858         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
859         OMX_ERRORTYPE error;
860         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
861         memset(&port_def_type,0,sizeof(port_def_type));
862         port_def_type.nSize=sizeof(port_def_type);
863         port_def_type.nVersion.nVersion=OMX_VERSION;
864         port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
865
866         error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
867
868         if (error!=OMX_ErrorNone){
869                         Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
870         }
871
872
873         port_def_type.nBufferCountActual=2;
874         port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
875
876         error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
877
878         if (error!=OMX_ErrorNone){
879                         Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
880         }
881
882
883         error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
884         if (error!=OMX_ErrorNone){
885                 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
886                 return 0;
887         }
888
889         input_bufs_omx_mutex.Lock();
890         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
891                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
892                 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
893                 if (error!=OMX_ErrorNone){
894                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
895                                 input_bufs_omx_mutex.Unlock();
896                         return 0;
897                 }
898                 input_bufs_omx_all.push_back(buf_head);
899                 input_bufs_omx_free.push_back(buf_head);
900         }
901         omx_first_frame=true;
902
903         firstsynched=false;
904         cur_input_buf_omx=NULL;
905         input_bufs_omx_mutex.Unlock();
906
907         if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
908                 return 0;
909         }
910
911         return 1;
912 }
913
914 int AudioVPE::DestroyInputBufsOMX() //call with clock mutex locked
915 {
916         OMX_ERRORTYPE error;
917
918         cur_input_buf_omx=NULL;
919         input_bufs_omx_mutex.Lock();
920         for (int i=0; i< input_bufs_omx_all.size();i++) {
921                 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
922                 if (error!=OMX_ErrorNone){
923                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
924                         input_bufs_omx_mutex.Unlock();
925                         return 0;
926                 }
927
928         }
929         input_bufs_omx_all.clear();
930         input_bufs_omx_free.clear();
931         input_bufs_omx_mutex.Unlock();
932
933 }
934
935
936 int AudioVPE::DeAllocateCodecsOMX()
937 {
938         OMX_ERRORTYPE error;
939         omx_running=false;
940         VideoVPEOGL *video=(VideoVPEOGL*)Video::getInstance();
941          Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
942         DeinitDecoderLibAV();
943
944    video->LockClock();
945    if (cur_input_buf_omx) {
946                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
947                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
948                 if (error!=OMX_ErrorNone) {
949                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
950                 }
951
952                 cur_input_buf_omx=NULL;//write out old data
953         }
954
955         if (omx_aud_rend/*dec*/) {
956                 // first stop the omx elements
957         /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
958                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
959                 }*/
960
961                 video->UnlockClock();
962                 video->idleClock();
963                  video->LockClock();
964
965                 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
966                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
967                 }
968
969         // TODO proper deinit sequence
970                 // first flush all buffers
971
972                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
973                 if (error!=OMX_ErrorNone) {
974                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
975
976                 }
977
978         /*      error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
979                 if (error!=OMX_ErrorNone){
980                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
981
982                 }*/
983
984
985         /*      if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
986                         Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
987                 }*/
988
989
990
991                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
992                 if (error!=OMX_ErrorNone){
993                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
994
995                 }
996
997                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
998                 if (error!=OMX_ErrorNone){
999                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1000
1001                 }
1002
1003                 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1004                         !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1005                                 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1006                 }
1007
1008                 DestroyInputBufsOMX();
1009
1010                 //todo flushing
1011                 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1012                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1013                 }
1014
1015         /*      if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1016                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1017                 }
1018
1019                 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1020                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1021                 }*/
1022
1023
1024                 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1025                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1026                 }
1027
1028                 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1029                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1030                 }
1031
1032
1033
1034         /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1035                 if (error!=OMX_ErrorNone) {
1036                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1037
1038                 }*/
1039
1040
1041
1042                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1043                 if (error!=OMX_ErrorNone) {
1044                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1045
1046                 }
1047
1048                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1049                 if (error!=OMX_ErrorNone) {
1050                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1051
1052                 }
1053
1054                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1055                 if (error!=OMX_ErrorNone) {
1056                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1057
1058                 }
1059
1060
1061                 //error=OMX_FreeHandle(omx_aud_dec);
1062                 error=OMX_FreeHandle(omx_aud_rend);
1063                 video->UnlockClock();
1064                 video->destroyClock();
1065                 omx_aud_rend/*dec*/=NULL;
1066                 if (error!=OMX_ErrorNone) {
1067                         Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1068                 }
1069         } else  video->UnlockClock();
1070           Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1071
1072         return 1;
1073 }
1074
1075
1076
1077 int AudioVPE::stop()
1078 {
1079   if (!initted) return 0;
1080
1081   Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1082   DeAllocateCodecsOMX();
1083   //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1084   return 1;
1085 }
1086
1087 int AudioVPE::mute()
1088 {
1089   if (!initted) return 0;
1090
1091 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
1092   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1093
1094   muted = 1;
1095   return 1;
1096 }
1097
1098 int AudioVPE::unMute()
1099 {
1100   if (!initted) return 0;
1101
1102 //  if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
1103   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1104
1105   muted = 0;
1106   return 1;
1107 }
1108
1109 int AudioVPE::pause() {
1110         if (!initted)
1111                 return 0;
1112         if (!paused) {
1113                 paused = true;
1114                 VideoVPEOGL *vw = (VideoVPEOGL*) Video::getInstance();
1115                 vw->LockClock();
1116                 OMX_ERRORTYPE error;
1117                 error = OMX_SendCommand(omx_aud_rend, OMX_CommandFlush,
1118                                 omx_rend_input_port, NULL);
1119                 if (error != OMX_ErrorNone) {
1120                         Log::getInstance()->log("Audio", Log::DEBUG,
1121                                         "OMX_Flush rend in failed %x", error);
1122
1123                 }
1124                 vw->UnlockClock();
1125                 vw->clockPause();
1126         }
1127         return 1;
1128 }
1129
1130 int AudioVPE::unPause()
1131 {
1132   if (!initted) return 0;
1133   if (paused) {
1134           paused=false; // may be also change omx clock
1135          VideoVPEOGL *vw = (VideoVPEOGL*) Video::getInstance();
1136           vw->clockUnpause();
1137   }
1138   return 1;
1139 }
1140
1141 int AudioVPE::reset()
1142 {
1143   if (!initted) return 0;
1144 //test();
1145   Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1146   DeAllocateCodecsOMX();
1147
1148 //  if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1149 //  Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1150  // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1151
1152   doMuting();
1153   return 1;
1154 }
1155
1156 int AudioVPE::setVolume(int tvolume)
1157 {
1158   // parameter: 0 for silence, 20 for full
1159   if ((tvolume < 0) || (tvolume > 20)) return 0;
1160
1161 // volume = 2 * (20 - volume);
1162 // Right, that one was rubbish... 0-10 were almost
1163 // inaudible, 11-20 did what should have been done
1164 // over the whole 0-20 range
1165
1166   tvolume = 20 - tvolume;
1167
1168   audio_volume Avolume;
1169   Avolume.frontleft = tvolume + Aoffset.frontleft;
1170   Avolume.frontright = tvolume + Aoffset.frontright;
1171   Avolume.rearleft = tvolume + Aoffset.rearleft;
1172   Avolume.rearright = tvolume + Aoffset.rearright;
1173   Avolume.center = tvolume + Aoffset.center;
1174   Avolume.lfe = tvolume + Aoffset.lfe;
1175
1176 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
1177
1178 //  unsigned long vol = (tvolume << 24) | (tvolume << 16);
1179 //
1180 //  Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
1181 //  Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
1182
1183 //  if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
1184   return 1;
1185 }
1186
1187 #ifdef DEV
1188 int AudioVPE::test()
1189 {
1190 //  ULLONG stc = 0;
1191 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1192
1193 /*  aud_sync_parms_t a;
1194   a.parm1 = 0;
1195   a.parm2 = 0;
1196 */
1197 //  int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1198
1199
1200   /*OK*/ //printf("Audio sync disable = %i\n", b);
1201
1202   return 1;
1203
1204
1205 }
1206 #endif
1207
1208 void AudioVPE::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1209 {
1210   packet = mplist.front();
1211 }
1212
1213 UINT AudioVPE::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1214         DeliverMediaPacket(packet, buffer, samplepos);
1215         if (*samplepos == packet.length) {
1216                 *samplepos = 0;
1217                 return 1;
1218         } else
1219                 return 0;
1220 }
1221
1222
1223 long long AudioVPE::correctAudioLatency(long long pts,int addsamples,int srate) {
1224
1225         VideoVPEOGL *video = (VideoVPEOGL*) Video::getInstance();
1226         video->LockClock();
1227         OMX_PARAM_U32TYPE audio_lat;
1228         OMX_ERRORTYPE error;
1229         memset(&audio_lat, 0, sizeof(audio_lat));
1230         audio_lat.nSize = sizeof(audio_lat);
1231         audio_lat.nVersion.nVersion = OMX_VERSION;
1232         audio_lat.nPortIndex = omx_rend_input_port;
1233
1234         error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1235                         &audio_lat);
1236          video->UnlockClock();
1237         if (error != OMX_ErrorNone) {
1238                 Log::getInstance()->log("Audio", Log::DEBUG,
1239                                 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1240                                 omx_rend_input_port);
1241                 return pts; // no correction in case of error
1242         }
1243         /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1244                         audio_lat.nU32);*/
1245
1246         long long workpts=0;
1247         workpts+=addsamples;
1248         workpts-=audio_lat.nU32;
1249         workpts*=10LL*1000LL*1000LL;
1250         workpts=workpts/((long long)srate); //one second /samplerate
1251         workpts+=pts;
1252
1253         return workpts;
1254 }
1255
1256
1257
1258 UINT AudioVPE::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1259                 UINT *samplepos) {
1260         /*First Check, if we have an audio sample*/
1261         VideoVPEOGL *vw = (VideoVPEOGL*) Video::getInstance();
1262         bool achange=false;
1263         OMX_ERRORTYPE error;
1264         Log *logger=Log::getInstance();
1265         if (vw->InIframemode()) {
1266                 samplepos = 0;
1267                 MILLISLEEP(10);
1268                 return 0; //Not in iframe mode!
1269         }
1270
1271         if (!omx_running) return 0; // if we are not runnig do not do this
1272         if (paused) return 0; //Block if we pause
1273
1274         UINT headerstrip = 0;
1275         if (packet.disconti) {
1276                 firstsynched = false;
1277                 if (cur_input_buf_omx) {
1278                         OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1279                                         cur_input_buf_omx);
1280                         if (error != OMX_ErrorNone) {
1281                                 Log::getInstance()->log("Audio", Log::DEBUG,
1282                                                 "OMX_EmptyThisBuffer failed %x", error);
1283                         }
1284                         cur_input_buf_omx = NULL;
1285                 }
1286         }
1287
1288         if (packet.type != lastAType) {//Format Change //Push data out !
1289                 firstsynched = false;
1290                 achange=true;
1291                 lastAType = packet.type;
1292
1293                 if (cur_input_buf_omx) {
1294                         OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1295                                         cur_input_buf_omx);
1296                         if (error != OMX_ErrorNone) {
1297                                 Log::getInstance()->log("Audio", Log::DEBUG,
1298                                                 "OMX_EmptyThisBuffer failed %x", error);
1299                         }
1300                         cur_input_buf_omx = NULL;
1301                 }
1302                 if (!ChangeAudioPortConfig()) {
1303                         Log::getInstance()->log("Audio", Log::DEBUG,
1304                                         "Changing audio port config failed", error);
1305                 }
1306
1307         }
1308
1309         /*Inspect PES-Header */
1310         if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1311                 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1312                 if (packet.type == MPTYPE_AC3)
1313                         headerstrip += 4; //skip ac3 bytes
1314                 *samplepos += headerstrip;
1315                 if (packet.synched) {
1316                         if (cur_input_buf_omx) {
1317                                 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1318                                 OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1319                                                 cur_input_buf_omx);
1320                                 if (error != OMX_ErrorNone) {
1321                                         Log::getInstance()->log("Audio", Log::DEBUG,
1322                                                         "OMX_EmptyThisBuffer failed %x", error);
1323                                 }
1324                                 //FrameWaitforDisplay(lastreftimeOMX);
1325                                  vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1326
1327                                 cur_input_buf_omx = NULL;//write out old data
1328                         }
1329                         firstsynched = true;
1330                 } else {
1331                         if (!firstsynched) {//
1332                                 *samplepos = packet.length;//if we have not processed at least one
1333                                 return packet.length;//synched packet ignore it!
1334                         }
1335                 }
1336         }
1337         if (!cur_input_buf_omx) {
1338                 input_bufs_omx_mutex.Lock();
1339                 if (input_bufs_omx_free.size()==0) {
1340                         input_bufs_omx_mutex.Unlock();
1341                         //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1342                         return 0; // we do not have a free media sample
1343
1344                 }
1345                 cur_input_buf_omx=input_bufs_omx_free.front();
1346                 cur_input_buf_omx->nFilledLen=0;
1347                 cur_input_buf_omx->nOffset=0;
1348                 cur_input_buf_omx->nTimeStamp=0;
1349                 input_bufs_omx_free.pop_front();
1350                 input_bufs_omx_mutex.Unlock();
1351         }
1352
1353
1354         if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1355                 if (packet.synched) {
1356                         //Log::getInstance()->log("Audio", Log::DEBUG,
1357                         //              "packet synched marker");
1358
1359                         //lastreftimePTS=packet.pts;
1360                         if (omx_first_frame) { // TODO time
1361                                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1362                                 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1363                                 omx_first_frame = false;
1364                         } else {
1365                                 cur_input_buf_omx->nFlags = 0;
1366                                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1367                         }
1368                         lastreftimeOMX = packet.presentation_time;
1369                         Log::getInstance()->log("Audio", Log::DEBUG,
1370                                         "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1371                         lastreftimePTS = packet.pts;
1372                         cur_input_buf_omx->nTimeStamp =0;// lastreftimeOMX; // the clock component is faulty;
1373                 } else {
1374                 //      Log::getInstance()->log("Audio", Log::DEBUG,
1375                         //                                      "packet NOT synched marker");
1376                         cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1377                         cur_input_buf_omx->nTimeStamp = 0;
1378
1379                 }
1380                 if (packet.disconti || achange) {
1381                         cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1382                         //mp23codec_context_libav->frame_size=-1;
1383                         //ac3codec_context_libav->frame_size=-1;
1384                 }
1385
1386         }
1387
1388         unsigned int haveToCopy=packet.length-*samplepos;
1389         if (passthrough) {
1390                 //TODO
1391         } else {
1392                 int len;
1393                 int gotta;
1394                 int framesize=0;
1395                 int errcount=0;
1396
1397                 AVCodecContext *current_context;
1398                 switch (packet.type) {
1399                 case MPTYPE_MPEG_AUDIO:
1400                 case MPTYPE_MPEG_AUDIO_LAYER3: {
1401                         current_context = mp23codec_context_libav;
1402                         if (current_context->frame_size<0) framesize=1152; //Maximum framesize
1403                         else framesize=current_context->frame_size;
1404                 }break;
1405                 case MPTYPE_AC3:
1406                 case MPTYPE_AC3_PRE13: {
1407                         current_context = ac3codec_context_libav;
1408                 }break;
1409                 };
1410
1411                 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
1412                 incoming_paket_libav.size = haveToCopy;
1413
1414                 while (haveToCopy> 0 && errcount<2) {
1415
1416                         //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
1417                         len = avcodec_decode_audio4(current_context, decode_frame_libav,
1418                                         &gotta, &incoming_paket_libav);
1419                 //      Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
1420                         if (len>0) {
1421                                 incoming_paket_libav.data += len;
1422                                 haveToCopy -= len;
1423                                 *samplepos += len;
1424                                 errcount=0;
1425                                 if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
1426                                 else framesize=haveToCopy;
1427                         } else {
1428                                 errcount++;
1429                                 framesize=haveToCopy;
1430                         }
1431
1432                         incoming_paket_libav.size =framesize;
1433                         if (gotta) {
1434                                 //Log::getInstance()->log("Audio", Log::DEBUG,
1435                                 //                                      "Got a frame");
1436                                 int dsize = av_samples_get_buffer_size(NULL,
1437                                                 current_context->channels, decode_frame_libav->nb_samples,
1438                                                 current_context->sample_fmt, 1);
1439                                 if ((cur_input_buf_omx->nFilledLen + dsize)
1440                                                 > cur_input_buf_omx->nAllocLen ) {
1441                                         // I doubt that this will ever happen
1442                                         Log::getInstance()->log("Audio", Log::DEBUG,
1443                                                                                 "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1444                                         OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1445                                                         cur_input_buf_omx);
1446                                         if (error != OMX_ErrorNone) {
1447                                                 Log::getInstance()->log("Audio", Log::DEBUG,
1448                                                                 "OMX_EmptyThisBuffer failed %x", error);
1449                                         }
1450                                         cur_input_buf_omx = NULL;
1451
1452                                         if (!cur_input_buf_omx) {
1453                                                 int count = 0;
1454                                                 while (count < 10 && omx_running) {
1455                                                         count++;
1456                                                         input_bufs_omx_mutex.Lock();
1457                                                         if (input_bufs_omx_free.size() == 0) {
1458                                                                 input_bufs_omx_mutex.Unlock();
1459                                                                 Log::getInstance()->log("Audio", Log::DEBUG,
1460                                                                                 "Deliver MediaPacket no free sample");
1461                                                                 MILLISLEEP(5);
1462                                                                 if (!omx_running) return false;
1463                                                                 continue;
1464                                                         }
1465                                                         cur_input_buf_omx = input_bufs_omx_free.front();
1466                                                         cur_input_buf_omx->nFilledLen = 0;
1467                                                         cur_input_buf_omx->nOffset = 0;
1468                                                         cur_input_buf_omx->nTimeStamp = 0;
1469                                                         input_bufs_omx_free.pop_front();
1470                                                         input_bufs_omx_mutex.Unlock();
1471                                                         break;
1472                                                 }
1473                                                 if (!cur_input_buf_omx) return *samplepos;
1474                                         }
1475
1476                                 }
1477
1478                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
1479                                 memcpy(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen,
1480                                                 decode_frame_libav->data[0], dsize);
1481                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
1482                                 cur_input_buf_omx->nFilledLen += dsize;
1483                         } else {
1484                                 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d",incoming_paket_libav.size);
1485                         }
1486
1487                 }
1488
1489         }
1490
1491         if (cur_input_buf_omx->nFilledLen) {
1492                 Log::getInstance()->log("Audio", Log::DEBUG,
1493                                                                                         "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1494                 error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1495                 if (error != OMX_ErrorNone) {
1496                         Log::getInstance()->log("Audio", Log::DEBUG,
1497                                         "OMX_EmptyThisBuffer failed %x", error);
1498                 }
1499                 if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1500                 cur_input_buf_omx = NULL;
1501         }
1502
1503
1504
1505         *samplepos=packet.length;
1506         return packet.length;
1507
1508 }
1509
1510
1511
1512 long long AudioVPE::SetStartOffset(long long curreftime, bool *rsync){
1513   VideoVPEOGL *vw=(VideoVPEOGL*)Video::getInstance();
1514   return vw->SetStartAudioOffset(curreftime,rsync);
1515 }
1516
1517 void AudioVPE::ResetTimeOffsets() {
1518   VideoVPEOGL *vw=(VideoVPEOGL*)Video::getInstance();
1519   vw->ResetTimeOffsets();
1520 }
1521
1522