aboutsummaryrefslogtreecommitdiff
path: root/samples/DX_APIUsage/GfeSDKHighlights.cpp
blob: 0839c6ae012778399b2cff205830699b5147d728 (plain) (blame)
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
/* Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
/*!
* \file
* Shows example usage of the GfeSDK C++ API by a directX application.
*/

#include <Windows.h>

#include "GfeSDKHighlights.hpp"

#include <json/json.h>

char g_logBuffer[512];
void dbgprint(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    vsprintf_s(g_logBuffer, sizeof(g_logBuffer) / sizeof(g_logBuffer[0]), fmt, args);
    printf(g_logBuffer);
    printf("\n");
    OutputDebugStringA(g_logBuffer);
    OutputDebugStringA("\n");
    va_end(args);
}
#define LOG(fmt, ...) dbgprint(fmt, __VA_ARGS__)

static std::wstring permissionToStringW(GfeSDK::NVGSDK_Permission p)
{
    switch (p)
    {
    case GfeSDK::NVGSDK_PERMISSION_MUST_ASK:    return L"Must Ask";
    case GfeSDK::NVGSDK_PERMISSION_GRANTED:     return L"Granted";
    case GfeSDK::NVGSDK_PERMISSION_DENIED:      return L"Denied";
    default:
        return L"UNKNOWN";
    }
}

HighlightsWrapper::HighlightsWrapper() :
    m_currentPermission(permissionToStringW(GfeSDK::NVGSDK_PERMISSION_MUST_ASK))
{
}

void HighlightsWrapper::Init(char const* gameName, char const* defaultLocale, GfeSDK::NVGSDK_Highlight* highlights, size_t numHighlights)
{
    using namespace std::placeholders;

    //! [Creation CPP]
    GfeSDK::CreateInputParams createParams;
    createParams.appName = "gfesdk_dx_sample";  // appName will only be used/visible if GFE cannot identify your game
    createParams.pollForCallbacks = true;       // We will poll for callbacks in order to execute callbacks from game loop
    createParams.requiredScopes = {
        GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS,
        GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS_VIDEO,
        GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS_SCREENSHOT
    };
    createParams.notificationCallback = std::bind(&HighlightsWrapper::OnNotification, this, _1, _2);

    GfeSDK::CreateResponse response;
    GfeSDK::Core* gfesdkCore = GfeSDK::Core::Create(createParams, response);
    if (GfeSDK::NVGSDK_SUCCEEDED(response.returnCode))
    {
        // Valid handle has been returned
        LOG("Success: %s", GfeSDK::NVGSDK_RetCodeToString(response.returnCode));
        LOG("PC is running GFE version %s", response.nvidiaGfeVersion.c_str());
        LOG("PC is running GfeSDK version %d.%d", response.versionMajor, response.versionMinor);
        switch (response.returnCode)
        {
        case GfeSDK::NVGSDK_SUCCESS_VERSION_OLD_GFE:
            LOG("Compatible, but the user should update to the latest version of GFE.");
            break;
        case GfeSDK::NVGSDK_SUCCESS_VERSION_OLD_SDK:
            LOG("Compatible, but this application should update to a more recent GfeSDK to get latest features/bugfixes");
            break;
        }
    }
    else
    {
        // No valid handle
        LOG("Failure: %s", GfeSDK::NVGSDK_RetCodeToString(response.returnCode));
        switch (response.returnCode)
        {
        case GfeSDK::NVGSDK_ERR_SDK_VERSION:
            LOG("This version of the SDK is too old to communicate with the user's SDK. We're never planning on this happening.");
            LOG("PC is running GFE version %s", response.nvidiaGfeVersion.c_str());
            LOG("PC is running GfeSDK version %d.%d", response.versionMajor, response.versionMinor);
            break;
        case GfeSDK::NVGSDK_SUCCESS_VERSION_OLD_SDK:
            LOG("The installed version of GFE is too old to continue. User must upgrade.");
            LOG("PC is running GFE version %s", response.nvidiaGfeVersion.c_str());
            LOG("PC is running GfeSDK version %d.%d", response.versionMajor, response.versionMinor);
            break;
        }
        return;
    }
    //! [Creation CPP]

    m_gfesdk.reset(gfesdkCore);
    UpdateLastResultString(response.returnCode);

    if (response.scopePermissions.find(GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS_VIDEO) != response.scopePermissions.end())
    {
        m_currentPermission = permissionToStringW(response.scopePermissions[GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS_VIDEO]);
    }

    //! [Permissions CPP]
    // Request Permissions if user hasn't decided yet
    GfeSDK::RequestPermissionsParams requestPermissionsParams;

    // 'response' came from create call. It tells us which permissions we requested during Create,
    // but the user hasn't yet made a decision on
    for (auto&& entry : response.scopePermissions)
    {
        if (entry.second == GfeSDK::NVGSDK_PERMISSION_MUST_ASK)
        {
            requestPermissionsParams.scopes.push_back(entry.first);
        }
    }

    if (!requestPermissionsParams.scopes.empty())
    {
        // If the user hasn't given permission for recording yet, ask them to do so now via overlay
        m_gfesdk->RequestPermissionsAsync(requestPermissionsParams, [this, defaultLocale, highlights, numHighlights](GfeSDK::NVGSDK_RetCode rc, void* cbContext) {
            UpdateLastResultString(rc);
            if (GfeSDK::NVGSDK_SUCCEEDED(rc))
            {
                ConfigureHighlights(defaultLocale, highlights, numHighlights);
            }
        });
    }
    else
    {
        // Otherwise, go ahead and set up now
        ConfigureHighlights(defaultLocale, highlights, numHighlights);
    }
    //! [Permissions CPP]
}

void HighlightsWrapper::DeInit()
{
    // Releasing from smart pointer just to document how to delete handle
    GfeSDK::Core* gfesdkCore = m_gfesdk.release();

    //! [Release CPP]
    delete gfesdkCore;
    //! [Release CPP]
}

void HighlightsWrapper::OnTick(void)
{
    if (!m_gfesdk) return;

    // Poll for callbacks in order to execute these callbacks from the game update thread,
    // not a third party thead owned by GfeSDK. This lets us update game state from the callbacks
    // without causing a threading problem
    m_gfesdk->Poll();
}

void HighlightsWrapper::OnNotification(GfeSDK::NVGSDK_NotificationType type, GfeSDK::NotificationBase const& notification)
{
    switch (type)
    {
    case GfeSDK::NVGSDK_NOTIFICATION_PERMISSIONS_CHANGED:
    {
        GfeSDK::PermissionsChangedNotification const& n = static_cast<GfeSDK::PermissionsChangedNotification const&>(notification);

        auto hlPermission = n.scopePermissions.find(GfeSDK::NVGSDK_SCOPE_HIGHLIGHTS_VIDEO);
        if (hlPermission != n.scopePermissions.end())
        {
            m_currentPermission = permissionToStringW(hlPermission->second);
        }
        break;
    }
    case GfeSDK::NVGSDK_NOTIFICATION_OVERLAY_STATE_CHANGED:
    {
        GfeSDK::OverlayStateChangedNotification const& n = static_cast<GfeSDK::OverlayStateChangedNotification const&>(notification);

        m_lastOverlayEvent.clear();
        switch (n.state)
        {
        case GfeSDK::NVGSDK_OVERLAY_STATE_MAIN:                 m_lastOverlayEvent += L"Main Overlay Window";   break;
        case GfeSDK::NVGSDK_OVERLAY_STATE_PERMISSION:           m_lastOverlayEvent += L"Permission Overlay Window"; break;
        case GfeSDK::NVGSDK_OVERLAY_STATE_HIGHLIGHTS_SUMMARY:   m_lastOverlayEvent += L"Highlights Summary Overlay Window"; break;
        default:
            m_lastOverlayEvent += L"UNKNOWNWindow";
        }
        m_lastOverlayEvent += (n.open ? L" OPEN" : L" CLOSE");
        break;
    }
    }
}

void HighlightsWrapper::OnOpenGroup(std::string const& id)
{
    if (!m_highlights) return;

    //! [OpenGroup CPP]
    GfeSDK::HighlightOpenGroupParams params;
    params.groupId = id;
    params.groupDescriptionLocaleTable["en-US"] = id;
    m_highlights->OpenGroupAsync(params, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
    //! [OpenGroup CPP]
}

void HighlightsWrapper::OnCloseGroup(std::string const& id, bool destroy)
{
    if (!m_highlights) return;

    //! [CloseGroup CPP]
    GfeSDK::HighlightCloseGroupParams params;
    params.groupId = id;
    params.destroyHighlights = destroy;
    m_highlights->CloseGroupAsync(params, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
    //! [CloseGroup CPP]

#ifdef DOXYGEN
    //! [Async Call No Callback]
    // If the caller doesn't care about the return value, no need to pass callbacks
    GfeSDK::HighlightCloseGroupParams params = { "GROUP_1" };
    m_highlights->CloseGroupAsync(params);
    //! [Async Call No Callback]
#endif
}

void HighlightsWrapper::OnSaveScreenshot(std::string const& highlightId, std::string const& groupId)
{
    GfeSDK::ScreenshotHighlightParams params;
    params.groupId = groupId;
    params.highlightId = highlightId;
    m_highlights->SetScreenshotHighlightAsync(params, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
}

void HighlightsWrapper::OnSaveVideo(std::string const& highlightId, std::string const& groupId, int startDelta, int endDelta)
{
    //! [SaveVideo CPP]
    GfeSDK::VideoHighlightParams params;
    params.startDelta = startDelta;
    params.endDelta = endDelta;
    params.groupId = groupId;
    params.highlightId = highlightId;
    m_highlights->SetVideoHighlightAsync(params, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
    //! [SaveVideo CPP]
}

void HighlightsWrapper::OnGetNumHighlights(std::string const& groupId, GfeSDK::NVGSDK_HighlightSignificance sigFilter, GfeSDK::NVGSDK_HighlightType tagFilter)
{
    GfeSDK::GroupView v;
    v.groupId = groupId;
    v.significanceFilter = sigFilter;
    v.tagsFilter = tagFilter;

    m_highlights->GetNumberOfHighlightsAsync(v, [this](GfeSDK::NVGSDK_RetCode rc, GfeSDK::GetNumberOfHighlightsResponse const* response, void*) {
        UpdateLastResultString(rc);
        if (GfeSDK::NVGSDK_SUCCEEDED(rc))
        {
            m_lastQueryResult = std::to_wstring(response->numHighlights);
        }
    });
}

void HighlightsWrapper::OnOpenSummary(char const* groupIds[], size_t numGroups, GfeSDK::NVGSDK_HighlightSignificance sigFilter, GfeSDK::NVGSDK_HighlightType tagFilter)
{
    //! [OpenSummary CPP]
    GfeSDK::SummaryParams params;

    // Can show more than one group at a time, each with their own filters if desired
    for (size_t i = 0; i < numGroups; ++i)
    {
        GfeSDK::GroupView v;
        v.groupId = groupIds[i];
        v.significanceFilter = sigFilter;
        v.tagsFilter = tagFilter;
        params.groupViews.push_back(v);
    }

    m_highlights->OpenSummaryAsync(params, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
    //! [OpenSummary CPP]
}

void HighlightsWrapper::OnRequestLanguage(void)
{
    //! [Asynchonous Call]
    // We are passing two arguments to this function, the function lambda, and a user context. In this case, we're passing
    // the 'this' pointer as the user context. This gets passed through unmodified for use in the callback function.
    m_gfesdk->GetUILanguageAsync([this](GfeSDK::NVGSDK_RetCode rc, GfeSDK::GetUILanguageResponse const* response, void* context) {
        // Passed this pointer through as context
        HighlightsWrapper* thiz = reinterpret_cast<HighlightsWrapper*>(context);

        UpdateLastResultString(rc);
        if (GfeSDK::NVGSDK_SUCCEEDED(rc))
        {
            m_lastQueryResult = m_converter.from_bytes(response->cultureCode);
        }
    }, this);
    //! [Asynchonous Call]
}

void HighlightsWrapper::OnRequestUserSettings(void)
{
    m_highlights->GetUserSettingsAsync([this](GfeSDK::NVGSDK_RetCode rc, GfeSDK::GetUserSettingsResponse const* response, void*) {
        UpdateLastResultString(rc);
        m_lastQueryResult = L"";
        if (GfeSDK::NVGSDK_SUCCEEDED(rc))
        {
            for (auto it = response->highlightSettings.begin(); it != response->highlightSettings.end(); ++it)
            {
                m_lastQueryResult += L"\n" + m_converter.from_bytes(it->highlightId) + (it->enabled ? L": ON" : L": OFF");
            }
        }
    });
}

wchar_t const* HighlightsWrapper::GetCurrentPermissionStr(void)
{
    return m_currentPermission.c_str();
}

wchar_t const* HighlightsWrapper::GetLastOverlayEvent(void)
{
    return m_lastOverlayEvent.c_str();
}

wchar_t const* HighlightsWrapper::GetLastResult(void)
{
    return m_lastResult.c_str();
}

wchar_t const* HighlightsWrapper::GetLastQueryResult(void)
{
    return m_lastQueryResult.c_str();
}

void HighlightsWrapper::ConfigureHighlights(char const* defaultLocale, GfeSDK::NVGSDK_Highlight* highlights, size_t numHighlights)
{
    //! [ConfigureHighlights CPP]
    // Create handle to highlights module
    m_highlights.reset(GfeSDK::Highlights::Create(m_gfesdk.get()));

    GfeSDK::HighlightConfigParams configParams;
    configParams.defaultLocale = defaultLocale;

    // Set up highlight definition table
    for (size_t i = 0; i < numHighlights; ++i)
    {
        GfeSDK::HighlightDefinition highlightDef;
        highlightDef.id = highlights[i].id;
        highlightDef.userDefaultInterest = highlights[i].userInterest;
        highlightDef.significance = highlights[i].significance;
        highlightDef.highlightTags = highlights[i].highlightTags;
        for (size_t j = 0; j < highlights[i].nameTableSize; ++j)
        {
            highlightDef.nameLocaleTable[highlights[i].nameTable[j].localeCode] = highlights[i].nameTable[j].localizedString;
        }

        configParams.highlightDefinitions.push_back(highlightDef);
    }

    m_highlights->ConfigureAsync(configParams, [this](GfeSDK::NVGSDK_RetCode rc, void*) {
        UpdateLastResultString(rc);
    });
    //! [ConfigureHighlights CPP]
}

void HighlightsWrapper::UpdateLastResultString(GfeSDK::NVGSDK_RetCode rc)
{
    m_lastResult = m_converter.from_bytes(GfeSDK::RetCodeToString(rc));
}