Merge pull request #4196 from arnova/sub_fallback
[vuplus_xbmc] / xbmc / cores / AudioEngine / Sinks / osx / CoreAudioHelpers.cpp
1 /*
2  *      Copyright (C) 2011-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 "CoreAudioHelpers.h"
22
23 #include <sstream>
24
25 // Helper Functions
26 std::string GetError(OSStatus error)
27 {
28   char buffer[128];
29  
30   *(UInt32 *)(buffer + 1) = CFSwapInt32HostToBig(error);
31   if (isprint(buffer[1]) && isprint(buffer[2]) &&
32       isprint(buffer[3]) && isprint(buffer[4]))
33   {
34     buffer[0] = buffer[5] = '\'';
35     buffer[6] = '\0';
36   }
37   else
38   {
39     // no, format it as an integer
40     sprintf(buffer, "%d", (int)error);
41   }
42  
43   return std::string(buffer);
44 }
45
46 const char* StreamDescriptionToString(AudioStreamBasicDescription desc, std::string &str)
47 {
48   char fourCC[5] = {
49     (desc.mFormatID >> 24) & 0xFF,
50     (desc.mFormatID >> 16) & 0xFF,
51     (desc.mFormatID >>  8) & 0xFF,
52      desc.mFormatID        & 0xFF,
53     0
54   };
55
56   std::stringstream sstr; 
57   switch (desc.mFormatID)
58   {
59     case kAudioFormatLinearPCM:
60       sstr  << "["
61             << fourCC
62             << "] "
63             << ((desc.mFormatFlags & kAudioFormatFlagIsNonMixable) ? "" : "Mixable " )
64             << ((desc.mFormatFlags & kAudioFormatFlagIsNonInterleaved) ? "Non-" : "" )
65             << "Interleaved "
66             << desc.mChannelsPerFrame
67             << " Channel "
68             << desc.mBitsPerChannel
69             << "-bit "
70             << ((desc.mFormatFlags & kAudioFormatFlagIsFloat) ? "Floating Point " : "Signed Integer ")
71             << ((desc.mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE")
72             << " ("
73             << (UInt32)desc.mSampleRate
74             << "Hz)";
75       str = sstr.str();
76       break;
77     case kAudioFormatAC3:
78       sstr  << "["
79             << fourCC
80             << "] "
81             << ((desc.mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE")
82             << " AC-3/DTS ("
83             << (UInt32)desc.mSampleRate
84             << "Hz)";
85       str = sstr.str();                
86       break;
87     case kAudioFormat60958AC3:
88       sstr  << "["
89             << fourCC
90             << "] AC-3/DTS for S/PDIF "
91             << ((desc.mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE")
92             << " ("
93             << (UInt32)desc.mSampleRate
94             << "Hz)";
95       str = sstr.str();
96       break;
97     default:
98       sstr  << "["
99             << fourCC
100             << "]";
101       break;
102   }
103   return str.c_str();
104 }