-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppRecording.cpp
135 lines (103 loc) · 5.07 KB
/
AppRecording.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
#include "stdafx.h"
#include <chrono>
using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Storage;
using namespace winrt::Windows::Media;
bool RecordTimeSpan(uint32_t TimeInSeconds)
{
TCHAR TempPath[MAX_PATH] = TEXT("");
GetTempPath(ARRAYSIZE(TempPath), TempPath);
TCHAR TempFileName[MAX_PATH] = TEXT("");
GetTempFileName(TempPath, TEXT("vid"), 0, TempFileName);
try
{
auto AsyncOutputFile = StorageFile::GetFileFromPathAsync(TempFileName);
StorageFile OutputFile = AsyncOutputFile.get();
AppRecording::AppRecordingManager AppRecordingManager = AppRecording::AppRecordingManager::GetDefault();
AppRecording::AppRecordingStatus AppRecordingStatus = AppRecordingManager.GetStatus();
// make sure that time span / "background" recording is enabled at the OS level and for the Game
if (!AppRecordingStatus.CanRecordTimeSpan())
{
AppRecording::AppRecordingStatusDetails AppRecordingStatusDetails = AppRecordingStatus.Details();
bool IsAnyAppBroadcasting = AppRecordingStatusDetails.IsAnyAppBroadcasting();
bool IsAppInactive = AppRecordingStatusDetails.IsAppInactive();
bool IsBlockedForApp = AppRecordingStatusDetails.IsBlockedForApp();
bool IsCaptureResourceUnavailable = AppRecordingStatusDetails.IsCaptureResourceUnavailable();
bool IsDisabledBySystem = AppRecordingStatusDetails.IsDisabledBySystem();
bool IsDisabledByUser = AppRecordingStatusDetails.IsDisabledByUser();
bool IsGameStreamInProgress = AppRecordingStatusDetails.IsGameStreamInProgress();
bool IsGpuConstrained = AppRecordingStatusDetails.IsGpuConstrained();
bool IsTimeSpanRecordingDisabled = AppRecordingStatusDetails.IsTimeSpanRecordingDisabled();
if (AppRecordingStatusDetails.IsTimeSpanRecordingDisabled())
{
MessageBox(nullptr, TEXT("IsTimeSpanRecordingDisabled is True"), TEXT("RecordTimeSpan"), MB_ICONERROR);
return false;
}
MessageBox(nullptr, TEXT("CanRecordTimeSpan is False"), TEXT("RecordTimeSpan"), MB_ICONERROR);
return false;
}
AppRecording::AppRecordingStatusDetails AppRecordingStatusDetails = AppRecordingStatus.Details();
// cap duration at what is available
TimeSpan TimeAvailable = AppRecordingStatus.HistoricalBufferDuration();
std::chrono::seconds TimeRequested(TimeInSeconds);
TimeSpan TimeDuration = std::min<std::common_type_t<std::chrono::seconds, TimeSpan>>(TimeRequested, TimeAvailable);
auto AsyncAppRecordingResult = AppRecordingManager.RecordTimeSpanToFileAsync(winrt::clock::now() - TimeDuration, TimeDuration, OutputFile);
AppRecording::AppRecordingResult AppRecordingResult = AsyncAppRecordingResult.get();
}
catch (winrt::hresult_error const& Exception)
{
MessageBox(nullptr, Exception.message().c_str(), TEXT("RecordTimeSpan"), MB_ICONERROR);
return false;
}
MessageBox(nullptr, TEXT("Success!"), TEXT("RecordTimeSpan"), MB_ICONINFORMATION);
return true;
}
concurrency::task<bool> RecordTimeSpanAsync(uint32_t TimeInSeconds)
{
return concurrency::create_task([=] {return RecordTimeSpan(TimeInSeconds); });
}
bool StartRecording()
{
TCHAR TempPath[MAX_PATH] = TEXT("");
GetTempPath(ARRAYSIZE(TempPath), TempPath);
TCHAR TempFileName[MAX_PATH] = TEXT("");
GetTempFileName(TempPath, TEXT("vid"), 0, TempFileName);
try
{
auto AsyncOutputFile = StorageFile::GetFileFromPathAsync(TempFileName);
StorageFile OutputFile = AsyncOutputFile.get();
AppRecording::AppRecordingManager AppRecordingManager = AppRecording::AppRecordingManager::GetDefault();
AppRecording::AppRecordingStatus AppRecordingStatus = AppRecordingManager.GetStatus();
// make sure that recording is enabled at the OS level and for the Game
if (!AppRecordingStatus.CanRecord())
{
AppRecording::AppRecordingStatusDetails AppRecordingStatusDetails = AppRecordingStatus.Details();
bool IsAnyAppBroadcasting = AppRecordingStatusDetails.IsAnyAppBroadcasting();
bool IsAppInactive = AppRecordingStatusDetails.IsAppInactive();
bool IsBlockedForApp = AppRecordingStatusDetails.IsBlockedForApp();
bool IsCaptureResourceUnavailable = AppRecordingStatusDetails.IsCaptureResourceUnavailable();
bool IsDisabledBySystem = AppRecordingStatusDetails.IsDisabledBySystem();
bool IsDisabledByUser = AppRecordingStatusDetails.IsDisabledByUser();
bool IsGameStreamInProgress = AppRecordingStatusDetails.IsGameStreamInProgress();
bool IsGpuConstrained = AppRecordingStatusDetails.IsGpuConstrained();
bool IsTimeSpanRecordingDisabled = AppRecordingStatusDetails.IsTimeSpanRecordingDisabled();
MessageBox(nullptr, TEXT("CanRecord is False"), TEXT("StartRecording"), MB_ICONERROR);
return false;
}
auto AsyncAppRecordingResult = AppRecordingManager.StartRecordingToFileAsync(OutputFile);
Sleep(1000);
AsyncAppRecordingResult.Cancel();
}
catch (winrt::hresult_error const& Exception)
{
MessageBox(nullptr, Exception.message().c_str(), TEXT("StartRecording"), MB_ICONERROR);
return false;
}
MessageBox(nullptr, TEXT("Success!"), TEXT("StartRecording"), MB_ICONINFORMATION);
return true;
}
concurrency::task<bool> StartRecordingAsync()
{
return concurrency::create_task([=] {return StartRecording(); });
}