Canaux de données personnalisés avec communication à distance holographique et l’API OpenXR
Utilisez des canaux de données personnalisés pour envoyer des données personnalisées via une connexion à distance établie.
Importante
Les canaux de données personnalisés nécessitent une application distante personnalisée et une application de lecteur personnalisée. Cela permet la communication entre les deux applications personnalisées.
Conseil
Vous trouverez un exemple de ping-pong simple dans les exemples distants et joueurs dans le référentiel github d’exemples de communication à distance Holographic.
Supprimez les marques de #define ENABLE_CUSTOM_DATA_CHANNEL_SAMPLE
commentaire dans les fichiers OpenXrProgramm.cpp et SamplePlayerMain.h pour activer l’exemple de code.
Remarque
La spécification détaillée se trouve dans le référentiel github d’exemples de communication à distance holographique.
Créer un canal de données personnalisé
Un canal de données personnalisé est défini par le XrRemotingDataChannelMSFT
handle :
XrRemotingDataChannelMSFT m_userDataChannel;
Une fois la connexion établie, de nouveaux canaux de données peuvent être créés via la xrCreateRemotingDataChannelMSFT
fonction :
XrRemotingDataChannelCreateInfoMSFT channelInfo{static_cast<XrStructureType>(XR_TYPE_REMOTING_DATA_CHANNEL_CREATE_INFO_MSFT)};
channelInfo.channelId = 0;
channelInfo.channelPriority = XR_REMOTING_DATA_CHANNEL_PRIORITY_LOW_MSFT;
CHECK_XRCMD(m_extensions.xrCreateRemotingDataChannelMSFT(m_instance.Get(), m_systemId, &channelInfo, &m_userDataChannel));
Des canaux de données personnalisés peuvent être créés à partir du lecteur et de l’application distante, même si les runtimes sont différents.
Si un canal de données est créé par le côté joueur, le côté distant reçoit une notification avec la structure d’événements XrEventDataRemotingDataChannelCreatedMSFT
:
XrEventDataBuffer eventData{};
while (pollEvent(eventData))
{
switch (eventData.type)
{
case XR_TYPE_EVENT_DATA_REMOTING_DATA_CHANNEL_CREATED_MSFT:
{
auto channelCreatedEventData = reinterpret_cast<const XrEventDataRemotingDataChannelCreatedMSFT*>(&eventData);
m_userDataChannel = channelCreatedEventData->channel;
break;
}
}
}
L’état initial XrRemotingDataChannelStatusMSFT
après l’appel xrCreateRemotingDataChannelMSFT
est XR_REMOTING_DATA_CHANNEL_STATUS_OPEN_PENDING_MSFT
.
Une fois que le canal de données a été entièrement établi, l’état du canal passe à XR_REMOTING_DATA_CHANNEL_STATUS_OPENED_MSFT
.
La XrEventDataRemotingDataChannelOpenedMSFT
structure d’événements est placée dans la file d’attente d’événements lorsque l’état d’un canal de données créé précédemment passe de XR_REMOTING_DATA_CHANNEL_STATUS_OPEN_PENDING_MSFT
à XR_REMOTING_DATA_CHANNEL_STATUS_OPENED_MSFT
.
Obtenir l’état du canal
La xrGetRemotingDataChannelStateMSFT
fonction peut être utilisée pour interroger l’état du canal de données :
XrRemotingDataChannelStateMSFT channelState{static_cast<XrStructureType>(XR_TYPE_REMOTING_DATA_CHANNEL_STATE_MSFT)};
CHECK_XRCMD(m_extensions.xrGetRemotingDataChannelStateMSFT(m_userDataChannel, &channelState));
Envoyer des données
Si le canal est ouvert, la xrSendRemotingDataMSFT
fonction est utilisée pour envoyer des données au côté joueur :
if (channelState.connectionStatus == XR_REMOTING_DATA_CHANNEL_STATUS_OPENED_MSFT) {
// Only send the packet if the send queue is smaller than 1MiB
if (channelState.sendQueueSize >= 1 * 1024 * 1024) {
return;
}
uint8_t data[] = {1};
XrRemotingDataChannelSendDataInfoMSFT sendInfo{
static_cast<XrStructureType>(XR_TYPE_REMOTING_DATA_CHANNEL_SEND_DATA_INFO_MSFT)};
sendInfo.data = data;
sendInfo.size = sizeof(data);
sendInfo.guaranteedDelivery = true;
CHECK_XRCMD(m_extensions.xrSendRemotingDataMSFT(m_userDataChannel, &sendInfo));
}
Remarque
Les données que vous envoyez via un canal de données personnalisé partagent la bande passante avec d’autres canaux de données qu’utilise Holographic Remoting.
Récupérer des données
Chaque fois que des données arrivent via un canal de données, une structure d’événements XrEventDataRemotingDataChannelDataReceivedMSFT
est placée dans la file d’attente des événements.
Les paquets reçus peuvent être récupérés avec la xrRetrieveRemotingDataMSFT
fonction :
XrEventDataBuffer eventData{};
while (pollEvent(eventData))
{
switch (eventData.type)
{
case XR_TYPE_EVENT_DATA_REMOTING_DATA_CHANNEL_DATA_RECEIVED_MSFT:
{
auto dataReceivedEventData = reinterpret_cast<const XrEventDataRemotingDataChannelDataReceivedMSFT*>(&eventData);
std::vector<uint8_t> packet(dataReceivedEventData->size);
uint32_t dataBytesCount;
CHECK_XRCMD(m_extensions.xrRetrieveRemotingDataMSFT(dataReceivedEventData->channel,
dataReceivedEventData->packetId,
static_cast<uint32_t>(packet.size()),
&dataBytesCount,
packet.data()));
break;
}
}
}
Détruire un canal de données
Vous pouvez détruire un canal de données avec xrDestroyRemotingDataChannelMSFT
:
CHECK_XRCMD(m_extensions.xrDestroyRemotingDataChannelMSFT(m_userDataChannel));
Le XrRemotingDataChannelMSFT
handle n’est pas valide après l’appel xrDestroyRemotingDataChannelMSFT
et le handle de canal de données ne doit pas être utilisé par la suite.
Le XrEventDataRemotingDataChannelClosedMSFT
sera placé dans la file d’attente des événements au cas où le côté joueur se fermerait ou détruisait le canal de données.
L’état du canal de données passe à XR_REMOTING_DATA_CHANNEL_STATUS_CLOSED_MSFT
.
Pour un canal de données fermé, le XrRemotingDataChannelMSFT
handle reste valide.