登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

Live

[复制链接]
来自: James1991 分类: iOS精品源码 上传时间: 2016-11-10 16:35:30
Tag:音视频 直播 推流 rmtp 播放器

项目介绍:

Live

https://github.com/ltebean/Live

这个项目是为了演示如何构建一个直播广播应用程序。 它包括以下功能:

1 创建一个房间来广播直播流
2 加入房间观看直播
3 发送喜欢,礼物和评论

image  image

主要技术点介绍

如何运行

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"
}

此项目可以运行在模拟器上,如果通过广播,需要真机运行

教程

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

code4app

相关源码推荐:

我来说两句
*滑动验证:
所有评论(23)
code4app热心网友 2016-11-10 17:12:03
很给力,Code4App有你更精彩!
回复
王颖博 2016-11-10 18:26:26
感谢分享,Code4App有你更精彩
回复
撕波大爷 2016-11-11 09:42:06
精华内容,楼主V5!
回复
code4app热心网友 2016-11-11 10:06:48
淡定,淡定,淡定……
回复
李世铿 2016-11-11 10:09:52
正需要啊,感谢楼主无私分享!
回复
芸朵 2016-11-11 10:09:59
楼主用心了,内容非常精彩。
回复
code4app热心网友 2016-11-11 10:13:20
楼主用心了,内容非常精彩。
回复
code4app热心网友 2016-11-11 10:16:04
学习学习!
回复
code4app热心网友 2016-11-11 10:18:53
正需要啊,感谢楼主无私分享!
回复
123下一页
提取码:  下载次数:74 状态:已购或VIP 售价:0(原价:10)金钱 下载权限:初级码农 
1750 1 74
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表