我目前正在尝试制作一个应用程序,该应用程序将采用一个 NDI 视频源通道和一个音频源通道,并最终将其合并为一个作为自己的流,但截至目前,我的程序甚至没有被 NDI 识别分析工具,我不知道该怎么办。至少现在我希望我的程序能够注册正在运行的源,并允许我选择一个用于视频,一个用于音频。
#include <iostream>
#include "Processing.NDI.Lib.h"
int main() {
// Initialize the NDI library
NDIlib_initialize();
// Find all available sources on the local network
const NDIlib_find_create_t NDI_find_create_desc = { true, nullptr };
NDIlib_find_instance_t pNDI_find = NDIlib_find_create_v2(&NDI_find_create_desc);
if (!pNDI_find) {
std::cerr << "Failed to create NDI find instance\n";
return -1;
}
std::cout << "Searching for NDI sources...\n";
uint32_t num_sources = 0;
const NDIlib_source_t* pSources = nullptr;
NDIlib_find_wait_for_sources(pNDI_find, 1000 /* timeout_ms */);
pSources = NDIlib_find_get_current_sources(pNDI_find, &num_sources);
if (num_sources == 0) {
std::cout << "No NDI sources found\n";
return 0;
}
// Print out the available sources`your text`
std::cout << "Available NDI sources:\n";
for (uint32_t i = 0; i < num_sources; ++i) {
const NDIlib_source_t* pSource = &pSources[i];
if (pSource->p_ndi_name && pSource->p_url_address && pSource->fourcc_type == NDIlib_fourCC_type_video) { //unable to find version 5.5 names
std::cout << i << ": " << pSource->p_ndi_name << " (" << pSource->p_url_address << ")\n";
}
}
// Prompt the user to select an audio and video source
uint32_t video_source_idx = 0;
uint32_t audio_source_idx = 0;
std::cout << "Enter the index of the video source you want to use: ";
std::cin >> video_source_idx;
std::cout << "Enter the index of the audio source you want to use: ";
std::cin >> audio_source_idx;
// Create an NDI source that combines the selected audio and video sources
const NDIlib_send_create_t NDI_send_create_desc = {
"Combined Source", // Name of the NDI source
nullptr, // Groups (optional)
true, // Clock video (required for synchronization)
true // Clock audio (required for synchronization)
};
NDIlib_send_instance_t pNDI_send = NDIlib_send_create(&NDI_send_create_desc);
if (!pNDI_send) {
std::cerr << "Failed to create NDI send instance\n";
return -1;
}
// Connect to the selected video and audio sources
const NDIlib_routing_create_t NDI_routing_create_desc = {
nullptr, // Groups (optional)
1 // Number of inputs to create
};
NDIlib_routing_instance_t pNDI_routing = NDIlib_routing_create_v3(&NDI_routing_create_desc);
if (!pNDI_routing) {
std::cerr << "Failed to create NDI routing instance\n";
return -1;
}
const NDIlib_source_t* pVideoSource = &pSources[video_source_idx];
if (NDIlib_routing_change_v3(pNDI_routing, &pVideoSource->source_id, nullptr, NDIlib_routing_preference_best_quality) != NDIlib_frame_type_none) {
std::cout << "Connected\n";
return 0;
}
}
以下是官方 Finder 示例在 SDK 附带的 C++ 示例中的实现方式:
#include <cstdio>
#include <chrono>
#include <Processing.NDI.Lib.h>
#ifdef _WIN32
#ifdef _WIN64
#pragma comment(lib, "Processing.NDI.Lib.x64.lib")
#else // _WIN64
#pragma comment(lib, "Processing.NDI.Lib.x86.lib")
#endif // _WIN64
#endif // _WIN32
int main(int argc, char* argv[])
{
// Not required, but "correct" (see the SDK documentation).
if (!NDIlib_initialize())
return 0;
// We are going to create an NDI finder that locates sources on the network.
NDIlib_find_instance_t pNDI_find = NDIlib_find_create_v2();
if (!pNDI_find)
return 0;
// Run for one minute
using namespace std::chrono;
for (const auto start = high_resolution_clock::now(); high_resolution_clock::now() - start < minutes(1);) {
// Wait up till 5 seconds to check for new sources to be added or removed
if (!NDIlib_find_wait_for_sources(pNDI_find, 5000 /* milliseconds */)) {
printf("No change to the sources found.\n");
continue;
}
// Get the updated list of sources
uint32_t no_sources = 0;
const NDIlib_source_t* p_sources = NDIlib_find_get_current_sources(pNDI_find, &no_sources);
// Display all the sources.
printf("Network sources (%u found).\n", no_sources);
for (uint32_t i = 0; i < no_sources; i++)
printf("%u. %s\n", i + 1, p_sources[i].p_ndi_name);
}
// Destroy the NDI finder
NDIlib_find_destroy(pNDI_find);
// Finished
NDIlib_destroy();
// Success. We are done
return 0;