1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  */
11 
12 #include "helpers.h"
13 
14 #include <string>
15 
16 #include "rtc_base/checks.h"
17 #include "rtc_base/logging.h"
18 
19 // Copies characters from a CFStringRef into a std::string.
CFStringToString(const CFStringRef cf_string)20 std::string CFStringToString(const CFStringRef cf_string) {
21   RTC_DCHECK(cf_string);
22   std::string std_string;
23   // Get the size needed for UTF8 plus terminating character.
24   size_t buffer_size =
25       CFStringGetMaximumSizeForEncoding(CFStringGetLength(cf_string),
26                                         kCFStringEncodingUTF8) +
27       1;
28   std::unique_ptr<char[]> buffer(new char[buffer_size]);
29   if (CFStringGetCString(cf_string, buffer.get(), buffer_size,
30                          kCFStringEncodingUTF8)) {
31     // Copy over the characters.
32     std_string.assign(buffer.get());
33   }
34   return std_string;
35 }
36 
37 // Convenience function for setting a VT property.
SetVTSessionProperty(VTSessionRef session,CFStringRef key,int32_t value)38 void SetVTSessionProperty(VTSessionRef session,
39                           CFStringRef key,
40                           int32_t value) {
41   CFNumberRef cfNum =
42       CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
43   OSStatus status = VTSessionSetProperty(session, key, cfNum);
44   CFRelease(cfNum);
45   if (status != noErr) {
46     std::string key_string = CFStringToString(key);
47     RTC_LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
48                       << " to " << value << ": " << status;
49   }
50 }
51 
52 // Convenience function for setting a VT property.
SetVTSessionProperty(VTSessionRef session,CFStringRef key,uint32_t value)53 void SetVTSessionProperty(VTSessionRef session,
54                           CFStringRef key,
55                           uint32_t value) {
56   int64_t value_64 = value;
57   CFNumberRef cfNum =
58       CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value_64);
59   OSStatus status = VTSessionSetProperty(session, key, cfNum);
60   CFRelease(cfNum);
61   if (status != noErr) {
62     std::string key_string = CFStringToString(key);
63     RTC_LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
64                       << " to " << value << ": " << status;
65   }
66 }
67 
68 // Convenience function for setting a VT property.
SetVTSessionProperty(VTSessionRef session,CFStringRef key,bool value)69 void SetVTSessionProperty(VTSessionRef session, CFStringRef key, bool value) {
70   CFBooleanRef cf_bool = (value) ? kCFBooleanTrue : kCFBooleanFalse;
71   OSStatus status = VTSessionSetProperty(session, key, cf_bool);
72   if (status != noErr) {
73     std::string key_string = CFStringToString(key);
74     RTC_LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
75                       << " to " << value << ": " << status;
76   }
77 }
78 
79 // Convenience function for setting a VT property.
SetVTSessionProperty(VTSessionRef session,CFStringRef key,CFStringRef value)80 void SetVTSessionProperty(VTSessionRef session,
81                           CFStringRef key,
82                           CFStringRef value) {
83   OSStatus status = VTSessionSetProperty(session, key, value);
84   if (status != noErr) {
85     std::string key_string = CFStringToString(key);
86     std::string val_string = CFStringToString(value);
87     RTC_LOG(LS_ERROR) << "VTSessionSetProperty failed to set: " << key_string
88                       << " to " << val_string << ": " << status;
89   }
90 }
91