{"id":153,"date":"2024-08-22T11:47:49","date_gmt":"2024-08-22T03:47:49","guid":{"rendered":"https:\/\/blog.superfyx.top\/?p=153"},"modified":"2024-08-22T11:49:48","modified_gmt":"2024-08-22T03:49:48","slug":"%e5%a4%a7%e7%96%86%e5%9b%be%e4%bc%a0rtmp%e6%8e%a8%e6%b5%81%e8%bf%9b%e5%8c%961","status":"publish","type":"post","link":"https:\/\/blog.superfyx.top\/?p=153","title":{"rendered":"\u5927\u7586\u56fe\u4f20rtmp\u63a8\u6d41\u8fdb\u53161"},"content":{"rendered":"\n<p>\u56e0\u4e3a\u4e1a\u52a1\u9700\u6c42\uff0c\u6709\u4e2a\u81ea\u5df1\u5199\u63a8\u6d41\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u9700\u8981\u901a\u8fc7\u5927\u7586\u7684MSDK\u91cc\u62ff\u5230h264\u540e\u518d\u63a8\u5230\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u672c\u6765\u5927\u7586\u81ea\u5e26rtmp\u63a8\u6d41\u529f\u80fd\u7684\uff0c\u4f46\u662f\u6211\u5c31\u8981\u81ea\u5df1\u518d\u4f20\u4e00\u4e0b\uff08\u4e07\u4e00\u6211\u60f3\u5728\u91cc\u9762\u52a0\u4e0a\u6c34\u5370\u5462\uff09\uff0c\u6240\u4ee5\u6211\u5c31\u5148\u7528\u4e00\u4e2a\u5e93\u624b\u6413\u4e86\u7b2c\u4e00\u4e2a\u7248\u672c\u3002<\/p>\n\n\n\n<p>1\u3001\u5148\u5b89\u88c5\u5bfc\u5165\u5f15\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\/\/ffmpeg rtmp\u63a8\u6d41<br>implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.4.LTS'<\/code><\/pre>\n\n\n\n<p>2\u3001\u9996\u5148\u5927\u7586\u7684h264\u89c6\u9891\u5e27\u5f00\u542f\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>MediaDataCenter.getInstance().getCameraStreamManager().addReceiveStreamListener(ComponentIndexType.LEFT_OR_MAIN, reciveh264data);<\/code><\/pre>\n\n\n\n<p>3\u3001\u7136\u540e\u56de\u8c03\u5185\u5bb9\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code> private ICameraStreamManager.ReceiveStreamListener reciveh264data = new ICameraStreamManager.ReceiveStreamListener() {\n\n        private String tempFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + \"tempfile.h264\";\n        private RTMPStreamer rtmpStreamer = new RTMPStreamer(tempFilePath);\n        private boolean isStreamingStarted = false;\n        @Override\n        public void onReceiveStream(@NonNull byte&#91;] data, int offset, int length, @NonNull StreamInfo info) {\n            LogUtils.d(\"h264data\", \"data\u957f\u5ea6:\" + data.length + \"data offset\u5ea6:\" + offset + \",StreamInfo:\" + info.toString());\n            LogUtils.e(\"H264FileWriter\", \"\u5199\u5165\u6570\u636e\u5931\u8d25: \" + tempFilePath);\n            \/\/ \u5199\u5165\u6bcf\u4e00\u5e27\u6570\u636e\u5230\u4e34\u65f6\u6587\u4ef6\n            rtmpStreamer.sendFrame(Arrays.copyOfRange(data, offset, offset + length));\n            \/\/ \u542f\u52a8\u63a8\u6d41\uff08\u53ea\u9700\u542f\u52a8\u4e00\u6b21\uff09\n            if (!isStreamingStarted) {\n                rtmpStreamer.startStreaming(\"rtmp:\/\/192.168.1.10\/live\/test\");\n                isStreamingStarted = true;  \/\/ \u786e\u4fdd\u53ea\u542f\u52a8\u4e00\u6b21\n            } \n        }\n    };<\/code><\/pre>\n\n\n\n<p>4\u3001\u7136\u540e\u662fffmpeg\u63a8\u6d41\u7c7bRTMPStreamer<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>public class RTMPStreamer {\r\n\r\n    private FileOutputStream fileOutputStream;\r\n    private String tempFilePath;\r\n\r\n    public RTMPStreamer(String tempFilePath) {\r\n        this.tempFilePath = tempFilePath;\r\n    }\r\n\r\n    public void startStreaming(String rtmpUrl) {\r\n        String command = \"-re -i \" + tempFilePath + \" -c:v copy -f flv \" + rtmpUrl;\r\n\r\n        FFmpeg.executeAsync(command, (executionId, returnCode) -> {\r\n            if (returnCode == Config.RETURN_CODE_SUCCESS) {\r\n                LogUtils.d(\"FFmpeg\", \"\u63a8\u6d41\u6210\u529f\uff0c\u4efb\u52a1ID: \" + executionId);\r\n            } else {\r\n                LogUtils.e(\"FFmpeg\", \"\u63a8\u6d41\u5931\u8d25\uff0c\u4efb\u52a1ID: \" + executionId + \"\uff0c\u9519\u8bef\u7801: \" + returnCode);\r\n            }\r\n        });\r\n    }\r\n\r\n    public void sendFrame(byte&#91;] data) {\r\n        try {\r\n            if (fileOutputStream == null) {\r\n                fileOutputStream = new FileOutputStream(tempFilePath, true);  \/\/ \u8ffd\u52a0\u6a21\u5f0f\r\n            }\r\n            fileOutputStream.write(data);\r\n            fileOutputStream.flush();\r\n        } catch (IOException e) {\r\n            LogUtils.e(\"RTMPStreamer\", \"\u5199\u5165\u5e27\u6570\u636e\u5931\u8d25: \" + e.getMessage());\r\n        }\r\n    }\r\n\r\n    public void stopStreaming() {\r\n        try {\r\n            if (fileOutputStream != null) {\r\n                fileOutputStream.close();\r\n            }\r\n        } catch (IOException e) {\r\n            LogUtils.e(\"RTMPStreamer\", \"\u5173\u95ed\u6587\u4ef6\u6d41\u5931\u8d25: \" + e.getMessage());\r\n        }\r\n        FFmpeg.cancel(); \/\/ \u53d6\u6d88FFmpeg\u4efb\u52a1\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ul>\n<li><strong>\u5ef6\u8fdf<\/strong>: \u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6709\u4e00\u5b9a\u5ef6\u8fdf\uff0c\u56e0\u4e3a\u6570\u636e\u5148\u88ab\u5199\u5165\u6587\u4ef6\u518d\u63a8\u6d41\u3002\u82e5\u5bf9\u5ef6\u8fdf\u8981\u6c42\u975e\u5e38\u4e25\u683c\uff0c\u9700\u8981\u8003\u8651\u5176\u4ed6\u66f4\u5e95\u5c42\u7684\u65b9\u6848\uff0c\u5982\u81ea\u5b9a\u4e49\u7684\u539f\u751f\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u7ba1\u7406<\/strong>: \u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u6587\u4ef6\u4e00\u76f4\u589e\u5927\uff0c\u53ef\u4ee5\u5b9a\u671f\u6e05\u7406\u6216\u5206\u6bb5\u5199\u5165\u591a\u4e2a\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u662f\u5b9e\u73b0\u63a8\u6d41\u7684\u7b2c\u4e00\u6b65\uff0c\u4f53\u9a8c\u5361\uff0c\u5ef6\u8fdf\u4f1a\u968f\u7740\u6587\u4ef6\u7684\u589e\u5927\u800c\u8d8a\u6765\u8d8a\u5927\uff0c\u6240\u4ee5\u4f5c\u4e3a\u7ec3\u624b\u7248\uff0c\u5f3a\u70c8\u4e0d\u5efa\u8bae\u4f7f\u7528\u5230\u9879\u76ee\u91cc\uff0c\u8fd9\u5ef6\u8fdf\u9ad8\u8fbe\u51e0\u5341\u79d2\uff0c\u5f88\u9614\u6015\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6ce2\u64cd\u4f5c\u5b8c\u7f8e\uff0c\u6536\u5de5\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u4e1a\u52a1\u9700\u6c42\uff0c\u6709\u4e2a\u81ea\u5df1\u5199\u63a8\u6d41\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u9700\u8981\u901a\u8fc7\u5927\u7586\u7684MSDK\u91cc\u62ff\u5230h264\u540e\u518d\u63a8\u5230\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u672c\u6765\u5927\u7586\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,3],"tags":[17,20,5,19,18],"_links":{"self":[{"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":4,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.superfyx.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}