Merge pull request #3886 from FernetMenta/aefixes
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / ActiveAE / ActiveAE.cpp
1 /*
2  *      Copyright (C) 2010-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "ActiveAE.h"
22
23 using namespace ActiveAE;
24 #include "ActiveAESound.h"
25 #include "ActiveAEStream.h"
26 #include "Utils/AEUtil.h"
27 #include "Encoders/AEEncoderFFmpeg.h"
28
29 #include "settings/Settings.h"
30 #include "settings/AdvancedSettings.h"
31 #include "windowing/WindowingFactory.h"
32
33 #define MAX_CACHE_LEVEL 0.5   // total cache time of stream in seconds
34 #define MAX_WATER_LEVEL 0.25  // buffered time after stream stages in seconds
35
36 void CEngineStats::Reset(unsigned int sampleRate)
37 {
38   CSingleLock lock(m_lock);
39   m_sinkUpdate = XbmcThreads::SystemClockMillis();
40   m_sinkDelay = 0;
41   m_sinkCacheTotal = 0;
42   m_sinkLatency = 0;
43   m_sinkSampleRate = sampleRate;
44   m_bufferedSamples = 0;
45   m_suspended = false;
46 }
47
48 void CEngineStats::UpdateSinkDelay(double delay, int samples)
49 {
50   CSingleLock lock(m_lock);
51   m_sinkUpdate = XbmcThreads::SystemClockMillis();
52   m_sinkDelay = delay;
53   if (samples > m_bufferedSamples)
54   {
55     CLog::Log(LOGERROR, "CEngineStats::UpdateSinkDelay - inconsistency in buffer time");
56   }
57   else
58     m_bufferedSamples -= samples;
59 }
60
61 void CEngineStats::AddSamples(int samples, std::list<CActiveAEStream*> &streams)
62 {
63   CSingleLock lock(m_lock);
64   m_bufferedSamples += samples;
65
66   //update buffered time of streams
67   std::list<CActiveAEStream*>::iterator it;
68   for(it=streams.begin(); it!=streams.end(); ++it)
69   {
70     float delay = 0;
71     std::deque<CSampleBuffer*>::iterator itBuf;
72     for(itBuf=(*it)->m_processingSamples.begin(); itBuf!=(*it)->m_processingSamples.end(); ++itBuf)
73     {
74       delay += (float)(*itBuf)->pkt->nb_samples / (*itBuf)->pkt->config.sample_rate;
75     }
76     delay += (*it)->m_resampleBuffers->GetDelay();
77     (*it)->m_bufferedTime = delay;
78   }
79 }
80
81 float CEngineStats::GetDelay()
82 {
83   CSingleLock lock(m_lock);
84   unsigned int now = XbmcThreads::SystemClockMillis();
85   float delay = m_sinkDelay - (double)(now-m_sinkUpdate) / 1000;
86   delay += (float)m_bufferedSamples / m_sinkSampleRate;
87
88   if (delay < 0)
89     delay = 0.0;
90
91   return delay;
92 }
93
94 // this is used to sync a/v so we need to add sink latency here
95 float CEngineStats::GetDelay(CActiveAEStream *stream)
96 {
97   CSingleLock lock(m_lock);
98   unsigned int now = XbmcThreads::SystemClockMillis();
99   float delay = m_sinkDelay - (double)(now-m_sinkUpdate) / 1000;
100   delay += m_sinkLatency;
101   delay += (float)m_bufferedSamples / m_sinkSampleRate;
102
103   if (delay < 0)
104     delay = 0.0;
105
106   delay += stream->m_bufferedTime;
107   return delay;
108 }
109
110 float CEngineStats::GetCacheTime(CActiveAEStream *stream)
111 {
112   CSingleLock lock(m_lock);
113   float delay = (float)m_bufferedSamples / m_sinkSampleRate;
114
115   delay += stream->m_bufferedTime;
116   return delay;
117 }
118
119 float CEngineStats::GetCacheTotal(CActiveAEStream *stream)
120 {
121   return MAX_CACHE_LEVEL + m_sinkCacheTotal;
122 }
123
124 float CEngineStats::GetWaterLevel()
125 {
126   return (float)m_bufferedSamples / m_sinkSampleRate;
127 }
128
129 void CEngineStats::SetSuspended(bool state)
130 {
131   CSingleLock lock(m_lock);
132   m_suspended = state;
133 }
134
135 bool CEngineStats::IsSuspended()
136 {
137   CSingleLock lock(m_lock);
138   return m_suspended;
139 }
140
141 CActiveAE::CActiveAE() :
142   CThread("ActiveAE"),
143   m_controlPort("OutputControlPort", &m_inMsgEvent, &m_outMsgEvent),
144   m_dataPort("OutputDataPort", &m_inMsgEvent, &m_outMsgEvent),
145   m_sink(&m_outMsgEvent)
146 {
147   m_sinkBuffers = NULL;
148   m_silenceBuffers = NULL;
149   m_encoderBuffers = NULL;
150   m_vizBuffers = NULL;
151   m_vizBuffersInput = NULL;
152   m_volume = 1.0;
153   m_aeVolume = 1.0;
154   m_muted = false;
155   m_aeMuted = false;
156   m_mode = MODE_PCM;
157   m_encoder = NULL;
158   m_audioCallback = NULL;
159   m_vizInitialized = false;
160   m_sinkHasVolume = false;
161 }
162
163 CActiveAE::~CActiveAE()
164 {
165   Dispose();
166 }
167
168 void CActiveAE::Dispose()
169 {
170 #if defined(HAS_GLX) || defined(TARGET_DARWIN_OSX)
171   g_Windowing.Unregister(this);
172 #endif
173
174   m_bStop = true;
175   m_outMsgEvent.Set();
176   StopThread();
177   m_controlPort.Purge();
178   m_dataPort.Purge();
179   m_sink.Dispose();
180
181   m_dllAvFormat.Unload();
182   m_dllAvCodec.Unload();
183   m_dllAvUtil.Unload();
184 }
185
186 //-----------------------------------------------------------------------------
187 // Behavior
188 //-----------------------------------------------------------------------------
189
190 enum AE_STATES
191 {
192   AE_TOP = 0,                      // 0
193   AE_TOP_ERROR,                    // 1
194   AE_TOP_UNCONFIGURED,             // 2
195   AE_TOP_RECONFIGURING,            // 3
196   AE_TOP_CONFIGURED,               // 4
197   AE_TOP_CONFIGURED_SUSPEND,       // 5
198   AE_TOP_CONFIGURED_IDLE,          // 6
199   AE_TOP_CONFIGURED_PLAY,          // 7
200 };
201
202 int AE_parentStates[] = {
203     -1,
204     0, //TOP_ERROR
205     0, //TOP_UNCONFIGURED
206     0, //TOP_CONFIGURED
207     0, //TOP_RECONFIGURING
208     4, //TOP_CONFIGURED_SUSPEND
209     4, //TOP_CONFIGURED_IDLE
210     4, //TOP_CONFIGURED_PLAY
211 };
212
213 void CActiveAE::StateMachine(int signal, Protocol *port, Message *msg)
214 {
215   for (int state = m_state; ; state = AE_parentStates[state])
216   {
217     switch (state)
218     {
219     case AE_TOP: // TOP
220       if (port == &m_controlPort)
221       {
222         switch (signal)
223         {
224         case CActiveAEControlProtocol::GETSTATE:
225           msg->Reply(CActiveAEControlProtocol::ACC, &m_state, sizeof(m_state));
226           return;
227         case CActiveAEControlProtocol::VOLUME:
228           m_volume = *(float*)msg->data;
229           if (m_sinkHasVolume)
230             m_sink.m_controlPort.SendOutMessage(CSinkControlProtocol::VOLUME, &m_volume, sizeof(float));
231           return;
232         case CActiveAEControlProtocol::MUTE:
233           m_muted = *(bool*)msg->data;
234           return;
235         case CActiveAEControlProtocol::KEEPCONFIG:
236           m_extKeepConfig = *(unsigned int*)msg->data;
237           return;
238         default:
239           break;
240         }
241       }
242       else if (port == &m_dataPort)
243       {
244         switch (signal)
245         {
246         case CActiveAEDataProtocol::NEWSOUND:
247           CActiveAESound *sound;
248           sound = *(CActiveAESound**)msg->data;
249           if (sound)
250           {
251             m_sounds.push_back(sound);
252             ResampleSounds();
253           }
254           return;
255         case CActiveAEDataProtocol::FREESTREAM:
256           CActiveAEStream *stream;
257           stream = *(CActiveAEStream**)msg->data;
258           DiscardStream(stream);
259           return;
260         case CActiveAEDataProtocol::FREESOUND:
261           sound = *(CActiveAESound**)msg->data;
262           DiscardSound(sound);
263           return;
264         case CActiveAEDataProtocol::DRAINSTREAM:
265           stream = *(CActiveAEStream**)msg->data;
266           stream->m_drain = true;
267           stream->m_resampleBuffers->m_drain = true;
268           msg->Reply(CActiveAEDataProtocol::ACC);
269           stream->m_streamPort->SendInMessage(CActiveAEDataProtocol::STREAMDRAINED);
270           return;
271         default:
272           break;
273         }
274       }
275       else if (port == &m_sink.m_dataPort)
276       {
277         switch (signal)
278         {
279         case CSinkDataProtocol::RETURNSAMPLE:
280           CSampleBuffer **buffer;
281           buffer = (CSampleBuffer**)msg->data;
282           if (buffer)
283           {
284             (*buffer)->Return();
285           }
286           return;
287         default:
288           break;
289         }
290       }
291       {
292         std::string portName = port == NULL ? "timer" : port->portName;
293         CLog::Log(LOGWARNING, "CActiveAE::%s - signal: %d from port: %s not handled for state: %d", __FUNCTION__, signal, portName.c_str(), m_state);
294       }
295       return;
296
297     case AE_TOP_ERROR:
298       if (port == NULL) // timeout
299       {
300         switch (signal)
301         {
302         case CActiveAEControlProtocol::TIMEOUT:
303           m_extError = false;
304           LoadSettings();
305           Configure();
306           if (!m_extError)
307           {
308             m_state = AE_TOP_CONFIGURED_IDLE;
309             m_extTimeout = 0;
310           }
311           else
312           {
313             m_state = AE_TOP_ERROR;
314             m_extTimeout = 500;
315           }
316           return;
317         default:
318           break;
319         }
320       }
321       break;
322
323     case AE_TOP_UNCONFIGURED:
324       if (port == &m_controlPort)
325       {
326         switch (signal)
327         {
328         case CActiveAEControlProtocol::INIT:
329           m_extError = false;
330           m_sink.EnumerateSinkList(false);
331           LoadSettings();
332           Configure();
333           msg->Reply(CActiveAEControlProtocol::ACC);
334           if (!m_extError)
335           {
336             m_state = AE_TOP_CONFIGURED_IDLE;
337             m_extTimeout = 0;
338           }
339           else
340           {
341             m_state = AE_TOP_ERROR;
342             m_extTimeout = 500;
343           }
344           return;
345
346         default:
347           break;
348         }
349       }
350       break;
351
352     case AE_TOP_RECONFIGURING:
353       if (port == NULL) // timeout
354       {
355         switch (signal)
356         {
357         case CActiveAEControlProtocol::TIMEOUT:
358           // drain
359           if (RunStages())
360           {
361             m_extTimeout = 0;
362             return;
363           }
364           if (!m_sinkBuffers->m_inputSamples.empty() || !m_sinkBuffers->m_outputSamples.empty())
365           {
366             m_extTimeout = 100;
367             return;
368           }
369           if (NeedReconfigureSink())
370             DrainSink();
371
372           if (!m_extError)
373             Configure();
374           if (!m_extError)
375           {
376             m_state = AE_TOP_CONFIGURED_PLAY;
377             m_extTimeout = 0;
378           }
379           else
380           {
381             m_state = AE_TOP_ERROR;
382             m_extTimeout = 500;
383           }
384           m_extDeferData = false;
385           return;
386         default:
387           break;
388         }
389       }
390       break;
391
392     case AE_TOP_CONFIGURED:
393       if (port == &m_controlPort)
394       {
395         switch (signal)
396         {
397         case CActiveAEControlProtocol::RECONFIGURE:
398           if (m_streams.empty())
399           {
400             bool silence = false;
401             m_sink.m_controlPort.SendOutMessage(CSinkControlProtocol::SILENCEMODE, &silence, sizeof(bool));
402           }
403           LoadSettings();
404           ChangeResamplers();
405           if (!NeedReconfigureBuffers() && !NeedReconfigureSink())
406             return;
407           m_state = AE_TOP_RECONFIGURING;
408           m_extTimeout = 0;
409           // don't accept any data until we are reconfigured
410           m_extDeferData = true;
411           return;
412         case CActiveAEControlProtocol::SUSPEND:
413           UnconfigureSink();
414           m_stats.SetSuspended(true);
415           m_state = AE_TOP_CONFIGURED_SUSPEND;
416           m_extDeferData = true;
417           return;
418         case CActiveAEControlProtocol::DISPLAYLOST:
419           if (m_sink.GetDeviceType(m_mode == MODE_PCM ? m_settings.device : m_settings.passthoughdevice) == AE_DEVTYPE_HDMI)
420           {
421             UnconfigureSink();
422             m_stats.SetSuspended(true);
423             m_state = AE_TOP_CONFIGURED_SUSPEND;
424             m_extDeferData = true;
425           }
426           return;
427         case CActiveAEControlProtocol::PAUSESTREAM:
428           CActiveAEStream *stream;
429           stream = *(CActiveAEStream**)msg->data;
430           if (stream->m_paused != true && m_streams.size() == 1)
431             FlushEngine();
432           stream->m_paused = true;
433           return;
434         case CActiveAEControlProtocol::RESUMESTREAM:
435           stream = *(CActiveAEStream**)msg->data;
436           stream->m_paused = false;
437           m_state = AE_TOP_CONFIGURED_PLAY;
438           m_extTimeout = 0;
439           return;
440         case CActiveAEControlProtocol::FLUSHSTREAM:
441           stream = *(CActiveAEStream**)msg->data;
442           SFlushStream(stream);
443           msg->Reply(CActiveAEControlProtocol::ACC);
444           m_state = AE_TOP_CONFIGURED_PLAY;
445           m_extTimeout = 0;
446           return;
447         case CActiveAEControlProtocol::STREAMAMP:
448           MsgStreamParameter *par;
449           par = (MsgStreamParameter*)msg->data;
450           par->stream->m_limiter.SetAmplification(par->parameter.float_par);
451           par->stream->m_amplify = par->parameter.float_par;
452           return;
453         case CActiveAEControlProtocol::STREAMVOLUME:
454           par = (MsgStreamParameter*)msg->data;
455           par->stream->m_volume = par->parameter.float_par;
456           return;
457         case CActiveAEControlProtocol::STREAMRGAIN:
458           par = (MsgStreamParameter*)msg->data;
459           par->stream->m_rgain = par->parameter.float_par;
460           return;
461         case CActiveAEControlProtocol::STREAMRESAMPLERATIO:
462           par = (MsgStreamParameter*)msg->data;
463           if (par->stream->m_resampleBuffers)
464           {
465             par->stream->m_resampleBuffers->m_resampleRatio = par->parameter.double_par;
466           }
467           return;
468         case CActiveAEControlProtocol::STREAMFADE:
469           MsgStreamFade *fade;
470           fade = (MsgStreamFade*)msg->data;
471           fade->stream->m_fadingBase = fade->from;
472           fade->stream->m_fadingTarget = fade->target;
473           fade->stream->m_fadingTime = fade->millis;
474           fade->stream->m_fadingSamples = -1;
475           return;
476         case CActiveAEControlProtocol::STOPSOUND:
477           CActiveAESound *sound;
478           sound = *(CActiveAESound**)msg->data;
479           SStopSound(sound);
480           return;
481         default:
482           break;
483         }
484       }
485       else if (port == &m_dataPort)
486       {
487         switch (signal)
488         {
489         case CActiveAEDataProtocol::PLAYSOUND:
490           CActiveAESound *sound;
491           sound = *(CActiveAESound**)msg->data;
492           if (m_settings.guisoundmode == AE_SOUND_OFF ||
493              (m_settings.guisoundmode == AE_SOUND_IDLE && !m_streams.empty()))
494             return;
495           if (sound)
496           {
497             SoundState st = {sound, 0};
498             m_sounds_playing.push_back(st);
499             m_extTimeout = 0;
500             m_state = AE_TOP_CONFIGURED_PLAY;
501           }
502           return;
503         case CActiveAEDataProtocol::NEWSTREAM:
504           MsgStreamNew *streamMsg;
505           CActiveAEStream *stream;
506           streamMsg = (MsgStreamNew*)msg->data;
507           stream = CreateStream(streamMsg);
508           if(stream)
509           {
510             msg->Reply(CActiveAEDataProtocol::ACC, &stream, sizeof(CActiveAEStream*));
511             LoadSettings();
512             Configure();
513             if (!m_extError)
514             {
515               m_state = AE_TOP_CONFIGURED_PLAY;
516               m_extTimeout = 0;
517             }
518             else
519             {
520               m_state = AE_TOP_ERROR;
521               m_extTimeout = 500;
522             }
523           }
524           else
525             msg->Reply(CActiveAEDataProtocol::ERR);
526           return;
527         case CActiveAEDataProtocol::STREAMSAMPLE:
528           MsgStreamSample *msgData;
529           CSampleBuffer *samples;
530           msgData = (MsgStreamSample*)msg->data;
531           samples = msgData->stream->m_processingSamples.front();
532           msgData->stream->m_processingSamples.pop_front();
533           if (samples != msgData->buffer)
534             CLog::Log(LOGERROR, "CActiveAE - inconsistency in stream sample message");
535           if (msgData->buffer->pkt->nb_samples == 0)
536             msgData->buffer->Return();
537           else
538             msgData->stream->m_resampleBuffers->m_inputSamples.push_back(msgData->buffer);
539           m_extTimeout = 0;
540           m_state = AE_TOP_CONFIGURED_PLAY;
541           return;
542         case CActiveAEDataProtocol::FREESTREAM:
543           stream = *(CActiveAEStream**)msg->data;
544           DiscardStream(stream);
545           if (m_streams.empty())
546           {
547             if (m_extKeepConfig)
548               m_extDrainTimer.Set(m_extKeepConfig);
549             else
550               m_extDrainTimer.Set(m_stats.GetDelay() * 1000);
551             m_extDrain = true;
552           }
553           m_extTimeout = 0;
554           m_state = AE_TOP_CONFIGURED_PLAY;
555           return;
556         case CActiveAEDataProtocol::DRAINSTREAM:
557           stream = *(CActiveAEStream**)msg->data;
558           stream->m_drain = true;
559           stream->m_resampleBuffers->m_drain = true;
560           m_extTimeout = 0;
561           m_state = AE_TOP_CONFIGURED_PLAY;
562           msg->Reply(CActiveAEDataProtocol::ACC);
563           return;
564         default:
565           break;
566         }
567       }
568       else if (port == &m_sink.m_dataPort)
569       {
570         switch (signal)
571         {
572         case CSinkDataProtocol::RETURNSAMPLE:
573           CSampleBuffer **buffer;
574           buffer = (CSampleBuffer**)msg->data;
575           if (buffer)
576           {
577             (*buffer)->Return();
578           }
579           m_extTimeout = 0;
580           m_state = AE_TOP_CONFIGURED_PLAY;
581           return;
582         default:
583           break;
584         }
585       }
586       break;
587
588     case AE_TOP_CONFIGURED_SUSPEND:
589       if (port == &m_controlPort)
590       {
591         bool displayReset = false;
592         switch (signal)
593         {
594         case CActiveAEControlProtocol::DISPLAYRESET:
595           displayReset = true;
596         case CActiveAEControlProtocol::INIT:
597           m_extError = false;
598           if (!displayReset)
599           {
600             m_sink.EnumerateSinkList(true);
601             LoadSettings();
602           }
603           Configure();
604           if (!displayReset)
605             msg->Reply(CActiveAEControlProtocol::ACC);
606           if (!m_extError)
607           {
608             m_state = AE_TOP_CONFIGURED_PLAY;
609             m_extTimeout = 0;
610           }
611           else
612           {
613             m_state = AE_TOP_ERROR;
614             m_extTimeout = 500;
615           }
616           m_stats.SetSuspended(false);
617           m_extDeferData = false;
618           return;
619         default:
620           break;
621         }
622       }
623       else if (port == &m_sink.m_dataPort)
624       {
625         switch (signal)
626         {
627         case CSinkDataProtocol::RETURNSAMPLE:
628           CSampleBuffer **buffer;
629           buffer = (CSampleBuffer**)msg->data;
630           if (buffer)
631           {
632             (*buffer)->Return();
633           }
634           return;
635         default:
636           break;
637         }
638       }
639       break;
640
641     case AE_TOP_CONFIGURED_IDLE:
642       if (port == NULL) // timeout
643       {
644         switch (signal)
645         {
646         case CActiveAEControlProtocol::TIMEOUT:
647           ResampleSounds();
648           ClearDiscardedBuffers();
649           if (m_extDrain)
650           {
651             if (m_extDrainTimer.IsTimePast())
652             {
653               Configure();
654               if (!m_extError)
655               {
656                 m_state = AE_TOP_CONFIGURED_PLAY;
657                 m_extTimeout = 0;
658               }
659               else
660               {
661                 m_state = AE_TOP_ERROR;
662                 m_extTimeout = 500;
663               }
664             }
665             else
666               m_extTimeout = m_extDrainTimer.MillisLeft();
667           }
668           else
669             m_extTimeout = 5000;
670           return;
671         default:
672           break;
673         }
674       }
675       break;
676
677     case AE_TOP_CONFIGURED_PLAY:
678       if (port == NULL) // timeout
679       {
680         switch (signal)
681         {
682         case CActiveAEControlProtocol::TIMEOUT:
683           if (m_extError)
684           {
685             m_state = AE_TOP_ERROR;
686             m_extTimeout = 100;
687             return;
688           }
689           if (RunStages())
690           {
691             m_extTimeout = 0;
692             return;
693           }
694           if (!m_extDrain && HasWork())
695           {
696             ClearDiscardedBuffers();
697             m_extTimeout = 100;
698             return;
699           }
700           m_extTimeout = 0;
701           m_state = AE_TOP_CONFIGURED_IDLE;
702           return;
703         default:
704           break;
705         }
706       }
707       break;
708
709     default: // we are in no state, should not happen
710       CLog::Log(LOGERROR, "CActiveAE::%s - no valid state: %d", __FUNCTION__, m_state);
711       return;
712     }
713   } // for
714 }
715
716 void CActiveAE::Process()
717 {
718   Message *msg = NULL;
719   Protocol *port = NULL;
720   bool gotMsg;
721   XbmcThreads::EndTime timer;
722
723   m_state = AE_TOP_UNCONFIGURED;
724   m_extTimeout = 1000;
725   m_bStateMachineSelfTrigger = false;
726   m_extDrain = false;
727   m_extDeferData = false;
728   m_extKeepConfig = 0;
729
730   // start sink
731   m_sink.Start();
732
733   while (!m_bStop)
734   {
735     gotMsg = false;
736     timer.Set(m_extTimeout);
737
738     if (m_bStateMachineSelfTrigger)
739     {
740       m_bStateMachineSelfTrigger = false;
741       // self trigger state machine
742       StateMachine(msg->signal, port, msg);
743       if (!m_bStateMachineSelfTrigger)
744       {
745         msg->Release();
746         msg = NULL;
747       }
748       continue;
749     }
750     // check control port
751     else if (m_controlPort.ReceiveOutMessage(&msg))
752     {
753       gotMsg = true;
754       port = &m_controlPort;
755     }
756     // check sink data port
757     else if (m_sink.m_dataPort.ReceiveInMessage(&msg))
758     {
759       gotMsg = true;
760       port = &m_sink.m_dataPort;
761     }
762     else if (!m_extDeferData)
763     {
764       // check data port
765       if (m_dataPort.ReceiveOutMessage(&msg))
766       {
767         gotMsg = true;
768         port = &m_dataPort;
769       }
770       // stream data ports
771       else
772       {
773         std::list<CActiveAEStream*>::iterator it;
774         for(it=m_streams.begin(); it!=m_streams.end(); ++it)
775         {
776           if((*it)->m_streamPort->ReceiveOutMessage(&msg))
777           {
778             gotMsg = true;
779             port = &m_dataPort;
780             break;
781           }
782         }
783       }
784     }
785
786     if (gotMsg)
787     {
788       StateMachine(msg->signal, port, msg);
789       if (!m_bStateMachineSelfTrigger)
790       {
791         msg->Release();
792         msg = NULL;
793       }
794       continue;
795     }
796
797     // wait for message
798     else if (m_outMsgEvent.WaitMSec(m_extTimeout))
799     {
800       m_extTimeout = timer.MillisLeft();
801       continue;
802     }
803     // time out
804     else
805     {
806       msg = m_controlPort.GetMessage();
807       msg->signal = CActiveAEControlProtocol::TIMEOUT;
808       port = 0;
809       // signal timeout to state machine
810       StateMachine(msg->signal, port, msg);
811       if (!m_bStateMachineSelfTrigger)
812       {
813         msg->Release();
814         msg = NULL;
815       }
816     }
817   }
818 }
819
820 AEAudioFormat CActiveAE::GetInputFormat(AEAudioFormat *desiredFmt)
821 {
822   AEAudioFormat inputFormat;
823
824   if (m_streams.empty())
825   {
826     inputFormat.m_dataFormat    = AE_FMT_FLOAT;
827     inputFormat.m_sampleRate    = 44100;
828     inputFormat.m_encodedRate   = 0;
829     inputFormat.m_channelLayout = AE_CH_LAYOUT_2_0;
830     inputFormat.m_frames        = 0;
831     inputFormat.m_frameSamples  = 0;
832     inputFormat.m_frameSize     = 0;
833   }
834   // force input format after unpausing slave
835   else if (desiredFmt != NULL)
836   {
837     inputFormat = *desiredFmt;
838   }
839   // keep format when having multiple streams
840   else if (m_streams.size() > 1 && m_silenceBuffers == NULL)
841   {
842     inputFormat = m_inputFormat;
843   }
844   else
845   {
846     inputFormat = m_streams.front()->m_format;
847     m_inputFormat = inputFormat;
848   }
849
850   return inputFormat;
851 }
852
853 void CActiveAE::Configure(AEAudioFormat *desiredFmt)
854 {
855   bool initSink = false;
856
857   AEAudioFormat sinkInputFormat, inputFormat;
858   AEAudioFormat oldInternalFormat = m_internalFormat;
859   AEAudioFormat oldSinkRequestFormat = m_sinkRequestFormat;
860
861   inputFormat = GetInputFormat(desiredFmt);
862
863   m_sinkRequestFormat = inputFormat;
864   ApplySettingsToFormat(m_sinkRequestFormat, m_settings, (int*)&m_mode);
865   m_extKeepConfig = 0;
866
867   std::string device = AE_IS_RAW(m_sinkRequestFormat.m_dataFormat) ? m_settings.passthoughdevice : m_settings.device;
868   std::string driver;
869   CAESinkFactory::ParseDevice(device, driver);
870   if ((!CompareFormat(m_sinkRequestFormat, m_sinkFormat) && !CompareFormat(m_sinkRequestFormat, oldSinkRequestFormat)) ||
871       m_currDevice.compare(device) != 0 ||
872       m_settings.driver.compare(driver) != 0)
873   {
874     if (!InitSink())
875       return;
876     m_settings.driver = driver;
877     m_currDevice = device;
878     initSink = true;
879     m_stats.Reset(m_sinkFormat.m_sampleRate);
880     m_sink.m_controlPort.SendOutMessage(CSinkControlProtocol::VOLUME, &m_volume, sizeof(float));
881
882     // limit buffer size in case of sink returns large buffer
883     unsigned int buffertime = (m_sinkFormat.m_frames*1000) / m_sinkFormat.m_sampleRate;
884     if (buffertime > 80)
885     {
886       CLog::Log(LOGWARNING, "ActiveAE::%s - sink returned large buffer of %d ms, reducing to 80 ms", __FUNCTION__, buffertime);
887       m_sinkFormat.m_frames = 80 * m_sinkFormat.m_sampleRate / 1000;
888     }
889   }
890
891   if (m_silenceBuffers)
892   {
893     m_discardBufferPools.push_back(m_silenceBuffers);
894     m_silenceBuffers = NULL;
895   }
896
897   // buffers for driving gui sounds if no streams are active
898   if (m_streams.empty())
899   {
900     inputFormat = m_sinkFormat;
901     inputFormat.m_dataFormat = AE_FMT_FLOAT;
902     inputFormat.m_frameSize = inputFormat.m_channelLayout.Count() *
903                               (CAEUtil::DataFormatToBits(inputFormat.m_dataFormat) >> 3);
904     m_silenceBuffers = new CActiveAEBufferPool(inputFormat);
905     m_silenceBuffers->Create(MAX_WATER_LEVEL*1000);
906     sinkInputFormat = inputFormat;
907     m_internalFormat = inputFormat;
908
909     bool silence = false;
910     m_sink.m_controlPort.SendOutMessage(CSinkControlProtocol::SILENCEMODE, &silence, sizeof(bool));
911
912     delete m_encoder;
913     m_encoder = NULL;
914
915     if (m_encoderBuffers)
916     {
917       m_discardBufferPools.push_back(m_encoderBuffers);
918       m_encoderBuffers = NULL;
919     }
920     if (m_vizBuffers)
921     {
922       m_discardBufferPools.push_back(m_vizBuffers);
923       m_vizBuffers = NULL;
924     }
925     if (m_vizBuffersInput)
926     {
927       m_discardBufferPools.push_back(m_vizBuffersInput);
928       m_vizBuffersInput = NULL;
929     }
930   }
931   // resample buffers for streams
932   else
933   {
934     bool silence = true;
935     m_sink.m_controlPort.SendOutMessage(CSinkControlProtocol::SILENCEMODE, &silence, sizeof(bool));
936
937     AEAudioFormat outputFormat;
938     if (m_mode == MODE_RAW)
939     {
940       outputFormat = inputFormat;
941       sinkInputFormat = m_sinkFormat;
942     }
943     // transcode everything with more than 2 channels
944     else if (m_mode == MODE_TRANSCODE)
945     {
946       outputFormat = inputFormat;
947       outputFormat.m_dataFormat = AE_FMT_FLOATP;
948       outputFormat.m_sampleRate = 48000;
949
950       // setup encoder
951       if (!m_encoder)
952       {
953         m_encoder = new CAEEncoderFFmpeg();
954         m_encoder->Initialize(outputFormat, true);
955         m_encoderFormat = outputFormat;
956       }
957       else
958         outputFormat = m_encoderFormat;
959
960       outputFormat.m_channelLayout = m_encoderFormat.m_channelLayout;
961       outputFormat.m_frames = m_encoderFormat.m_frames;
962
963       // encoder buffer
964       if (m_encoder->GetCodecID() == AV_CODEC_ID_AC3)
965       {
966         AEAudioFormat format;
967         format.m_channelLayout = AE_CH_LAYOUT_2_0;
968         format.m_dataFormat = AE_FMT_S16NE;
969         format.m_frameSize = 2* (CAEUtil::DataFormatToBits(format.m_dataFormat) >> 3);
970         format.m_frames = AC3_FRAME_SIZE;
971         format.m_sampleRate = 48000;
972         if (m_encoderBuffers && initSink)
973         {
974           m_discardBufferPools.push_back(m_encoderBuffers);
975           m_encoderBuffers = NULL;
976         }
977         if (!m_encoderBuffers)
978         {
979           m_encoderBuffers = new CActiveAEBufferPool(format);
980           m_encoderBuffers->Create(MAX_WATER_LEVEL*1000);
981         }
982       }
983
984       sinkInputFormat = m_sinkFormat;
985     }
986     else
987     {
988       outputFormat = m_sinkFormat;
989       outputFormat.m_dataFormat = AE_FMT_FLOAT;
990       outputFormat.m_frameSize = outputFormat.m_channelLayout.Count() *
991                                  (CAEUtil::DataFormatToBits(outputFormat.m_dataFormat) >> 3);
992       // TODO: adjust to decoder
993       sinkInputFormat = outputFormat;
994     }
995     m_internalFormat = outputFormat;
996
997     std::list<CActiveAEStream*>::iterator it;
998     for(it=m_streams.begin(); it!=m_streams.end(); ++it)
999     {
1000       // check if we support input format of stream
1001       if (!AE_IS_RAW((*it)->m_format.m_dataFormat) && 
1002           CActiveAEResample::GetAVSampleFormat((*it)->m_format.m_dataFormat) == AV_SAMPLE_FMT_FLT &&
1003           (*it)->m_format.m_dataFormat != AE_FMT_FLOAT)
1004       {
1005         (*it)->m_convertFn = CAEConvert::ToFloat((*it)->m_format.m_dataFormat);
1006         (*it)->m_format.m_dataFormat = AE_FMT_FLOAT;
1007       }
1008
1009       if (!(*it)->m_inputBuffers)
1010       {
1011         // align input buffers with period of sink or encoder
1012         (*it)->m_format.m_frames = m_internalFormat.m_frames * ((float)(*it)->m_format.m_sampleRate / m_internalFormat.m_sampleRate);
1013
1014         // create buffer pool
1015         (*it)->m_inputBuffers = new CActiveAEBufferPool((*it)->m_format);
1016         (*it)->m_inputBuffers->Create(MAX_CACHE_LEVEL*1000);
1017         (*it)->m_streamSpace = (*it)->m_format.m_frameSize * (*it)->m_format.m_frames;
1018
1019         // if input format does not follow ffmpeg channel mask, we may need to remap channels
1020         (*it)->InitRemapper();
1021       }
1022       if (initSink && (*it)->m_resampleBuffers)
1023       {
1024         m_discardBufferPools.push_back((*it)->m_resampleBuffers);
1025         (*it)->m_resampleBuffers = NULL;
1026       }
1027       if (!(*it)->m_resampleBuffers)
1028       {
1029         (*it)->m_resampleBuffers = new CActiveAEBufferPoolResample((*it)->m_inputBuffers->m_format, outputFormat, m_settings.resampleQuality);
1030         (*it)->m_resampleBuffers->m_changeResampler = (*it)->m_forceResampler;
1031         (*it)->m_resampleBuffers->Create(MAX_CACHE_LEVEL*1000, false, m_settings.stereoupmix, m_settings.normalizelevels);
1032       }
1033       if (m_mode == MODE_TRANSCODE || m_streams.size() > 1)
1034         (*it)->m_resampleBuffers->m_fillPackets = true;
1035
1036       // amplification
1037       (*it)->m_limiter.SetSamplerate(outputFormat.m_sampleRate);
1038     }
1039
1040     // buffers for viz
1041     if (!AE_IS_RAW(inputFormat.m_dataFormat))
1042     {
1043       if (initSink && m_vizBuffers)
1044       {
1045         m_discardBufferPools.push_back(m_vizBuffers);
1046         m_vizBuffers = NULL;
1047         m_discardBufferPools.push_back(m_vizBuffersInput);
1048         m_vizBuffersInput = NULL;
1049       }
1050       if (!m_vizBuffers)
1051       {
1052         AEAudioFormat vizFormat = m_internalFormat;
1053         vizFormat.m_channelLayout = AE_CH_LAYOUT_2_0;
1054         vizFormat.m_dataFormat = AE_FMT_FLOAT;
1055
1056         // input buffers
1057         m_vizBuffersInput = new CActiveAEBufferPool(m_internalFormat);
1058         m_vizBuffersInput->Create(2000);
1059
1060         // resample buffers
1061         m_vizBuffers = new CActiveAEBufferPoolResample(m_internalFormat, vizFormat, m_settings.resampleQuality);
1062         // TODO use cache of sync + water level
1063         m_vizBuffers->Create(2000, false, false);
1064         m_vizInitialized = false;
1065       }
1066     }
1067   }
1068
1069   // resample buffers for sink
1070   if (m_sinkBuffers && 
1071      (!CompareFormat(m_sinkBuffers->m_format,m_sinkFormat) || !CompareFormat(m_sinkBuffers->m_inputFormat, sinkInputFormat)))
1072   {
1073     m_discardBufferPools.push_back(m_sinkBuffers);
1074     m_sinkBuffers = NULL;
1075   }
1076   if (!m_sinkBuffers)
1077   {
1078     m_sinkBuffers = new CActiveAEBufferPoolResample(sinkInputFormat, m_sinkFormat, m_settings.resampleQuality);
1079     m_sinkBuffers->Create(MAX_WATER_LEVEL*1000, true, false);
1080   }
1081
1082   // reset gui sounds
1083   if (!CompareFormat(oldInternalFormat, m_internalFormat))
1084   {
1085     if (m_settings.guisoundmode == AE_SOUND_ALWAYS ||
1086        (m_settings.guisoundmode == AE_SOUND_IDLE && m_streams.empty()))
1087     {
1088       std::vector<CActiveAESound*>::iterator it;
1089       for (it = m_sounds.begin(); it != m_sounds.end(); ++it)
1090       {
1091         (*it)->SetConverted(false);
1092       }
1093     }
1094     m_sounds_playing.clear();
1095   }
1096
1097   ClearDiscardedBuffers();
1098   m_extDrain = false;
1099 }
1100
1101 CActiveAEStream* CActiveAE::CreateStream(MsgStreamNew *streamMsg)
1102 {
1103   // we only can handle a single pass through stream
1104   bool hasRawStream = false;
1105   bool hasStream = false;
1106   std::list<CActiveAEStream*>::iterator it;
1107   for(it = m_streams.begin(); it != m_streams.end(); ++it)
1108   {
1109     if((*it)->IsDrained())
1110       continue;
1111     if(AE_IS_RAW((*it)->m_format.m_dataFormat))
1112       hasRawStream = true;
1113     hasStream = true;
1114   }
1115   if (hasRawStream || (hasStream && AE_IS_RAW(streamMsg->format.m_dataFormat)))
1116   {
1117     return NULL;
1118   }
1119
1120   // create the stream
1121   CActiveAEStream *stream;
1122   stream = new CActiveAEStream(&streamMsg->format);
1123   stream->m_streamPort = new CActiveAEDataProtocol("stream",
1124                              &stream->m_inMsgEvent, &m_outMsgEvent);
1125
1126   // create buffer pool
1127   stream->m_inputBuffers = NULL; // create in Configure when we know the sink format
1128   stream->m_resampleBuffers = NULL; // create in Configure when we know the sink format
1129   stream->m_statsLock = m_stats.GetLock();
1130   stream->m_fadingSamples = 0;
1131   stream->m_started = false;
1132
1133   if (streamMsg->options & AESTREAM_PAUSED)
1134     stream->m_paused = true;
1135
1136   if (streamMsg->options & AESTREAM_FORCE_RESAMPLE)
1137     stream->m_forceResampler = true;
1138
1139   m_streams.push_back(stream);
1140
1141   return stream;
1142 }
1143
1144 void CActiveAE::DiscardStream(CActiveAEStream *stream)
1145 {
1146   std::list<CActiveAEStream*>::iterator it;
1147   for (it=m_streams.begin(); it!=m_streams.end(); )
1148   {
1149     if (stream == (*it))
1150     {
1151       while (!(*it)->m_processingSamples.empty())
1152       {
1153         (*it)->m_processingSamples.front()->Return();
1154         (*it)->m_processingSamples.pop_front();
1155       }
1156       m_discardBufferPools.push_back((*it)->m_inputBuffers);
1157       m_discardBufferPools.push_back((*it)->m_resampleBuffers);
1158       CLog::Log(LOGDEBUG, "CActiveAE::DiscardStream - audio stream deleted");
1159       delete (*it)->m_streamPort;
1160       delete (*it);
1161       it = m_streams.erase(it);
1162     }
1163     else
1164       ++it;
1165   }
1166
1167   ClearDiscardedBuffers();
1168 }
1169
1170 void CActiveAE::SFlushStream(CActiveAEStream *stream)
1171 {
1172   while (!stream->m_processingSamples.empty())
1173   {
1174     stream->m_processingSamples.front()->Return();
1175     stream->m_processingSamples.pop_front();
1176   }
1177   stream->m_resampleBuffers->Flush();
1178   stream->m_streamPort->Purge();
1179   stream->m_bufferedTime = 0.0;
1180   stream->m_paused = false;
1181
1182   // flush the engine if we only have a single stream
1183   if (m_streams.size() == 1)
1184   {
1185     FlushEngine();
1186   }
1187 }
1188
1189 void CActiveAE::FlushEngine()
1190 {
1191   if (m_sinkBuffers)
1192     m_sinkBuffers->Flush();
1193   if (m_vizBuffers)
1194     m_vizBuffers->Flush();
1195
1196   // send message to sink
1197   Message *reply;
1198   if (m_sink.m_controlPort.SendOutMessageSync(CSinkControlProtocol::FLUSH,
1199                                            &reply, 2000))
1200   {
1201     bool success = reply->signal == CSinkControlProtocol::ACC ? true : false;
1202     if (!success)
1203     {
1204       CLog::Log(LOGERROR, "ActiveAE::%s - returned error on flush", __FUNCTION__);
1205       m_extError = true;
1206     }
1207     reply->Release();
1208   }
1209   else
1210   {
1211     CLog::Log(LOGERROR, "ActiveAE::%s - failed to flush", __FUNCTION__);
1212     m_extError = true;
1213   }
1214   m_stats.Reset(m_sinkFormat.m_sampleRate);
1215 }
1216
1217 void CActiveAE::ClearDiscardedBuffers()
1218 {
1219   std::list<CActiveAEBufferPool*>::iterator it;
1220   for (it=m_discardBufferPools.begin(); it!=m_discardBufferPools.end(); ++it)
1221   {
1222     CActiveAEBufferPoolResample *rbuf = dynamic_cast<CActiveAEBufferPoolResample*>(*it);
1223     if (rbuf)
1224     {
1225       rbuf->Flush();
1226     }
1227     // if all buffers have returned, we can delete the buffer pool
1228     if ((*it)->m_allSamples.size() == (*it)->m_freeSamples.size())
1229     {
1230       delete (*it);
1231       CLog::Log(LOGDEBUG, "CActiveAE::ClearDiscardedBuffers - buffer pool deleted");
1232       m_discardBufferPools.erase(it);
1233       return;
1234     }
1235   }
1236 }
1237
1238 void CActiveAE::SStopSound(CActiveAESound *sound)
1239 {
1240   std::list<SoundState>::iterator it;
1241   for (it=m_sounds_playing.begin(); it!=m_sounds_playing.end(); ++it)
1242   {
1243     if (it->sound == sound)
1244     {
1245       m_sounds_playing.erase(it);
1246       return;
1247     }
1248   }
1249 }
1250
1251 void CActiveAE::DiscardSound(CActiveAESound *sound)
1252 {
1253   SStopSound(sound);
1254
1255   std::vector<CActiveAESound*>::iterator it;
1256   for (it=m_sounds.begin(); it!=m_sounds.end(); ++it)
1257   {
1258     if ((*it) == sound)
1259     {
1260       m_sounds.erase(it);
1261       return;
1262     }
1263   }
1264 }
1265
1266 void CActiveAE::ChangeResamplers()
1267 {
1268   std::list<CActiveAEStream*>::iterator it;
1269   for(it=m_streams.begin(); it!=m_streams.end(); ++it)
1270   {
1271     bool normalize = true;
1272     if (((*it)->m_resampleBuffers->m_format.m_channelLayout.Count() <
1273          (*it)->m_resampleBuffers->m_inputFormat.m_channelLayout.Count()) &&
1274          !m_settings.normalizelevels)
1275       normalize = false;
1276
1277     if ((*it)->m_resampleBuffers && (*it)->m_resampleBuffers->m_resampler &&
1278         (((*it)->m_resampleBuffers->m_resampleQuality != m_settings.resampleQuality) ||
1279         (((*it)->m_resampleBuffers->m_stereoUpmix != m_settings.stereoupmix)) ||
1280         ((*it)->m_resampleBuffers->m_normalize != normalize)))
1281     {
1282       (*it)->m_resampleBuffers->m_changeResampler = true;
1283     }
1284     (*it)->m_resampleBuffers->m_resampleQuality = m_settings.resampleQuality;
1285     (*it)->m_resampleBuffers->m_stereoUpmix = m_settings.stereoupmix;
1286     (*it)->m_resampleBuffers->m_normalize = normalize;
1287   }
1288 }
1289
1290 void CActiveAE::ApplySettingsToFormat(AEAudioFormat &format, AudioSettings &settings, int *mode)
1291 {
1292   int oldMode = m_mode;
1293   if (mode)
1294     *mode = MODE_PCM;
1295
1296   // raw pass through
1297   if (AE_IS_RAW(format.m_dataFormat))
1298   {
1299     if ((format.m_dataFormat == AE_FMT_AC3 && !settings.ac3passthrough) ||
1300         (format.m_dataFormat == AE_FMT_EAC3 && !settings.eac3passthrough) ||
1301         (format.m_dataFormat == AE_FMT_TRUEHD && !settings.truehdpassthrough) ||
1302         (format.m_dataFormat == AE_FMT_DTS && !settings.dtspassthrough) ||
1303         (format.m_dataFormat == AE_FMT_DTSHD && !settings.dtshdpassthrough))
1304     {
1305       CLog::Log(LOGERROR, "CActiveAE::ApplySettingsToFormat - input audio format is wrong");
1306     }
1307     if (mode)
1308       *mode = MODE_RAW;
1309   }
1310   // transcode
1311   else if (settings.channels <= AE_CH_LAYOUT_2_0 && // no multichannel pcm
1312            settings.passthrough &&
1313            settings.ac3passthrough &&
1314            !m_streams.empty() &&
1315            (format.m_channelLayout.Count() > 2 || settings.stereoupmix))
1316   {
1317     format.m_dataFormat = AE_FMT_AC3;
1318     format.m_sampleRate = 48000;
1319     format.m_channelLayout = AE_CH_LAYOUT_2_0;
1320     if (mode)
1321       *mode = MODE_TRANSCODE;
1322   }
1323   else
1324   {
1325     format.m_dataFormat = AE_FMT_FLOAT;
1326     // consider user channel layout for those cases
1327     // 1. input stream is multichannel
1328     // 2. stereo upmix is selected
1329     // 3. fixed mode
1330     if ((format.m_channelLayout.Count() > 2) ||
1331          settings.stereoupmix ||
1332          (settings.config == AE_CONFIG_FIXED))
1333     {
1334       CAEChannelInfo stdLayout;
1335       switch (settings.channels)
1336       {
1337         default:
1338         case  0: stdLayout = AE_CH_LAYOUT_2_0; break;
1339         case  1: stdLayout = AE_CH_LAYOUT_2_0; break;
1340         case  2: stdLayout = AE_CH_LAYOUT_2_1; break;
1341         case  3: stdLayout = AE_CH_LAYOUT_3_0; break;
1342         case  4: stdLayout = AE_CH_LAYOUT_3_1; break;
1343         case  5: stdLayout = AE_CH_LAYOUT_4_0; break;
1344         case  6: stdLayout = AE_CH_LAYOUT_4_1; break;
1345         case  7: stdLayout = AE_CH_LAYOUT_5_0; break;
1346         case  8: stdLayout = AE_CH_LAYOUT_5_1; break;
1347         case  9: stdLayout = AE_CH_LAYOUT_7_0; break;
1348         case 10: stdLayout = AE_CH_LAYOUT_7_1; break;
1349       }
1350
1351       if (m_settings.config == AE_CONFIG_FIXED || (settings.stereoupmix && format.m_channelLayout.Count() <= 2))
1352         format.m_channelLayout = stdLayout;
1353       else if (m_extKeepConfig && (settings.config == AE_CONFIG_AUTO) && (oldMode != MODE_RAW))
1354         format.m_channelLayout = m_internalFormat.m_channelLayout;
1355       else
1356         format.m_channelLayout.ResolveChannels(stdLayout);
1357     }
1358     // don't change from multi to stereo in AUTO mode
1359     else if ((settings.config == AE_CONFIG_AUTO) &&
1360               m_stats.GetWaterLevel() > 0 && m_internalFormat.m_channelLayout.Count() > 2)
1361     {
1362       format.m_channelLayout = m_internalFormat.m_channelLayout;
1363     }
1364
1365     if (m_sink.GetDeviceType(m_settings.device) == AE_DEVTYPE_IEC958)
1366     {
1367       if (format.m_sampleRate > m_settings.samplerate)
1368       {
1369         format.m_sampleRate = m_settings.samplerate;
1370         CLog::Log(LOGINFO, "CActiveAE::ApplySettings - limit samplerate for SPDIF to %d", format.m_sampleRate);
1371       }
1372       format.m_channelLayout = AE_CH_LAYOUT_2_0;
1373     }
1374
1375     if (m_settings.config == AE_CONFIG_FIXED)
1376     {
1377       format.m_sampleRate = m_settings.samplerate;
1378       CLog::Log(LOGINFO, "CActiveAE::ApplySettings - Forcing samplerate to %d", format.m_sampleRate);
1379     }
1380
1381     // sinks may not support mono
1382     if (format.m_channelLayout.Count() == 1)
1383     {
1384       format.m_channelLayout = AE_CH_LAYOUT_2_0;
1385     }
1386   }
1387 }
1388
1389 bool CActiveAE::NeedReconfigureBuffers()
1390 {
1391   AEAudioFormat newFormat = GetInputFormat();
1392   ApplySettingsToFormat(newFormat, m_settings);
1393
1394   if (newFormat.m_dataFormat != m_sinkRequestFormat.m_dataFormat ||
1395       newFormat.m_channelLayout != m_sinkRequestFormat.m_channelLayout ||
1396       newFormat.m_sampleRate != m_sinkRequestFormat.m_sampleRate)
1397     return true;
1398
1399   return false;
1400 }
1401
1402 bool CActiveAE::NeedReconfigureSink()
1403 {
1404   AEAudioFormat newFormat = GetInputFormat();
1405   ApplySettingsToFormat(newFormat, m_settings);
1406
1407   std::string device = AE_IS_RAW(newFormat.m_dataFormat) ? m_settings.passthoughdevice : m_settings.device;
1408   std::string driver;
1409   CAESinkFactory::ParseDevice(device, driver);
1410
1411   if (!CompareFormat(newFormat, m_sinkFormat) ||
1412       m_currDevice.compare(device) != 0 ||
1413       m_settings.driver.compare(driver) != 0)
1414     return true;
1415
1416   return false;
1417 }
1418
1419 bool CActiveAE::InitSink()
1420 {
1421   SinkConfig config;
1422   config.format = m_sinkRequestFormat;
1423   config.stats = &m_stats;
1424   config.device = AE_IS_RAW(m_sinkRequestFormat.m_dataFormat) ? &m_settings.passthoughdevice :
1425                                                                 &m_settings.device;
1426
1427   // send message to sink
1428   Message *reply;
1429   if (m_sink.m_controlPort.SendOutMessageSync(CSinkControlProtocol::CONFIGURE,
1430                                                  &reply,
1431                                                  5000,
1432                                                  &config, sizeof(config)))
1433   {
1434     bool success = reply->signal == CSinkControlProtocol::ACC ? true : false;
1435     if (!success)
1436     {
1437       reply->Release();
1438       CLog::Log(LOGERROR, "ActiveAE::%s - returned error", __FUNCTION__);
1439       m_extError = true;
1440       return false;
1441     }
1442     AEAudioFormat *data;
1443     data = (AEAudioFormat*)reply->data;
1444     if (data)
1445     {
1446       m_sinkFormat = *data;
1447     }
1448     m_sinkHasVolume = m_sink.HasVolume();
1449     reply->Release();
1450   }
1451   else
1452   {
1453     CLog::Log(LOGERROR, "ActiveAE::%s - failed to init", __FUNCTION__);
1454     m_extError = true;
1455     return false;
1456   }
1457
1458   m_inMsgEvent.Reset();
1459   return true;
1460 }
1461
1462 void CActiveAE::DrainSink()
1463 {
1464   // send message to sink
1465   Message *reply;
1466   if (m_sink.m_dataPort.SendOutMessageSync(CSinkDataProtocol::DRAIN,
1467                                                  &reply,
1468                                                  2000))
1469   {
1470     bool success = reply->signal == CSinkDataProtocol::ACC ? true : false;
1471     if (!success)
1472     {
1473       reply->Release();
1474       CLog::Log(LOGERROR, "ActiveAE::%s - returned error on drain", __FUNCTION__);
1475       m_extError = true;
1476       return;
1477     }
1478     reply->Release();
1479   }
1480   else
1481   {
1482     CLog::Log(LOGERROR, "ActiveAE::%s - failed to drain", __FUNCTION__);
1483     m_extError = true;
1484     return;
1485   }
1486 }
1487
1488 void CActiveAE::UnconfigureSink()
1489 {
1490   // send message to sink
1491   Message *reply;
1492   if (m_sink.m_controlPort.SendOutMessageSync(CSinkControlProtocol::UNCONFIGURE,
1493                                                  &reply,
1494                                                  2000))
1495   {
1496     bool success = reply->signal == CSinkControlProtocol::ACC ? true : false;
1497     if (!success)
1498     {
1499       CLog::Log(LOGERROR, "ActiveAE::%s - returned error", __FUNCTION__);
1500       m_extError = true;
1501     }
1502     reply->Release();
1503   }
1504   else
1505   {
1506     CLog::Log(LOGERROR, "ActiveAE::%s - failed to unconfigure", __FUNCTION__);
1507     m_extError = true;
1508   }
1509
1510   m_inMsgEvent.Reset();
1511 }
1512
1513
1514 bool CActiveAE::RunStages()
1515 {
1516   bool busy = false;
1517
1518   // serve input streams
1519   std::list<CActiveAEStream*>::iterator it;
1520   for (it = m_streams.begin(); it != m_streams.end(); ++it)
1521   {
1522     if ((*it)->m_resampleBuffers && !(*it)->m_paused)
1523       busy = (*it)->m_resampleBuffers->ResampleBuffers();
1524     else if ((*it)->m_resampleBuffers && 
1525             ((*it)->m_resampleBuffers->m_inputSamples.size() > (*it)->m_resampleBuffers->m_allSamples.size() * 0.5))
1526     {
1527       CSingleLock lock((*it)->m_streamLock);
1528       (*it)->m_streamIsBuffering = false;
1529     }
1530
1531     // provide buffers to stream
1532     float time = m_stats.GetCacheTime((*it));
1533     CSampleBuffer *buffer;
1534     if (!(*it)->m_drain)
1535     {
1536       while (time < MAX_CACHE_LEVEL && !(*it)->m_inputBuffers->m_freeSamples.empty())
1537       {
1538         buffer = (*it)->m_inputBuffers->GetFreeBuffer();
1539         (*it)->m_processingSamples.push_back(buffer);
1540         (*it)->m_streamPort->SendInMessage(CActiveAEDataProtocol::STREAMBUFFER, &buffer, sizeof(CSampleBuffer*));
1541         (*it)->IncFreeBuffers();
1542         time += (float)buffer->pkt->max_nb_samples / buffer->pkt->config.sample_rate;
1543       }
1544     }
1545     else
1546     {
1547       if ((*it)->m_resampleBuffers->m_inputSamples.empty() &&
1548           (*it)->m_resampleBuffers->m_outputSamples.empty() &&
1549           (*it)->m_processingSamples.empty())
1550       {
1551         (*it)->m_streamPort->SendInMessage(CActiveAEDataProtocol::STREAMDRAINED);
1552         (*it)->m_drain = false;
1553         (*it)->m_resampleBuffers->m_drain = false;
1554         (*it)->m_started = false;
1555
1556         // set variables being polled via stream interface
1557         CSingleLock lock((*it)->m_streamLock);
1558         if ((*it)->m_streamSlave)
1559         {
1560           CActiveAEStream *slave = (CActiveAEStream*)((*it)->m_streamSlave);
1561           slave->m_paused = false;
1562
1563           // TODO: find better solution for this
1564           // gapless bites audiophile
1565           if (m_settings.config == AE_CONFIG_MATCH)
1566             Configure(&slave->m_format);
1567
1568           (*it)->m_streamSlave = NULL;
1569         }
1570         (*it)->m_streamDrained = true;
1571         (*it)->m_streamDraining = false;
1572         (*it)->m_streamFading = false;
1573       }
1574     }
1575   }
1576
1577   if (m_stats.GetWaterLevel() < MAX_WATER_LEVEL &&
1578      (m_mode != MODE_TRANSCODE || (m_encoderBuffers && !m_encoderBuffers->m_freeSamples.empty())))
1579   {
1580     // mix streams and sounds sounds
1581     if (m_mode != MODE_RAW)
1582     {
1583       CSampleBuffer *out = NULL;
1584       if (!m_sounds_playing.empty() && m_streams.empty())
1585       {
1586         if (m_silenceBuffers && !m_silenceBuffers->m_freeSamples.empty())
1587         {
1588           out = m_silenceBuffers->GetFreeBuffer();
1589           for (int i=0; i<out->pkt->planes; i++)
1590           {
1591             memset(out->pkt->data[i], 0, out->pkt->linesize);
1592           }
1593           out->pkt->nb_samples = out->pkt->max_nb_samples;
1594         }
1595       }
1596
1597       // mix streams
1598       std::list<CActiveAEStream*>::iterator it;
1599
1600       // if we deal with more than a single stream, all streams
1601       // must provide samples for mixing
1602       bool allStreamsReady = true;
1603       for (it = m_streams.begin(); it != m_streams.end(); ++it)
1604       {
1605         if ((*it)->m_paused || !(*it)->m_started || !(*it)->m_resampleBuffers)
1606           continue;
1607
1608         if ((*it)->m_resampleBuffers->m_outputSamples.empty())
1609           allStreamsReady = false;
1610       }
1611
1612       bool needClamp = false;
1613       for (it = m_streams.begin(); it != m_streams.end() && allStreamsReady; ++it)
1614       {
1615         if ((*it)->m_paused || !(*it)->m_resampleBuffers)
1616           continue;
1617
1618         if (!(*it)->m_resampleBuffers->m_outputSamples.empty())
1619         {
1620           (*it)->m_started = true;
1621
1622           if (!out)
1623           {
1624             out = (*it)->m_resampleBuffers->m_outputSamples.front();
1625             (*it)->m_resampleBuffers->m_outputSamples.pop_front();
1626
1627             int nb_floats = out->pkt->nb_samples * out->pkt->config.channels / out->pkt->planes;
1628             int nb_loops = 1;
1629             float fadingStep = 0.0f;
1630
1631             // fading
1632             if ((*it)->m_fadingSamples == -1)
1633             {
1634               (*it)->m_fadingSamples = m_internalFormat.m_sampleRate * (float)(*it)->m_fadingTime / 1000.0f;
1635               (*it)->m_volume = (*it)->m_fadingBase;
1636             }
1637             if ((*it)->m_fadingSamples > 0)
1638             {
1639               nb_floats = out->pkt->config.channels / out->pkt->planes;
1640               nb_loops = out->pkt->nb_samples;
1641               float delta = (*it)->m_fadingTarget - (*it)->m_fadingBase;
1642               int samples = m_internalFormat.m_sampleRate * (float)(*it)->m_fadingTime / 1000.0f;
1643               fadingStep = delta / samples;
1644             }
1645
1646             // for stream amplification, 
1647             // turned off downmix normalization,
1648             // or if sink format is float (in order to prevent from clipping)
1649             // we need to run on a per sample basis
1650             if ((*it)->m_amplify != 1.0 || !(*it)->m_resampleBuffers->m_normalize || (m_sinkFormat.m_dataFormat == AE_FMT_FLOAT))
1651             {
1652               nb_floats = out->pkt->config.channels / out->pkt->planes;
1653               nb_loops = out->pkt->nb_samples;
1654             }
1655
1656             for(int i=0; i<nb_loops; i++)
1657             {
1658               if ((*it)->m_fadingSamples > 0)
1659               {
1660                 (*it)->m_volume += fadingStep;
1661                 (*it)->m_fadingSamples--;
1662
1663                 if ((*it)->m_fadingSamples == 0)
1664                 {
1665                   // set variables being polled via stream interface
1666                   CSingleLock lock((*it)->m_streamLock);
1667                   (*it)->m_streamFading = false;
1668                 }
1669               }
1670
1671               // volume for stream
1672               float volume = (*it)->m_volume * (*it)->m_rgain;
1673               if(nb_loops > 1)
1674                 volume *= (*it)->m_limiter.Run((float**)out->pkt->data, out->pkt->config.channels, i*nb_floats, out->pkt->planes > 1);
1675
1676               for(int j=0; j<out->pkt->planes; j++)
1677               {
1678 #ifdef __SSE__
1679                 CAEUtil::SSEMulArray((float*)out->pkt->data[j]+i*nb_floats, volume, nb_floats);
1680 #else
1681                 float* fbuffer = (float*) out->pkt->data[j]+i*nb_floats;
1682                 for (int k = 0; k < nb_floats; ++k)
1683                 {
1684                   fbuffer[k] *= volume;
1685                 }
1686 #endif
1687               }
1688             }
1689           }
1690           else
1691           {
1692             CSampleBuffer *mix = NULL;
1693             mix = (*it)->m_resampleBuffers->m_outputSamples.front();
1694             (*it)->m_resampleBuffers->m_outputSamples.pop_front();
1695
1696             int nb_floats = mix->pkt->nb_samples * mix->pkt->config.channels / mix->pkt->planes;
1697             int nb_loops = 1;
1698             float fadingStep = 0.0f;
1699
1700             // fading
1701             if ((*it)->m_fadingSamples == -1)
1702             {
1703               (*it)->m_fadingSamples = m_internalFormat.m_sampleRate * (float)(*it)->m_fadingTime / 1000.0f;
1704               (*it)->m_volume = (*it)->m_fadingBase;
1705             }
1706             if ((*it)->m_fadingSamples > 0)
1707             {
1708               nb_floats = mix->pkt->config.channels / mix->pkt->planes;
1709               nb_loops = mix->pkt->nb_samples;
1710               float delta = (*it)->m_fadingTarget - (*it)->m_fadingBase;
1711               int samples = m_internalFormat.m_sampleRate * (float)(*it)->m_fadingTime / 1000.0f;
1712               fadingStep = delta / samples;
1713             }
1714
1715             // for streams amplification of turned off downmix normalization
1716             // we need to run on a per sample basis
1717             if ((*it)->m_amplify != 1.0 || !(*it)->m_resampleBuffers->m_normalize)
1718             {
1719               nb_floats = out->pkt->config.channels / out->pkt->planes;
1720               nb_loops = out->pkt->nb_samples;
1721             }
1722
1723             for(int i=0; i<nb_loops; i++)
1724             {
1725               if ((*it)->m_fadingSamples > 0)
1726               {
1727                 (*it)->m_volume += fadingStep;
1728                 (*it)->m_fadingSamples--;
1729
1730                 if ((*it)->m_fadingSamples == 0)
1731                 {
1732                   // set variables being polled via stream interface
1733                   CSingleLock lock((*it)->m_streamLock);
1734                   (*it)->m_streamFading = false;
1735                 }
1736               }
1737
1738               // volume for stream
1739               float volume = (*it)->m_volume * (*it)->m_rgain;
1740               if(nb_loops > 1)
1741                 volume *= (*it)->m_limiter.Run((float**)mix->pkt->data, mix->pkt->config.channels, i*nb_floats, mix->pkt->planes > 1);
1742
1743               for(int j=0; j<out->pkt->planes && j<mix->pkt->planes; j++)
1744               {
1745                 float *dst = (float*)out->pkt->data[j]+i*nb_floats;
1746                 float *src = (float*)mix->pkt->data[j]+i*nb_floats;
1747 #ifdef __SSE__
1748                 CAEUtil::SSEMulAddArray(dst, src, volume, nb_floats);
1749                 for (int k = 0; k < nb_floats; ++k)
1750                 {
1751                   if (fabs(dst[k]) > 1.0f)
1752                   {
1753                     needClamp = true;
1754                     break;
1755                   }
1756                 }
1757 #else
1758                 for (int k = 0; k < nb_floats; ++k)
1759                 {
1760                   dst[k] += src[k] * volume;
1761                   if (fabs(dst[k]) > 1.0f)
1762                     needClamp = true;
1763                 }
1764 #endif
1765               }
1766             }
1767             mix->Return();
1768           }
1769           busy = true;
1770         }
1771       }// for
1772
1773       // finally clamp samples
1774       if(out && needClamp)
1775       {
1776         int nb_floats = out->pkt->nb_samples * out->pkt->config.channels / out->pkt->planes;
1777         for(int i=0; i<out->pkt->planes; i++)
1778         {
1779           CAEUtil::ClampArray((float*)out->pkt->data[i], nb_floats);
1780         }
1781       }
1782
1783       // process output buffer, gui sounds, encode, viz
1784       if (out)
1785       {
1786         // viz
1787         {
1788           CSingleLock lock(m_vizLock);
1789           if (m_audioCallback && m_vizBuffers && !m_streams.empty())
1790           {
1791             if (!m_vizInitialized)
1792             {
1793               m_audioCallback->OnInitialize(2, m_vizBuffers->m_format.m_sampleRate, 32);
1794               m_vizInitialized = true;
1795             }
1796
1797             if (!m_vizBuffersInput->m_freeSamples.empty())
1798             {
1799               // copy the samples into the viz input buffer
1800               CSampleBuffer *viz = m_vizBuffersInput->GetFreeBuffer();
1801               int samples = std::min(512, out->pkt->nb_samples);
1802               int bytes = samples * out->pkt->config.channels / out->pkt->planes * out->pkt->bytes_per_sample;
1803               for(int i= 0; i < out->pkt->planes; i++)
1804               {
1805                 memcpy(viz->pkt->data[i], out->pkt->data[i], bytes);
1806               }
1807               viz->pkt->nb_samples = samples;
1808               m_vizBuffers->m_inputSamples.push_back(viz);
1809             }
1810             else
1811               CLog::Log(LOGWARNING,"ActiveAE::%s - viz ran out of free buffers", __FUNCTION__);
1812             unsigned int now = XbmcThreads::SystemClockMillis();
1813             unsigned int timestamp = now + m_stats.GetDelay() * 1000;
1814             busy |= m_vizBuffers->ResampleBuffers(timestamp);
1815             while(!m_vizBuffers->m_outputSamples.empty())
1816             {
1817               CSampleBuffer *buf = m_vizBuffers->m_outputSamples.front();
1818               if ((now - buf->timestamp) & 0x80000000)
1819                 break;
1820               else
1821               {
1822                 int samples;
1823                 samples = std::min(512, buf->pkt->nb_samples);
1824                 m_audioCallback->OnAudioData((float*)(buf->pkt->data[0]), samples);
1825                 buf->Return();
1826                 m_vizBuffers->m_outputSamples.pop_front();
1827               }
1828             }
1829           }
1830           else if (m_vizBuffers)
1831             m_vizBuffers->Flush();
1832         }
1833
1834         // mix gui sounds
1835         MixSounds(*(out->pkt));
1836         if (!m_sinkHasVolume || m_muted)
1837           Deamplify(*(out->pkt));
1838
1839         if (m_mode == MODE_TRANSCODE && m_encoder)
1840         {
1841           CSampleBuffer *buf = m_encoderBuffers->GetFreeBuffer();
1842           m_encoder->Encode(out->pkt->data[0], out->pkt->planes*out->pkt->linesize,
1843                             buf->pkt->data[0], buf->pkt->planes*buf->pkt->linesize);
1844           buf->pkt->nb_samples = buf->pkt->max_nb_samples;
1845           out->Return();
1846           out = buf;
1847         }
1848         busy = true;
1849       }
1850
1851       // update stats
1852       if(out)
1853       {
1854         m_stats.AddSamples(out->pkt->nb_samples, m_streams);
1855         m_sinkBuffers->m_inputSamples.push_back(out);
1856       }
1857     }
1858     // pass through
1859     else
1860     {
1861       std::list<CActiveAEStream*>::iterator it;
1862       CSampleBuffer *buffer;
1863       for (it = m_streams.begin(); it != m_streams.end(); ++it)
1864       {
1865         if (!(*it)->m_resampleBuffers->m_outputSamples.empty())
1866         {
1867           buffer =  (*it)->m_resampleBuffers->m_outputSamples.front();
1868           (*it)->m_resampleBuffers->m_outputSamples.pop_front();
1869           m_stats.AddSamples(buffer->pkt->nb_samples, m_streams);
1870           m_sinkBuffers->m_inputSamples.push_back(buffer);
1871         }
1872       }
1873     }
1874
1875     // serve sink buffers
1876     busy = m_sinkBuffers->ResampleBuffers();
1877     while(!m_sinkBuffers->m_outputSamples.empty())
1878     {
1879       CSampleBuffer *out = NULL;
1880       out = m_sinkBuffers->m_outputSamples.front();
1881       m_sinkBuffers->m_outputSamples.pop_front();
1882       m_sink.m_dataPort.SendOutMessage(CSinkDataProtocol::SAMPLE,
1883           &out, sizeof(CSampleBuffer*));
1884       busy = true;
1885     }
1886   }
1887
1888   return busy;
1889 }
1890
1891 bool CActiveAE::HasWork()
1892 {
1893   if (!m_sounds_playing.empty())
1894     return true;
1895   if (!m_sinkBuffers->m_inputSamples.empty())
1896     return true;
1897   if (!m_sinkBuffers->m_outputSamples.empty())
1898     return true;
1899
1900   std::list<CActiveAEStream*>::iterator it;
1901   for (it = m_streams.begin(); it != m_streams.end(); ++it)
1902   {
1903     if (!(*it)->m_resampleBuffers->m_inputSamples.empty())
1904       return true;
1905     if (!(*it)->m_resampleBuffers->m_outputSamples.empty())
1906       return true;
1907     if (!(*it)->m_processingSamples.empty())
1908       return true;
1909   }
1910
1911   return false;
1912 }
1913
1914 void CActiveAE::MixSounds(CSoundPacket &dstSample)
1915 {
1916   if (m_sounds_playing.empty())
1917     return;
1918
1919   float volume;
1920   float *out;
1921   float *sample_buffer;
1922   int max_samples = dstSample.nb_samples;
1923
1924   std::list<SoundState>::iterator it;
1925   for (it = m_sounds_playing.begin(); it != m_sounds_playing.end(); )
1926   {
1927     if (!it->sound->IsConverted())
1928       ResampleSound(it->sound);
1929     int available_samples = it->sound->GetSound(false)->nb_samples - it->samples_played;
1930     int mix_samples = std::min(max_samples, available_samples);
1931     int start = it->samples_played *
1932                 m_dllAvUtil.av_get_bytes_per_sample(it->sound->GetSound(false)->config.fmt) *
1933                 it->sound->GetSound(false)->config.channels /
1934                 it->sound->GetSound(false)->planes;
1935
1936     for(int j=0; j<dstSample.planes; j++)
1937     {
1938       volume = it->sound->GetVolume();
1939       out = (float*)dstSample.data[j];
1940       sample_buffer = (float*)(it->sound->GetSound(false)->data[j]+start);
1941       int nb_floats = mix_samples * dstSample.config.channels / dstSample.planes;
1942 #ifdef __SSE__
1943       CAEUtil::SSEMulAddArray(out, sample_buffer, volume, nb_floats);
1944 #else
1945       for (int k = 0; k < nb_floats; ++k)
1946         *out++ += *sample_buffer++ * volume;
1947 #endif
1948     }
1949
1950     it->samples_played += mix_samples;
1951
1952     // no more frames, so remove it from the list
1953     if (it->samples_played >= it->sound->GetSound(false)->nb_samples)
1954     {
1955       it = m_sounds_playing.erase(it);
1956       continue;
1957     }
1958     ++it;
1959   }
1960 }
1961
1962 void CActiveAE::Deamplify(CSoundPacket &dstSample)
1963 {
1964   if (m_volume < 1.0 || m_muted)
1965   {
1966     float *buffer;
1967     int nb_floats = dstSample.nb_samples * dstSample.config.channels / dstSample.planes;
1968
1969     for(int j=0; j<dstSample.planes; j++)
1970     {
1971       buffer = (float*)dstSample.data[j];
1972 #ifdef __SSE__
1973       CAEUtil::SSEMulArray(buffer, m_muted ? 0.0 : m_volume, nb_floats);
1974 #else
1975       float *fbuffer = buffer;
1976       for (int i = 0; i < nb_floats; i++)
1977         *fbuffer++ *= m_volume;
1978 #endif
1979     }
1980   }
1981 }
1982
1983 //-----------------------------------------------------------------------------
1984 // Configuration
1985 //-----------------------------------------------------------------------------
1986
1987 void CActiveAE::LoadSettings()
1988 {
1989   m_settings.device = CSettings::Get().GetString("audiooutput.audiodevice");
1990   m_settings.passthoughdevice = CSettings::Get().GetString("audiooutput.passthroughdevice");
1991
1992   m_settings.config = CSettings::Get().GetInt("audiooutput.config");
1993   m_settings.channels = (m_sink.GetDeviceType(m_settings.device) == AE_DEVTYPE_IEC958) ? AE_CH_LAYOUT_2_0 : CSettings::Get().GetInt("audiooutput.channels");
1994   m_settings.samplerate = CSettings::Get().GetInt("audiooutput.samplerate");
1995
1996   m_settings.stereoupmix = IsSettingVisible("audiooutput.stereoupmix") ? CSettings::Get().GetBool("audiooutput.stereoupmix") : false;
1997   m_settings.normalizelevels = CSettings::Get().GetBool("audiooutput.normalizelevels");
1998   m_settings.guisoundmode = CSettings::Get().GetInt("audiooutput.guisoundmode");
1999
2000   m_settings.passthrough = m_settings.config == AE_CONFIG_FIXED ? false : CSettings::Get().GetBool("audiooutput.passthrough");
2001   m_settings.ac3passthrough = CSettings::Get().GetBool("audiooutput.ac3passthrough");
2002   m_settings.eac3passthrough = CSettings::Get().GetBool("audiooutput.eac3passthrough");
2003   m_settings.truehdpassthrough = CSettings::Get().GetBool("audiooutput.truehdpassthrough");
2004   m_settings.dtspassthrough = CSettings::Get().GetBool("audiooutput.dtspassthrough");
2005   m_settings.dtshdpassthrough = CSettings::Get().GetBool("audiooutput.dtshdpassthrough");
2006
2007   m_settings.resampleQuality = static_cast<AEQuality>(CSettings::Get().GetInt("audiooutput.processquality"));
2008 }
2009
2010 bool CActiveAE::Initialize()
2011 {
2012   if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllAvFormat.Load())
2013   {
2014     CLog::Log(LOGERROR,"CActiveAE::Initialize - failed to load ffmpeg libraries");
2015     return false;
2016   }
2017   m_dllAvFormat.av_register_all();
2018
2019   Create();
2020   Message *reply;
2021   if (m_controlPort.SendOutMessageSync(CActiveAEControlProtocol::INIT,
2022                                                  &reply,
2023                                                  5000))
2024   {
2025     bool success = reply->signal == CActiveAEControlProtocol::ACC ? true : false;
2026     reply->Release();
2027     if (!success)
2028     {
2029       CLog::Log(LOGERROR, "ActiveAE::%s - returned error", __FUNCTION__);
2030       Dispose();
2031       return false;
2032     }
2033   }
2034   else
2035   {
2036     CLog::Log(LOGERROR, "ActiveAE::%s - failed to init", __FUNCTION__);
2037     Dispose();
2038     return false;
2039   }
2040
2041   // hook into windowing for receiving display reset events
2042 #if defined(HAS_GLX) || defined(TARGET_DARWIN_OSX)
2043   g_Windowing.Register(this);
2044 #endif
2045
2046   m_inMsgEvent.Reset();
2047   return true;
2048 }
2049
2050 void CActiveAE::EnumerateOutputDevices(AEDeviceList &devices, bool passthrough)
2051 {
2052   m_sink.EnumerateOutputDevices(devices, passthrough);
2053 }
2054
2055 std::string CActiveAE::GetDefaultDevice(bool passthrough)
2056 {
2057   return m_sink.GetDefaultDevice(passthrough);
2058 }
2059
2060 void CActiveAE::OnSettingsChange(const std::string& setting)
2061 {
2062   if (setting == "audiooutput.passthroughdevice" ||
2063       setting == "audiooutput.audiodevice"       ||
2064       setting == "audiooutput.config"            ||
2065       setting == "audiooutput.ac3passthrough"    ||
2066       setting == "audiooutput.eac3passthrough"   ||
2067       setting == "audiooutput.dtspassthrough"    ||
2068       setting == "audiooutput.truehdpassthrough" ||
2069       setting == "audiooutput.dtshdpassthrough"  ||
2070       setting == "audiooutput.channels"          ||
2071       setting == "audiooutput.stereoupmix"       ||
2072       setting == "audiooutput.streamsilence"     ||
2073       setting == "audiooutput.processquality"    ||
2074       setting == "audiooutput.passthrough"       ||
2075       setting == "audiooutput.samplerate"        ||
2076       setting == "audiooutput.normalizelevels"   ||
2077       setting == "audiooutput.guisoundmode")
2078   {
2079     m_controlPort.SendOutMessage(CActiveAEControlProtocol::RECONFIGURE);
2080   }
2081 }
2082
2083 bool CActiveAE::SupportsRaw(AEDataFormat format)
2084 {
2085   if (!m_sink.HasPassthroughDevice())
2086     return false;
2087
2088   // those formats require HDMI
2089   if (format == AE_FMT_DTSHD || format == AE_FMT_TRUEHD)
2090   {
2091     if(m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.passthroughdevice")) != AE_DEVTYPE_HDMI)
2092       return false;
2093   }
2094
2095   // TODO: check ELD?
2096   return true;
2097 }
2098
2099 bool CActiveAE::SupportsSilenceTimeout()
2100 {
2101   return true;
2102 }
2103
2104 bool CActiveAE::SupportsQualityLevel(enum AEQuality level)
2105 {
2106   if (level == AE_QUALITY_LOW || level == AE_QUALITY_MID || level == AE_QUALITY_HIGH)
2107     return true;
2108
2109   return false;
2110 }
2111
2112 bool CActiveAE::IsSettingVisible(const std::string &settingId)
2113 {
2114   if (settingId == "audiooutput.samplerate")
2115   {
2116     if (m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.audiodevice")) == AE_DEVTYPE_IEC958)
2117       return true;
2118     if (CSettings::Get().GetInt("audiooutput.config") == AE_CONFIG_FIXED)
2119       return true;
2120   }
2121   else if (settingId == "audiooutput.channels")
2122   {
2123     if (m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.audiodevice")) != AE_DEVTYPE_IEC958)
2124       return true;
2125   }
2126   else if (settingId == "audiooutput.passthrough")
2127   {
2128     if (m_sink.HasPassthroughDevice() && CSettings::Get().GetInt("audiooutput.config") != AE_CONFIG_FIXED)
2129       return true;
2130   }
2131   else if (settingId == "audiooutput.truehdpassthrough")
2132   {
2133     if (m_sink.HasPassthroughDevice() &&
2134         CSettings::Get().GetInt("audiooutput.config") != AE_CONFIG_FIXED &&
2135         m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.passthroughdevice")) == AE_DEVTYPE_HDMI)
2136       return true;
2137   }
2138   else if (settingId == "audiooutput.dtshdpassthrough")
2139   {
2140     if (m_sink.HasPassthroughDevice() &&
2141         CSettings::Get().GetInt("audiooutput.config") != AE_CONFIG_FIXED &&
2142         m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.passthroughdevice")) == AE_DEVTYPE_HDMI)
2143       return true;
2144   }
2145   else if (settingId == "audiooutput.stereoupmix")
2146   {
2147     if (m_sink.GetDeviceType(CSettings::Get().GetString("audiooutput.audiodevice")) != AE_DEVTYPE_IEC958)
2148     {
2149       if (CSettings::Get().GetInt("audiooutput.channels") > AE_CH_LAYOUT_2_0)
2150         return true;
2151     }
2152     else
2153     {
2154       if (m_sink.HasPassthroughDevice() &&
2155           CSettings::Get().GetBool("audiooutput.passthrough") &&
2156           CSettings::Get().GetBool("audiooutput.ac3passthrough"))
2157         return true;
2158     }
2159   }
2160   return false;
2161 }
2162
2163 void CActiveAE::Shutdown()
2164 {
2165   Dispose();
2166 }
2167
2168 bool CActiveAE::Suspend()
2169 {
2170   return m_controlPort.SendOutMessage(CActiveAEControlProtocol::SUSPEND);
2171 }
2172
2173 bool CActiveAE::Resume()
2174 {
2175   Message *reply;
2176   if (m_controlPort.SendOutMessageSync(CActiveAEControlProtocol::INIT,
2177                                                  &reply,
2178                                                  5000))
2179   {
2180     bool success = reply->signal == CActiveAEControlProtocol::ACC ? true : false;
2181     reply->Release();
2182     if (!success)
2183     {
2184       CLog::Log(LOGERROR, "ActiveAE::%s - returned error", __FUNCTION__);
2185       return false;
2186     }
2187   }
2188   else
2189   {
2190     CLog::Log(LOGERROR, "ActiveAE::%s - failed to init", __FUNCTION__);
2191     return false;
2192   }
2193
2194   m_inMsgEvent.Reset();
2195   return true;
2196 }
2197
2198 bool CActiveAE::IsSuspended()
2199 {
2200   return m_stats.IsSuspended();
2201 }
2202
2203 float CActiveAE::GetVolume()
2204 {
2205   return m_aeVolume;
2206 }
2207
2208 void CActiveAE::SetVolume(const float volume)
2209 {
2210   m_aeVolume = std::max( 0.0f, std::min(1.0f, volume));
2211   m_controlPort.SendOutMessage(CActiveAEControlProtocol::VOLUME, &m_aeVolume, sizeof(float));
2212 }
2213
2214 void CActiveAE::SetMute(const bool enabled)
2215 {
2216   m_aeMuted = enabled;
2217   m_controlPort.SendOutMessage(CActiveAEControlProtocol::MUTE, &m_aeMuted, sizeof(bool));
2218 }
2219
2220 bool CActiveAE::IsMuted()
2221 {
2222   return m_aeMuted;
2223 }
2224
2225 void CActiveAE::SetSoundMode(const int mode)
2226 {
2227   return;
2228 }
2229
2230 void CActiveAE::KeepConfiguration(unsigned int millis)
2231 {
2232   unsigned int timeMs = millis;
2233   m_controlPort.SendOutMessage(CActiveAEControlProtocol::KEEPCONFIG, &timeMs, sizeof(unsigned int));
2234 }
2235
2236 void CActiveAE::OnLostDevice()
2237 {
2238 //  m_controlPort.SendOutMessage(CActiveAEControlProtocol::DISPLAYLOST);
2239 }
2240
2241 void CActiveAE::OnResetDevice()
2242 {
2243 //  m_controlPort.SendOutMessage(CActiveAEControlProtocol::DISPLAYRESET);
2244 }
2245
2246 //-----------------------------------------------------------------------------
2247 // Utils
2248 //-----------------------------------------------------------------------------
2249
2250 uint8_t **CActiveAE::AllocSoundSample(SampleConfig &config, int &samples, int &bytes_per_sample, int &planes, int &linesize)
2251 {
2252   uint8_t **buffer;
2253   planes = m_dllAvUtil.av_sample_fmt_is_planar(config.fmt) ? config.channels : 1;
2254   buffer = new uint8_t*[planes];
2255
2256   // align buffer to 16 in order to be compatible with sse in CAEConvert
2257   m_dllAvUtil.av_samples_alloc(buffer, &linesize, config.channels,
2258                                  samples, config.fmt, 16);
2259   bytes_per_sample = m_dllAvUtil.av_get_bytes_per_sample(config.fmt);
2260   return buffer;
2261 }
2262
2263 void CActiveAE::FreeSoundSample(uint8_t **data)
2264 {
2265   m_dllAvUtil.av_freep(data);
2266   delete [] data;
2267 }
2268
2269 bool CActiveAE::CompareFormat(AEAudioFormat &lhs, AEAudioFormat &rhs)
2270 {
2271   if (lhs.m_channelLayout != rhs.m_channelLayout ||
2272       lhs.m_dataFormat != rhs.m_dataFormat ||
2273       lhs.m_sampleRate != rhs.m_sampleRate ||
2274       lhs.m_frames != rhs.m_frames)
2275     return false;
2276   else
2277     return true;
2278 }
2279
2280 //-----------------------------------------------------------------------------
2281 // GUI Sounds
2282 //-----------------------------------------------------------------------------
2283
2284 /**
2285  * load sound from an audio file and store original format
2286  * register the sound in ActiveAE
2287  * later when the engine is idle it will convert the sound to sink format
2288  */
2289
2290 #define SOUNDBUFFER_SIZE 20480
2291
2292 IAESound *CActiveAE::MakeSound(const std::string& file)
2293 {
2294   AVFormatContext *fmt_ctx = NULL;
2295   AVCodecContext *dec_ctx = NULL;
2296   AVIOContext *io_ctx;
2297   AVInputFormat *io_fmt;
2298   AVCodec *dec = NULL;
2299   CActiveAESound *sound = NULL;
2300   SampleConfig config;
2301
2302   sound = new CActiveAESound(file);
2303   if (!sound->Prepare())
2304   {
2305     delete sound;
2306     return NULL;
2307   }
2308   int fileSize = sound->GetFileSize();
2309
2310   fmt_ctx = m_dllAvFormat.avformat_alloc_context();
2311   unsigned char* buffer = (unsigned char*)m_dllAvUtil.av_malloc(SOUNDBUFFER_SIZE+FF_INPUT_BUFFER_PADDING_SIZE);
2312   io_ctx = m_dllAvFormat.avio_alloc_context(buffer, SOUNDBUFFER_SIZE, 0,
2313                                             sound, CActiveAESound::Read, NULL, CActiveAESound::Seek);
2314   io_ctx->max_packet_size = sound->GetChunkSize();
2315   if(io_ctx->max_packet_size)
2316     io_ctx->max_packet_size *= SOUNDBUFFER_SIZE / io_ctx->max_packet_size;
2317
2318   if(!sound->IsSeekPosible())
2319     io_ctx->seekable = 0;
2320
2321   fmt_ctx->pb = io_ctx;
2322
2323   m_dllAvFormat.av_probe_input_buffer(io_ctx, &io_fmt, file.c_str(), NULL, 0, 0);
2324   if (!io_fmt)
2325   {
2326     m_dllAvFormat.avformat_close_input(&fmt_ctx);
2327     delete sound;
2328     return NULL;
2329   }
2330
2331   // find decoder
2332   if (m_dllAvFormat.avformat_open_input(&fmt_ctx, file.c_str(), NULL, NULL) == 0)
2333   {
2334     fmt_ctx->flags |= AVFMT_FLAG_NOPARSE;
2335     if (m_dllAvFormat.avformat_find_stream_info(fmt_ctx, NULL) >= 0)
2336     {
2337       dec_ctx = fmt_ctx->streams[0]->codec;
2338       dec = m_dllAvCodec.avcodec_find_decoder(dec_ctx->codec_id);
2339       config.sample_rate = dec_ctx->sample_rate;
2340       config.channels = dec_ctx->channels;
2341       config.channel_layout = dec_ctx->channel_layout;
2342     }
2343   }
2344   if (dec == NULL)
2345   {
2346     m_dllAvFormat.avformat_close_input(&fmt_ctx);
2347     delete sound;
2348     return NULL;
2349   }
2350
2351   dec_ctx = m_dllAvCodec.avcodec_alloc_context3(dec);
2352   dec_ctx->sample_rate = config.sample_rate;
2353   dec_ctx->channels = config.channels;
2354   if (!config.channel_layout)
2355     config.channel_layout = m_dllAvUtil.av_get_default_channel_layout(config.channels);
2356   dec_ctx->channel_layout = config.channel_layout;
2357
2358   AVPacket avpkt;
2359   AVFrame *decoded_frame = NULL;
2360   decoded_frame = m_dllAvCodec.avcodec_alloc_frame();
2361
2362   if (m_dllAvCodec.avcodec_open2(dec_ctx, dec, NULL) >= 0)
2363   {
2364     bool init = false;
2365
2366     // decode until eof
2367     m_dllAvCodec.av_init_packet(&avpkt);
2368     int len;
2369     while (m_dllAvFormat.av_read_frame(fmt_ctx, &avpkt) >= 0)
2370     {
2371       int got_frame = 0;
2372       len = m_dllAvCodec.avcodec_decode_audio4(dec_ctx, decoded_frame, &got_frame, &avpkt);
2373       if (len < 0)
2374       {
2375         m_dllAvCodec.avcodec_close(dec_ctx);
2376         m_dllAvUtil.av_free(dec_ctx);
2377         m_dllAvUtil.av_free(&decoded_frame);
2378         m_dllAvFormat.avformat_close_input(&fmt_ctx);
2379         delete sound;
2380         return NULL;
2381       }
2382       if (got_frame)
2383       {
2384         if (!init)
2385         {
2386           int samples = fileSize / m_dllAvUtil.av_get_bytes_per_sample(dec_ctx->sample_fmt) / config.channels;
2387           config.fmt = dec_ctx->sample_fmt;
2388           config.bits_per_sample = dec_ctx->bits_per_coded_sample;
2389           sound->InitSound(true, config, samples);
2390           init = true;
2391         }
2392         sound->StoreSound(true, decoded_frame->extended_data,
2393                           decoded_frame->nb_samples, decoded_frame->linesize[0]);
2394       }
2395     }
2396     m_dllAvCodec.avcodec_close(dec_ctx);
2397   }
2398
2399   m_dllAvUtil.av_free(dec_ctx);
2400   m_dllAvUtil.av_free(decoded_frame);
2401   m_dllAvFormat.avformat_close_input(&fmt_ctx);
2402
2403   sound->Finish();
2404
2405   // register sound
2406   m_dataPort.SendOutMessage(CActiveAEDataProtocol::NEWSOUND, &sound, sizeof(CActiveAESound*));
2407
2408   return sound;
2409 }
2410
2411 void CActiveAE::FreeSound(IAESound *sound)
2412 {
2413   m_dataPort.SendOutMessage(CActiveAEDataProtocol::FREESOUND, &sound, sizeof(CActiveAESound*));
2414 }
2415
2416 void CActiveAE::PlaySound(CActiveAESound *sound)
2417 {
2418   m_dataPort.SendOutMessage(CActiveAEDataProtocol::PLAYSOUND, &sound, sizeof(CActiveAESound*));
2419 }
2420
2421 void CActiveAE::StopSound(CActiveAESound *sound)
2422 {
2423   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STOPSOUND, &sound, sizeof(CActiveAESound*));
2424 }
2425
2426 /**
2427  * resample sounds to destination format for mixing
2428  * destination format is either format of stream or
2429  * default sink format when no stream is playing
2430  */
2431 void CActiveAE::ResampleSounds()
2432 {
2433   if (m_settings.guisoundmode == AE_SOUND_OFF ||
2434      (m_settings.guisoundmode == AE_SOUND_IDLE && !m_streams.empty()))
2435     return;
2436
2437   std::vector<CActiveAESound*>::iterator it;
2438   for (it = m_sounds.begin(); it != m_sounds.end(); ++it)
2439   {
2440     if (!(*it)->IsConverted())
2441     {
2442       ResampleSound(*it);
2443       // only do one sound, then yield to main loop
2444       break;
2445     }
2446   }
2447 }
2448
2449 bool CActiveAE::ResampleSound(CActiveAESound *sound)
2450 {
2451   SampleConfig orig_config, dst_config;
2452   uint8_t **dst_buffer;
2453   int dst_samples;
2454
2455   if (m_mode == MODE_RAW || m_internalFormat.m_dataFormat == AE_FMT_INVALID)
2456     return false;
2457
2458   if (!sound->GetSound(true))
2459     return false;
2460
2461   orig_config = sound->GetSound(true)->config;
2462
2463   dst_config.channel_layout = CActiveAEResample::GetAVChannelLayout(m_internalFormat.m_channelLayout);
2464   dst_config.channels = m_internalFormat.m_channelLayout.Count();
2465   dst_config.sample_rate = m_internalFormat.m_sampleRate;
2466   dst_config.fmt = CActiveAEResample::GetAVSampleFormat(m_internalFormat.m_dataFormat);
2467   dst_config.bits_per_sample = CAEUtil::DataFormatToUsedBits(m_internalFormat.m_dataFormat);
2468
2469   CActiveAEResample *resampler = new CActiveAEResample();
2470   resampler->Init(dst_config.channel_layout,
2471                   dst_config.channels,
2472                   dst_config.sample_rate,
2473                   dst_config.fmt,
2474                   dst_config.bits_per_sample,
2475                   orig_config.channel_layout,
2476                   orig_config.channels,
2477                   orig_config.sample_rate,
2478                   orig_config.fmt,
2479                   orig_config.bits_per_sample,
2480                   false,
2481                   true,
2482                   NULL,
2483                   m_settings.resampleQuality);
2484
2485   dst_samples = resampler->CalcDstSampleCount(sound->GetSound(true)->nb_samples,
2486                                               m_internalFormat.m_sampleRate,
2487                                               orig_config.sample_rate);
2488
2489   dst_buffer = sound->InitSound(false, dst_config, dst_samples);
2490   if (!dst_buffer)
2491   {
2492     delete resampler;
2493     return false;
2494   }
2495   int samples = resampler->Resample(dst_buffer, dst_samples,
2496                                     sound->GetSound(true)->data,
2497                                     sound->GetSound(true)->nb_samples,
2498                                     1.0);
2499
2500   sound->GetSound(false)->nb_samples = samples;
2501
2502   delete resampler;
2503   sound->SetConverted(true);
2504   return true;
2505 }
2506
2507 //-----------------------------------------------------------------------------
2508 // Streams
2509 //-----------------------------------------------------------------------------
2510
2511 IAEStream *CActiveAE::MakeStream(enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int encodedSampleRate, CAEChannelInfo channelLayout, unsigned int options)
2512 {
2513   //TODO: pass number of samples in audio packet
2514
2515   AEAudioFormat format;
2516   format.m_dataFormat = dataFormat;
2517   format.m_sampleRate = sampleRate;
2518   format.m_encodedRate = encodedSampleRate;
2519   format.m_channelLayout = channelLayout;
2520   format.m_frames = format.m_sampleRate / 10;
2521   format.m_frameSize = format.m_channelLayout.Count() *
2522                        (CAEUtil::DataFormatToBits(format.m_dataFormat) >> 3);
2523
2524   MsgStreamNew msg;
2525   msg.format = format;
2526   msg.options = options;
2527
2528   Message *reply;
2529   if (m_dataPort.SendOutMessageSync(CActiveAEDataProtocol::NEWSTREAM,
2530                                     &reply,10000,
2531                                     &msg, sizeof(MsgStreamNew)))
2532   {
2533     bool success = reply->signal == CActiveAEControlProtocol::ACC ? true : false;
2534     if (success)
2535     {
2536       CActiveAEStream *stream = *(CActiveAEStream**)reply->data;
2537       reply->Release();
2538       return stream;
2539     }
2540     reply->Release();
2541   }
2542
2543   CLog::Log(LOGERROR, "ActiveAE::%s - could not create stream", __FUNCTION__);
2544   return NULL;
2545 }
2546
2547 IAEStream *CActiveAE::FreeStream(IAEStream *stream)
2548 {
2549   m_dataPort.SendOutMessage(CActiveAEDataProtocol::FREESTREAM, &stream, sizeof(IAEStream*));
2550   return NULL;
2551 }
2552
2553 void CActiveAE::FlushStream(CActiveAEStream *stream)
2554 {
2555   Message *reply;
2556   if (m_controlPort.SendOutMessageSync(CActiveAEControlProtocol::FLUSHSTREAM,
2557                                        &reply,1000,
2558                                        &stream, sizeof(CActiveAEStream*)))
2559   {
2560     bool success = reply->signal == CActiveAEControlProtocol::ACC ? true : false;
2561     reply->Release();
2562     if (!success)
2563     {
2564       CLog::Log(LOGERROR, "CActiveAE::FlushStream - failed");
2565     }
2566   }
2567 }
2568
2569 void CActiveAE::PauseStream(CActiveAEStream *stream, bool pause)
2570 {
2571   // TODO pause sink, needs api change
2572   if (pause)
2573     m_controlPort.SendOutMessage(CActiveAEControlProtocol::PAUSESTREAM,
2574                                    &stream, sizeof(CActiveAEStream*));
2575   else
2576     m_controlPort.SendOutMessage(CActiveAEControlProtocol::RESUMESTREAM,
2577                                    &stream, sizeof(CActiveAEStream*));
2578 }
2579
2580 void CActiveAE::SetStreamAmplification(CActiveAEStream *stream, float amplify)
2581 {
2582   MsgStreamParameter msg;
2583   msg.stream = stream;
2584   msg.parameter.float_par = amplify;
2585   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STREAMAMP,
2586                                      &msg, sizeof(MsgStreamParameter));
2587 }
2588
2589 void CActiveAE::SetStreamReplaygain(CActiveAEStream *stream, float rgain)
2590 {
2591   MsgStreamParameter msg;
2592   msg.stream = stream;
2593   msg.parameter.float_par = rgain;
2594   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STREAMRGAIN,
2595                                      &msg, sizeof(MsgStreamParameter));
2596 }
2597
2598 void CActiveAE::SetStreamVolume(CActiveAEStream *stream, float volume)
2599 {
2600   MsgStreamParameter msg;
2601   msg.stream = stream;
2602   msg.parameter.float_par = volume;
2603   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STREAMVOLUME,
2604                                      &msg, sizeof(MsgStreamParameter));
2605 }
2606
2607 void CActiveAE::SetStreamResampleRatio(CActiveAEStream *stream, double ratio)
2608 {
2609   MsgStreamParameter msg;
2610   msg.stream = stream;
2611   msg.parameter.double_par = ratio;
2612   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STREAMRESAMPLERATIO,
2613                                      &msg, sizeof(MsgStreamParameter));
2614 }
2615
2616 void CActiveAE::SetStreamFade(CActiveAEStream *stream, float from, float target, unsigned int millis)
2617 {
2618   MsgStreamFade msg;
2619   msg.stream = stream;
2620   msg.from = from;
2621   msg.target = target;
2622   msg.millis = millis;
2623   m_controlPort.SendOutMessage(CActiveAEControlProtocol::STREAMFADE,
2624                                      &msg, sizeof(MsgStreamFade));
2625 }
2626
2627 void CActiveAE::RegisterAudioCallback(IAudioCallback* pCallback)
2628 {
2629   CSingleLock lock(m_vizLock);
2630   m_audioCallback = pCallback;
2631   m_vizInitialized = false;
2632 }
2633
2634 void CActiveAE::UnregisterAudioCallback()
2635 {
2636   CSingleLock lock(m_vizLock);
2637   m_audioCallback = NULL;
2638 }