首页 > 解决方案 > UV4L Restful API 轮询事件

问题描述

我正在使用 UV4L 的 restful API 来配置 Janus 客户端。有些事件是异步的,需要您轮询事件端点以获取响应,例如加入请求。

我的问题是,在发送加入请求时,我永远不会收到它的事件。我只在轮询事件端点时才得到这个:

/api/janus/client/events?maxev=0&timeout_s=5
{ what: 'keepalive' }

UV4L API 中没有记录此响应,因此我不确定这意味着什么。我确保我的配置已经为 UV4L 服务器启用了 keepalive(默认情况下它是打开的)。我将查询设置为 5 秒的超时并将 maxev 设置为 0,因此它应该获取所有事件。我的获取请求也立即返回,而不是保持活动 5 秒,这看起来很奇怪。使用内置的 UV4L restful api 面板时也会发生同样的事情。

在进行加入请求之前,设置和会话端点都已被调用并成功返回。获取房间参与者列表表明加入成功,我只是从未看到该事件。我需要事件知道何时可以安全地发布到房间。

这是我调用 Restful API 的代码,但因为 UV4L 面板做同样的事情,我不认为这是我的代码的问题:

class UV4L {
  static async joinRoom(info) {
    try {
      await fs.promises.access('/dev/video0');
    } catch(err) {
      throw new Error('No video camera detected');
    }

    const janusConfig = require('../janus.json');
    janusConfig.gateway.url = info.gateway.url;
    janusConfig.gateway.root = info.gateway.root;

    // Stop previous JANUS session
    console.log('Destroying previous session');
    const destroy = await UV4L.fetch(URL + '/api/janus/client', 'POST', {
      "what": "destroy",
      "plugin": "videoroom",
    });

    // Send the JANUS settings
    console.log('Updating Janus settings');
    const settings = await UV4L.fetch(URL + '/api/janus/client/settings', 'PUT', janusConfig);
    if(settings.response.code !== 200) {
      throw new Error(settings.response.reason);
    }

    // Create a new JANUS session
    console.log('Creating new session');
    const session = await UV4L.fetch(URL + '/api/janus/client', 'POST', {
      "what": "create",
      "plugin": "videoroom",
    });
    if(session.error) {
      throw new Error(session.error.reason);
    }

    // Join the JANUS room
    console.log('Joining JANUS room');
    const joinRoom = await UV4L.fetch(URL + '/api/janus/client/videoroom', 'POST', {
      "what": "join",
      "body": {
        room: info.room,
        room_pin: info.pin,
        username: os.hostname,
      }
    });

    console.log('Publishing stream to JANUS');
  }

  static async fetch(url, method, data) {
    // Generate transaction if it doesnt already exist
    if(data && !data.transaction) {
      data.transaction = uuidv4();
    }

    const options = {
      method,
      body: JSON.stringify(data)
    };

    // Enable keep alive if get
    if(method.toLowerCase() === 'get') {
      options.keepalive = true;
    }

    // Fetch from the API
    let response = await await fetch(url, options);

    // Convert response to JSON
    response = await response.json();

    // If the response is an ack, wait for its event
    if(data && response.what === 'ack') {
      response = await this.waitForAck(data.transaction);
    }

    return response;
  }

  static async waitForAck(transaction) {
    return new Promise(async (resolve, reject) => {
      // Poll event API until transaction found
      while(true) {
        const response = await this.fetch(URL + '/api/janus/client/events?maxev=0&timeout_s=5', 'get');
        if(response.transaction === transaction) {
          return response;
        }
      }
    });
  }
}

标签: javascriptrestuv4l

解决方案


推荐阅读