{"id":13466051,"url":"https://github.com/ltebean/Live","last_synced_at":"2025-03-25T21:31:15.251Z","repository":{"id":60623162,"uuid":"63140393","full_name":"ltebean/Live","owner":"ltebean","description":"Demonstrates how to build a live broadcast app(Swift 3)","archived":false,"fork":false,"pushed_at":"2018-12-13T23:19:14.000Z","size":26439,"stargazers_count":2415,"open_issues_count":22,"forks_count":418,"subscribers_count":109,"default_branch":"master","last_synced_at":"2025-03-24T20:12:27.270Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ltebean.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-12T08:26:27.000Z","updated_at":"2025-03-18T06:34:11.000Z","dependencies_parsed_at":"2022-10-02T07:40:26.464Z","dependency_job_id":null,"html_url":"https://github.com/ltebean/Live","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltebean%2FLive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltebean%2FLive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltebean%2FLive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ltebean%2FLive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ltebean","download_url":"https://codeload.github.com/ltebean/Live/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245547278,"owners_count":20633337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T15:00:38.547Z","updated_at":"2025-03-25T21:31:15.215Z","avatar_url":"https://github.com/ltebean.png","language":"Swift","readme":"This project is to demonstrate how to build a live broadcast app. It include these features:\n\n* Create a room to broadcast your live stream\n* Join a room to watch the live\n* Send likes, gifts, and comments\n\n![image](https://cloud.githubusercontent.com/assets/1646564/16943747/de7a0c36-4dcf-11e6-913f-103301ef8fda.png)\u0026emsp;\u0026emsp;![image](https://cloud.githubusercontent.com/assets/1646564/16943754/e1d036ee-4dcf-11e6-8994-cc2cf1709bb8.png)\n\n## Introduction\n\n* RTMP server - Nginx RTMP module(https://github.com/arut/nginx-rtmp-module)\n* WebSocket server - Socket.io(http://socket.io/)\n* iOS client - LFLiveKit(https://github.com/LaiFengiOS/LFLiveKit) to push stream, IJKPlayer(https://github.com/Bilibili/ijkplayer) to play stream\n\n## How to run\n\n\n#### 1. Nginx RTMP server\n\nYou need to can set up your own rtmp server, the guidance can be found here: \n\n* https://github.com/arut/nginx-rtmp-module\n* https://www.atlantic.net/community/howto/install-rtmp-ubuntu-14-04/\n\n\n#### 2. WebSocket server\n\nJust go to the `live-server` folder, run `npm install`, then start the server by `node app.js`\n\n#### 3. iOS client\n\nGo to the `live-ios` folder, run `pod install`(must use cocoapods 0.39.0)\n\nIn Config.swift, update the server url:\n```\nstruct Config {\n    static var rtmpPushUrl = \"rtmp://139.196.179.230/mytv/\"\n    static var rtmpPlayUrl = \"rtmp://139.196.179.230/mytv/\"\n    static var serverUrl = \"http://139.196.179.230:3000\"\n}\n\n```\n\nThe app can also run on a simulator, but to broadcast, you need to run it on a real device.\n\n\n## Tutorial\n\n#### 1. Live streaming\n\nThe basic live streaming flow is:\n```\nbroadcaster -\u003e rtmp -\u003e media server -\u003e cdn -\u003e rtmp or hls -\u003e audience\n```\n\nFor the simplest case, we don't need a cdn server, then the flow will be:\n```\nbroadcaster -\u003e rtmp -\u003e media server -\u003e rtmp or hls -\u003e audience\n```\n\nThat 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.\n\nSome explaination for RTMP and HLS:\n\n* 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.\n\n* 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.\n\nFor the media server, there are serveral choices:\n* Adobe media server\n* Red5\n* Nginx RTMP module\n* crtmpserver\n\nAfter setting up the server, you can test it using ffmpeg(install it by `brew install ffmpeg`).\n* push stream\n```\nffmpeg -f avfoundation -framerate 30  -i \"1:0\" -f flv rtmp://server-url\n```\n\n* watch the stream: go to this site: https://www.hlsplayer.net/rtmp-player\n\n\np.s. Lots of live stream cloud already covers the media server and cdn parts. You just need to push/pull the stream from it.\n\n\n#### 2. iOS RTMP libs\nThere are serveral open source projects supporting RTMP, this project uses:\n* LFLiveKit(https://github.com/LaiFengiOS/LFLiveKit) to push rtmp stream\n* IJKPlayer(https://github.com/Bilibili/ijkplayer) to pull rtmp stream\n\nYou can find the usage of these libs in their project pages.\n\n\n#### 3. Websocket server\nThis project uses socket.io to handle the client-server communication, the logic is very simple, on the server side:\n```js\nvar rooms = {}\n\nio.on('connection', function(socket) {\n\n  socket.on('create_room', function(room) {\n    var roomKey = room.key\n    rooms[roomKey] = room\n    socket.roomKey = roomKey\n    socket.join(roomKey)\n  })\n\n  socket.on('close_room', function(roomKey) {\n    delete rooms[roomKey]\n  })\n\n  socket.on('disconnect', function() {\n    if (socket.roomKey) {\n      delete rooms[socket.roomKey]\n    }\n  })\n\n  socket.on('join_room', function(roomKey) {\n    socket.join(roomKey)\n  })\n\n  socket.on('upvote', function(roomKey) {\n    io.to(roomKey).emit('upvote')\n  })\n\n  socket.on('gift', function(data) {\n    io.to(data.roomKey).emit('gift', data)\n  })\n  \n})\n\n```\n\nOn the client side, it uses the socket.io swift client(https://github.com/socketio/socket.io-client-swift), the logic is also simple:\n\ncreate, join, or close a room:\n```swift\nsocket.on(\"connect\") { data, ack in\n    self.socket.emit(\"create_room\", self.room)\n}\n\nsocket.on(\"connect\") { data, ack in\n    self.socket.emit(\"join_room\", self.room.key)\n}\n\nsocket.disconnect()\n```\n\npublish likes and comments events:\n```swift\nsocket.emit(\"upvote\", room.key)\nsocket.emit(\"comment\", [\n    \"roomKey\": room.key,\n    \"text\": text\n])\n```\n\nlisten likes and comments events:\n```swift\nsocket.on(\"upvote\") { data, ack in\n    self.emitterView.emitImage(R.image.heart()!)\n}\n        \nsocket.on(\"comment\") { data, ack in\n    let comment = Comment(dict: data[0] as! [String: AnyObject])\n    self.comments.append(comment)\n    self.tableView.reloadData()\n}\n```\n\n\n\n","funding_links":[],"categories":["Libs","Sample","Streaming [🔝](#readme)","Swift","HarmonyOS","Video"],"sub_categories":["Streaming","RSS","Windows Manager"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltebean%2FLive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fltebean%2FLive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fltebean%2FLive/lists"}