Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP Camera and Stream Source #434

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "IP Camera & Stream Sources",
"position": 4,
"link": {
"type": "generated-index",
"description": "IP Camera and External Stream Source streaming with Ant Media Server"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Dynamic Stream Pulling
description: Dynamic Stream Pulling
keywords: [Streaming IP cameras, Stream Sources, Ant Media Server Documentation, Ant Media Server Tutorials]
sidebar_position: 3
---

# Dynamic Stream Pulling

:::info
The Dynamic Stream Pulling feature is available in versions 2.8.3 and up.
:::

Dynamic Stream Pulling offers an efficient bandwidth optimization solution by automatically starting and stopping stream pulling based on user demand.

For example, when a user attempts to view an offline stream with a stream source or IP camera, Ant Media Server automatically initiates stream pulling, enabling the broadcast to go live.

Conversely, when viewership declines and there are no remaining viewers, Ant Media Server automatically halts stream pulling, returning the broadcast to an offline state.

This feature proves invaluable for bandwidth optimization, especially when continuous stream recording isn't necessary. By activating stream pulling only when someone tries to watch the stream and ceasing it when there are no viewers, Ant Media Server significantly conserves bandwidth resources.

Auto-stopping the stream with no viewer is valid for all play types (WebRTC, HLS, DASH, and LL-HLS).

To use the Dynamic Stream Pulling feature:

- Go to the Ant Media web panel and create a broadcast with the type Stream Source or IP Camera.

To enable the feature for the stream, check the ```Auto Start/Stop Streaming``` checkbox as shown below.

![](@site/static/img/dynamic-stream-pulling-1.png)

After creation, Ant Media Server will start pulling the stream automatically and broadcast status will turn to `Broadcasting`
Alternatively, you can also activate Auto Start/Stop Streaming for an existing broadcast by modifying its settings using the REST API.

To do that, send a `PUT` request using
[Update Broadcast Rest API](https://antmedia.io/rest/#/default/updateBroadcast).

**Here is the curl sample:**

```bash
curl --location --request PUT 'http(s)://AMS_DOMAIN:Port/AppName/rest/v2/broadcasts/streamId' --header 'Content-Type: application/json' --data '{"autoStartStopEnabled":true}'
```

- Open a new tab and start watching the live stream using the below URL.

```html
https://AMS_DOMAIN:Port/AppName/play.html?id=streamId&playOrder=webrtc
```

Now the server will start fetching streams.

- Close the player tab. Since there are no viewers anymore, Ant Media Server will stop pulling the stream within a few seconds, and the broadcast status will change to `Offline`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: IP Camera Streaming
description: IP Camera Streaming using ONVIF and RTSP
keywords: [Streaming IP cameras, Ant Media Server Documentation, Ant Media Server Tutorials]
sidebar_position: 1
---

# IP Camera Streaming

Ant Media Server users can pull IP camera streams easily from the management panel. In other words, you don’t need to write any commands or use a terminal to be able to re-stream external sources.

In order for IP camera re-streaming, the camera should support the ONVIF standard. ONVIF makes it easy to manage IP cameras. All CRUD and PTZ operations are based on well-defined SOAP messages.

![](@site/static/img/onvif_conformance.gif)

Their are two ways to pull the IP camera stream on AMS:

1. Using the ONVIF URL
2. Using the direct RTSP URL

Let’s have a look at how to pull a stream from an IP camera.

## Add IP Camera - ONVIF URL

- Go to the management panel, select **LiveApp** from applications, then click on **New Live Stream** and select **IP Camera**.

![](@site/static/img/re-stream-add-ip-camera-1.png)

- Fill in the **Stream Name**, **Camera Username**, **Camera Password** and any custom **StreamId**.

- You should add the ONVIF URL of the IP camera. Generally, it is in the following format: ```IP-ADDRESS-OF-IPCAMERA:ONVIF PORT```.

- If you don't know the ONVIF URL, you can use the **Auto Discover** feature. If the IP camera and the server are on the same network, the Ant Media server can discover them automatically.
![](@site/static/img/publish-live-stream/IP-Camera-and-External-Sources/IP-Camera-Add.png)

:::info
If you want to use a secured domain instead of an IP address, make sure to prepend with the `http(s)` e.g.

`https://dynamic.dns.net:443`
:::

## Add IP Camera - RTSP URL

If the IP camera does not support the ONVIF, then a direct RTSP URL can also be used to pull the stream directly on the Ant Media Server.

:::info
If the IP Camera is added directly using the RTSP URL, then the CRUD and PTZ operations cannot be performed on it using the AMS APIs.
:::

In order to add an IP camera with an RTSP URL, follow these steps:

* First, log in to the management panel. Click on
**New Live Stream** > **Stream Source**. Define stream name,RTSP URL and stream Id.
* AMS starts to pull the camera stream automatically
* As the stream starts to pull, you can watch it from the AMS panel.

![](@site/static/img/publish-live-stream/IP-Camera-and-External-Sources/Stream-Source.png)

You can learn more about Stream Source in the next section.

### RTSP Transport Type

In some cases, the RTSP camera does not start and shows the status as `preparing` on the dashboard. By default, the [**RTSP pull transport type**](https://antmedia.io/javadoc/io/antmedia/AppSettings.html#rtspPullTransportType) is set to pull the stream with both `TCP` and ⁣`UDP` but sometimes the camera only supports only TCP or UDP at a time, so you can change it with the application settings.

Under the application's Advanced settings, their is the below property:

```js
"rtspPullTransportType": "3",
```

- If it is 1, the stream can be pulled with both TCP.
- If it is 2, the stream can be pulled with UDP only.
- If it is 3, the stream can be pulled with both UDP and TCP.

:::info
If the camera is accessible via FFMPEG or VLC but still does not start pulling on the AMS, try changing this parameter.
:::

## IP Camera Playback

If IP cameras are accessible and properly configured, Ant Media Server adds their streams as a live stream and begins to pull streams from them. The management panel displays its current status. To watch the stream, click the **Play button**.

![](@site/static/img/publish-live-stream/IP-Camera-and-External-Sources/IP-Camera-Play.png)

The IP camera stream can be monitored with any output protocol, like `WebRTC`, `HLS`⁣, ⁣`DASH` and `LL-HLS`. Check out the [playback section](https://antmedia.io/docs/category/playing-live-streams/) for more details.

Check out the [recording documentation](https://antmedia.io/docs/category/recording-live-streams/) to record the IP camera streams.

## Rest API to add IP Camera Stream

This [Rest API](https://antmedia.io/rest/#/default/createBroadcast) can be used to create the live stream.

```bash
curl -X POST -H "Content-Type: application/json" "https://IP-address-or-domain:Port/App-Name/rest/v2/broadcasts/create?autoStart=false" -d '{
"type":"ipCamera","name":"test","streamId":"test","ipAddr": "127.0.0.1:8080","username": "camera-username","password":"camera-password"}'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Stream Sources
description: Restream external stream sources with AMS
keywords: [Streaming IP cameras, Ant Media Server Documentation, Ant Media Server Tutorials]
sidebar_position: 2
---

# Restream External Stream Sources

Ant Media Server (AMS) can handle a variety of streaming flows. It can accept and create streaming media as well as pull live streams from external sources such as live TV streams, IP camera streams, and other types of live streams.

The stream sources that Ant Media Server can fetch are: **RTSP, RTMP, HLS, SRT, UDP, FLV, etc.**

In order to restream from an external source, follow these steps:

- First, log in to the management panel. Click on
**New Live Stream** > **Stream Source**. Define stream name, stream URL, and stream ID.
- AMS starts to pull streams.
- As the stream starts to pull, you can watch it from the AMS panel.

![](@site/static/img/publish-live-stream/IP-Camera-and-External-Sources/Stream-Source.png)

:::info
Make sure that the port that you are using to pull the stream source should be whitelisted on the firewall to avoid any issue.
:::

In AMS v2.5.3 and later, the stream auto-fetcher is disabled by default, so streams do not start themselves after a server restart.

Change the following flag to true in the Advance application settings so that streams start fetching automatically.

```js
"startStreamFetcherAutomatically": true,
```

Check out the [recording documentation](https://antmedia.io/docs/category/recording-live-streams/) to record the source streams on the Ant Media Server.

### Restream UDP Source

For other stream sources like RTSP, RTMP, etc., you can directly pull it as the stream is already available. But if you want to send a stream to AMS with UDP, then first you have to publish it to the server and then you have to pull it.

You can pull the UDP stream on a server as a stream source by following the below steps.

- First, send the stream with UDP to the Ant Media Server IP address using an encoder or FFMPEG. In this example, we used FFMPEG command.

```bash
ffmpeg -f lavfi -re -i smptebars=duration=60:size=1280x720:rate=30 -f lavfi -re -i sine=frequency=1000:duration=60:sample_rate=44100 -pix_fmt yuv420p -c:v libx264 -b:v 1000k -g 30 -keyint_min 120 -profile:v baseline -preset veryfast -f mpegts "udp://server-IP:5000?pkt_size=1316"
```

You can change the port number as per your requirements. We published the stream on port 5000. Also, make sure that the used port is whitelisted on the firewall.

- Once the stream starts pushing on the server's IP address, you can pull it **(udp://127.0.0.1:5000)** as a stream source on Ant Media Server.

## Rest API to add Stream Source

This [Rest API](https://antmedia.io/rest/#/default/createBroadcast) can be used to create the live stream.

```bash
curl -X POST -H "Content-Type: application/json" "https://IP-address-or-domain:Port/App-Name/rest/v2/broadcasts/create?autoStart=false" -d '{ "type":"streamSource","name":"test",
"streamId":"test","streamUrl":"YOUR_STREAM_SOURCE_URL"}'
```
145 changes: 0 additions & 145 deletions docs/guides/publish-live-stream/ip-cameras-and-external-resources.md

This file was deleted.