Skip to content

Commit 1807a40

Browse files
committed
Minor changes
1 parent 4d32e4f commit 1807a40

File tree

4 files changed

+68
-34
lines changed

4 files changed

+68
-34
lines changed

example.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,41 @@ grandiose.find({
2626

2727
console.log('Found NDI sources', sources.length)
2828

29+
console.log(sources)
30+
2931
const source = sources[0]
3032

3133
console.log('NDI Source chosen', source)
3234

3335
const receiver = await grandiose.receive({ source: source })
3436

3537
// Read 10 frames
36-
for (let i = 0; i < 10; i++) {
38+
const frameCount = {}
39+
for (let i = 0; i < 50; i++) {
3740
try {
38-
console.log('Requesting video frame', i)
39-
const videoFrame = await receiver.video(receiveTimeout)
40-
console.log('Received video frame', i)
41-
console.log(videoFrame)
41+
console.log('Requesting frame', i)
42+
const frame = await receiver.data(receiveTimeout) // Any type of frame
43+
if (!typeof frame === 'object' || !frame.type) {
44+
continue
45+
}
46+
// Switch based on type of frame
47+
switch (frame.type) {
48+
case 'statusChange':
49+
console.log('Status change:', frame)
50+
break
51+
case 'audio':
52+
console.log('Audio frame received', i)
53+
// console.log('Audio frame data:', frame)
54+
break
55+
case 'video':
56+
console.log('Video frame received', i)
57+
console.log('Video frame framerate:', frame.frameRateN / frame.frameRateD)
58+
break
59+
default:
60+
// Do not do anything with this type of data frame
61+
console.log('Unhandled type:', frame.type)
62+
break
63+
}
4264
} catch (e) {
4365
console.error('Error on requested video frame', i)
4466
console.error(e)

src/grandiose.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
#endif // _WIN64
2626
#endif // _WIN32
2727

28+
#include "node_api.h"
29+
/* Includes of grandiose methods */
2830
#include "grandiose_util.h"
2931
#include "grandiose_find.h"
3032
#include "grandiose_send.h"
3133
#include "grandiose_send_video.h"
3234
#include "grandiose_receive.h"
33-
#include "node_api.h"
3435

3536
napi_value version(napi_env env, napi_callback_info info) {
3637
napi_status status;

src/grandiose_send_video.cc

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ napi_value sendVideo(napi_env env, napi_callback_info info)
3939

4040
// Create an NDI source that is called "My 16bpp Audio" and is clocked to the audio.
4141
NDIlib_send_create_t NDI_send_create_desc;
42+
43+
// Set name of NDI source
4244
NDI_send_create_desc.p_ndi_name = "My 8bpp video";
4345
NDI_send_create_desc.clock_audio = true;
4446

@@ -47,40 +49,49 @@ napi_value sendVideo(napi_env env, napi_callback_info info)
4749
if (!pNDI_send)
4850
NAPI_THROW_ERROR("Failed to create send instance.");
4951

50-
// We are going to create a 1920x1080 interlaced frame at 29.97Hz.
51-
NDIlib_video_frame_v2_t NDI_video_frame;
52-
NDI_video_frame.xres = 1920;
53-
NDI_video_frame.yres = 1080;
54-
NDI_video_frame.FourCC = NDIlib_FourCC_type_BGRX;
55-
NDI_video_frame.p_data = (uint8_t*)malloc(NDI_video_frame.xres*NDI_video_frame.yres * 4);
52+
// We are going to create a 1920x1080 interlaced frame at 29.97Hz.
53+
NDIlib_video_frame_v2_t NDI_video_frame;
54+
55+
NDI_video_frame.xres = 1920;
56+
NDI_video_frame.yres = 1080;
57+
NDI_video_frame.FourCC = NDIlib_FourCC_type_BGRX;
58+
59+
// Calculate number of pixels in video frame
60+
const auto number_of_pixels = NDI_video_frame.xres * NDI_video_frame.yres;
61+
62+
// Allocate buffer for the video frame
63+
// 8 bit per channel (BGRX) per pixel
64+
NDI_video_frame.p_data = (uint8_t *)malloc(number_of_pixels * 4);
5665

57-
// Run for one minute
58-
using namespace std::chrono;
59-
for (const auto start = high_resolution_clock::now(); high_resolution_clock::now() - start < minutes(5);)
60-
{ // Get the current time
61-
const auto start_send = high_resolution_clock::now();
66+
// Run for 5 minutes
67+
using namespace std::chrono;
68+
for (const auto start = high_resolution_clock::now(); high_resolution_clock::now() - start < minutes(5);)
69+
{ // Get the current time
70+
const auto start_send = high_resolution_clock::now();
6271

63-
// Send 200 frames
64-
for (int idx = 200; idx; idx--)
65-
{ // Fill in the buffer. It is likely that you would do something much smarter than this.
66-
memset((void*)NDI_video_frame.p_data, (idx & 1) ? 255 : 0, NDI_video_frame.xres*NDI_video_frame.yres * 4);
72+
// Send 200 frames
73+
for (int idx = 200; idx; idx--)
74+
{
75+
// Fill in the buffer. It is likely that you would do something much smarter than this.
76+
// All pixels is painted the same color - the color is switching between black and white based on the frame number
77+
memset((void *)NDI_video_frame.p_data, (idx & 1) ? 255 : 0, number_of_pixels * 4);
6778

68-
// We now submit the frame. Note that this call will be clocked so that we end up submitting at exactly 29.97fps.
69-
NDIlib_send_send_video_v2(pNDI_send, &NDI_video_frame);
70-
}
79+
// We now submit the frame. Note that this call will be clocked so that we end up submitting at exactly 29.97fps.
80+
NDIlib_send_send_video_v2(pNDI_send, &NDI_video_frame);
81+
}
7182

72-
// Just display something helpful
73-
printf("200 frames sent, at %1.2ffps\n", 200.0f / duration_cast<duration<float>>(high_resolution_clock::now() - start_send).count());
74-
}
83+
// Just display something helpful
84+
printf("200 frames sent, at %1.2ffps\n", 200.0f / duration_cast<duration<float>>(high_resolution_clock::now() - start_send).count());
85+
}
7586

76-
// Free the video frame
77-
free(NDI_video_frame.p_data);
87+
// Free the video frame
88+
free(NDI_video_frame.p_data);
7889

79-
// Destroy the NDI sender
80-
NDIlib_send_destroy(pNDI_send);
90+
// Destroy the NDI sender
91+
NDIlib_send_destroy(pNDI_send);
8192

82-
// Not required, but nice
83-
NDIlib_destroy();
93+
// Not required, but nice
94+
NDIlib_destroy();
8495

8596
status = napi_get_undefined(env, &result);
8697
CHECK_STATUS;

src/grandiose_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <chrono>
2121
#include <string>
2222
#include <Processing.NDI.Lib.h>
23-
#include "grandiose_util.h"
2423
#include "node_api.h"
24+
#include "grandiose_util.h"
2525

2626
/*
2727
* Got this from https://codereview.stackexchange.com/q/164322

0 commit comments

Comments
 (0)