2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JanusプラグインAPIの実行方法

Last updated at Posted at 2023-05-01

公式ドキュメントがちょっとわかりづらかったので覚書。
どうやら
①セッションを作成
②プラグインへのハンドルを取得
③プラグインAPIのリクエスト
と3回リクエストしなければならないらしい。

PHPで雑に書くとこんな感じ
下記はvideoroomプラグインの部屋の取得だが、他もパラメータを変えればいけるはず

get_room_test.php
<?php

$ch = curl_init("https://mydomain:8089/janus");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "janus" => "create",
    "transaction" => "123456789" // ここはランダムな数字
]));

$res = curl_exec($ch);
$res_obj = json_decode($res, true);

if(empty($res)){
    error_log(curl_error($ch));
}

curl_close($ch);

echo "****request1****\n";
echo $res . "\n";

if(!isset($res_obj['data']['id'])){
    error_log($res);
    exit();
}
$sid = $res_obj['data']['id'];

// セッションidをパスに入れてもう一度リクエスト
$ch = curl_init("https://mydomain:8089/janus/$sid");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "janus" => "attach",
    "plugin" => "janus.plugin.videoroom", // アクセスしたいプラグイン
    "transaction" => "2234567890" // createと同じでも同じでなくてもいい
]));

$res = curl_exec($ch);
$res_obj = json_decode($res, true);

if(empty($res)){
    error_log(curl_error($ch));
}

curl_close($ch);

echo "****request2****\n";
echo $res . "\n";

if(!isset($res_obj['data']['id'])){
    error_log($res);
    exit();
}
$pid = $res_obj['data']['id'];

// 更にプラグインハンドルidをパスに入れてリクエスト
$ch = curl_init("https://mydomain:8089/janus/$sid/$pid");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "janus" => "message",
    "body" => [
        "request" => "listparticipants", // 部屋の情報取得のメソッド
        "room" => 1234 // 対象の部屋のid(room-xxxxのxxxx部分)
    ],
    "transaction" => "3234567890" // create,attachと同じでも同じでなくてもいい
]));

$res = curl_exec($ch);
$res_obj = json_decode($res, true);

if(empty($res)){
    error_log(curl_error($ch));
    exit();
}

curl_close($ch);

echo "****request2****\n";
echo $res . "\n";
結果
****request1****
{
   "janus": "success",
   "transaction": "123456789",
   "data": {
      "id": 2032583429975705
   }
}
****request2****
{
   "janus": "success",
   "session_id": 2032583429975705,
   "transaction": "2234567890",
   "data": {
      "id": 2319648518525006
   }
}
****request3****
{
   "janus": "success",
   "session_id": 2032583429975705,
   "transaction": "3234567890",
   "sender": 2319648518525006,
   "plugindata": {
      "plugin": "janus.plugin.videoroom",
      "data": {
         "videoroom": "participants",
         "room": 1234,
         "participants": []
      }
   }
}

参考
https://janus.conf.meetecho.com/docs/rest.html
https://janus.conf.meetecho.com/docs/videoroom.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?