-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathBackEndCapture.cpp
executable file
·266 lines (234 loc) · 10 KB
/
BackEndCapture.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
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
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
#include "BackEndCapture.h"
#include "ApiLock.h"
#include <ppltasks.h>
using namespace PhoneVoIPApp::BackEnd;
using namespace Windows::System::Threading;
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Platform;
using namespace Windows::Phone::Media::Capture;
using namespace Windows::Storage::Streams;
using namespace Concurrency;
BackEndCapture::BackEndCapture() :
started(false),
videoOnlyDevice(nullptr),
pVideoSink(NULL),
pVideoDevice(NULL),
cameraLocation(CameraSensorLocation::Front)
{
hStopCompleted = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
if (!hStopCompleted)
{
throw ref new Platform::Exception(HRESULT_FROM_WIN32(GetLastError()), L"Could not create shutdown event");
}
hStartCompleted = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
if (!hStartCompleted)
{
throw ref new Platform::Exception(HRESULT_FROM_WIN32(GetLastError()), L"Could not create start event");
}
}
void BackEndCapture::Start(CameraLocation newCameraLocation)
{
// Make sure only one API call is in progress at a time
std::lock_guard<std::recursive_mutex> lock(g_apiLock);
if (started)
return;
if(newCameraLocation == CameraLocation::Front)
{
cameraLocation = Windows::Phone::Media::Capture::CameraSensorLocation::Front;
}
else if(newCameraLocation == CameraLocation::Back)
{
cameraLocation = Windows::Phone::Media::Capture::CameraSensorLocation::Back;
}
InitCapture();
started = true;
}
void BackEndCapture::Stop()
{
// Make sure only one API call is in progress at a time
std::lock_guard<std::recursive_mutex> lock(g_apiLock);
::OutputDebugString(L"+[BackendCapture::Stop] => Trying to stop capture\n");
if (!started)
{
::OutputDebugString(L"-[BackendCapture::Stop] => finished stopping capture\n");
return;
}
if (videoOnlyDevice)
{
OutputDebugString(L"Destroying VideoCaptureDevice\n");
try
{
videoOnlyDevice->StopRecordingAsync()->Completed = ref new AsyncActionCompletedHandler([this] (IAsyncAction ^action, Windows::Foundation::AsyncStatus status){
if(status == Windows::Foundation::AsyncStatus::Completed)
{
OutputDebugString(L"[BackendCapture::StopRecordingAsync] Video successfully stopped.\n");
}
else
{
OutputDebugString(L"[BackEndCapture::StopRecordingAsync] Error occurred while stopping recording.\n");
}
this->videoCaptureAction = nullptr;
this->videoOnlyDevice = nullptr;
started = false;
SetEvent(hStopCompleted);
});
}
catch(...)
{
// A Platform::ObjectDisposedException can be raised if the app has had its access
// to video revoked (most commonly when the app is going out of the foreground)
OutputDebugString(L"Exception caught while destroying video capture\n");
this->videoCaptureAction = nullptr;
this->videoOnlyDevice = nullptr;
started = false;
SetEvent(hStopCompleted);
}
if (pVideoDevice)
{
pVideoDevice->Release();
pVideoDevice = NULL;
}
if (pVideoSink)
{
pVideoSink->Release();
pVideoSink = NULL;
}
}
::OutputDebugString(L"-[BackendCapture::Stop] => finished stopping capture\n");
}
BackEndCapture::~BackEndCapture()
{
if(m_ToggleThread)
{
m_ToggleThread->Cancel();
m_ToggleThread->Close();
m_ToggleThread = nullptr;
}
}
void BackEndCapture::InitCapture()
{
::OutputDebugString(L"+[BackendCapture::InitCapture] => Initializing Capture\n");
Windows::Foundation::Size dimensions;
dimensions.Width = 640;
dimensions.Height = 480;
Collections::IVectorView<Size> ^availableSizes = AudioVideoCaptureDevice::GetAvailableCaptureResolutions(this->cameraLocation);
Collections::IIterator<Windows::Foundation::Size> ^availableSizesIterator = availableSizes->First();
IAsyncOperation<AudioVideoCaptureDevice^> ^openOperation = nullptr;
while(!openOperation && availableSizesIterator->HasCurrent)
{
// TODO: You should select the appropriate resolution that's supported here,
// TODO: and then setup your renderer with that selected res.
// TODO: This shows how to iterate through all supported resolutions. We're assuming 640x480 support
if(availableSizesIterator->Current.Height == 480 && availableSizesIterator->Current.Width == 640)
{
openOperation = AudioVideoCaptureDevice::OpenForVideoOnlyAsync(this->cameraLocation, dimensions);
}
availableSizesIterator->MoveNext();
}
openOperation->Completed = ref new AsyncOperationCompletedHandler<AudioVideoCaptureDevice^>([this] (IAsyncOperation<AudioVideoCaptureDevice^> ^operation, Windows::Foundation::AsyncStatus status)
{
if(status == Windows::Foundation::AsyncStatus::Completed)
{
std::lock_guard<std::recursive_mutex> lock(g_apiLock);
::OutputDebugString(L"+[BackendCapture::InitCapture] => OpenAsyncOperation started\n");
auto videoDevice = operation->GetResults();
this->videoOnlyDevice = videoDevice;
IAudioVideoCaptureDeviceNative *pNativeDevice = NULL;
HRESULT hr = reinterpret_cast<IUnknown*>(videoDevice)->QueryInterface(__uuidof(IAudioVideoCaptureDeviceNative), (void**) &pNativeDevice);
if (NULL == pNativeDevice || FAILED(hr))
{
throw ref new FailureException("Unable to QI IAudioVideoCaptureDeviceNative");
}
// Save off the native device
this->pVideoDevice = pNativeDevice;
// Create the sink
MakeAndInitialize<CaptureSampleSink>(&(this->pVideoSink), transportController);
this->pVideoSink->SetTransport(this->transportController);
pNativeDevice->SetVideoSampleSink(this->pVideoSink);
// Use the same encoding format as in VideoMediaStreamSource.cs
videoDevice->VideoEncodingFormat = CameraCaptureVideoFormat::H264;
SetEvent(hStartCompleted);
// Start recording to our sink
this->videoCaptureAction = videoDevice->StartRecordingToSinkAsync();
videoCaptureAction->Completed = ref new AsyncActionCompletedHandler([this] (IAsyncAction ^asyncInfo, Windows::Foundation::AsyncStatus status)
{
if(status == Windows::Foundation::AsyncStatus::Completed)
{
::OutputDebugString(L"[BackendCapture::InitCapture] => StartRecordingToSinkAsync completed\n");
}
else if(status == Windows::Foundation::AsyncStatus::Error || status == Windows::Foundation::AsyncStatus::Canceled)
{
::OutputDebugString(L"[BackendCapture::InitCapture] => StartRecordingToSinkAsync did not complete\n");
}
});
::OutputDebugString(L"-[BackendCapture::InitCapture] => OpenAsyncOperation Completed\n");
}
else if(status == Windows::Foundation::AsyncStatus::Canceled)
{
::OutputDebugString(L"[BackendCapture::InitCapture] => OpenAsyncOperation Canceled\n");
}
else if(status == Windows::Foundation::AsyncStatus::Error)
{
::OutputDebugString(L"[BackendCapture::InitCapture] => OpenAsyncOperation encountered an error.\n");
}
});
::OutputDebugString(L"-[BackendCapture::InitCapture] => Initializing Capture\n");
}
void BackEndCapture::ToggleCamera()
{
std::lock_guard<std::recursive_mutex> lock(g_apiLock);
if(m_ToggleThread)
{
m_ToggleThread->Cancel();
m_ToggleThread->Close();
m_ToggleThread = nullptr;
}
m_ToggleThread = ThreadPool::RunAsync(ref new WorkItemHandler(this, &BackEndCapture::ToggleCameraThread), WorkItemPriority::High, WorkItemOptions::TimeSliced);
}
void BackEndCapture::ToggleCameraThread(Windows::Foundation::IAsyncAction^ operation)
{
::OutputDebugString(L"+[BackendCapture::ToggleCamera] => Toggling camera \n");
CameraLocation newCameraLocation;
ResetEvent(hStopCompleted);
Stop();
DWORD waitResult = WaitForSingleObjectEx(hStopCompleted, INFINITE, FALSE);
if(waitResult == WAIT_OBJECT_0)
{
ResetEvent(hStartCompleted);
if(cameraLocation == Windows::Phone::Media::Capture::CameraSensorLocation::Back)
{
newCameraLocation = CameraLocation::Front;
}
else
{
newCameraLocation = CameraLocation::Back;
}
Start(newCameraLocation);
}
else
{
throw ref new Platform::Exception(HRESULT_FROM_WIN32(waitResult), L"Error waiting for capture to stop when toggling cameras");
}
waitResult = WaitForSingleObjectEx(hStartCompleted, INFINITE, FALSE);
if(waitResult == WAIT_OBJECT_0)
{
CameraLocationChanged(newCameraLocation);
}
else
{
throw ref new Platform::Exception(HRESULT_FROM_WIN32(waitResult), L"Error waiting for capture to start when toggling cameras");
}
::OutputDebugString(L"-[BackendCapture::ToggleCamera] => Toggling camera \n");
}
void BackEndCapture::SetTransport(BackEndTransport^ transport)
{
transportController = transport;
}