-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
SoftAE.cpp
1534 lines (1317 loc) · 44.4 KB
/
SoftAE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2012 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <sstream>
#include <iterator>
#include "system.h"
#include "utils/log.h"
#include "utils/TimeUtils.h"
#include "utils/MathUtils.h"
#include "utils/EndianSwap.h"
#include "threads/SingleLock.h"
#include "settings/GUISettings.h"
#include "settings/Settings.h"
#include "settings/AdvancedSettings.h"
#include "SoftAE.h"
#include "SoftAESound.h"
#include "SoftAEStream.h"
#include "AESinkFactory.h"
#include "Interfaces/AESink.h"
#include "Utils/AEUtil.h"
#include "Encoders/AEEncoderFFmpeg.h"
using namespace std;
/* Define idle wait time based on platform in milliseconds */
/* Higher wait times reduce thread CPU overhead when in */
/* idle or Suspend() modes */
#if defined (TARGET_WINDOWS) || defined (TARGET_LINUX) || \
defined (TARGET_DARWIN_OSX) || defined (TARGET_FREEBSD)
#define SOFTAE_IDLE_WAIT_MSEC 50 // shorter sleep for HTPC's
#elif defined (TARGET_RASPBERRY_PI) || defined (TARGET_ANDROID)
#define SOFTAE_IDLE_WAIT_MSEC 100 // longer for R_PI and Android
#else
#define SOFTAE_IDLE_WAIT_MSEC 100 // catchall for undefined platforms
#endif
CSoftAE::CSoftAE():
m_thread (NULL ),
m_audiophile (true ),
m_running (false ),
m_reOpen (false ),
m_closeSink (false ),
m_realSuspend (false ),
m_isSuspended (false ),
m_softSuspend (false ),
m_softSuspendTimer (0 ),
m_sink (NULL ),
m_transcode (false ),
m_rawPassthrough (false ),
m_soundMode (AE_SOUND_OFF),
m_streamsPlaying (false ),
m_encoder (NULL ),
m_converted (NULL ),
m_convertedSize (0 ),
m_masterStream (NULL ),
m_outputStageFn (NULL ),
m_streamStageFn (NULL )
{
unsigned int c_retry = 5;
CAESinkFactory::EnumerateEx(m_sinkInfoList);
while(m_sinkInfoList.size() == 0 && c_retry > 0)
{
CLog::Log(LOGNOTICE, "No Devices found - retry: %d", c_retry);
Sleep(2000);
c_retry--;
// retry the enumeration
CAESinkFactory::EnumerateEx(m_sinkInfoList, true);
}
CLog::Log(LOGNOTICE, "Found %lu Lists of Devices", m_sinkInfoList.size());
PrintSinks();
}
CSoftAE::~CSoftAE()
{
Deinitialize();
/* free the streams */
CSingleLock streamLock(m_streamLock);
while (!m_streams.empty())
{
CSoftAEStream *s = m_streams.front();
delete s;
}
/* free the sounds */
CSingleLock soundLock(m_soundLock);
while (!m_sounds.empty())
{
CSoftAESound *s = m_sounds.front();
m_sounds.pop_front();
delete s;
}
}
IAESink *CSoftAE::GetSink(AEAudioFormat &newFormat, bool passthrough, std::string &device)
{
device = passthrough ? m_passthroughDevice : m_device;
/* if we are raw, force the sample rate */
if (AE_IS_RAW(newFormat.m_dataFormat))
{
switch (newFormat.m_dataFormat)
{
case AE_FMT_AC3:
case AE_FMT_DTS:
break;
case AE_FMT_EAC3:
newFormat.m_sampleRate = 192000;
break;
case AE_FMT_TRUEHD:
case AE_FMT_DTSHD:
newFormat.m_sampleRate = 192000;
break;
default:
break;
}
}
IAESink *sink = CAESinkFactory::Create(device, newFormat, passthrough);
return sink;
}
/* this method MUST be called while holding m_streamLock */
inline CSoftAEStream *CSoftAE::GetMasterStream()
{
/* remove any destroyed streams first */
for (StreamList::iterator itt = m_streams.begin(); itt != m_streams.end();)
{
CSoftAEStream *stream = *itt;
if (stream->IsDestroyed())
{
RemoveStream(m_playingStreams, stream);
RemoveStream(m_streams , stream);
delete stream;
continue;
}
++itt;
}
if (!m_newStreams.empty())
return m_newStreams.back();
if (!m_streams.empty())
return m_streams.back();
return NULL;
}
/* save method to call outside of the main thread, use this one */
void CSoftAE::OpenSink()
{
m_reOpenEvent.Reset();
m_reOpen = true;
m_reOpenEvent.Wait();
m_wake.Set();
}
void CSoftAE::InternalCloseSink()
{
/* close the old sink if it was open */
if (m_sink)
{
CExclusiveLock sinkLock(m_sinkLock);
m_sink->Drain();
m_sink->Deinitialize();
delete m_sink;
m_sink = NULL;
}
m_closeSink = false;
m_closeEvent.Set();
}
/* this must NEVER be called from outside the main thread or Initialization */
void CSoftAE::InternalOpenSink()
{
/* save off our raw/passthrough mode for checking */
bool wasTranscode = m_transcode;
bool wasRawPassthrough = m_rawPassthrough;
bool reInit = false;
LoadSettings();
/* initialize for analog output */
m_rawPassthrough = false;
m_streamStageFn = &CSoftAE::RunStreamStage;
m_outputStageFn = &CSoftAE::RunOutputStage;
/* initialize the new format for basic 2.0 output */
AEAudioFormat newFormat;
newFormat.m_dataFormat = AE_FMT_FLOAT;
newFormat.m_sampleRate = 44100;
newFormat.m_encodedRate = 0;
newFormat.m_channelLayout = m_stereoUpmix ? m_stdChLayout : AE_CH_LAYOUT_2_0;
newFormat.m_frames = 0;
newFormat.m_frameSamples = 0;
newFormat.m_frameSize = 0;
CSingleLock streamLock(m_streamLock);
m_masterStream = GetMasterStream();
if (m_masterStream)
{
/* choose the sample rate & channel layout based on the master stream */
newFormat.m_sampleRate = m_masterStream->GetSampleRate();
if (!m_stereoUpmix)
newFormat.m_channelLayout = m_masterStream->m_initChannelLayout;
if (m_masterStream->IsRaw())
{
newFormat.m_sampleRate = m_masterStream->GetSampleRate();
newFormat.m_encodedRate = m_masterStream->GetEncodedSampleRate();
newFormat.m_dataFormat = m_masterStream->GetDataFormat();
newFormat.m_channelLayout = m_masterStream->m_initChannelLayout;
m_rawPassthrough = true;
m_streamStageFn = &CSoftAE::RunRawStreamStage;
m_outputStageFn = &CSoftAE::RunRawOutputStage;
}
else
{
if (!m_transcode)
newFormat.m_channelLayout.ResolveChannels(m_stdChLayout);
else
{
if (m_masterStream->m_initChannelLayout == AE_CH_LAYOUT_2_0)
m_transcode = false;
m_encoderInitFrameSizeMul = 1.0 / (newFormat.m_channelLayout.Count() *
(CAEUtil::DataFormatToBits(newFormat.m_dataFormat) >> 3));
m_encoderInitSampleRateMul = 1.0 / newFormat.m_sampleRate;
}
}
/* if the stream is paused we cant use it for anything else */
if (m_masterStream->m_paused)
m_masterStream = NULL;
}
else
m_transcode = false;
if (!m_rawPassthrough && m_transcode)
newFormat.m_dataFormat = AE_FMT_AC3;
streamLock.Leave();
std::string device, driver;
if (m_transcode || m_rawPassthrough)
device = m_passthroughDevice;
else
device = m_device;
CAESinkFactory::ParseDevice(device, driver);
if (driver.empty() && m_sink)
driver = m_sink->GetName();
if (m_rawPassthrough)
CLog::Log(LOGINFO, "CSoftAE::InternalOpenSink - RAW passthrough enabled");
else if (m_transcode)
CLog::Log(LOGINFO, "CSoftAE::InternalOpenSink - Transcode passthrough enabled");
/*
try to use 48000hz if we are going to transcode, this prevents the sink
from being re-opened repeatedly when switching sources, which locks up
some receivers & crappy integrated sound drivers. Check for as.xml override
*/
if (m_transcode && !m_rawPassthrough)
{
enum AEChannel ac3Layout[3] = {AE_CH_RAW, AE_CH_RAW, AE_CH_NULL};
newFormat.m_channelLayout = ac3Layout;
m_outputStageFn = &CSoftAE::RunTranscodeStage;
if (!g_advancedSettings.m_allowTranscode44100)
newFormat.m_sampleRate = 48000;
}
/*
if there is an audio resample rate set, use it, this MAY NOT be honoured as
the audio sink may not support the requested format, and may change it.
*/
if (g_advancedSettings.m_audioResample)
{
newFormat.m_sampleRate = g_advancedSettings.m_audioResample;
CLog::Log(LOGINFO, "CSoftAE::InternalOpenSink - Forcing samplerate to %d", newFormat.m_sampleRate);
}
/* only re-open the sink if its not compatible with what we need */
std::string sinkName;
if (m_sink)
{
sinkName = m_sink->GetName();
std::transform(sinkName.begin(), sinkName.end(), sinkName.begin(), ::toupper);
}
if (!m_sink || sinkName != driver || !m_sink->IsCompatible(newFormat, device))
{
CLog::Log(LOGINFO, "CSoftAE::InternalOpenSink - sink incompatible, re-starting");
/* take the sink lock */
CExclusiveLock sinkLock(m_sinkLock);
reInit = true;
//close the sink cause it gets reinited
InternalCloseSink();
/* get the display name of the device */
GetDeviceFriendlyName(device);
/* if we already have a driver, prepend it to the device string */
if (!driver.empty())
device = driver + ":" + device;
/* create the new sink */
m_sink = GetSink(newFormat, m_transcode || m_rawPassthrough, device);
/* perform basic sanity checks on the format returned by the sink */
ASSERT(newFormat.m_channelLayout.Count() > 0);
ASSERT(newFormat.m_dataFormat <= AE_FMT_FLOAT);
ASSERT(newFormat.m_frames > 0);
ASSERT(newFormat.m_frameSamples > 0);
ASSERT(newFormat.m_frameSize == (CAEUtil::DataFormatToBits(newFormat.m_dataFormat) >> 3) * newFormat.m_channelLayout.Count());
ASSERT(newFormat.m_sampleRate > 0);
CLog::Log(LOGDEBUG, "CSoftAE::InternalOpenSink - %s Initialized:", m_sink->GetName());
CLog::Log(LOGDEBUG, " Output Device : %s", m_deviceFriendlyName.c_str());
CLog::Log(LOGDEBUG, " Sample Rate : %d", newFormat.m_sampleRate);
CLog::Log(LOGDEBUG, " Sample Format : %s", CAEUtil::DataFormatToStr(newFormat.m_dataFormat));
CLog::Log(LOGDEBUG, " Channel Count : %d", newFormat.m_channelLayout.Count());
CLog::Log(LOGDEBUG, " Channel Layout: %s", ((std::string)newFormat.m_channelLayout).c_str());
CLog::Log(LOGDEBUG, " Frames : %d", newFormat.m_frames);
CLog::Log(LOGDEBUG, " Frame Samples : %d", newFormat.m_frameSamples);
CLog::Log(LOGDEBUG, " Frame Size : %d", newFormat.m_frameSize);
m_sinkFormat = newFormat;
m_sinkFormatSampleRateMul = 1.0 / (double)newFormat.m_sampleRate;
m_sinkFormatFrameSizeMul = 1.0 / (double)newFormat.m_frameSize;
m_sinkBlockSize = newFormat.m_frames * newFormat.m_frameSize;
// check if sink controls volume, if so, init the volume.
m_sinkHandlesVolume = m_sink->HasVolume();
if (m_sinkHandlesVolume)
m_sink->SetVolume(m_volume);
/* invalidate the buffer */
m_buffer.Empty();
}
else
CLog::Log(LOGINFO, "CSoftAE::InternalOpenSink - keeping old sink with : %s, %s, %dhz",
CAEUtil::DataFormatToStr(newFormat.m_dataFormat),
((std::string)newFormat.m_channelLayout).c_str(),
newFormat.m_sampleRate);
reInit = (reInit || m_chLayout != m_sinkFormat.m_channelLayout);
m_chLayout = m_sinkFormat.m_channelLayout;
size_t neededBufferSize = 0;
if (m_rawPassthrough)
{
if (!wasRawPassthrough)
m_buffer.Empty();
m_convertFn = NULL;
m_bytesPerSample = CAEUtil::DataFormatToBits(m_sinkFormat.m_dataFormat) >> 3;
m_frameSize = m_sinkFormat.m_frameSize;
neededBufferSize = m_sinkFormat.m_frames * m_sinkFormat.m_frameSize;
}
else
{
/* if we are transcoding */
if (m_transcode)
{
if (!wasTranscode || wasRawPassthrough)
{
/* invalidate the buffer */
m_buffer.Empty();
if (m_encoder)
m_encoder->Reset();
}
/* configure the encoder */
AEAudioFormat encoderFormat;
encoderFormat.m_dataFormat = AE_FMT_FLOAT;
encoderFormat.m_sampleRate = m_sinkFormat.m_sampleRate;
encoderFormat.m_encodedRate = 0;
encoderFormat.m_channelLayout = m_chLayout;
encoderFormat.m_frames = 0;
encoderFormat.m_frameSamples = 0;
encoderFormat.m_frameSize = 0;
if (!m_encoder || !m_encoder->IsCompatible(encoderFormat))
{
m_buffer.Empty();
SetupEncoder(encoderFormat);
m_encoderFormat = encoderFormat;
if (encoderFormat.m_frameSize > 0)
m_encoderFrameSizeMul = 1.0 / (double)m_sinkFormat.m_frameSize;
else
m_encoderFrameSizeMul = 1.0;
}
/* remap directly to the format we need for encode */
reInit = (reInit || m_chLayout != m_encoderFormat.m_channelLayout);
m_chLayout = m_encoderFormat.m_channelLayout;
m_convertFn = CAEConvert::FrFloat(m_encoderFormat.m_dataFormat);
neededBufferSize = m_encoderFormat.m_frames * sizeof(float) * m_chLayout.Count();
CLog::Log(LOGDEBUG, "CSoftAE::InternalOpenSink - Encoding using layout: %s", ((std::string)m_chLayout).c_str());
}
else
{
m_convertFn = CAEConvert::FrFloat(m_sinkFormat.m_dataFormat);
neededBufferSize = m_sinkFormat.m_frames * sizeof(float) * m_chLayout.Count();
CLog::Log(LOGDEBUG, "CSoftAE::InternalOpenSink - Using speaker layout: %s", CAEUtil::GetStdChLayoutName(m_stdChLayout));
}
m_bytesPerSample = CAEUtil::DataFormatToBits(AE_FMT_FLOAT) >> 3;
m_frameSize = m_bytesPerSample * m_chLayout.Count();
}
CLog::Log(LOGDEBUG, "CSoftAE::InternalOpenSink - Internal Buffer Size: %d", (int)neededBufferSize);
if (m_buffer.Size() < neededBufferSize)
m_buffer.Alloc(neededBufferSize);
if (reInit)
{
if (!m_rawPassthrough)
{
/* re-init incompatible sounds */
CSingleLock soundLock(m_soundLock);
for (SoundList::iterator itt = m_sounds.begin(); itt != m_sounds.end(); ++itt)
{
CSoftAESound *sound = *itt;
if (!sound->IsCompatible())
{
StopSound(sound);
sound->Initialize();
}
}
}
/* re-init streams */
streamLock.Enter();
for (StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
(*itt)->Initialize();
streamLock.Leave();
}
/* any new streams need to be initialized */
for (StreamList::iterator itt = m_newStreams.begin(); itt != m_newStreams.end(); ++itt)
{
(*itt)->Initialize();
m_streams.push_back(*itt);
if (!(*itt)->m_paused)
m_playingStreams.push_back(*itt);
}
m_newStreams.clear();
m_streamsPlaying = !m_playingStreams.empty();
m_softSuspend = false;
/* notify any event listeners that we are done */
m_reOpen = false;
m_reOpenEvent.Set();
m_wake.Set();
}
void CSoftAE::ResetEncoder()
{
if (m_encoder)
m_encoder->Reset();
m_encodedBuffer.Empty();
}
bool CSoftAE::SetupEncoder(AEAudioFormat &format)
{
ResetEncoder();
delete m_encoder;
m_encoder = NULL;
if (!m_transcode)
return false;
m_encoder = new CAEEncoderFFmpeg();
if (m_encoder->Initialize(format))
return true;
delete m_encoder;
m_encoder = NULL;
return false;
}
void CSoftAE::Shutdown()
{
Deinitialize();
}
bool CSoftAE::Initialize()
{
CSingleLock lock(m_threadLock);
InternalOpenSink();
m_running = true;
m_thread = new CThread(this, "CSoftAE");
m_thread->Create();
m_thread->SetPriority(THREAD_PRIORITY_ABOVE_NORMAL);
return true;
}
void CSoftAE::OnSettingsChange(const std::string& setting)
{
if (setting == "audiooutput.passthroughdevice" ||
setting == "audiooutput.audiodevice" ||
setting == "audiooutput.mode" ||
setting == "audiooutput.ac3passthrough" ||
setting == "audiooutput.dtspassthrough" ||
setting == "audiooutput.passthroughaac" ||
setting == "audiooutput.truehdpassthrough" ||
setting == "audiooutput.dtshdpassthrough" ||
setting == "audiooutput.channels" ||
setting == "audiooutput.useexclusivemode" ||
setting == "audiooutput.multichannellpcm" ||
setting == "audiooutput.stereoupmix")
{
OpenSink();
}
if (setting == "audiooutput.normalizelevels" || setting == "audiooutput.stereoupmix")
{
/* re-init stream reamppers */
CSingleLock streamLock(m_streamLock);
for (StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
(*itt)->InitializeRemap();
}
}
void CSoftAE::LoadSettings()
{
m_audiophile = g_advancedSettings.m_audioAudiophile;
if (m_audiophile)
CLog::Log(LOGINFO, "CSoftAE::LoadSettings - Audiophile switch enabled");
m_stereoUpmix = g_guiSettings.GetBool("audiooutput.stereoupmix");
if (m_stereoUpmix)
CLog::Log(LOGINFO, "CSoftAE::LoadSettings - Stereo upmix is enabled");
/* load the configuration */
m_stdChLayout = AE_CH_LAYOUT_2_0;
switch (g_guiSettings.GetInt("audiooutput.channels"))
{
default:
case 0: m_stdChLayout = AE_CH_LAYOUT_2_0; break; /* dont alow 1_0 output */
case 1: m_stdChLayout = AE_CH_LAYOUT_2_0; break;
case 2: m_stdChLayout = AE_CH_LAYOUT_2_1; break;
case 3: m_stdChLayout = AE_CH_LAYOUT_3_0; break;
case 4: m_stdChLayout = AE_CH_LAYOUT_3_1; break;
case 5: m_stdChLayout = AE_CH_LAYOUT_4_0; break;
case 6: m_stdChLayout = AE_CH_LAYOUT_4_1; break;
case 7: m_stdChLayout = AE_CH_LAYOUT_5_0; break;
case 8: m_stdChLayout = AE_CH_LAYOUT_5_1; break;
case 9: m_stdChLayout = AE_CH_LAYOUT_7_0; break;
case 10: m_stdChLayout = AE_CH_LAYOUT_7_1; break;
}
// force optical/coax to 2.0 output channels
if (!m_rawPassthrough && g_guiSettings.GetInt("audiooutput.mode") == AUDIO_IEC958)
m_stdChLayout = AE_CH_LAYOUT_2_0;
/* get the output devices and ensure they exist */
m_device = g_guiSettings.GetString("audiooutput.audiodevice");
m_passthroughDevice = g_guiSettings.GetString("audiooutput.passthroughdevice");
VerifySoundDevice(m_device , false);
VerifySoundDevice(m_passthroughDevice, true );
m_transcode = (
g_guiSettings.GetBool("audiooutput.ac3passthrough") /*||
g_guiSettings.GetBool("audiooutput.dtspassthrough") */
) && (
(g_guiSettings.GetInt("audiooutput.mode") == AUDIO_IEC958) ||
(g_guiSettings.GetInt("audiooutput.mode") == AUDIO_HDMI && !g_guiSettings.GetBool("audiooutput.multichannellpcm"))
);
}
void CSoftAE::VerifySoundDevice(std::string& device, bool passthrough)
{
/* check that the specified device exists */
std::string firstDevice;
for (AESinkInfoList::iterator itt = m_sinkInfoList.begin(); itt != m_sinkInfoList.end(); ++itt)
{
AESinkInfo sinkInfo = *itt;
for (AEDeviceInfoList::iterator itt2 = sinkInfo.m_deviceInfoList.begin(); itt2 != sinkInfo.m_deviceInfoList.end(); ++itt2)
{
CAEDeviceInfo& devInfo = *itt2;
if (passthrough && devInfo.m_deviceType == AE_DEVTYPE_PCM)
continue;
std::string deviceName = sinkInfo.m_sinkName + ":" + devInfo.m_deviceName;
/* remember the first device so we can default to it if required */
if (firstDevice.empty())
firstDevice = deviceName;
if (device == deviceName)
return;
}
}
/* if the device wasnt found, set it to the first viable output */
device = firstDevice;
}
inline void CSoftAE::GetDeviceFriendlyName(std::string &device)
{
m_deviceFriendlyName = "Device not found";
/* Match the device and find its friendly name */
for (AESinkInfoList::iterator itt = m_sinkInfoList.begin(); itt != m_sinkInfoList.end(); ++itt)
{
AESinkInfo sinkInfo = *itt;
for (AEDeviceInfoList::iterator itt2 = sinkInfo.m_deviceInfoList.begin(); itt2 != sinkInfo.m_deviceInfoList.end(); ++itt2)
{
CAEDeviceInfo& devInfo = *itt2;
if (devInfo.m_deviceName == device)
{
m_deviceFriendlyName = devInfo.m_displayName;
break;
}
}
}
return;
}
void CSoftAE::Deinitialize()
{
CSingleLock lock(m_threadLock);
if (m_thread)
{
Stop();
m_thread->StopThread(true);
delete m_thread;
m_thread = NULL;
}
if (m_sink)
{
/* shutdown the sink */
m_sink->Deinitialize();
delete m_sink;
m_sink = NULL;
}
delete m_encoder;
m_encoder = NULL;
ResetEncoder();
m_buffer.DeAlloc();
_aligned_free(m_converted);
m_converted = NULL;
m_convertedSize = 0;
m_sinkInfoList.clear();
}
void CSoftAE::EnumerateOutputDevices(AEDeviceList &devices, bool passthrough)
{
for (AESinkInfoList::iterator itt = m_sinkInfoList.begin(); itt != m_sinkInfoList.end(); ++itt)
{
AESinkInfo sinkInfo = *itt;
for (AEDeviceInfoList::iterator itt2 = sinkInfo.m_deviceInfoList.begin(); itt2 != sinkInfo.m_deviceInfoList.end(); ++itt2)
{
CAEDeviceInfo devInfo = *itt2;
if (passthrough && devInfo.m_deviceType == AE_DEVTYPE_PCM)
continue;
std::string device = sinkInfo.m_sinkName + ":" + devInfo.m_deviceName;
std::stringstream ss;
/* add the sink name if we have more then one sink type */
if (m_sinkInfoList.size() > 1)
ss << sinkInfo.m_sinkName << ": ";
ss << devInfo.m_displayName;
if (!devInfo.m_displayNameExtra.empty())
ss << ", " << devInfo.m_displayNameExtra;
devices.push_back(AEDevice(ss.str(), device));
}
}
}
std::string CSoftAE::GetDefaultDevice(bool passthrough)
{
for (AESinkInfoList::iterator itt = m_sinkInfoList.begin(); itt != m_sinkInfoList.end(); ++itt)
{
AESinkInfo sinkInfo = *itt;
for (AEDeviceInfoList::iterator itt2 = sinkInfo.m_deviceInfoList.begin(); itt2 != sinkInfo.m_deviceInfoList.end(); ++itt2)
{
CAEDeviceInfo devInfo = *itt2;
if (passthrough && devInfo.m_deviceType == AE_DEVTYPE_PCM)
continue;
std::string device = sinkInfo.m_sinkName + ":" + devInfo.m_deviceName;
return device;
}
}
return "default";
}
bool CSoftAE::SupportsRaw()
{
/* CSoftAE supports raw formats */
return true;
}
void CSoftAE::PauseStream(CSoftAEStream *stream)
{
CSingleLock streamLock(m_streamLock);
RemoveStream(m_playingStreams, stream);
stream->m_paused = true;
streamLock.Leave();
OpenSink();
}
void CSoftAE::ResumeStream(CSoftAEStream *stream)
{
CSingleLock streamLock(m_streamLock);
m_playingStreams.push_back(stream);
stream->m_paused = false;
streamLock.Leave();
m_streamsPlaying = true;
OpenSink();
}
void CSoftAE::Stop()
{
m_running = false;
m_isSuspended = false;
m_wake.Set();
/* wait for the thread to stop */
CSingleLock lock(m_runningLock);
}
void CSoftAE::SetSoundMode(const int mode)
{
m_soundMode = mode;
/* stop all currently playing sounds if they are being turned off */
if (mode == AE_SOUND_OFF || (mode == AE_SOUND_IDLE && m_streamsPlaying))
StopAllSounds();
}
IAEStream *CSoftAE::MakeStream(enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int encodedSampleRate, CAEChannelInfo channelLayout, unsigned int options/* = 0 */)
{
CAEChannelInfo channelInfo(channelLayout);
CLog::Log(LOGINFO, "CSoftAE::MakeStream - %s, %u, %s",
CAEUtil::DataFormatToStr(dataFormat),
sampleRate, ((std::string)channelInfo).c_str());
/* ensure we have the encoded sample rate if the format is RAW */
if (AE_IS_RAW(dataFormat))
ASSERT(encodedSampleRate);
CSingleLock streamLock(m_streamLock);
CSoftAEStream *stream = new CSoftAEStream(dataFormat, sampleRate, encodedSampleRate, channelLayout, options);
m_newStreams.push_back(stream);
streamLock.Leave();
OpenSink();
return stream;
}
IAESound *CSoftAE::MakeSound(const std::string& file)
{
CSingleLock soundLock(m_soundLock);
CSoftAESound *sound = new CSoftAESound(file);
if (!sound->Initialize())
{
delete sound;
return NULL;
}
m_sounds.push_back(sound);
return sound;
}
void CSoftAE::PlaySound(IAESound *sound)
{
if (m_soundMode == AE_SOUND_OFF || (m_soundMode == AE_SOUND_IDLE && m_streamsPlaying))
return;
float *samples = ((CSoftAESound*)sound)->GetSamples();
if (!samples)
return;
/* add the sound to the play list */
CSingleLock soundSampleLock(m_soundSampleLock);
SoundState ss = {
((CSoftAESound*)sound),
samples,
((CSoftAESound*)sound)->GetSampleCount()
};
m_playing_sounds.push_back(ss);
/* wake to play the sound */
m_softSuspend = false;
m_wake.Set();
}
void CSoftAE::FreeSound(IAESound *sound)
{
if (!sound)
return;
sound->Stop();
CSingleLock soundLock(m_soundLock);
for (SoundList::iterator itt = m_sounds.begin(); itt != m_sounds.end(); ++itt)
if (*itt == sound)
{
m_sounds.erase(itt);
break;
}
delete (CSoftAESound*)sound;
}
void CSoftAE::GarbageCollect()
{
}
unsigned int CSoftAE::GetSampleRate()
{
if (m_transcode && m_encoder && !m_rawPassthrough)
return m_encoderFormat.m_sampleRate;
return m_sinkFormat.m_sampleRate;
}
void CSoftAE::StopSound(IAESound *sound)
{
CSingleLock lock(m_soundSampleLock);
for (SoundStateList::iterator itt = m_playing_sounds.begin(); itt != m_playing_sounds.end(); )
{
if ((*itt).owner == sound)
{
(*itt).owner->ReleaseSamples();
itt = m_playing_sounds.erase(itt);
}
else
++itt;
}
}
IAEStream *CSoftAE::FreeStream(IAEStream *stream)
{
CSingleLock lock(m_streamLock);
RemoveStream(m_playingStreams, (CSoftAEStream*)stream);
RemoveStream(m_streams , (CSoftAEStream*)stream);
lock.Leave();
// Close completely when we go to suspend, reopen as it was old behaviour
// m_realSuspend is currently only used on Linux Systems as it is needed
// for suspend and resume. Not opening when masterstream stops means
// clipping on S/PDIF.
if(m_realSuspend)
{
m_closeEvent.Reset();
m_closeSink = true;
m_closeEvent.Wait();
m_wake.Set();
}
else if (m_masterStream == stream)
OpenSink();
delete (CSoftAEStream*)stream;
return NULL;
}
double CSoftAE::GetDelay()
{
double delayBuffer = 0.0, delaySink = 0.0, delayTranscoder = 0.0;
CSharedLock sinkLock(m_sinkLock);
if (m_sink)
delaySink = m_sink->GetDelay();
if (m_transcode && m_encoder && !m_rawPassthrough)
{
delayBuffer = (double)m_buffer.Used() * m_encoderInitFrameSizeMul * m_encoderInitSampleRateMul;
delayTranscoder = m_encoder->GetDelay((double)m_encodedBuffer.Used() * m_encoderFrameSizeMul);
}
else
delayBuffer = (double)m_buffer.Used() * m_sinkFormatFrameSizeMul *m_sinkFormatSampleRateMul;
return delayBuffer + delaySink + delayTranscoder;
}
double CSoftAE::GetCacheTime()
{
double timeBuffer = 0.0, timeSink = 0.0, timeTranscoder = 0.0;
CSharedLock sinkLock(m_sinkLock);
if (m_sink)
timeSink = m_sink->GetCacheTime();
if (m_transcode && m_encoder && !m_rawPassthrough)
{
timeBuffer = (double)m_buffer.Used() * m_encoderInitFrameSizeMul * m_encoderInitSampleRateMul;
timeTranscoder = m_encoder->GetDelay((double)m_encodedBuffer.Used() * m_encoderFrameSizeMul);
}
else
timeBuffer = (double)m_buffer.Used() * m_sinkFormatFrameSizeMul *m_sinkFormatSampleRateMul;
return timeBuffer + timeSink + timeTranscoder;
}
double CSoftAE::GetCacheTotal()
{
double timeBuffer = 0.0, timeSink = 0.0, timeTranscoder = 0.0;
CSharedLock sinkLock(m_sinkLock);
if (m_sink)
timeSink = m_sink->GetCacheTotal();
if (m_transcode && m_encoder && !m_rawPassthrough)
{
timeBuffer = (double)m_buffer.Size() * m_encoderInitFrameSizeMul * m_encoderInitSampleRateMul;
timeTranscoder = m_encoder->GetDelay((double)m_encodedBuffer.Size() * m_encoderFrameSizeMul);
}
else
timeBuffer = (double)m_buffer.Size() * m_sinkFormatFrameSizeMul *m_sinkFormatSampleRateMul;
return timeBuffer + timeSink + timeTranscoder;
}
bool CSoftAE::IsSuspended()
{
return m_isSuspended;
}
float CSoftAE::GetVolume()
{
return m_volume;
}
void CSoftAE::SetVolume(float volume)
{
m_volume = volume;
if (!m_sinkHandlesVolume)
return;
CSharedLock sinkLock(m_sinkLock);
if (m_sink)
m_sink->SetVolume(m_volume);
}
void CSoftAE::StopAllSounds()
{
CSingleLock lock(m_soundSampleLock);
while (!m_playing_sounds.empty())
{
SoundState *ss = &(*m_playing_sounds.begin());
ss->owner->ReleaseSamples();
m_playing_sounds.pop_front();
}
}
bool CSoftAE::Suspend()
{
CLog::Log(LOGDEBUG, "CSoftAE::Suspend - Suspending AE processing");
m_isSuspended = true;
CSingleLock streamLock(m_streamLock);
for (StreamList::iterator itt = m_playingStreams.begin(); itt != m_playingStreams.end(); ++itt)
{
CSoftAEStream *stream = *itt;
stream->Flush();
}
#if defined(TARGET_LINUX)
/*workaround sinks not playing sound after resume */
StopAllSounds();
CExclusiveLock sinkLock(m_sinkLock);
for (AESinkInfoList::iterator itt = m_sinkInfoList.begin(); itt != m_sinkInfoList.end(); ++itt)
{
itt->m_deviceInfoList.pop_back();