This project is to demonstrate how to build a live broadcast app. It include these features:
* Create a room to broadcast your live stream
* Join a room to watch the live
* Send likes, gifts, and comments
![image](https://cloud.githubusercontent.com/assets/1646564/16943747/de7a0c36-4dcf-11e6-913f-103301ef8fda.png)  ![image](https://cloud.githubusercontent.com/assets/1646564/16943754/e1d036ee-4dcf-11e6-8994-cc2cf1709bb8.png)
## Introduction
* RTMP server - Nginx RTMP module(https://github.com/arut/nginx-rtmp-module)
* WebSocket server - Socket.io(http://socket.io/)
* iOS client - LFLiveKit(https://github.com/LaiFengiOS/LFLiveKit) to push stream, IJKPlayer(https://github.com/Bilibili/ijkplayer) to play stream
## How to run
#### 1. Nginx RTMP server
You need to can set up your own rtmp server, the guidance can be found here:
* https://github.com/arut/nginx-rtmp-module
* https://www.atlantic.net/community/howto/install-rtmp-ubuntu-14-04/
#### 2. WebSocket server
Just go to the `live-server` folder, run `npm install`, then start the server by `node app.js`
#### 3. iOS client
Go to the `live-ios` folder, run `pod install`(must use cocoapods 0.39.0)
In Config.swift, update the server url:
```
struct Config {
static var rtmpPushUrl = "rtmp://139.196.179.230/mytv/"
static var rtmpPlayUrl = "rtmp://139.196.179.230/mytv/"
static var serverUrl = "http://139.196.179.230:3000"
}
```
The app can also run on a simulator, but to broadcast, you need to run it on a real device.
## Tutorial
#### 1. Live streaming
The basic live streaming flow is:
```
broadcaster -> rtmp -> media server -> cdn -> rtmp or hls -> audience
```
For the simplest case, we don't need a cdn server, then the flow will be:
```
broadcaster -> rtmp -> media server -> rtmp or hls -> audience
```
That is, the boadcaster push the live stream using the RTMP protocal to a media server, the audience pull the stream from the server using RTMP or HLS protocal.
Some explaination for RTMP and HLS:
* RTMP: RTMP is used to stream audio, video or data and is originally a proprietary protocol introduced by Macromedia (owned by Adobe). The protocol is TCP-based and offers therefore persistent connections. In short, RTMP encapsulates MP3/AAC audio and MP4/FLV video multimedia streams.
* HLS: HTTP Live Streaming is known as HLS. As the name implies, it is the media streaming communications protocol based on HTTP; developed by Apple as part of their QuickTime, Safari, OS X, and iOS products. How does it work? It breaks the overall stream into a sequence of small HTTP-based files (.ts: Transport Stream). These transport stream files are indexed in the file .m3u8. It is required to download first the .m3u8 playlist to play a live stream.
For the media server, there are serveral choices:
* Adobe media server
* Red5
* Nginx RTMP module
* crtmpserver
After setting up the server, you can test it using ffmpeg(install it by `brew install ffmpeg`).
* push stream
```
ffmpeg -f avfoundation -framerate 30 -i "1:0" -f flv rtmp://server-url
```
* watch the stream: go to this site: https://www.hlsplayer.net/rtmp-player
p.s. Lots of live stream cloud already covers the media server and cdn parts. You just need to push/pull the stream from it.
#### 2. iOS RTMP libs
There are serveral open source projects supporting RTMP, this project uses:
* LFLiveKit(https://github.com/LaiFengiOS/LFLiveKit) to push rtmp stream
* IJKPlayer(https://github.com/Bilibili/ijkplayer) to pull rtmp stream
You can find the usage of these libs in their project pages.
#### 3. Websocket server
This project uses socket.io to handle the client-server communication, the logic is very simple, on the server side:
```js
var rooms = {}
io.on('connection', function(socket) {
socket.on('create_room', function(room) {
var roomKey = room.key
rooms[roomKey] = room
socket.roomKey = roomKey
socket.join(roomKey)
})
socket.on('close_room', function(roomKey) {
delete rooms[roomKey]
})
socket.on('disconnect', function() {
if (socket.roomKey) {
delete rooms[socket.roomKey]
}
})
socket.on('join_room', function(roomKey) {
socket.join(roomKey)
})
socket.on('upvote', function(roomKey) {
io.to(roomKey).emit('upvote')
})
socket.on('gift', function(data) {
io.to(data.roomKey).emit('gift', data)
})
})
```
On the client side, it uses the socket.io swift client(https://github.com/socketio/socket.io-client-swift), the logic is also simple:
create, join, or close a room:
```swift
socket.on("connect") { data, ack in
self.socket.emit("create_room", self.room)
}
socket.on("connect") { data, ack in
self.socket.emit("join_room", self.room.key)
}
socket.disconnect()
```
publish likes and comments events:
```swift
socket.emit("upvote", room.key)
socket.emit("comment", [
"roomKey": room.key,
"text": text
])
```
listen likes and comments events:
```swift
socket.on("upvote") { data, ack in
self.emitterView.emitImage(R.image.heart()!)
}
socket.on("comment") { data, ack in
let comment = Comment(dict: data[0] as! [String: AnyObject])
self.comments.append(comment)
self.tableView.reloadData()
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
swift-iOS演示如何构建一个直播应用
共69个文件
swift:20个
h:12个
json:10个
需积分: 10 3 下载量 63 浏览量
2019-08-15
06:36:29
上传
评论
收藏 25.78MB ZIP 举报
温馨提示
这个项目是为了演示如何建立一个现场直播的应用程序。
资源推荐
资源详情
资源评论
收起资源包目录
swift-iOS演示如何构建一个直播应用.zip (69个子文件)
Live-master
live-ios
Live.xcodeproj
project.xcworkspace
contents.xcworkspacedata 149B
project.pbxproj 29KB
IJKMediaFramework.framework
_CodeSignature
CodeRequirements 184B
CodeResources 4KB
CodeSignature 4KB
CodeDirectory 172B
Info.plist 773B
Modules
module.modulemap 115B
Headers
IJKFFMonitor.h 2KB
IJKMPMoviePlayerController.h 1KB
IJKAVMoviePlayerController.h 4KB
IJKFFOptions.h 3KB
IJKKVOController.h 1KB
IJKMediaModule.h 1KB
IJKMediaPlayback.h 8KB
IJKMediaPlayer.h 1KB
IJKFFMoviePlayerController.h 5KB
IJKNotificationManager.h 1KB
IJKMediaFramework.h 3KB
IJKMediaFramework 56.67MB
Podfile.lock 864B
Podfile 254B
Live.xcworkspace
contents.xcworkspacedata 222B
Live
GiftDisplayArea.swift 3KB
String.swift 1KB
GiftDisplayView.xib 6KB
Raleway-Regular.ttf 174KB
XibBasedView.swift 1KB
Info.plist 2KB
AppDelegate.swift 3KB
UIFont.swift 394B
NavigationController.swift 598B
HomeViewController.swift 3KB
Live-Bridging-Header.h 197B
WaveEmitterView.swift 3KB
Model.swift 1KB
UIView.swift 342B
Base.lproj
LaunchScreen.storyboard 2KB
Main.storyboard 57KB
TextField.swift 594B
BroadcasterViewController.swift 4KB
DesignableButton.swift 792B
UIColor.swift 975B
GiftChooserViewController.swift 1KB
LiveOverlayViewController.swift 5KB
Assets.xcassets
heart.imageset
hearts (1).png 1KB
Contents.json 307B
gift-1.imageset
gift.png 2KB
Contents.json 301B
icon-close.imageset
close.png 912B
Contents.json 302B
gift-3.imageset
present.png 2KB
Contents.json 304B
icon-gift.imageset
678132-gift-128.png 3KB
Contents.json 312B
gift-2.imageset
gift-1.png 2KB
Contents.json 303B
icon-like.imageset
hearts (1).png 2KB
Contents.json 307B
AppIcon.appiconset
Contents.json 585B
Contents.json 62B
GiftDisplayView.swift 2KB
Config.swift 348B
R.generated.swift 13KB
AudienceViewController.swift 3KB
README.md 5KB
.gitignore 300B
live-server
app.js 1KB
package.json 284B
共 69 条
- 1
资源评论
weixin_39840515
- 粉丝: 446
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功