OpenTokでBroadcastのサンプルアプリ

OpenTok のブロードキャストサンプルが、
ブラウザ用しかなかったので、Restful APIにして、iOS/Androidで使えるサンプルの作成。

また、HLSストリーミング Viewer も提供。

ブラウザ用の公式サンプルは下記。
- [opentok/broadcast-sample-app](https://github.com/opentok/broadcast-sample-app)

## ■ レポジトリ

[yyoshiki41/opentok-php-sample](https://github.com/yyoshiki41/opentok-php-sample)

[公式のPHP版SDK](https://github.com/opentok/OpenTok-PHP-SDK) を使っています。

## ■ Prerequirement

OpenTokは、30日のトライアルがあるので登録。
Herokuにデプロイすることを想定してます。
(localでも動かせるようにしています。)


## ■ API Endpoint

### Session

#### - Get

```
GET /session/:role
```

##### Query Parameters

| Name | Description |
| ------- | ------- | ------- | ------- |
| **sessionId** | *string* | Session ID | `"hogehoge"` |

`role` については、下記を参照。
https://tokbox.com/developer/guides/create-token/

##### ※ 注意

現在クライアントのsdkの中では、jsライブラリしか、`moderator`に対応していない。

> Moderator — In addition to publishing and subscribing to streams, moderators can force other clients to disconnect from a session or force a client to stop publishing an audio-video stream. (Only the OpenTok.js library supports moderation features. Moderation is not supported in clients using the OpenTok Android or iOS SDK.)

対処法としては、OpenTokのRestAPIを叩くAPIを用意して、任意のユーザーにだけ叩けるように設定する感じにすれば良いかなと思います。
https://tokbox.com/developer/guides/moderation/rest/

##### Curl Example

```bash
$ curl https://*.herokuapp.com/session/publisher
{
  "apiKey": "****",
  "sessionId": "****",
  "token": "****"
}
```

### Broadcast

#### - Start

```
POST /broadcast/start
```

##### Parameters

| Name | Type | Description | Example |
| ------- | ------- | ------- | ------- |
| **sessionId** | *string* | Session ID | `"hogehoge"` |

##### Curl Example

```bash
$ curl -X POST "https://*.herokuapp.com/broadcast/start"
-d '{"sessionId": "hogehoge"}'
{
  "id": "*",
  "sessionId": "hogehoge",
  "projectId": 1,
  "createdAt": 1491738710532,
  "broadcastUrls": {
    "hls": "https://cdn-broadcast002-iad.tokbox.com/20666/20666_dca9aebc-8cdd-4313-a260-b36ffc147ac7.smil/playlist.m3u8"
  },
  "updatedAt": 1491738713409,
  "status": "started",
  "partnerId": 2
}
```

#### - Stop

```
POST /broadcast/stop
```

##### Parameters

| Name | Type | Description | Example |
| ------- | ------- | ------- | ------- |
| **broadcastId** | *string* | Broadcast ID | `"hogehoge"` |

##### Curl Example

```bash
$ curl -X POST "https://*.herokuapp.com/broadcast/stop"
-d '{"broadcastId": "hogehoge"}'
{
  "id": "hogehoge",
  "sessionId": "hogehoge",
  "projectId": 1,
  "createdAt": 1491738713410,
  "broadcastUrls": null,
  "updatedAt": 1491738713410,
  "status": "stopped",
  "partnerId": 2
}
```

#### - Get

```
GET /broadcast/:broadcastId
```

##### Parameters

| Name | Type | Description | Example |
| ------- | ------- | ------- | ------- |
| **broadcastId** | *string* | Broadcast ID | `"hogehoge"` |

##### Curl Example

```bash
$ curl "https://*.herokuapp.com/broadcast/hogehoge"
{
  "id": "hogehoge",
  "sessionId": "hogehoge",
  "projectId": 1,
  "createdAt": 1491738710532,
  "broadcastUrls": {
    "hls": "https://cdn-broadcast002-iad.tokbox.com/20666/20666_dca9aebc-8cdd-4313-a260-b36ffc147ac7.smil/playlist.m3u8"
  },
  "updatedAt": 1491738713409,
  "status": "started",
  "partnerId": 2
}
```

### Debug

```
GET /debug
```

##### Curl Example

```bash
$ curl https://*.herokuapp.com/debug
{
  "apiKey": "****",
  "mode": "production"
}
```

### HLS View

```
GET /hls
```

#### Query Parameters

| Name | Type | Description | Example |
| ------- | ------- | ------- | ------- |
| **url** | *string* | HLS URL | `"https://cdn-broadcast002-iad.tokbox.com/20666/20666_dca9aebc-8cdd-4313-a260-b36ffc147ac7.smil/playlist.m3u8"` |
| **availableAt** | *string* | Unixtime | `"1491536899079"` |

#### Open your browser

```
https://*.herokuapp.com/hls?url=https://cdn-broadcast002-iad.tokbox.com/20666/20666_dca9aebc-8cdd-4313-a260-b36ffc147ac7.smil/playlist.m3u8&availableAt=1491536899079
```


## ■ Deploying to Heroku

-  `OPENTOK_KEY` -- Your OpenTok API Key
-  `OPENTOK_SECRET` -- Your OpenTok API Secret
-  `SLIM_MODE` -- Set this to `production` when the environment variables should be used to
      configure the application. The Slim application will only start reading its Heroku config when
      its mode is set to `'production'`