Skip to content

Commit d964e5e

Browse files
fixed CPU hanging issue
1 parent f58ccc2 commit d964e5e

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,15 @@ public async Task ChangeLiveStream()
342342
}
343343
_selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList[_appModel.SelectedCameraIndex];
344344

345+
MediaCapture.FindAllVideoProfiles(_selectedMediaFrameSourceGroup.Id);
345346
// Create MediaCapture and its settings
346347
var settings = new MediaCaptureInitializationSettings
347348
{
348349
SourceGroup = _selectedMediaFrameSourceGroup,
349350
PhotoCaptureSource = PhotoCaptureSource.Auto,
350351
MemoryPreference = _appModel.UseGPU ? MediaCaptureMemoryPreference.Auto : MediaCaptureMemoryPreference.Cpu,
351-
StreamingCaptureMode = StreamingCaptureMode.Video
352+
StreamingCaptureMode = StreamingCaptureMode.Video,
353+
MediaCategory = MediaCategory.Communications,
352354
};
353355
_mediaCapture = new MediaCapture();
354356
await _mediaCapture.InitializeAsync(settings);
@@ -378,6 +380,12 @@ public async Task ChangeLiveStream()
378380
}
379381
}
380382

383+
384+
private void _mediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
385+
{
386+
Debug.WriteLine("_mediaCapture FAIL! " + errorEventArgs.Message);
387+
}
388+
381389
private async Task LoadModelAsync()
382390
{
383391
Debug.Write("LoadModelBegin | ");

Samples/StyleTransfer/Package.appxmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Identity
1010
Name="7fafd263-abdf-4f88-87a1-ffb0922489d1"
1111
Publisher="CN=t-limay"
12-
Version="1.0.0.0" />
12+
Version="1.0.8.0" />
1313

1414
<mp:PhoneIdentity PhoneProductId="7fafd263-abdf-4f88-87a1-ffb0922489d1" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
1515

Samples/StyleTransfer/StyleTransfer.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717
<FileAlignment>512</FileAlignment>
1818
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1919
<WindowsXamlEnableOverview>true</WindowsXamlEnableOverview>
20-
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
20+
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
21+
<GenerateAppInstallerFile>True</GenerateAppInstallerFile>
22+
<AppxAutoIncrementPackageRevision>False</AppxAutoIncrementPackageRevision>
23+
<GenerateTestArtifacts>True</GenerateTestArtifacts>
24+
<AppxBundle>Always</AppxBundle>
25+
<AppxBundlePlatforms>x64</AppxBundlePlatforms>
26+
<AppInstallerUri>C:\Users\t-limay</AppInstallerUri>
27+
<HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks>
28+
<PackageCertificateKeyFile>StyleTransfer_TemporaryKey.pfx</PackageCertificateKeyFile>
29+
<AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm>
2130
</PropertyGroup>
2231
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
2332
<DebugSymbols>true</DebugSymbols>
@@ -234,6 +243,9 @@
234243
<Name>StyleTransferEffectCpp</Name>
235244
</ProjectReference>
236245
</ItemGroup>
246+
<ItemGroup>
247+
<None Include="StyleTransfer_TemporaryKey.pfx" />
248+
</ItemGroup>
237249
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
238250
<VisualStudioVersion>14.0</VisualStudioVersion>
239251
</PropertyGroup>

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,41 @@ namespace winrt::StyleTransferEffectCpp::implementation
2424
bool StyleTransferEffect::IsReadOnly() { return false; }
2525
void StyleTransferEffect::DiscardQueuedFrames() {}
2626

27-
void StyleTransferEffect::Close(MediaEffectClosedReason) {
27+
void StyleTransferEffect::Close(MediaEffectClosedReason m) {
2828
OutputDebugString(L"Close Begin | ");
29-
Processing.lock();
29+
std::lock_guard<mutex> guard{ Processing };
3030
OutputDebugString(L"Close\n");
3131
if (Binding != nullptr) Binding.Clear();
3232
if (Session != nullptr) Session.Close();
3333
outputTransformed.Close();
34-
Processing.unlock();
3534
}
3635

3736
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
38-
OutputDebugString(L"Start ProcessFrame | ");
37+
// Play around with dropping frames so as to not overwhelm when processing slowly
38+
if ((frameNum + 1) % 5 == 0) {
39+
frameNum = 0;
40+
OutputDebugString(L"Drop");
41+
return;
42+
}
43+
OutputDebugString(L"PF Start | ");
3944
auto startSync = std::chrono::high_resolution_clock::now();
4045

4146
VideoFrame inputFrame = context.InputFrame();
4247
VideoFrame outputFrame = context.OutputFrame();
4348

44-
Processing.lock();
49+
std::lock_guard<mutex> guard{ Processing };
4550
OutputDebugString(L"PF Locked | ");
4651
Binding.Bind(InputImageDescription, inputFrame);
4752
Binding.Bind(OutputImageDescription, outputTransformed);
4853

4954
OutputDebugString(L"PF Eval | ");
5055
Session.Evaluate(Binding, L"test");
51-
outputTransformed.CopyToAsync(context.OutputFrame());
52-
Processing.unlock();
53-
OutputDebugString(L"PF Unlocked");
56+
outputTransformed.CopyToAsync(outputFrame).get();
5457

5558
auto syncTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startSync);
5659
Notifier.SetFrameRate(1000.f / syncTime.count()); // Convert to FPS: milli to seconds, invert
60+
OutputDebugString(L"PF End\n ");
61+
frameNum++;
5762
}
5863

5964
void StyleTransferEffect::SetEncodingProperties(VideoEncodingProperties props, IDirect3DDevice device) {

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
3636
VideoEncodingProperties encodingProperties;
3737
std::mutex Processing;
3838
StyleTransferEffectNotifier Notifier;
39+
int frameNum = 0;
3940
};
4041
}
4142

0 commit comments

Comments
 (0)