Skip to content

Commit 41262cd

Browse files
committed
can access audio/video buffer using arrays rather than iMemoryBuffer
1 parent 56f7574 commit 41262cd

File tree

6 files changed

+178
-23
lines changed

6 files changed

+178
-23
lines changed

windows/wrapper/override/cppwinrt/AudioData.cpp

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
using namespace winrt;
3232

33+
34+
struct __declspec(uuid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")) __declspec(novtable) IMemoryBufferByteAccess : ::IUnknown
35+
{
36+
virtual HRESULT __stdcall GetBuffer(uint8_t** value, uint32_t* capacity) = 0;
37+
};
38+
3339
struct AudioDataVectorView : implements<
3440
AudioDataVectorView,
3541
Windows::Foundation::Collections::IVectorView<int16_t>,
@@ -279,18 +285,34 @@ bool Org::WebRtc::implementation::AudioData::ReadOnly()
279285
}
280286

281287
//------------------------------------------------------------------------------
282-
Windows::Foundation::Collections::IVectorView< int16_t > Org::WebRtc::implementation::AudioData::Data()
288+
Windows::Foundation::IMemoryBuffer Org::WebRtc::implementation::AudioData::Data()
283289
{
284-
if (!native_) { throw hresult_error(E_POINTER); }
290+
if (!native_)
291+
return {nullptr};
292+
293+
Windows::Foundation::MemoryBuffer memBuffer{ static_cast<uint32_t>(sizeof(int16_t)*native_->size()) };
285294

286-
auto data = native_->data();
287-
auto size = native_->size();
295+
auto ref = memBuffer.CreateReference();
288296

289-
return winrt::make<AudioDataVectorView>(data, size);
297+
auto byteAccess = ref.as<IMemoryBufferByteAccess>();
298+
if (!byteAccess)
299+
return {nullptr};
300+
301+
uint8_t* dest{};
302+
uint32_t destSize {};
303+
if (FAILED(byteAccess->GetBuffer(&dest, &destSize)))
304+
return {nullptr};
305+
306+
if (destSize != (sizeof(int16_t)*native_->size()))
307+
return {nullptr};
308+
309+
memcpy(dest, native_->data(), destSize);
310+
311+
return memBuffer;
290312
}
291313

292314
//------------------------------------------------------------------------------
293-
void Org::WebRtc::implementation::AudioData::Data(Windows::Foundation::Collections::IVectorView< int16_t > const & value)
315+
void Org::WebRtc::implementation::AudioData::Data(Windows::Foundation::IMemoryBuffer const& value)
294316
{
295317
if (!native_)
296318
throw hresult_error(E_POINTER);
@@ -300,30 +322,91 @@ void Org::WebRtc::implementation::AudioData::Data(Windows::Foundation::Collectio
300322
return;
301323
}
302324

303-
auto data = native_->data();
304-
if (!data) {
305-
native_->wrapper_init_org_webRtc_AudioData(SafeInt<size_t>(value.Size()));
325+
auto ref = value.CreateReference();
326+
327+
auto byteAccess = ref.as<IMemoryBufferByteAccess>();
328+
if (!byteAccess) {
329+
native_->wrapper_init_org_webRtc_AudioData();
330+
return;
331+
}
332+
333+
uint8_t* source{};
334+
uint32_t sourceSize{};
335+
if (FAILED(byteAccess->GetBuffer(&source, &sourceSize))) {
336+
native_->wrapper_init_org_webRtc_AudioData();
337+
return;
306338
}
307339

308340
ZS_ASSERT(!native_->readOnly());
309341

310342
auto bufferSize = native_->size();
311-
decltype(bufferSize) passedSize = SafeInt<decltype(bufferSize)>(value.Size());
343+
uint64_t passedSize = sourceSize / sizeof(int16_t);;
312344

313345
ZS_ASSERT(passedSize <= bufferSize);
314346

315347
if (passedSize > bufferSize)
316348
passedSize = bufferSize;
317349

318350
int16_t * const firstData = native_->mutableData();
319-
int16_t * const lastData = firstData + passedSize;
320351

321-
if (!firstData)
322-
throw hresult_error(E_UNEXPECTED);
352+
memcpy(firstData, source, sizeof(int16_t)*passedSize);
353+
}
323354

324-
winrt::array_view<int16_t> view(firstData, lastData);
325-
value.GetMany(0, view);
355+
//------------------------------------------------------------------------------
356+
uint64_t Org::WebRtc::implementation::AudioData::Length()
357+
{
358+
if (!native_)
359+
return {};
360+
361+
return SafeInt<uint64_t>(native_->size());
326362
}
327363

364+
//------------------------------------------------------------------------------
365+
uint64_t Org::WebRtc::implementation::AudioData::GetData(com_array<int16_t>& values)
366+
{
367+
if (!native_)
368+
return 0;
369+
370+
uint64_t inSize = SafeInt<decltype(inSize)>(values.size());
371+
372+
uint64_t size = native_->size();
373+
374+
size = inSize < size ? inSize : size;
375+
376+
auto dest = values.data();
377+
auto source = native_->data();
378+
379+
if ((!dest) ||
380+
(!source))
381+
return 0;
382+
383+
memcpy(dest, source, sizeof(int16_t)*size);
384+
385+
return size;
386+
}
387+
388+
//------------------------------------------------------------------------------
389+
uint64_t Org::WebRtc::implementation::AudioData::SetData(array_view<int16_t const> values)
390+
{
391+
if (!native_)
392+
return 0;
393+
394+
uint64_t inSize = SafeInt<decltype(inSize)>(values.size());
395+
396+
uint64_t size = native_->size();
397+
398+
size = inSize < size ? inSize : size;
399+
400+
auto source = values.data();
401+
auto dest = native_->mutableData();
402+
403+
if ((!dest) ||
404+
(!source))
405+
return 0;
406+
407+
memcpy(dest, source, sizeof(int16_t)*size);
408+
409+
return size;
410+
}
328411

329412
#endif //ifndef CPPWINRT_USE_GENERATED_ORG_WEBRTC_AUDIODATA

windows/wrapper/override/cppwinrt/AudioData.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ namespace winrt {
7373
/// <summary>
7474
/// Gets or sets the audio samples data. <summary> </summary>
7575
/// </summary>
76-
Windows::Foundation::Collections::IVectorView< int16_t > Data();
77-
void Data(Windows::Foundation::Collections::IVectorView< int16_t > const & value);
76+
Windows::Foundation::IMemoryBuffer Data();
77+
void Data(Windows::Foundation::IMemoryBuffer const& value);
7878

79+
uint64_t Length();
80+
uint64_t GetData(com_array<int16_t>& values);
81+
uint64_t SetData(array_view<int16_t const> values);
7982
};
8083

8184
} // namespace implementation

windows/wrapper/override/cppwinrt/VideoData.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "RTCRtpTransceiver.h"
2828
#include "RTCRtpSender.h"
2929

30+
#include <zsLib/SafeInt.h>
31+
3032
using namespace winrt;
3133

3234
struct __declspec(uuid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")) __declspec(novtable) IMemoryBufferByteAccess : ::IUnknown
@@ -178,6 +180,62 @@ bool Org::WebRtc::implementation::VideoData::Is16BitColorSpace()
178180
return ::Internal::Helper::ToCppWinrt_Bool(native_->get_is16BitColorSpace());
179181
}
180182

183+
//------------------------------------------------------------------------------
184+
uint64_t Org::WebRtc::implementation::VideoData::Length()
185+
{
186+
if (!native_)
187+
return 0;
188+
189+
return SafeInt<uint64_t>(native_->get_size());
190+
}
191+
192+
//------------------------------------------------------------------------------
193+
uint64_t Org::WebRtc::implementation::VideoData::GetData8bit(com_array<uint8_t>& values)
194+
{
195+
if (!native_)
196+
return 0;
197+
198+
uint64_t inSize = SafeInt<decltype(inSize)>(values.size());
199+
200+
uint64_t size = native_->get_size();
201+
202+
size = inSize < size ? inSize : size;
203+
204+
auto dest = values.data();
205+
auto source = native_->get_data8bit();
206+
207+
if ((!dest) ||
208+
(!source))
209+
return 0;
210+
211+
memcpy(dest, source, sizeof(uint8_t)*size);
212+
213+
return size;
214+
}
215+
216+
//------------------------------------------------------------------------------
217+
uint64_t Org::WebRtc::implementation::VideoData::GetData16bit(com_array<uint16_t>& values)
218+
{
219+
if (!native_)
220+
return 0;
221+
222+
uint64_t inSize = SafeInt<decltype(inSize)>(values.size());
223+
uint64_t size = native_->get_size();
224+
225+
size = inSize < size ? inSize : size;
226+
227+
auto dest = values.data();
228+
auto source = native_->get_data16bit();
229+
230+
if ((!dest) ||
231+
(!source))
232+
return 0;
233+
234+
memcpy(dest, source, sizeof(uint16_t)*size);
235+
236+
return size;
237+
}
238+
181239
//------------------------------------------------------------------------------
182240
winrt::Windows::Foundation::IMemoryBuffer Org::WebRtc::implementation::VideoData::Data8bit()
183241
{

windows/wrapper/override/cppwinrt/VideoData.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ namespace winrt {
4747
static wrapper::org::webRtc::VideoDataPtr FromCppWinrt(Org::WebRtc::VideoData const & value);
4848
static wrapper::org::webRtc::VideoDataPtr FromCppWinrt(Org::WebRtc::IVideoData const & value);
4949

50-
51-
52-
5350
public:
5451
/// <summary>
5552
/// Cast from Org::WebRtc::IVideoData to VideoData
@@ -61,6 +58,11 @@ namespace winrt {
6158

6259
bool Is8BitColorSpace();
6360
bool Is16BitColorSpace();
61+
62+
uint64_t Length();
63+
uint64_t GetData8bit(com_array<uint8_t>& values);
64+
uint64_t GetData16bit(com_array<uint16_t>& values);
65+
6466
/// <summary>
6567
/// Gets or sets the video 8 bit color space data. <summary>
6668
/// </summary>
@@ -71,7 +73,6 @@ namespace winrt {
7173
/// </summary>
7274
/// </summary>
7375
Windows::Foundation::IMemoryBuffer Data16bit();
74-
7576
};
7677

7778
} // namespace implementation

windows/wrapper/override/msidl/Org.WebRtc.AudioData.idl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ namespace Org
1919
Boolean ReadOnly { get; };
2020

2121
/// <summary>
22-
/// Gets or sets the audio samples data.
22+
/// Gets or sets the audio samples data (int16s encoded in little endian 2 byte pairs).
2323
/// </summary>
24-
Windows.Foundation.Collections.IVectorView< Int16 > Data { get; set; };
24+
Windows.Foundation.IMemoryBuffer Data{ get; set; };
25+
26+
UInt64 Length{ get; };
27+
28+
UInt64 GetData(out Int16[] values);
29+
UInt64 SetData(Int16[] values);
2530
};
2631

2732
runtimeclass AudioData : [default] IAudioData, Windows.Foundation.IClosable

windows/wrapper/override/msidl/Org.WebRtc.VideoData.idl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ namespace Org
1717

1818
Boolean Is16BitColorSpace { get; };
1919

20+
UInt64 Length{ get; };
21+
22+
UInt64 GetData8bit(out UInt8[] values);
23+
UInt64 GetData16bit(out UInt16[] values);
24+
2025
/// <summary>
2126
/// Gets or sets the video 8 bit color space data.
2227
/// </summary>

0 commit comments

Comments
 (0)