# Harmonic
A high performance RTMP live streaming application framework
# Usage
Program.cs
```csharp
using Harmonic.Hosting;
using System;
using System.Net;
namespace demo
{
class Program
{
static void Main(string[] args)
{
RtmpServer server = new RtmpServerBuilder()
.UseStartup<Startup>()
.Build();
var tsk = server.StartAsync();
tsk.Wait();
}
}
}
```
StartUp.cs
```csharp
using Autofac;
using Harmonic.Hosting;
namespace demo
{
class Startup : IStartup
{
public void ConfigureServices(ContainerBuilder builder)
{
}
}
}
```
Build a server like this to support websocket-flv transmission
```csharp
RtmpServer server = new RtmpServerBuilder()
.UseStartup<Startup>()
.UseWebSocket(c =>
{
c.BindEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080);
})
.Build();
```
# Scalability
Harmonic will scan your assembly and try to find classes that inherit from `RtmpController` or `WebSocketController` then register them into Harmonic, and map controller by url `rtmp://<address>/<controller_name>/<streamName>` for rtmp and `ws://<address>/<controller_name>/<streamName>`. the controller_name is controller class's name then remove the `Controller` suffix, for example `Living` is controller_name of `LivingController`. once Harmonic found any class that inherts from `RtmpController` or `WebSocketController`, it will never register `RtmpController` and `WebSocketController`.
You can also inherit builtin classes `LivingController` or `WebSocketPlayController`, when Harmonic found a class that inherit from them, it will not register `LivingController` and `WebSocketPlayController`. When you want to custom streaming logic, you can create a class that inherits from `LivingController` or `WebSocketPlayController`.
```csharp
public class MyLivingController : LivingController
{
[RpcMethod("createStream")]
public new uint CreateStream()
{
var stream = RtmpSession.CreateNetStream<MyLivingStream>();
return stream.MessageStream.MessageStreamId;
}
}
public class MyLivingStream : LivingStream
{
[RpcMethod(Name = "publish")]
public void Publish([FromOptionalArgument] string publishingName, [FromOptionalArgument] string publishingType)
{
if (...)
{
}
// your logic
base.Publish(publishingName, publishingType);
}
}
```
## RtmpController and WebSocketController
RtmpController and WebSocketController are two abstract basic controller, they are intended for serving video on rtmp protocol and websocket protocol.
When a controller class inherit from RtmpController, it will become an rtmp controller, it will working on rtmp protocol, and supports every rtmp features.
When a controller class inherit from WebSocketController, it will become a websocket controller, it can only send flv header and tags.
## Recording
The `RecordController` can record video, by default, it will save flv files into `working_dir/Record`.
You can overrite the recording configuration by register you own configure class in `StartUp` class
```csharp
class MyRecordConfiguration: RecordServiceConfiguration
{
public override string RecordPath { get; set; } = @"MyRecordPath";
public override string FilenameFormat { get; set; } = @"recorded-{streamName}";
};
class Startup : IStartup
{
public void ConfigureServices(ContainerBuilder builder)
{
builder.Register(c => new MyRecordConfiguration()).As<RecordServiceConfiguration>();
}
}
```
## Websocket
websocket protocol and rtmp protocol are running on two different controllers, so when you push vide to url: `rtmp://127.0.0.1/living/a`, the corresponding playing url for websocket is `ws://127.0.0.1/websocketplay/a`
## Internal Controllers
### LivingController
LivingController provides a simple living service, it recieves video or audio data and broadcast data to other plays.
### RecordController
RecordController supports video recording, and can be configured.
### WebsocketPlayController
WebsocketPlayController supports two modes: lving mode and vod mode. when stream name in url is not in living, this controller will try to find a stream in recording folder, then play it.
## Internal Classes
### NetConnection
NetConnection is responsible for managing all NetStreams, process some control messages, rpc support, handle `connect`, `createStream` and another command messages.
### NetStream
NetStream is created by NetConnection, it reperents a logic stream, all of RtmpController is NetStream.
### MessageStream
MessageStream repersents a logic rtmp stream. Every message must be sent on a specific MessageStream.
### ChunkStream
Message will be break into chunks before sending to peer. chunks must be send on a ChunkStream, in a ChunkStream, every chunk must be sent one by one. that means you can't send a message concurrently on one ChunkStream, but message can be sent concurrently on some different ChunkStream.
### RtmpSession
RtmpSession is a bridge from NetStream to RtmpServer, controllers can access to it's own RtmpSession property to send message, or close connection or something else.
## Rpc
See [rpc-docs](rpc.md)
## Api
See [api-docs](api.md)
## Dependency injection in controllers
Harmonic uses autofac as the DI framework, you can register you own service in `StartUp`, and use it in your controller
```csharp
class Startup : IStartup
{
public void ConfigureServices(ContainerBuilder builder)
{
builder.Register(c => new MyService()).AsSelf();
}
}
class MyController: LivingController
{
public MyController(MyService service)
{
//...
}
}
```
## Custom message
To add your own custom message, you need to write a message class, then rengister the message class when you call `UseHarmonic`
for example:
```csharp
static void Main(string[] args)
{
RtmpServer server = new RtmpServerBuilder()
.UseStartup<Startup>()
.UseWebSocket(c =>
{
c.BindEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080);
})
.UseHarmonic(c => {
c.RegisterMessage<MyMessage>();
})
.Build();
var tsk = server.StartAsync();
tsk.Wait();
}
```
# Test
## push video file using ffmpeg
```bash
ffmpeg -i test.mp4 -f flv -vcodec h264 -acodec aac "rtmp://127.0.0.1/living/streamName"
```
## play rtmp stream using ffplay
```bash
ffplay "rtmp://127.0.0.1/living/streamName"
```
## play flv stream using [flv.js](https://github.com/Bilibili/flv.js) by websocket
```html
<video id="player"></video>
<script>
if (flvjs.isSupported()) {
var player = document.getElementById('player');
var flvPlayer = flvjs.createPlayer({
type: 'flv',
url: "ws://127.0.0.1/websocketplay/streamName"
});
flvPlayer.attachMediaElement(player);
flvPlayer.load();
flvPlayer.play();
}
</script>
```
```
没有合适的资源?快使用搜索试试~ 我知道了~
高性能、可扩展的RTMP直播应用框架
共224个文件
cs:135个
amf3:48个
amf0:31个
0 下载量 174 浏览量
2024-02-02
11:31:23
上传
评论
收藏 148KB ZIP 举报
温馨提示
高性能、可扩展的RTMP直播应用框架
资源推荐
资源详情
资源评论
收起资源包目录
高性能、可扩展的RTMP直播应用框架 (224个子文件)
longstring.amf0 96KB
packet.amf0 276B
array.amf0 58B
ecmaarray.amf0 34B
xml.amf0 27B
object.amf0 23B
fkqudskxxb.amf0 13B
karxrlzavl.amf0 13B
rpwhhjwary.amf0 13B
gymtsavnng.amf0 13B
sjceuhcjfa.amf0 13B
grkogrokmq.amf0 13B
lbwkjydfuv.amf0 13B
qsqwosrxcl.amf0 13B
vsyrigrfbn.amf0 13B
vrouinfvzr.amf0 13B
date.amf0 11B
3.9231228517554273.amf0 9B
2.3763213699559538.amf0 9B
7.560779119365773.amf0 9B
1.1261843717924092.amf0 9B
9.844079468164518.amf0 9B
5.442560101247932.amf0 9B
9.719564819564491.amf0 9B
3.498957015368982.amf0 9B
4.485998361678176.amf0 9B
4.141123418573091.amf0 9B
true.amf0 2B
false.amf0 2B
null.amf0 1B
undefined.amf0 1B
packet.amf3 276B
object.amf3 133B
array.amf3 123B
dictionary.amf3 75B
vector_typted_object.amf3 64B
vector_any_object.amf3 45B
externalizable.amf3 30B
xml_document.amf3 28B
vector_double.amf3 27B
xml.amf3 24B
vector_int.amf3 15B
vector_uint.amf3 15B
cmaljzrwgc.amf3 12B
aoxqmkvbxa.amf3 12B
dfjfucqvpr.amf3 12B
bghnwadduz.amf3 12B
korbgwizge.amf3 12B
vqayztgtuf.amf3 12B
cuyerozwyf.amf3 12B
ubteltbaku.amf3 12B
fxxcsjosdu.amf3 12B
psvigwvvpx.amf3 12B
date.amf3 10B
8.451623695104308.amf3 9B
9.98509389093438.amf3 9B
6.863435764713296.amf3 9B
0.05806697191443333.amf3 9B
3.962148410082559.amf3 9B
4.465764800567858.amf3 9B
8.518697602984554.amf3 9B
7.645173446829178.amf3 9B
8.85002823631796.amf3 9B
9.838871036292584.amf3 9B
bytearray.amf3 5B
60.amf3 2B
82.amf3 2B
98.amf3 2B
72.amf3 2B
57.amf3 2B
73.amf3 2B
56.amf3 2B
78.amf3 2B
75.amf3 2B
67.amf3 2B
undefined.amf3 1B
null.amf3 1B
true.amf3 1B
false.amf3 1B
Amf3Reader.cs 39KB
Amf0Reader.cs 28KB
ChunkStreamContext.cs 26KB
Amf3Writer.cs 25KB
TestAmf3Reader.cs 20KB
TestAmf3Writer.cs 18KB
ByteBuffer.cs 15KB
Amf0Writer.cs 15KB
TestUnlimitedBuffer.cs 14KB
RecordStream.cs 13KB
RtmpSession.cs 13KB
TestAmf0Writer.cs 12KB
IOPipeLine.cs 9KB
TestAmf0Reader.cs 9KB
RpcService.cs 8KB
LivingStream.cs 7KB
RtmpServerOptions.cs 6KB
FlvDemuxer.cs 6KB
NetConnection.cs 6KB
CommandMessage.cs 6KB
NetworkBitConverter.cs 5KB
共 224 条
- 1
- 2
- 3
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功