• Stars
    star
    2,423
  • Rank 18,203 (Top 0.4 %)
  • Language
    Swift
  • Created almost 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Demonstrates how to build a live broadcast app(Swift 3)

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  image

Introduction

How to run

1. Nginx RTMP server

You need to can set up your own rtmp server, the guidance can be found here:

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

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:

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:

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:

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:

socket.emit("upvote", room.key)
socket.emit("comment", [
    "roomKey": room.key,
    "text": text
])

listen likes and comments events:

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()
}

More Repositories

1

LTNavigationBar

UINavigationBar Category which allows you to change its appearance dynamically
Objective-C
4,484
star
2

LTInfiniteScrollView

Paged scrollview allowing easily applying animation
Objective-C
889
star
3

novel-design

PinQu ios client
C
334
star
4

LTInfiniteScrollView-Swift

Paged scrollview allowing easily applying animation(Swift 3)
Swift
199
star
5

LTSlidingViewController

sliding view controller allowing custom transition
Objective-C
122
star
6

LTBounceSheet

Bounce sheet inspired by http://holko.pl/2014/06/26/recreating-skypes-action-sheet-animation/
Objective-C
57
star
7

spiderman

a crawler with visualized config board
JavaScript
49
star
8

LTVideoRecorder

A demo project demonstrating how to add filter, drawing, and text to a video
Swift
21
star
9

LTStackView

An animated stack view backed by facebook pop
Objective-C++
18
star
10

simple-co

A simplified co implementation only working with thunk
JavaScript
18
star
11

node-mongo

node+mongo project archetype demonstrating how to use node and mongo to build a restful webservice
JavaScript
18
star
12

Huixiang

http://huixiang.im ios client
Objective-C
17
star
13

sticky-header

pure javascript implementation that makes an element stick to the top when scrolling down the page
JavaScript
16
star
14

tabs

pure javascript implementation that quickly makes the markup tabbable
JavaScript
14
star
15

CubeKiller

A game built with SceneKit
Swift
14
star
16

Todo

todo list app for practice
Objective-C
14
star
17

Seaport

Hybrid app framework which makes it easy to ship static resources to client
Objective-C
11
star
18

LTWaveEmitterView

A wave emitter view
Swift
8
star
19

LTSharedViewTransition

View transition that allows two vcs have a shared view
Objective-C
7
star
20

LTSwipeTableViewCell

UITableViewCell subclass that allows to display swippable buttons
Objective-C
5
star
21

Wizard

website development and administration framework
Java
5
star
22

LTSwiftDate

Easy-to-use Date operation(Swift 3)
Swift
5
star
23

spring-cloud-netflix-demo

Demonstrate how to build micro service using spring cloud
Java
5
star
24

LTVideoKit

My learnings from AVFoundation
Objective-C
4
star
25

src2qiniu

save image from web to qiniu
JavaScript
3
star
26

LTInputPanel

Demonstrate how to build a custom chat input panel
Swift
3
star
27

LTWebImageView

UIImageView category to load image from web
Objective-C
2
star
28

user-graph

user graph api implemented with redis
JavaScript
2
star
29

conductor

ga config helper
JavaScript
2
star
30

wizard-admin

website development and administration framework
JavaScript
2
star
31

YCTableViewController

a base tableViewController which enables pulling to refresh or load more
Objective-C
1
star
32

LTUIKit

some IB_DESIGNABLE UI components and UIKit category methods
Objective-C
1
star
33

BulletTime

Swift
1
star
34

seaport-bridge

Seaport UIWebView javascript bridge
JavaScript
1
star
35

loaderjs

javascript loader implementation
JavaScript
1
star
36

feedie

feed api implemented with redis
JavaScript
1
star
37

Journey

https://itunes.apple.com/cn/app/journey-capture-life-moments/id1086435070?mt=8
Swift
1
star
38

spring-cloud-config

spring cloud config repo
1
star
39

node-proxy

This node.js server acts as proxy, it asks for contents from serveral backend servers, then assembles them according to the specified rule.
JavaScript
1
star
40

Todotrix

Swift
1
star
41

easy-yaml

easy-yaml provides an easy-to-use api to access the yaml configuraion
Java
1
star
42

taggie

tagging api implemented with redis
JavaScript
1
star