Sunshine master
Self-hosted game stream host for Moonlight.
coreaudio_helpers.h
1#pragma once
2
3#include <CoreFoundation/CoreFoundation.h>
4#include <cstdint>
5#include <ostream>
6#include <string>
7
8namespace ca {
9
10 // Display FourCC error codes, with fallback to integer.
11 // Usage: BOOST_LOG(error) << ca::Status(err);
12
13 // Some CoreAudio error examples:
14 // kAudioHardwareNoError = 0,
15 // kAudioHardwareNotRunningError = 'stop',
16 // kAudioHardwareUnspecifiedError = 'what',
17 // kAudioHardwareUnknownPropertyError = 'who?',
18 // kAudioHardwareBadPropertySizeError = '!siz',
19 // kAudioHardwareIllegalOperationError = 'nope',
20 // kAudioHardwareBadObjectError = '!obj',
21 // kAudioHardwareBadDeviceError = '!dev',
22 // kAudioHardwareBadStreamError = '!str',
23 // kAudioHardwareUnsupportedOperationError = 'unop',
24 // kAudioHardwareNotReadyError = 'nrdy',
25 // kAudioDeviceUnsupportedFormatError = '!dat',
26 // kAudioDevicePermissionsError = '!hog'
27
28 inline std::string OSStatusToString(OSStatus error) {
29 uint32_t be = CFSwapInt32HostToBig(static_cast<uint32_t>(error));
30 const unsigned char c1 = static_cast<unsigned char>((be >> 24) & 0xFF);
31 const unsigned char c2 = static_cast<unsigned char>((be >> 16) & 0xFF);
32 const unsigned char c3 = static_cast<unsigned char>((be >> 8) & 0xFF);
33 const unsigned char c4 = static_cast<unsigned char>((be >> 0) & 0xFF);
34
35 auto is_printable = [](unsigned char c) -> bool {
36 return c >= 32 && c <= 126;
37 };
38
39 if (is_printable(c1) && is_printable(c2) && is_printable(c3) && is_printable(c4)) {
40 char buf[8] = {};
41 buf[0] = '\'';
42 buf[1] = static_cast<char>(c1);
43 buf[2] = static_cast<char>(c2);
44 buf[3] = static_cast<char>(c3);
45 buf[4] = static_cast<char>(c4);
46 buf[5] = '\'';
47 buf[6] = '\0';
48 return std::string(buf);
49 }
50
51 return std::to_string(static_cast<int32_t>(error));
52 }
53
54 namespace detail {
55 struct StatusView {
56 OSStatus e;
57 };
58
59 inline std::ostream &operator<<(std::ostream &os, StatusView v) {
60 return os << OSStatusToString(v.e);
61 }
62 } // namespace detail
63
64 inline detail::StatusView Status(OSStatus e) {
65 return detail::StatusView {e};
66 }
67
68} // namespace ca
Definition coreaudio_helpers.h:55