
{"id":612,"date":"2016-07-19T10:14:07","date_gmt":"2016-07-19T02:14:07","guid":{"rendered":"http:\/\/www.jiandaima.com\/blog\/?p=612"},"modified":"2016-07-19T10:14:07","modified_gmt":"2016-07-19T02:14:07","slug":"live%ef%bc%9a%e4%b8%bb%e6%b5%81%e6%8a%80%e6%9c%af%e6%9e%84%e5%bb%ba%e5%8d%b3%e6%97%b6%e8%81%8a%e5%a4%a9%e5%ae%a4%e6%bc%94%e7%a4%ba%e5%ba%94%e7%94%a8%ef%bc%8c%e7%9b%b4%e6%92%ad%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.jiandaima.com\/blog\/archives\/612.html","title":{"rendered":"Live\uff1a\u4e3b\u6d41\u6280\u672f\u6784\u5efa\u5373\u65f6\u804a\u5929\u5ba4\u6f14\u793a\u5e94\u7528\uff0c\u76f4\u64ad\u6e90\u7801"},"content":{"rendered":"<p>Live \uff1a\u670d\u52a1\u7aef\u91c7\u7528 RTMP\uff08Nginx\uff09, WebSocket\uff08Socket.io\uff09\uff0c\u5ba2\u6237\u7aef VideoCore\uff0bIJKPlayer \u6784\u5efa\u5373\u65f6\u804a\u5929\u5ba4\u6f14\u793a\u5e94\u7528\u3002<br \/>\n\u8fd9\u4e2a\u9879\u76ee\u662f\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u73b0\u573a\u76f4\u64ad\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<br \/>\n\u5b83\u5305\u62ec\u8fd9\u4e9b\u529f\u80fd\uff1a<br \/>\n\u521b\u5efa\u4e00\u4e2a\u623f\u95f4\u5e7f\u64ad\u4f60\u7684\u76f4\u64ad\u6d41<br \/>\n\u52a0\u5165\u4e00\u4e2a\u623f\u95f4\u770b\u73b0\u573a<br \/>\n\u53d1\u9001\u559c\u6b22\uff0c\u793c\u7269\u548c\u8bc4\u8bba<\/p>\n<p><a href=\"https:\/\/www.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ%E6%88%AA%E5%9B%BE20160719094846-587x500.png\" alt=\"QQ\u622a\u56fe20160719094846\" width=\"587\" height=\"500\" class=\"aligncenter size-large wp-image-613\" \/><\/a><\/p>\n<p>\u7b80\u4ecb\uff1a<br \/>\nRTMP server - Nginx RTMP module(https:\/\/github.com\/arut\/nginx-rtmp-module)<br \/>\nWebSocket server - Socket.io(http:\/\/socket.io\/)<br \/>\niOS client - VideoCore(https:\/\/github.com\/jgh-\/VideoCore) to push stream, IJKPlayer(https:\/\/github.com\/Bilibili\/ijkplayer) to play stream<br \/>\n\u600e\u4e48\u8fd0\u884c\uff1a<br \/>\n\u53ef\u4ee5\u4f7f\u7528\u6211\u5efa\u7acb\u7684\u3002\u4e00\u4e2a\u6d4b\u8bd5\u670d\u52a1\u5668\u5728139.196.179.230\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884ciOS\u9879\u76ee\u3002<br \/>\n1. Nginx RTMP server<br \/>\n\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u5df1\u7684RTMP\u670d\u52a1\u5668\u7684\u6307\u5bfc\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff1a<br \/>\nhttps:\/\/github.com\/arut\/nginx-rtmp-module<br \/>\nhttps:\/\/www.atlantic.net\/community\/howto\/install-rtmp-ubuntu-14-04\/<br \/>\n2. WebSocket server<br \/>\n\u8fdb\u5165live-server\u76ee\u5f55\uff0c\u8fd0\u884cnpm install\uff0c\u7136\u540e\u542f\u52a8\u670d\u52a1\u5668\uff0c\u547d\u4ee4\u4e3a\uff1anode app.js<\/p>\n<p>3. iOS client<br \/>\n\u8fdb\u5165live-ios\u76ee\u5f55\uff0c\u8fd0\u884cpod install\uff08\u9700\u8981\u4f7f\u7528cocoapods\u7684\u7248\u672c\u4e3a0.39.0\uff09<br \/>\n\u5728Config.swift\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u66f4\u65b0\u4f60\u7684\u670d\u52a1\u5668\u5730\u5740\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nstruct Config {\r\n    static var rtmpPushUrl = &quot;rtmp:\/\/139.196.179.230\/mytv\/&quot;\r\n    static var rtmpPlayUrl = &quot;rtmp:\/\/139.196.179.230\/mytv\/&quot;\r\n    static var serverUrl = &quot;http:\/\/139.196.179.230:3000&quot;\r\n}\r\n\r\n<\/pre>\n<p>\u8be5\u5e94\u7528\u7a0b\u5e8f\u4e5f\u53ef\u4ee5\u8fd0\u884c\u5728\u6a21\u62df\u5668\u4e0a\uff0c\u4f46\u8981\u76f4\u64ad\uff0c\u4f60\u9700\u8981\u5728\u4e00\u4e2a\u771f\u6b63\u7684\u8bbe\u5907\u4e0a\u8fd0\u884c\u5b83\u3002<\/p>\n<p>\u5907\u6ce8\uff1a<br \/>\n1\uff0c\u6d41\u5a92\u4f53<br \/>\n\u57fa\u672c\u7684\u6d41\u5a92\u4f53\u6d41\u7a0b\uff1a<br \/>\n\u5e7f\u64ad->\u534f\u8bae->\u5a92\u4f53\u670d\u52a1\u5668-> CDN -> RTMP\u6216HLS ->\u89c2\u4f17<br \/>\n\u6700\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4e00\u4e2aCDN\u670d\u52a1\u5668\uff0c\u7136\u540e\u6d41\u7684\u8fc7\u7a0b<br \/>\n\u5e7f\u64ad->\u534f\u8bae->\u5a92\u4f53\u670d\u52a1\u5668 -> RTMP\u6216HLS ->\u89c2\u4f17<\/p>\n<p>\u5c31\u50cf\u8fd9\u6837\uff0cboadcaster\u63a8\u76f4\u64ad\u4f7f\u7528RTMP\u534f\u8bae\u7684\u5a92\u4f53\u670d\u52a1\u5668\uff0c\u89c2\u4f17\u5c06\u6d41\u4ece\u670d\u52a1\u5668\u4f7f\u7528RTMP\u6216HLS\u534f\u8bae\u3002<\/p>\n<p>\u5bf9\u4e8eRTMP\u548cHLS\u7684\u4e00\u4e9b\u89e3\u91ca\uff1a<br \/>\nRTMP\u534f\u8bae\u662f\u7528\u4e8e\uff1a\u97f3\u9891\u3001\u89c6\u9891\u6216\u6570\u636e\uff0c\u662f\u6700\u521d\u7531Macromedia\u516c\u53f8\u7684\u4e13\u6709\u534f\u8bae\uff08\u7531Adobe\uff09\u3002\u8be5\u534f\u8bae\u662f\u57fa\u4e8eTCP\u7684\uff0c\u63d0\u4f9b\u6301\u4e45\u8fde\u63a5\u3002\u603b\u4e4b\uff0cRTMP\u5c01\u88c5MP3 \/ AAC\u97f3\u9891\u548cMP4\u3001FLV\u89c6\u9891\u591a\u5a92\u4f53\u6d41\u3002<br \/>\nHLS\uff1aHTTP\u76f4\u64ad\u6d41\u5a92\u4f53\u79f0\u4e3aHLS\u3002\u987e\u540d\u601d\u4e49\uff0c\u5b83\u662f\u6d41\u5a92\u4f53\u57fa\u4e8eHTTP\u901a\u4fe1\u534f\u8bae\uff1b\u901a\u8fc7\u82f9\u679c\u7684QuickTime\uff0c\u90e8\u5206Safari\uff0cOS X\u548ciOS\u4ea7\u54c1\u7684\u5f00\u53d1\u3002\u90a3\u600e\u4e48\u4f7f\u7528\u5462\uff1f\u5b83\u6253\u7834\u4e86\u6574\u4f53\u6d41\u6210\u4e00\u7cfb\u5217\u5c0f\u7684\u57fa\u4e8eHTTP\u7684\u6587\u4ef6\uff08\u3002TS\u4f20\u8f93\u6d41\uff09\u3002\u8fd9\u4e9b\u4f20\u8f93\u6d41\u6587\u4ef6\u5728\u6587\u4ef6\u7d22\u5f15m3u8\u3002\u5b83\u9700\u8981\u5148\u4e0b\u8f7d\u7684m3u8\u64ad\u653e\u5217\u8868\u64ad\u653e\u76f4\u64ad\u3002<\/p>\n<p>\u5bf9\u4e8e\u5a92\u4f53\u670d\u52a1\u5668\uff0c\u6709\u51e0\u79cd\u9009\u62e9\uff1a<br \/>\nAdobe\u5a92\u4f53\u670d\u52a1\u5668<br \/>\nRed5<br \/>\nNginx\u7684rtmp\u6a21\u5757<br \/>\ncrtmpserver<\/p>\n<p>\u5728\u8bbe\u7f6e\u670d\u52a1\u5668\uff0c\u4f60\u53ef\u4ee5\u6d4b\u8bd5\u4f7f\u7528ffmpeg<br \/>\n(install it by brew install ffmpeg).<\/p>\n<p>\u63a8\u6d41<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nffmpeg -f avfoundation -framerate 30  -i &quot;1:0&quot; -f flv rtmp:\/\/server-url\r\n<\/pre>\n<p>\u89c2\u770b\u6d41\uff1a\u53bb\u8fd9\u4e2a\u7f51\u7ad9\uff1ahttps:\/\/www.hlsplayer.net\/rtmp-player<\/p>\n<p>P.S.\u5f88\u591a\u76f4\u64ad\u4e91\u5df2\u7ecf\u8986\u76d6\u4e86\u5a92\u4f53\u670d\u52a1\u5668\u548cCDN\u7684\u90e8\u5206\u3002\u4f60\u53ea\u9700\u8981\u63a8\/\u62c9\u4ece\u5b83\u7684\u6d41\u3002<\/p>\n<p>2. iOS RTMP libs<br \/>\n\u6709\u51e0\u4e2a\u5f00\u6e90\u9879\u76ee\u652f\u6301RTMP\uff0c\u672c\u9879\u76ee\u91c7\u7528\uff1a<br \/>\nVideoCore(https:\/\/github.com\/jgh-\/VideoCore) to push rtmp stream<br \/>\nIJKPlayer(https:\/\/github.com\/Bilibili\/ijkplayer) to pull rtmp stream<br \/>\n\u4f60\u53ef\u4ee5\u5728\u81ea\u5df1\u7684\u9879\u76ee\u9875\u9762\u627e\u5230\u8fd9\u4e9b\u5e93\u7684\u4f7f\u7528\u3002<br \/>\n3. Websocket server<\/p>\n<p>\u672c\u9879\u76ee\u91c7\u7528socket.io\u5904\u7406\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u901a\u4fe1\uff0c\u903b\u8f91\u5f88\u7b80\u5355\uff0c\u5728\u670d\u52a1\u5668\u7aef\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar rooms = &#x5B;]\r\n\r\nio.on('connection', function(socket) {\r\n\r\n  socket.on('create_room', function(roomKey) {\r\n    rooms.push(roomKey)\r\n    socket.roomKey = roomKey;\r\n    socket.join(roomKey);\r\n  });\r\n\r\n  socket.on('close_room', function(roomKey) {\r\n    closeRoom(roomKey)\r\n  });\r\n\r\n  socket.on('disconnect', function(roomKey) {\r\n    if (socket.roomKey) {\r\n      closeRoom(socket.roomKey)\r\n    }\r\n  });\r\n\r\n  socket.on('join_room', function(roomKey) {\r\n    socket.join(roomKey);\r\n  });\r\n\r\n  socket.on('upvote', function(roomKey) {\r\n    io.to(roomKey).emit('upvote')\r\n  });\r\n\r\n  socket.on('comment', function(data) {\r\n    io.to(data.roomKey).emit('comment', data)\r\n  });\r\n});\r\n<\/pre>\n<p>\u5728\u5ba2\u6237\u7aef\uff0c\u5229\u7528\u5ba2\u6237\u7aefsocket.io swift  \u5ba2\u6237\u7aef(https:\/\/github.com\/socketio\/socket.io-client-swift),\uff0c<\/p>\n<p>\u903b\u8f91\u4e5f\u5f88\u7b80\u5355\uff1a<br \/>\n\u521b\u5efa\uff0c\u52a0\u5165\u6216\u5173\u95ed\u4e00\u4e2a\u623f\u95f4<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsocket.on(&quot;connect&quot;) {data, ack in\r\n    self.socket.emit(&quot;create_room&quot;, self.room.key)\r\n}\r\n\r\nsocket.on(&quot;connect&quot;) {data, ack in\r\n    self.socket.emit(&quot;join_room&quot;, self.room.key)\r\n}\r\n\r\nsocket.emitWithAck(&quot;close_room&quot;, room.key)(timeoutAfter: 0) {data in\r\n    self.socket.disconnect()\r\n}\r\n<\/pre>\n<p>\u53d1\u5e03\u559c\u6b22\u548c\u8bc4\u8bba\u4e8b\u4ef6\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsocket.emit(&quot;upvote&quot;, room.key)\r\nsocket.emit(&quot;comment&quot;, &#x5B;\r\n    &quot;roomKey&quot;: room.key,\r\n    &quot;text&quot;: text\r\n])\r\n<\/pre>\n<p>\u76d1\u542c\u559c\u6b22\u548c\u8bc4\u8bba\u4e8b\u4ef6\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsocket.on(&quot;upvote&quot;) {data ,ack in\r\n    self.emitterView.emitImage(R.image.heart()!)\r\n}\r\n\r\nsocket.on(&quot;comment&quot;) {data ,ack in\r\n    let comment = Comment(dict: data&#x5B;0] as! &#x5B;String: AnyObject])\r\n    self.comments.append(comment)\r\n    self.tableView.reloadData()\r\n}\r\n<\/pre>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>Live \uff1a\u670d\u52a1\u7aef\u91c7\u7528 RTMP\uff08Nginx\uff09, WebSocket\uff08Socket.io\uff09\uff0c\u5ba2\u6237\u7aef VideoCore\uff0bIJKPlayer \u6784\u5efa\u5373\u65f6\u804a\u5929\u5ba4\u6f14\u793a\u5e94\u7528\u3002\u8fd9\u4e2a\u9879\u76ee\u662f\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u73b0\u573a\u76f4\u64ad\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u5305\u62ec\u8fd9\u4e9b\u529f\u80fd\uff1a\u521b\u5efa\u4e00\u4e2a\u623f\u95f4\u5e7f\u64ad\u4f60\u7684\u76f4\u64ad\u6d41\uff0c\u52a0\u5165\u4e00\u4e2a\u623f\u95f4\u770b\u73b0\u573a\uff0c\u53d1\u9001\u559c\u6b22\uff0c\u793c\u7269\u548c\u8bc4\u8bba<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[162,161,160],"class_list":["post-612","post","type-post","status-publish","format-standard","hentry","category-application-source-code","tag-live","tag-161","tag-160"],"post_thumbnail_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","content_first_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_medium_image_300":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_thumbnail_image_624":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_frist_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_medium_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_large_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_full_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ\u622a\u56fe20160719094846.png","post_all_images":[{"imagesurl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/07\/QQ%E6%88%AA%E5%9B%BE20160719094846-587x500.png","id":"image0"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":5,"total_comments":2,"category_name":"\u5e94\u7528\u7a0b\u5e8f\u6e90\u7801","post_date":"2016-07-19","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2020\/02\/img_5e44112722964.png","avatarurls":[],"related_posts":[],"pageviews":928,"next_post_id":633,"next_post_title":"Android\u4f18\u79c0\u5f00\u53d1\u8d44\u6e90","previous_post_id":491,"previous_post_title":"GitHub \u6392\u540d\u524d 100 \u7684\u5b89\u5353\u3001iOS\u9879\u76ee\u7b80\u4ecb","_links":{"self":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/comments?post=612"}],"version-history":[{"count":0,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/612\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/media?parent=612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/categories?post=612"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/tags?post=612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}