├── .gitignore ├── LICENSE ├── README.md ├── README_EN.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── test_video.mp4 │ ├── java │ └── com │ │ └── chillingvan │ │ └── instantvideo │ │ └── sample │ │ ├── CrashHandler.java │ │ ├── MainActivity.java │ │ ├── test │ │ ├── VideoFrameHandlerHelper.java │ │ ├── audio │ │ │ ├── TestAudioEncoder.java │ │ │ └── TestAudioEncoderActivity.java │ │ ├── camera │ │ │ ├── CameraPreviewTextureView.java │ │ │ └── TestCameraAndVideoActivity.java │ │ ├── publisher │ │ │ ├── MediaPlayerHelper.java │ │ │ ├── TestCameraPublisherActivity.java │ │ │ └── TestMp4MuxerActivity.java │ │ └── video │ │ │ ├── ProduceTextureView.java │ │ │ ├── TestVideoEncoder.java │ │ │ └── TestVideoEncoderActivity.java │ │ └── util │ │ └── ScreenUtil.java │ └── res │ ├── drawable-nodpi │ ├── baboon.png │ └── lenna.png │ ├── layout │ ├── activity_camera_and_video.xml │ ├── activity_main.xml │ ├── activity_test_audio_encoder.xml │ ├── activity_test_camera_publisher.xml │ ├── activity_test_mp4_muxer.xml │ └── activity_test_video_encoder.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ ├── values-zh-rCN │ └── strings.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── applibs ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── chillingvan │ │ └── lib │ │ ├── camera │ │ ├── CameraInterface.java │ │ ├── CameraUtils.java │ │ └── InstantVideoCamera.java │ │ ├── encoder │ │ ├── MediaCodecInputStream.java │ │ ├── audio │ │ │ └── AACEncoder.java │ │ └── video │ │ │ └── H264Encoder.java │ │ ├── muxer │ │ ├── BaseMuxer.java │ │ ├── BufferInfoEx.java │ │ ├── FramePool.java │ │ ├── FrameSender.java │ │ ├── IMuxer.java │ │ ├── MP4Muxer.java │ │ ├── RTMPStreamMuxer.java │ │ └── TimeIndexCounter.java │ │ └── publisher │ │ ├── CameraStreamPublisher.java │ │ └── StreamPublisher.java │ └── res │ └── values │ └── strings.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidInstantVideo 2 | 展现Android硬编码下的视频数据流动,可以对视频做处理,例如加滤镜,加水印等。 3 | 4 | 本项目主要是为了展现Android使用硬编码下的视频数据流动,目前完成了H264和AAC编码以及对视频帧的图像处理,以及RTMP直播推流。欢迎Fork和Pull Request。 5 | 6 | [English README](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/README_EN.md) 7 | 8 | **感谢以下项目** 9 | [LibRtmp-Client-for-Android](https://github.com/ant-media/LibRtmp-Client-for-Android) 10 | 11 | 12 | ## 使用要求 13 | * Android API 18 以上 14 | 15 | ## 用法 16 | 17 | 18 | ### 功能: 19 | 20 | * 硬编码H264格式视频 + 对视频帧的图像处理 21 | 22 | 具体看例子里的 [TestVideoEncoder](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoder.java) 23 | 其中实现图片处理的部分: 24 | ```java 25 | public class TestVideoEncoder { 26 | //... 27 | public void prepareEncoder() { 28 | //... 29 | h264Encoder.setOnDrawListener(new H264Encoder.OnDrawListener() { 30 | @Override 31 | public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { 32 | // 此处可以使用canvasGL的drawTexture, drawBitmap等方法实现对视频帧的处理. 33 | // 34 | // 35 | } 36 | }); 37 | //... 38 | } 39 | //... 40 | } 41 | ``` 42 | 实现图像处理的关键是用了[android-opengl-canvas](https://github.com/ChillingVan/android-openGL-canvas) 43 | 44 | 例子中生成的h264文件在/storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_h264_encode.h264,可以在代码里修改输出路径 45 | ```java 46 | public class TestVideoEncoder { 47 | public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) { 48 | try { 49 | os = new FileOutputStream(ctx.getExternalFilesDir(null) + File.separator + "test_h264_encode.h264"); 50 | } catch (FileNotFoundException e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | ``` 56 | 57 | 对于生成的文件,可以在PC上使用 [PotPlayer](http://potplayer.daum.net/?lang=zh_CN) 播放 58 | 59 | 60 | * 使用 camera 录像并处理 61 | 62 | 关于Camera,我已经封装好了 [InstantVideoCamera](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/applibs/src/main/java/com/chillingvan/lib/camera/InstantVideoCamera.java) 63 | 64 | 具体可以查看例子,目前例子在我的4.4手机上录下来的文件有一卡一卡的现象,在一部6.0的手机上没有。所以说其实MediaCodec的坑不少,看过不少sdk似乎都是使用ffmpeg作录制。 65 | 66 | 67 | * 编码产生aac格式音频 68 | 69 | 直接使用手机进行录音。 70 | 具体看例子里的 [TestAudioEncoder](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoder.java) 71 | 72 | 73 | 例子中生成的aac文件在/storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_aac_encode.aac",可以在代码里修改输出路径 74 | 对于生成的文件,可以在PC上使用 [PotPlayer](http://potplayer.daum.net/?lang=zh_CN) 播放 75 | 76 | 77 | * 使用 [LibRtmp](https://github.com/ant-media/LibRtmp-Client-for-Android) 将h264和aac变成RTMP流 发送到服务器 78 | 79 | 需要测试的话,请自行搭建RTMP服务器。我用的是自己搭建的Nginx服务器,用的Module是[nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module)。搭建服务器不需要写代码,根据教程敲几行命令就行。 80 | 可以用开源直播软件[OBS](https://obsproject.com/)对比播放效果。播放器用各种都行,VLC,PotPlayer,ffplay都可以。我用的是ffplay,注意,因为只是简单的服务器,所以要先开播放器连接后再开始启动推流。 81 | 例如我使用的命令是:.\ffplay.exe "rtmp://localhost:19305/live/room live=1" 82 | 83 | 可以看例子[TestCameraPublisherActivity](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestCameraPublisherActivity.java) 84 | ```java 85 | 86 | public class TestCameraPublisherActivity extends AppCompatActivity { 87 | ... 88 | 89 | @Override 90 | protected void onCreate(Bundle savedInstanceState) { 91 | ... 92 | handler = new Handler(handlerThread.getLooper()) { 93 | @Override 94 | public void handleMessage(Message msg) { 95 | super.handleMessage(msg); 96 | StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(); 97 | streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() { 98 | @Override 99 | public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { 100 | 101 | // Here you can do video process 102 | // 此处可以视频处理,例如加水印等等 103 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight()); 104 | Loggers.i("DEBUG", "gl draw"); 105 | } 106 | }); 107 | try { 108 | streamPublisher.startPublish(addrEditText.getText().toString(), streamPublisherParam.width, streamPublisherParam.height); 109 | } catch (IOException e) { 110 | e.printStackTrace(); 111 | } 112 | } 113 | }; 114 | 115 | // streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(), cameraPreviewTextureView, instantVideoCamera); 116 | String filename = getExternalFilesDir(null) + "/test_flv_encode.flv"; 117 | streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(filename), cameraPreviewTextureView, instantVideoCamera); 118 | } 119 | ... 120 | } 121 | ``` 122 | 123 | ### 最近更新 124 | 1. 添加使用Android MediaMuxer的 Mp4Muxer,输出Mp4文件。例子 TestMp4MuxerActivity 125 | 2. 修改 IMuxer 接口,使之更通用。给StreamPublisherParam添加更多参数。 126 | 127 | ### TODO 128 | 129 | 1. RTSP流 130 | 131 | ### 关于 Pull Request 132 | 133 | 欢迎Fork! 134 | 添加了功能发出 Pull Request 的话,希望能在sample的module里添加相应的测试代码,最好在文件开头加上自己的license注释。 135 | 136 | 137 | ### 相关博文 138 | 139 | [使用MediaCodec和RTMP做直播推流](http://www.jianshu.com/p/3c479c0f4876) 140 | 141 | ## License 142 | Copyright 2017 ChillingVan. 143 | 144 | Licensed under the Apache License, Version 2.0 (the "License"); 145 | you may not use this file except in compliance with the License. 146 | You may obtain a copy of the License at 147 | 148 | http://www.apache.org/licenses/LICENSE-2.0 149 | 150 | Unless required by applicable law or agreed to in writing, software 151 | distributed under the License is distributed on an "AS IS" BASIS, 152 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 153 | See the License for the specific language governing permissions and 154 | limitations under the License. 155 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # AndroidInstantVideo 2 | Show the stream of Android video hardware encode, including video processing and video publishing. 3 | 4 | 1. Use MediaCodec for H264 encoding. 5 | 2. Use MediaCodec for AAC encoding. 6 | 3. Use RTMP for video publishing. 7 | 8 | 9 | **Thanks for these projects** 10 | 11 | [LibRtmp-Client-for-Android](https://github.com/ant-media/LibRtmp-Client-for-Android) 12 | 13 | 14 | ## Requirement 15 | * Android API >= 18 16 | 17 | 18 | ### Feature 19 | 20 | * MediaCodec Hardware encoding for H264 + Video Processing 21 | 22 | Sample: [TestVideoEncoder](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoder.java) 23 | The Video Processing Part: 24 | ```java 25 | public class TestVideoEncoder { 26 | //... 27 | public void prepareEncoder() { 28 | //... 29 | h264Encoder.setOnDrawListener(new H264Encoder.OnDrawListener() { 30 | @Override 31 | public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { 32 | // 此处可以使用canvasGL的drawTexture, drawBitmap等方法实现对视频帧的处理. 33 | // 34 | // 35 | } 36 | }); 37 | //... 38 | } 39 | //... 40 | } 41 | ``` 42 | The key for video frame processing : [android-opengl-canvas](https://github.com/ChillingVan/android-openGL-canvas) 43 | 44 | H264 test file: /storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_h264_encode.h264,可以在代码里修改输出路径 45 | ```java 46 | public class TestVideoEncoder { 47 | public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) { 48 | try { 49 | os = new FileOutputStream(ctx.getExternalFilesDir(null) + File.separator + "test_h264_encode.h264"); 50 | } catch (FileNotFoundException e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | ``` 56 | 57 | You can use [PotPlayer](http://potplayer.daum.net/?lang=zh_CN) to play the test files. 58 | 59 | 60 | * Camera 61 | 62 | I wrap a class for simple usage [InstantVideoCamera](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/applibs/src/main/java/com/chillingvan/lib/camera/InstantVideoCamera.java) 63 | Not work well at my 4.4 phone but fine on my 6.0 phone. 64 | 65 | 66 | 67 | * AAC Audio 68 | 69 | Sample: [TestAudioEncoder](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoder.java) 70 | 71 | 72 | Test file directory:/storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_aac_encode.aac",可以在代码里修改输出路径 73 | You can use [PotPlayer](http://potplayer.daum.net/?lang=zh_CN) to play the test files. 74 | 75 | 76 | * [LibRtmp](https://github.com/ant-media/LibRtmp-Client-for-Android) for RTMP Stream 77 | 78 | Use [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) for your Nginx test local server. 79 | Use [OBS](https://obsproject.com/) for comparing effect. And player can be VLC,PotPlayer,ffplay. Make sure you use player open stream first because of the simple test server. 80 | 81 | Sample: [TestCameraPublisherActivity](https://github.com/ChillingVan/AndroidInstantVideo/blob/master/app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestCameraPublisherActivity.java) 82 | ```java 83 | 84 | public class TestCameraPublisherActivity extends AppCompatActivity { 85 | ... 86 | 87 | @Override 88 | protected void onCreate(Bundle savedInstanceState) { 89 | ... 90 | handler = new Handler(handlerThread.getLooper()) { 91 | @Override 92 | public void handleMessage(Message msg) { 93 | super.handleMessage(msg); 94 | StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(); 95 | streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() { 96 | @Override 97 | public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { 98 | 99 | // Here you can do video process 100 | // 此处可以视频处理,例如加水印等等 101 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight()); 102 | Loggers.i("DEBUG", "gl draw"); 103 | } 104 | }); 105 | try { 106 | streamPublisher.startPublish(addrEditText.getText().toString(), streamPublisherParam.width, streamPublisherParam.height); 107 | } catch (IOException e) { 108 | e.printStackTrace(); 109 | } 110 | } 111 | }; 112 | 113 | // streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(), cameraPreviewTextureView, instantVideoCamera); 114 | String filename = getExternalFilesDir(null) + "/test_flv_encode.flv"; 115 | streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(filename), cameraPreviewTextureView, instantVideoCamera); 116 | } 117 | ... 118 | } 119 | ``` 120 | 121 | ### Latest Update: 122 | 1. Add Mp4Muxer(Use Android MediaMuxer). Sample: TestMp4MuxerActivity 123 | 2. Update Interface Imuxer. Add more parameters to StreamPublisherParam. 124 | 125 | ### TODO 126 | 127 | 1. RTSP Stream. 128 | 129 | ### Pull Request 130 | 131 | Welcome. 132 | Please add sample code to sample module. Add your license comment. 133 | 134 | 135 | 136 | ## License 137 | Copyright 2017 ChillingVan. 138 | 139 | Licensed under the Apache License, Version 2.0 (the "License"); 140 | you may not use this file except in compliance with the License. 141 | You may obtain a copy of the License at 142 | 143 | http://www.apache.org/licenses/LICENSE-2.0 144 | 145 | Unless required by applicable law or agreed to in writing, software 146 | distributed under the License is distributed on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 148 | See the License for the specific language governing permissions and 149 | limitations under the License. 150 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | apply plugin: 'com.android.application' 22 | 23 | android { 24 | compileSdkVersion 32 25 | defaultConfig { 26 | applicationId "com.chillingvan.instantvideo.sample" 27 | minSdkVersion 18 28 | targetSdkVersion 32 29 | versionCode 3 30 | versionName "1.0.4" 31 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' 32 | externalNativeBuild { 33 | cmake { 34 | cppFlags "" 35 | } 36 | } 37 | } 38 | buildTypes { 39 | release { 40 | minifyEnabled false 41 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 42 | } 43 | } 44 | } 45 | 46 | dependencies { 47 | implementation fileTree(include: ['*.jar'], dir: 'libs') 48 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 49 | exclude group: 'com.android.support', module: 'support-annotations' 50 | }) 51 | api project(':applibs') 52 | implementation 'com.github.tajchert:nammu:1.5.1' 53 | implementation 'androidx.appcompat:appcompat:1.4.2' 54 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 55 | testImplementation 'junit:junit:4.12' 56 | } 57 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\develop-win\android\dev-tools-win\android-studio-windows\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /app/src/main/assets/test_video.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChillingVan/AndroidInstantVideo/7088bcecadc8e1d72d37ee02f74fdc00a0f11ba1/app/src/main/assets/test_video.mp4 -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/CrashHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample; 22 | 23 | import android.content.Context; 24 | 25 | import java.io.File; 26 | import java.io.FileOutputStream; 27 | import java.io.IOException; 28 | import java.io.PrintWriter; 29 | import java.io.StringWriter; 30 | import java.io.Writer; 31 | import java.text.DateFormat; 32 | import java.text.SimpleDateFormat; 33 | import java.util.Date; 34 | 35 | /** 36 | * Created by ling on 16-3-27. 37 | */ 38 | public class CrashHandler implements Thread.UncaughtExceptionHandler { 39 | 40 | private static CrashHandler INSTANCE = new CrashHandler(); 41 | private static Context mContext; 42 | public static String CRASH_PATH; 43 | 44 | private Thread.UncaughtExceptionHandler mDefaultHandler; 45 | 46 | private CrashHandler() { 47 | mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); 48 | Thread.setDefaultUncaughtExceptionHandler(this); 49 | } 50 | 51 | public static CrashHandler init(Context applicationContext) { 52 | if (mContext == null) { 53 | mContext = applicationContext; 54 | CRASH_PATH = mContext.getExternalFilesDir(null) + File.separator + "crash"; 55 | } 56 | return INSTANCE; 57 | } 58 | 59 | @Override 60 | public void uncaughtException(Thread thread, Throwable ex) { 61 | handleException(ex); 62 | mDefaultHandler.uncaughtException(thread, ex); 63 | } 64 | 65 | private boolean handleException(Throwable ex) { 66 | if (ex == null) { 67 | return false; 68 | } 69 | saveErrorInfo(ex); 70 | 71 | return true; 72 | } 73 | 74 | private void saveErrorInfo(Throwable ex) { 75 | File path = new File(CRASH_PATH); 76 | if (!path.exists()) { 77 | path.mkdirs(); 78 | } 79 | 80 | File file = new File(CRASH_PATH + File.separator + "log_" + convertYYMMDDHHmm(System.currentTimeMillis()) + ".txt"); 81 | FileOutputStream fos = null; 82 | try { 83 | fos = new FileOutputStream(file); 84 | if (!file.exists()) { 85 | file.createNewFile(); 86 | } 87 | Writer writer = new StringWriter(); 88 | PrintWriter pw = new PrintWriter(writer); 89 | ex.printStackTrace(pw); 90 | pw.close(); 91 | String error = writer.toString(); 92 | fos.write(error.getBytes()); 93 | } catch (IOException e) { 94 | e.printStackTrace(); 95 | } finally { 96 | try { 97 | if (fos != null) { 98 | fos.close(); 99 | } 100 | } catch (IOException e) { 101 | e.printStackTrace(); 102 | } 103 | } 104 | } 105 | 106 | private static String convertYYMMDDHHmm(long time) { 107 | Date date = new Date(time); 108 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); 109 | return dateFormat.format(date); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample; 22 | 23 | import android.Manifest; 24 | import android.app.ListActivity; 25 | import android.content.Intent; 26 | import android.content.pm.PackageManager; 27 | import android.content.pm.ResolveInfo; 28 | import android.os.Bundle; 29 | import android.view.View; 30 | import android.widget.ListView; 31 | import android.widget.SimpleAdapter; 32 | import android.widget.Toast; 33 | 34 | import com.chillingvan.canvasgl.util.FileLogger; 35 | import com.chillingvan.canvasgl.util.Loggers; 36 | 37 | import java.text.Collator; 38 | import java.util.ArrayList; 39 | import java.util.Collections; 40 | import java.util.Comparator; 41 | import java.util.HashMap; 42 | import java.util.List; 43 | import java.util.Map; 44 | 45 | import pl.tajchert.nammu.Nammu; 46 | import pl.tajchert.nammu.PermissionCallback; 47 | 48 | public class MainActivity extends ListActivity { 49 | private static final String CATEGORY_ACTIVITIES = "com.chillingvan.instantvideo.sample"; 50 | public static final String INTENT_PATH = "com.chillingvan.instantvideo.sample.Path"; 51 | 52 | 53 | @Override 54 | public void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | Loggers.DEBUG = true; 57 | FileLogger.init(getExternalFilesDir(null).getAbsolutePath()); 58 | FileLogger.d("MainActivity", "init -----------------"); 59 | CrashHandler.init(getApplicationContext()); 60 | getPermission(); 61 | 62 | Intent intent = getIntent(); 63 | String path = intent.getStringExtra(INTENT_PATH); 64 | 65 | if (path == null) { 66 | path = ""; 67 | } 68 | 69 | /*** The "title" is the "key" in "Map" ***/ 70 | setListAdapter(new SimpleAdapter(this, getData(path), 71 | android.R.layout.simple_list_item_1, new String[] { "title" }, 72 | new int[] { android.R.id.text1 })); 73 | 74 | //If you can get the keyboard, you can type to search the items of the list 75 | getListView().setTextFilterEnabled(true); 76 | 77 | } 78 | 79 | private void getPermission() { 80 | Nammu.INSTANCE.init(getApplicationContext()); 81 | askPermission(Manifest.permission.CAMERA); 82 | askPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); 83 | askPermission(Manifest.permission.RECORD_AUDIO); 84 | } 85 | 86 | private void askPermission(String permissionName) { 87 | if (!Nammu.INSTANCE.checkPermission(permissionName)) { 88 | if (Nammu.INSTANCE.shouldShowRequestPermissionRationale(this, permissionName)) { 89 | Toast.makeText(this, R.string.permission_tips, Toast.LENGTH_SHORT).show(); 90 | Nammu.INSTANCE.askForPermission(MainActivity.this, permissionName, new PermissionCallback() { 91 | @Override 92 | public void permissionGranted() { } 93 | @Override 94 | public void permissionRefused() { } 95 | }); 96 | } else { 97 | Nammu.INSTANCE.askForPermission(MainActivity.this, permissionName, new PermissionCallback() { 98 | @Override 99 | public void permissionGranted() { } 100 | @Override 101 | public void permissionRefused() { } 102 | }); 103 | } 104 | } 105 | } 106 | 107 | protected List> getData(String prefix) { 108 | List> myData = new ArrayList<>(); 109 | 110 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 111 | mainIntent.addCategory(CATEGORY_ACTIVITIES); 112 | 113 | PackageManager pm = getPackageManager(); 114 | List list = pm.queryIntentActivities(mainIntent, 0); 115 | 116 | if (null == list) { 117 | return myData; 118 | } 119 | 120 | String[] prefixPath; 121 | String prefixWithSlash = prefix; 122 | 123 | if (prefix.equals("")) { 124 | prefixPath = null; 125 | } else { 126 | prefixPath = prefix.split("/"); 127 | prefixWithSlash = prefix + "/"; 128 | } 129 | 130 | int len = list.size(); 131 | 132 | Map entries = new HashMap<>(); 133 | 134 | for (int i = 0; i < len; i++) { 135 | ResolveInfo info = list.get(i); 136 | CharSequence labelSeq = info.loadLabel(pm); 137 | String label = labelSeq != null 138 | ? labelSeq.toString() 139 | : info.activityInfo.name; 140 | if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) { 141 | 142 | String[] labelPath = label.split("/"); 143 | 144 | String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]; 145 | 146 | if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) { 147 | addItem(myData, nextLabel, activityIntent( 148 | info.activityInfo.applicationInfo.packageName, 149 | info.activityInfo.name)); 150 | } else { 151 | if (entries.get(nextLabel) == null) { 152 | addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel)); 153 | entries.put(nextLabel, true); 154 | } 155 | } 156 | } 157 | } 158 | 159 | Collections.sort(myData, sDisplayNameComparator); 160 | 161 | return myData; 162 | } 163 | 164 | /** 165 | * Compare the tile text using UTF code(Collator). 166 | */ 167 | private final static Comparator> sDisplayNameComparator = 168 | new Comparator>() { 169 | private final Collator collator = Collator.getInstance(); 170 | 171 | public int compare(Map map1, Map map2) { 172 | return collator.compare(map1.get("title"), map2.get("title")); 173 | } 174 | }; 175 | 176 | protected Intent activityIntent(String pkg, String componentName) { 177 | Intent result = new Intent(); 178 | result.setClassName(pkg, componentName); 179 | return result; 180 | } 181 | 182 | /** 183 | * 184 | * @param path 185 | * @return 186 | */ 187 | protected Intent browseIntent(String path) { 188 | Intent result = new Intent(); 189 | result.setClass(this, MainActivity.class); 190 | result.putExtra(INTENT_PATH, path); 191 | return result; 192 | } 193 | 194 | /** 195 | * Add activity data, packaged by intent. 196 | * @param data 197 | * @param name 198 | * @param intent 199 | */ 200 | protected void addItem(List> data, String name, Intent intent) { 201 | Map temp = new HashMap(); 202 | temp.put("title", name); 203 | temp.put("intent", intent); 204 | data.add(temp); 205 | } 206 | 207 | @Override 208 | @SuppressWarnings("unchecked") 209 | public void onListItemClick(ListView l, View v, int position, long id) { 210 | Map map = (Map)l.getItemAtPosition(position); 211 | 212 | Intent intent = (Intent) map.get("intent"); 213 | startActivity(intent); 214 | } 215 | 216 | 217 | } 218 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/VideoFrameHandlerHelper.java: -------------------------------------------------------------------------------- 1 | package com.chillingvan.instantvideo.sample.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | 9 | import com.chillingvan.canvasgl.ICanvasGL; 10 | import com.chillingvan.canvasgl.androidCanvas.IAndroidCanvasHelper; 11 | import com.chillingvan.instantvideo.sample.util.ScreenUtil; 12 | 13 | /** 14 | * Created by Chilling on 2022/11/14. 15 | */ 16 | 17 | public class VideoFrameHandlerHelper { 18 | 19 | private final Context context; 20 | 21 | public VideoFrameHandlerHelper(Context context) { 22 | this.context = context; 23 | } 24 | 25 | private IAndroidCanvasHelper drawTextHelper = IAndroidCanvasHelper.Factory.createAndroidCanvasHelper(IAndroidCanvasHelper.MODE.MODE_ASYNC); 26 | private Paint textPaint = new Paint(); 27 | 28 | { 29 | initTextPaint(); 30 | } 31 | 32 | private void initTextPaint() { 33 | textPaint.setColor(Color.WHITE); 34 | textPaint.setTextSize(ScreenUtil.dpToPx(context, 15)); 35 | } 36 | 37 | public void initDrawHelper(int width, int height) { 38 | drawTextHelper.init(width, height); 39 | } 40 | 41 | public void drawText(ICanvasGL canvasGL) { 42 | drawTextHelper.draw(new IAndroidCanvasHelper.CanvasPainter() { 43 | @Override 44 | public void draw(Canvas androidCanvas, Bitmap drawingBitmap) { 45 | androidCanvas.drawText("白色, White", 100, 100, textPaint); 46 | } 47 | }); 48 | Bitmap outputBitmap = drawTextHelper.getOutputBitmap(); 49 | canvasGL.invalidateTextureContent(outputBitmap); 50 | canvasGL.drawBitmap(outputBitmap, 0, 0); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.audio; 22 | 23 | import android.content.Context; 24 | import android.media.MediaCodec; 25 | import android.media.MediaRecorder; 26 | 27 | import com.chillingvan.canvasgl.util.Loggers; 28 | import com.chillingvan.lib.encoder.MediaCodecInputStream; 29 | import com.chillingvan.lib.encoder.audio.AACEncoder; 30 | import com.chillingvan.lib.publisher.StreamPublisher; 31 | 32 | import java.io.File; 33 | import java.io.FileNotFoundException; 34 | import java.io.FileOutputStream; 35 | import java.io.IOException; 36 | import java.io.OutputStream; 37 | 38 | /** 39 | * Created by Chilling on 2017/1/23. 40 | */ 41 | 42 | public class TestAudioEncoder { 43 | 44 | 45 | private AACEncoder aacEncoder; 46 | private byte[] writeBuffer = new byte[1024 * 64]; 47 | private OutputStream os; 48 | private boolean isStart; 49 | 50 | public TestAudioEncoder(Context ctx) { 51 | 52 | try { 53 | os = new FileOutputStream(ctx.getExternalFilesDir(null) + File.separator + "test_aac_encode.aac"); 54 | } catch (FileNotFoundException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | 59 | public void prepareEncoder() { 60 | try { 61 | StreamPublisher.StreamPublisherParam.Builder builder = new StreamPublisher.StreamPublisherParam.Builder(); 62 | builder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 63 | StreamPublisher.StreamPublisherParam streamPublisherParam = builder.createStreamPublisherParam(); 64 | aacEncoder = new AACEncoder(streamPublisherParam); 65 | aacEncoder.setOnDataComingCallback(new AACEncoder.OnDataComingCallback() { 66 | @Override 67 | public void onComing() { 68 | write(); 69 | } 70 | }); 71 | } catch (IOException e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | 76 | public void start() { 77 | if (!isStart) { 78 | aacEncoder.start(); 79 | isStart = true; 80 | } 81 | } 82 | 83 | 84 | public void stop() { 85 | isStart = false; 86 | if (aacEncoder != null) { 87 | aacEncoder.close(); 88 | aacEncoder = null; 89 | } 90 | } 91 | 92 | public boolean isStart() { 93 | return isStart; 94 | } 95 | 96 | public void write() { 97 | MediaCodecInputStream mediaCodecInputStream = aacEncoder.getMediaCodecInputStream(); 98 | MediaCodecInputStream.readAll(mediaCodecInputStream, writeBuffer, new MediaCodecInputStream.OnReadAllCallback() { 99 | boolean shouldAddPacketHeader = true; 100 | byte[] header = new byte[7]; 101 | @Override 102 | public void onReadOnce(byte[] buffer, int readSize, MediaCodec.BufferInfo bufferInfo) { 103 | if (readSize <= 0) { 104 | return; 105 | } 106 | try { 107 | Loggers.d("TestAudioEncoder", String.format("onReadOnce: readSize:%d, bufferInfo:%d", readSize, bufferInfo.size)); 108 | if (shouldAddPacketHeader) { 109 | Loggers.d("TestAudioEncoder", String.format("onReadOnce: add packet header")); 110 | AACEncoder.addADTStoPacket(header, 7 + bufferInfo.size, StreamPublisher.StreamPublisherParam.DEFAULT_CHANNEL_CNT); 111 | os.write(header); 112 | } 113 | os.write(buffer, 0, readSize); 114 | os.flush(); 115 | } catch (IOException e) { 116 | e.printStackTrace(); 117 | } 118 | shouldAddPacketHeader = readSize >= bufferInfo.size; 119 | } 120 | }); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoderActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.audio; 22 | 23 | import android.os.Bundle; 24 | import androidx.appcompat.app.AppCompatActivity; 25 | import android.view.View; 26 | import android.widget.TextView; 27 | 28 | import com.chillingvan.instantvideo.sample.R; 29 | 30 | public class TestAudioEncoderActivity extends AppCompatActivity { 31 | 32 | private TestAudioEncoder testAudioEncoder; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_test_audio_encoder); 38 | 39 | testAudioEncoder = new TestAudioEncoder(getApplicationContext()); 40 | testAudioEncoder.prepareEncoder(); 41 | } 42 | 43 | 44 | 45 | 46 | @Override 47 | protected void onPause() { 48 | super.onPause(); 49 | testAudioEncoder.stop(); 50 | } 51 | 52 | public void clickStartTest(View view) { 53 | TextView textView = (TextView) view; 54 | if (testAudioEncoder.isStart()) { 55 | testAudioEncoder.stop(); 56 | textView.setText("RECORD"); 57 | } else { 58 | testAudioEncoder.prepareEncoder(); 59 | testAudioEncoder.start(); 60 | textView.setText("PAUSE"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/camera/CameraPreviewTextureView.java: -------------------------------------------------------------------------------- 1 | package com.chillingvan.instantvideo.sample.test.camera; 2 | 3 | import android.content.Context; 4 | import android.graphics.SurfaceTexture; 5 | import android.util.AttributeSet; 6 | 7 | import com.chillingvan.canvasgl.ICanvasGL; 8 | import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView; 9 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 10 | import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper; 11 | import com.chillingvan.instantvideo.sample.test.VideoFrameHandlerHelper; 12 | import com.chillingvan.lib.encoder.video.H264Encoder; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * Created by Leon on 2017/4/19. 18 | */ 19 | 20 | public class CameraPreviewTextureView extends GLMultiTexProducerView { 21 | 22 | private H264Encoder.OnDrawListener onDrawListener; 23 | private VideoFrameHandlerHelper videoFrameHandlerHelper = new VideoFrameHandlerHelper(getContext().getApplicationContext()); 24 | 25 | public CameraPreviewTextureView(Context context) { 26 | super(context); 27 | } 28 | 29 | public CameraPreviewTextureView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | } 32 | 33 | public CameraPreviewTextureView(Context context, AttributeSet attrs, int defStyleAttr) { 34 | super(context, attrs, defStyleAttr); 35 | } 36 | 37 | @Override 38 | protected int getInitialTexCount() { 39 | return 1; 40 | } 41 | 42 | @Override 43 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 44 | super.onSurfaceTextureAvailable(surface, width, height); 45 | if (mSharedEglContext == null) { 46 | setSharedEglContext(EglContextWrapper.EGL_NO_CONTEXT_WRAPPER); 47 | } 48 | } 49 | 50 | @Override 51 | public void onSurfaceChanged(int width, int height) { 52 | super.onSurfaceChanged(width, height); 53 | videoFrameHandlerHelper.initDrawHelper(width, height); 54 | } 55 | 56 | @Override 57 | protected void onGLDraw(ICanvasGL canvas, List producedTextures, List consumedTextures) { 58 | onDrawListener.onGLDraw(canvas, producedTextures, consumedTextures); 59 | videoFrameHandlerHelper.drawText(canvas); 60 | } 61 | 62 | public void setOnDrawListener(H264Encoder.OnDrawListener l) { 63 | onDrawListener = l; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/camera/TestCameraAndVideoActivity.java: -------------------------------------------------------------------------------- 1 | package com.chillingvan.instantvideo.sample.test.camera; 2 | 3 | import android.graphics.SurfaceTexture; 4 | import android.hardware.Camera; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.HandlerThread; 8 | import android.os.Message; 9 | import androidx.appcompat.app.AppCompatActivity; 10 | import android.view.View; 11 | import android.widget.TextView; 12 | 13 | import com.chillingvan.canvasgl.ICanvasGL; 14 | import com.chillingvan.canvasgl.glcanvas.RawTexture; 15 | import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView; 16 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 17 | import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper; 18 | import com.chillingvan.canvasgl.glview.texture.gles.GLThread; 19 | import com.chillingvan.instantvideo.sample.R; 20 | import com.chillingvan.instantvideo.sample.test.video.TestVideoEncoder; 21 | import com.chillingvan.lib.camera.InstantVideoCamera; 22 | import com.chillingvan.lib.encoder.video.H264Encoder; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Data Stream: 28 | * Camera -> SurfaceTexture -> Surface -> MediaCodec -> encode data(byte[]) -> File 29 | */ 30 | public class TestCameraAndVideoActivity extends AppCompatActivity { 31 | 32 | private TestVideoEncoder testVideoEncoder; 33 | private CameraPreviewTextureView cameraPreviewTextureView; 34 | private InstantVideoCamera instantVideoCamera; 35 | private HandlerThread outputVideoThread; 36 | private Handler outputVideoHandler; 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_camera_and_video); 42 | cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view); 43 | cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() { 44 | @Override 45 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 46 | 47 | GLTexture texture = producedTextures.get(0); 48 | SurfaceTexture surfaceTexture = texture.getSurfaceTexture(); 49 | RawTexture rawTexture = texture.getRawTexture(); 50 | canvasGL.drawSurfaceTexture(rawTexture, surfaceTexture, 0, 0, rawTexture.getWidth(), rawTexture.getHeight()); 51 | } 52 | }); 53 | instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720); 54 | 55 | } 56 | 57 | private void initWriteFileHandler() { 58 | outputVideoThread = new HandlerThread("outputVideoThread"); 59 | outputVideoThread.start(); 60 | 61 | outputVideoHandler = new Handler(outputVideoThread.getLooper()) { 62 | @Override 63 | public void handleMessage(Message msg) { 64 | super.handleMessage(msg); 65 | if (msg.what == 0) { 66 | testVideoEncoder.write(); 67 | } 68 | } 69 | }; 70 | } 71 | 72 | @Override 73 | protected void onResume() { 74 | super.onResume(); 75 | initWriteFileHandler(); 76 | instantVideoCamera.openCamera(); 77 | initCameraTexture(); 78 | cameraPreviewTextureView.onResume(); 79 | } 80 | 81 | private void initCameraTexture() { 82 | cameraPreviewTextureView.setOnCreateGLContextListener(new GLThread.OnCreateGLContextListener() { 83 | @Override 84 | public void onCreate(EglContextWrapper eglContext) { 85 | testVideoEncoder = new TestVideoEncoder(getApplicationContext(), eglContext); 86 | } 87 | }); 88 | cameraPreviewTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() { 89 | @Override 90 | public void onCreated(List producedTextureList) { 91 | GLTexture texture = producedTextureList.get(0); 92 | SurfaceTexture surfaceTexture = texture.getSurfaceTexture(); 93 | testVideoEncoder.addSharedTexture(texture.getRawTexture(), surfaceTexture); 94 | surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() { 95 | @Override 96 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { 97 | cameraPreviewTextureView.requestRenderAndWait(); 98 | if (testVideoEncoder.encodeAFrame()) { 99 | outputVideoHandler.sendEmptyMessage(0); 100 | } 101 | } 102 | }); 103 | 104 | instantVideoCamera.setPreview(surfaceTexture); 105 | instantVideoCamera.startPreview(); 106 | } 107 | }); 108 | } 109 | 110 | 111 | @Override 112 | protected void onPause() { 113 | super.onPause(); 114 | instantVideoCamera.release(); 115 | cameraPreviewTextureView.onPause(); 116 | 117 | if (testVideoEncoder.isStart()) { 118 | testVideoEncoder.stop(); 119 | } 120 | outputVideoThread.quit(); 121 | } 122 | 123 | 124 | @Override 125 | protected void onDestroy() { 126 | super.onDestroy(); 127 | testVideoEncoder.destroy(); 128 | } 129 | public void clickStartTest(View view) { 130 | TextView textView = (TextView) view; 131 | if (testVideoEncoder.isStart()) { 132 | testVideoEncoder.stop(); 133 | textView.setText("START"); 134 | } else { 135 | testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() { 136 | @Override 137 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 138 | GLTexture texture = consumedTextures.get(0); 139 | SurfaceTexture outsideSurfaceTexture = texture.getSurfaceTexture(); 140 | RawTexture outsideTexture = texture.getRawTexture(); 141 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight()); 142 | } 143 | 144 | }); 145 | testVideoEncoder.start(); 146 | textView.setText("STOP"); 147 | } 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/MediaPlayerHelper.java: -------------------------------------------------------------------------------- 1 | package com.chillingvan.instantvideo.sample.test.publisher; 2 | 3 | import android.content.Context; 4 | import android.content.res.AssetFileDescriptor; 5 | import android.media.AudioManager; 6 | import android.media.MediaPlayer; 7 | import android.view.Surface; 8 | import android.widget.Toast; 9 | 10 | import com.chillingvan.canvasgl.util.Loggers; 11 | 12 | import java.io.IOException; 13 | 14 | /** 15 | * Created by Chilling on 2018/4/14. 16 | */ 17 | public class MediaPlayerHelper { 18 | 19 | public static final String TEST_VIDEO_MP4 = "test_video.mp4"; 20 | private MediaPlayer mediaPlayer; 21 | private String videoName; 22 | 23 | public MediaPlayerHelper() { 24 | this(TEST_VIDEO_MP4); 25 | } 26 | 27 | public MediaPlayerHelper(String videoName) { 28 | this.videoName = videoName; 29 | } 30 | 31 | public boolean isPlaying() { 32 | if (mediaPlayer != null) { 33 | return mediaPlayer.isPlaying(); 34 | } 35 | return false; 36 | } 37 | 38 | public boolean isLooping() { 39 | if (mediaPlayer != null) { 40 | return mediaPlayer.isLooping(); 41 | } 42 | return false; 43 | } 44 | 45 | public void restart() { 46 | if (mediaPlayer != null) { 47 | mediaPlayer.start(); 48 | } 49 | 50 | } 51 | 52 | public void playMedia(final Context context, Surface mediaSurface) { 53 | mediaPlayer = new MediaPlayer(); 54 | // mediaPlayer.setVolume(0, 0); 55 | try { 56 | AssetFileDescriptor afd = context.getAssets().openFd(videoName); 57 | mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 58 | afd.close(); 59 | } catch (IOException e) { 60 | e.printStackTrace(); 61 | } 62 | mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 63 | mediaPlayer.setSurface(mediaSurface); 64 | mediaPlayer.setLooping(true); 65 | 66 | mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() { 67 | @Override 68 | public void onSeekComplete(MediaPlayer mediaPlayer) { 69 | Loggers.i("onSeekComplete","onSeekComplete----"+mediaPlayer.getCurrentPosition()); 70 | } 71 | }); 72 | 73 | mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 74 | @Override 75 | public void onPrepared(MediaPlayer mediaPlayer) { 76 | Toast.makeText(context, "onPrepare --> Start", Toast.LENGTH_SHORT).show(); 77 | mediaPlayer.start(); 78 | } 79 | }); 80 | 81 | 82 | mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 83 | @Override 84 | public void onCompletion(MediaPlayer m) { 85 | Toast.makeText(context, "End Play", Toast.LENGTH_LONG).show(); 86 | m.stop(); 87 | m.release(); 88 | mediaPlayer = null; 89 | } 90 | }); 91 | 92 | try { 93 | mediaPlayer.prepare(); 94 | } catch (IOException e) { 95 | e.printStackTrace(); 96 | } 97 | 98 | } 99 | 100 | public void pause() { 101 | if (mediaPlayer != null) { 102 | mediaPlayer.pause(); 103 | } 104 | } 105 | 106 | 107 | public void release() { 108 | if (mediaPlayer != null) { 109 | mediaPlayer.release(); 110 | mediaPlayer = null; 111 | } 112 | } 113 | } -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestCameraPublisherActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.publisher; 22 | 23 | import android.graphics.SurfaceTexture; 24 | import android.hardware.Camera; 25 | import android.os.Bundle; 26 | import android.os.Handler; 27 | import android.os.HandlerThread; 28 | import android.os.Message; 29 | import android.view.View; 30 | import android.widget.EditText; 31 | import android.widget.TextView; 32 | 33 | import com.chillingvan.canvasgl.ICanvasGL; 34 | import com.chillingvan.canvasgl.glcanvas.BasicTexture; 35 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 36 | import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter; 37 | import com.chillingvan.canvasgl.textureFilter.HueFilter; 38 | import com.chillingvan.canvasgl.textureFilter.TextureFilter; 39 | import com.chillingvan.instantvideo.sample.R; 40 | import com.chillingvan.instantvideo.sample.test.VideoFrameHandlerHelper; 41 | import com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView; 42 | import com.chillingvan.lib.camera.InstantVideoCamera; 43 | import com.chillingvan.lib.encoder.video.H264Encoder; 44 | import com.chillingvan.lib.muxer.RTMPStreamMuxer; 45 | import com.chillingvan.lib.publisher.CameraStreamPublisher; 46 | import com.chillingvan.lib.publisher.StreamPublisher; 47 | 48 | import java.io.IOException; 49 | import java.util.List; 50 | 51 | import androidx.annotation.Nullable; 52 | import androidx.appcompat.app.AppCompatActivity; 53 | 54 | public class TestCameraPublisherActivity extends AppCompatActivity { 55 | 56 | private CameraStreamPublisher streamPublisher; 57 | private CameraPreviewTextureView cameraPreviewTextureView; 58 | private InstantVideoCamera instantVideoCamera; 59 | private Handler handler; 60 | private EditText addrEditText; 61 | private HandlerThread handlerThread; 62 | private TextureFilter textureFilterLT; 63 | private TextureFilter textureFilterRT; 64 | private VideoFrameHandlerHelper videoFrameHandlerHelper; 65 | 66 | @Override 67 | protected void onCreate(Bundle savedInstanceState) { 68 | super.onCreate(savedInstanceState); 69 | initFrameHandlerHelper(); 70 | setContentView(R.layout.activity_test_camera_publisher); 71 | cameraPreviewTextureView = findViewById(R.id.camera_produce_view); 72 | cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() { 73 | @Override 74 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 75 | 76 | GLTexture texture = producedTextures.get(0); 77 | drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture()); 78 | } 79 | 80 | }); 81 | addrEditText = (EditText) findViewById(R.id.ip_input_test); 82 | 83 | 84 | instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 640, 480); 85 | // instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720); 86 | 87 | handlerThread = new HandlerThread("StreamPublisherOpen"); 88 | handlerThread.start(); 89 | handler = new Handler(handlerThread.getLooper()) { 90 | @Override 91 | public void handleMessage(Message msg) { 92 | super.handleMessage(msg); 93 | // StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(); 94 | // StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(1080, 640, 9500 * 1000, 30, 1, 44100, 19200); 95 | StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam.Builder().setWidth(540).setHeight(750).setVideoBitRate(1500 * 1000).setFrameRate(30).setIframeInterval(1).setSamplingRate(44100).setAudioBitRate(32000).createStreamPublisherParam(); 96 | streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_flv_encode.flv"; 97 | // streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_mp4_encode.mp4"; 98 | streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() { 99 | @Override 100 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 101 | GLTexture texture = consumedTextures.get(0); 102 | drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture()); 103 | } 104 | }); 105 | try { 106 | streamPublisherParam.outputUrl = addrEditText.getText().toString(); 107 | streamPublisher.startPublish(); 108 | } catch (IOException e) { 109 | e.printStackTrace(); 110 | runOnUiThread(new Runnable() { 111 | @Override 112 | public void run() { 113 | ((TextView)findViewById(R.id.test_camera_button)).setText("START"); 114 | } 115 | }); 116 | } 117 | } 118 | }; 119 | 120 | streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(), cameraPreviewTextureView, instantVideoCamera); 121 | // streamPublisher = new CameraStreamPublisher(new MP4Muxer(), cameraPreviewTextureView, instantVideoCamera); 122 | } 123 | 124 | private void initFrameHandlerHelper() { 125 | videoFrameHandlerHelper = new VideoFrameHandlerHelper(getApplicationContext()); 126 | } 127 | 128 | private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { 129 | // Here you can do video process 130 | // 此处可以视频处理,例如加水印等等 131 | if(textureFilterLT == null) { 132 | textureFilterLT = new BasicTextureFilter(); 133 | } 134 | if(textureFilterRT == null) { 135 | textureFilterRT = new HueFilter(180); 136 | } 137 | int width = outsideTexture.getWidth(); 138 | int height = outsideTexture.getHeight(); 139 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, width /2, height /2, textureFilterLT); 140 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, height/2, width/2, height, textureFilterRT); 141 | videoFrameHandlerHelper.initDrawHelper(width/2, height/2); 142 | videoFrameHandlerHelper.drawText(canvasGL); 143 | } 144 | 145 | @Override 146 | protected void onResume() { 147 | super.onResume(); 148 | streamPublisher.resumeCamera(); 149 | } 150 | 151 | @Override 152 | protected void onPause() { 153 | super.onPause(); 154 | streamPublisher.pauseCamera(); 155 | if (streamPublisher.isStart()) { 156 | streamPublisher.closeAll(); 157 | } 158 | ((TextView)findViewById(R.id.test_camera_button)).setText("START"); 159 | } 160 | 161 | @Override 162 | protected void onDestroy() { 163 | super.onDestroy(); 164 | handlerThread.quitSafely(); 165 | } 166 | 167 | public void clickStartTest(View view) { 168 | TextView textView = (TextView) view; 169 | if (streamPublisher.isStart()) { 170 | streamPublisher.closeAll(); 171 | textView.setText("START"); 172 | } else { 173 | streamPublisher.resumeCamera(); 174 | handler.sendEmptyMessage(1); 175 | textView.setText("STOP"); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestMp4MuxerActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.publisher; 22 | 23 | import android.graphics.SurfaceTexture; 24 | import android.hardware.Camera; 25 | import android.media.MediaRecorder; 26 | import android.os.Bundle; 27 | import android.os.Handler; 28 | import android.os.HandlerThread; 29 | import android.os.Message; 30 | import androidx.annotation.Nullable; 31 | import androidx.appcompat.app.AppCompatActivity; 32 | import android.view.Surface; 33 | import android.view.View; 34 | import android.widget.Button; 35 | import android.widget.TextView; 36 | 37 | import com.chillingvan.canvasgl.ICanvasGL; 38 | import com.chillingvan.canvasgl.glcanvas.BasicTexture; 39 | import com.chillingvan.canvasgl.glcanvas.RawTexture; 40 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 41 | import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter; 42 | import com.chillingvan.canvasgl.textureFilter.HueFilter; 43 | import com.chillingvan.canvasgl.textureFilter.TextureFilter; 44 | import com.chillingvan.canvasgl.util.Loggers; 45 | import com.chillingvan.instantvideo.sample.R; 46 | import com.chillingvan.instantvideo.sample.test.VideoFrameHandlerHelper; 47 | import com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView; 48 | import com.chillingvan.lib.camera.InstantVideoCamera; 49 | import com.chillingvan.lib.encoder.video.H264Encoder; 50 | import com.chillingvan.lib.muxer.MP4Muxer; 51 | import com.chillingvan.lib.publisher.CameraStreamPublisher; 52 | import com.chillingvan.lib.publisher.StreamPublisher; 53 | 54 | import java.io.IOException; 55 | import java.util.List; 56 | 57 | /** 58 | * This sample shows how to record a mp4 file. It cannot pause but only can restart. If you want to pause when recording, you need to generate a new file and merge old files and new file. 59 | */ 60 | public class TestMp4MuxerActivity extends AppCompatActivity { 61 | 62 | private CameraStreamPublisher streamPublisher; 63 | private CameraPreviewTextureView cameraPreviewTextureView; 64 | private InstantVideoCamera instantVideoCamera; 65 | private Handler handler; 66 | private HandlerThread handlerThread; 67 | private TextView outDirTxt; 68 | private String outputDir; 69 | 70 | private MediaPlayerHelper mediaPlayer = new MediaPlayerHelper(); 71 | private Surface mediaSurface; 72 | 73 | private VideoFrameHandlerHelper videoFrameHandlerHelper; 74 | private Button startButton; 75 | 76 | @Override 77 | protected void onCreate(Bundle savedInstanceState) { 78 | super.onCreate(savedInstanceState); 79 | initFrameHandlerHelper(); 80 | outputDir = getExternalFilesDir(null) + "/test_mp4_encode.mp4"; 81 | setContentView(R.layout.activity_test_mp4_muxer); 82 | cameraPreviewTextureView = findViewById(R.id.camera_produce_view); 83 | cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() { 84 | @Override 85 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 86 | GLTexture texture = producedTextures.get(0); 87 | GLTexture mediaTexture = producedTextures.get(1); 88 | drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture(), mediaTexture); 89 | } 90 | 91 | }); 92 | outDirTxt = (TextView) findViewById(R.id.output_dir_txt); 93 | outDirTxt.setText(outputDir); 94 | startButton = findViewById(R.id.test_camera_button); 95 | 96 | 97 | instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 640, 480); 98 | // instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720); 99 | 100 | handlerThread = new HandlerThread("StreamPublisherOpen"); 101 | handlerThread.start(); 102 | handler = new Handler(handlerThread.getLooper()) { 103 | @Override 104 | public void handleMessage(Message msg) { 105 | super.handleMessage(msg); 106 | playMedia(); 107 | // StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(); 108 | // StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(1080, 640, 9500 * 1000, 30, 1, 44100, 19200); 109 | StreamPublisher.StreamPublisherParam.Builder builder = new StreamPublisher.StreamPublisherParam.Builder(); 110 | StreamPublisher.StreamPublisherParam streamPublisherParam = builder 111 | .setWidth(1080) 112 | .setHeight(750) 113 | .setVideoBitRate(1500 * 1000) 114 | .setFrameRate(30) 115 | .setIframeInterval(1) 116 | .setSamplingRate(44100) 117 | .setAudioBitRate(32000) 118 | .setAudioSource(MediaRecorder.AudioSource.MIC) 119 | .createStreamPublisherParam(); 120 | streamPublisherParam.outputFilePath = outputDir; 121 | streamPublisherParam.setInitialTextureCount(2); 122 | streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() { 123 | @Override 124 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 125 | GLTexture texture = consumedTextures.get(1); 126 | GLTexture mediaTexture = consumedTextures.get(0); 127 | drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture(), mediaTexture); 128 | Loggers.i("DEBUG", "gl draw"); 129 | } 130 | 131 | }); 132 | try { 133 | streamPublisher.startPublish(); 134 | } catch (IOException e) { 135 | e.printStackTrace(); 136 | ((TextView)findViewById(R.id.test_camera_button)).setText("START"); 137 | } 138 | } 139 | }; 140 | 141 | streamPublisher = new CameraStreamPublisher(new MP4Muxer(), cameraPreviewTextureView, instantVideoCamera); 142 | streamPublisher.setOnSurfacesCreatedListener(new CameraStreamPublisher.OnSurfacesCreatedListener() { 143 | @Override 144 | public void onCreated(List producedTextureList, StreamPublisher streamPublisher) { 145 | GLTexture texture = producedTextureList.get(1); 146 | GLTexture mediaTexture = producedTextureList.get(1); 147 | streamPublisher.addSharedTexture(new GLTexture(mediaTexture.getRawTexture(), mediaTexture.getSurfaceTexture())); 148 | mediaSurface = new Surface(texture.getSurfaceTexture()); 149 | } 150 | }); 151 | } 152 | 153 | private void initFrameHandlerHelper() { 154 | videoFrameHandlerHelper = new VideoFrameHandlerHelper(getApplicationContext()); 155 | } 156 | 157 | private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture, GLTexture mediaTexture) { 158 | // Here you can do video process 159 | // 此处可以视频处理,例如加水印等等 160 | TextureFilter textureFilterLT = new BasicTextureFilter(); 161 | TextureFilter textureFilterRT = new HueFilter(180); 162 | int width = outsideTexture.getWidth(); 163 | int height = outsideTexture.getHeight(); 164 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, width /2, height /2, textureFilterLT); 165 | canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, height/2, width/2, height, textureFilterRT); 166 | videoFrameHandlerHelper.initDrawHelper(width/2, height/2); 167 | videoFrameHandlerHelper.drawText(canvasGL); 168 | SurfaceTexture mediaSurfaceTexture = mediaTexture.getSurfaceTexture(); 169 | RawTexture mediaRawTexture = mediaTexture.getRawTexture(); 170 | mediaRawTexture.setIsFlippedVertically(true); 171 | canvasGL.drawSurfaceTexture(mediaRawTexture, mediaSurfaceTexture, width/2, height/2, width, height); 172 | } 173 | 174 | @Override 175 | protected void onResume() { 176 | super.onResume(); 177 | streamPublisher.resumeCamera(); 178 | } 179 | 180 | @Override 181 | protected void onPause() { 182 | super.onPause(); 183 | streamPublisher.pauseCamera(); 184 | if (streamPublisher.isStart()) { 185 | streamPublisher.closeAll(); 186 | } 187 | if (mediaPlayer.isPlaying()) { 188 | mediaPlayer.release(); 189 | } 190 | startButton.setText("START"); 191 | } 192 | 193 | private void playMedia() { 194 | if ((mediaPlayer.isPlaying() || mediaPlayer.isLooping())) { 195 | return; 196 | } 197 | 198 | mediaPlayer.playMedia(this, mediaSurface); 199 | } 200 | 201 | @Override 202 | protected void onDestroy() { 203 | super.onDestroy(); 204 | if (mediaPlayer.isPlaying()) { 205 | mediaPlayer.release(); 206 | } 207 | handlerThread.quitSafely(); 208 | } 209 | 210 | public void clickStartTest(View view) { 211 | TextView textView = (TextView) view; 212 | if (streamPublisher.isStart()) { 213 | streamPublisher.closeAll(); 214 | if (mediaPlayer.isPlaying()) { 215 | mediaPlayer.pause(); 216 | } 217 | textView.setText("START"); 218 | } else { 219 | streamPublisher.resumeCamera(); 220 | handler.sendEmptyMessage(1); 221 | textView.setText("STOP"); 222 | } 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/video/ProduceTextureView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.video; 22 | 23 | import android.content.Context; 24 | import android.graphics.Bitmap; 25 | import android.graphics.BitmapFactory; 26 | import android.graphics.SurfaceTexture; 27 | import android.opengl.GLES20; 28 | import androidx.annotation.Nullable; 29 | import android.util.AttributeSet; 30 | 31 | import com.chillingvan.canvasgl.ICanvasGL; 32 | import com.chillingvan.canvasgl.glcanvas.BasicTexture; 33 | import com.chillingvan.canvasgl.glcanvas.RawTexture; 34 | import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView; 35 | import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter; 36 | import com.chillingvan.canvasgl.textureFilter.TextureFilter; 37 | import com.chillingvan.instantvideo.sample.R; 38 | 39 | import static com.chillingvan.instantvideo.sample.test.video.TestVideoEncoder.drawCnt; 40 | 41 | /** 42 | * Created by Chilling on 2016/11/3. 43 | */ 44 | 45 | public class ProduceTextureView extends GLSurfaceTextureProducerView { 46 | 47 | private TextureFilter textureFilter = new BasicTextureFilter(); 48 | private Bitmap bitmap; 49 | 50 | public ProduceTextureView(Context context) { 51 | super(context); 52 | } 53 | 54 | public ProduceTextureView(Context context, AttributeSet attrs) { 55 | super(context, attrs); 56 | } 57 | 58 | public ProduceTextureView(Context context, AttributeSet attrs, int defStyleAttr) { 59 | super(context, attrs, defStyleAttr); 60 | } 61 | 62 | 63 | @Override 64 | public void onSurfaceCreated() { 65 | bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna); 66 | setProducedTextureTarget(GLES20.GL_TEXTURE_2D); 67 | super.onSurfaceCreated(); 68 | } 69 | 70 | public void setTextureFilter(TextureFilter textureFilter) { 71 | this.textureFilter = textureFilter; 72 | } 73 | 74 | 75 | @Override 76 | protected void onGLDraw(ICanvasGL canvas, SurfaceTexture producedSurfaceTexture, RawTexture producedRawTexture, @Nullable SurfaceTexture sharedSurfaceTexture, @Nullable BasicTexture sharedTexture) { 77 | if (drawCnt == 19 || drawCnt == 39) { 78 | canvas.drawBitmap(bitmap, 0, 0); 79 | } 80 | TestVideoEncoder.drawRect(canvas, drawCnt); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.video; 22 | 23 | import android.content.Context; 24 | import android.graphics.Color; 25 | import android.graphics.Rect; 26 | import android.graphics.SurfaceTexture; 27 | import android.media.MediaCodec; 28 | import android.util.Log; 29 | 30 | import com.chillingvan.canvasgl.ICanvasGL; 31 | import com.chillingvan.canvasgl.glcanvas.GLPaint; 32 | import com.chillingvan.canvasgl.glcanvas.RawTexture; 33 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 34 | import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper; 35 | import com.chillingvan.canvasgl.util.Loggers; 36 | import com.chillingvan.lib.encoder.MediaCodecInputStream; 37 | import com.chillingvan.lib.encoder.video.H264Encoder; 38 | import com.chillingvan.lib.publisher.StreamPublisher; 39 | 40 | import java.io.File; 41 | import java.io.FileNotFoundException; 42 | import java.io.FileOutputStream; 43 | import java.io.IOException; 44 | import java.io.OutputStream; 45 | import java.util.ArrayList; 46 | import java.util.List; 47 | 48 | /** 49 | * Created by Chilling on 2016/12/10. 50 | */ 51 | 52 | public class TestVideoEncoder { 53 | 54 | private H264Encoder h264Encoder; 55 | private byte[] writeBuffer = new byte[1024 * 64]; 56 | private Context ctx; 57 | private EglContextWrapper eglCtx; 58 | public static int drawCnt; 59 | private OutputStream os; 60 | private List glTextureList = new ArrayList<>(); 61 | 62 | public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) { 63 | this.ctx = ctx; 64 | this.eglCtx = eglCtx; 65 | 66 | try { 67 | os = new FileOutputStream(ctx.getExternalFilesDir(null) + File.separator + "test_h264_encode.h264"); 68 | } catch (FileNotFoundException e) { 69 | e.printStackTrace(); 70 | } 71 | } 72 | 73 | public void prepareEncoder(H264Encoder.OnDrawListener onDrawListener) { 74 | try { 75 | h264Encoder = new H264Encoder(new StreamPublisher.StreamPublisherParam.Builder().createStreamPublisherParam(), eglCtx); 76 | for (GLTexture texture : glTextureList) { 77 | h264Encoder.addSharedTexture(texture); 78 | } 79 | h264Encoder.setOnDrawListener(onDrawListener); 80 | } catch (IOException | IllegalStateException e) { 81 | e.printStackTrace(); 82 | } 83 | } 84 | 85 | public void addSharedTexture(RawTexture outsideTexture, SurfaceTexture outsideSurfaceTexture) { 86 | glTextureList.add(new GLTexture(outsideTexture, outsideSurfaceTexture)); 87 | } 88 | 89 | public static void drawRect(ICanvasGL canvasGL, int drawCnt) { 90 | GLPaint glPaint = new GLPaint(); 91 | glPaint.setColor(Color.BLUE); 92 | canvasGL.drawRect(new Rect(10 * drawCnt - 20, 50, 10 * drawCnt, 100), glPaint); 93 | } 94 | 95 | 96 | public void start() { 97 | Log.d("TestVideoEncoder", "start: "); 98 | h264Encoder.start(); 99 | } 100 | 101 | public void stop() { 102 | if (h264Encoder == null) { 103 | return; 104 | } 105 | h264Encoder.close(); 106 | } 107 | 108 | public boolean isStart() { 109 | return h264Encoder != null && h264Encoder.isStart(); 110 | } 111 | 112 | public void destroy() { 113 | try { 114 | os.close(); 115 | } catch (IOException e) { 116 | e.printStackTrace(); 117 | } 118 | if (h264Encoder == null) { 119 | return; 120 | } 121 | h264Encoder.close(); 122 | } 123 | 124 | public boolean encodeAFrame() { 125 | if (isStart()) { 126 | Loggers.d("TestVideoEncoder", "writeAFrame"); 127 | h264Encoder.requestRenderAndWait(); 128 | return true; 129 | } 130 | return false; 131 | } 132 | 133 | public void write() { 134 | MediaCodecInputStream mediaCodecInputStream = h264Encoder.getMediaCodecInputStream(); 135 | MediaCodecInputStream.readAll(mediaCodecInputStream, writeBuffer, new MediaCodecInputStream.OnReadAllCallback() { 136 | @Override 137 | public void onReadOnce(byte[] buffer, int readSize, MediaCodec.BufferInfo bufferInfo) { 138 | Loggers.d("TestVideoEncoder", String.format("onReadOnce: readSize:%d", readSize)); 139 | if (readSize <= 0) { 140 | return; 141 | } 142 | try { 143 | os.write(buffer, 0, readSize); 144 | os.flush(); 145 | } catch (IOException e) { 146 | e.printStackTrace(); 147 | } 148 | } 149 | }); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoderActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * Copyright (C) 2017 ChillingVan 5 | * * * 6 | * * * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * * * you may not use this file except in compliance with the License. 8 | * * * You may obtain a copy of the License at 9 | * * * 10 | * * * http://www.apache.org/licenses/LICENSE-2.0 11 | * * * 12 | * * * Unless required by applicable law or agreed to in writing, software 13 | * * * distributed under the License is distributed on an "AS IS" BASIS, 14 | * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * * * See the License for the specific language governing permissions and 16 | * * * limitations under the License. 17 | * * 18 | * 19 | */ 20 | 21 | package com.chillingvan.instantvideo.sample.test.video; 22 | 23 | import android.graphics.Bitmap; 24 | import android.graphics.BitmapFactory; 25 | import android.graphics.SurfaceTexture; 26 | import android.os.Bundle; 27 | import android.os.Handler; 28 | import android.os.HandlerThread; 29 | import android.os.Message; 30 | import androidx.appcompat.app.AppCompatActivity; 31 | import android.util.Log; 32 | import android.view.View; 33 | 34 | import com.chillingvan.canvasgl.ICanvasGL; 35 | import com.chillingvan.canvasgl.glcanvas.RawTexture; 36 | import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView; 37 | import com.chillingvan.canvasgl.glview.texture.GLTexture; 38 | import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper; 39 | import com.chillingvan.canvasgl.glview.texture.gles.GLThread; 40 | import com.chillingvan.instantvideo.sample.R; 41 | import com.chillingvan.lib.encoder.video.H264Encoder; 42 | 43 | import java.util.List; 44 | 45 | 46 | public class TestVideoEncoderActivity extends AppCompatActivity { 47 | 48 | private TestVideoEncoder testVideoEncoder; 49 | private ProduceTextureView produceTextureView; 50 | private Handler handler; 51 | private HandlerThread inputHandlerThread; 52 | private HandlerThread outputVideoThread; 53 | 54 | @Override 55 | protected void onCreate(Bundle savedInstanceState) { 56 | super.onCreate(savedInstanceState); 57 | setContentView(R.layout.activity_test_video_encoder); 58 | produceTextureView = (ProduceTextureView) findViewById(R.id.produce_view); 59 | 60 | 61 | produceTextureView.setOnCreateGLContextListener(new GLThread.OnCreateGLContextListener() { 62 | @Override 63 | public void onCreate(EglContextWrapper eglContext) { 64 | testVideoEncoder = new TestVideoEncoder(getApplicationContext(), eglContext); 65 | } 66 | }); 67 | produceTextureView.setOnSurfaceTextureSet(new GLSurfaceTextureProducerView.OnSurfaceTextureSet() { 68 | @Override 69 | public void onSet(SurfaceTexture surfaceTexture, RawTexture rawTexture) { 70 | testVideoEncoder.addSharedTexture(rawTexture, surfaceTexture); 71 | surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() { 72 | @Override 73 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { 74 | } 75 | }); 76 | } 77 | }); 78 | 79 | 80 | outputVideoThread = new HandlerThread("outputVideoThread"); 81 | outputVideoThread.start(); 82 | 83 | final Handler outputVideoHandler = new Handler(outputVideoThread.getLooper()) { 84 | @Override 85 | public void handleMessage(Message msg) { 86 | super.handleMessage(msg); 87 | testVideoEncoder.write(); 88 | } 89 | }; 90 | 91 | inputHandlerThread = new HandlerThread("encoder"); 92 | inputHandlerThread.start(); 93 | 94 | handler = new Handler(inputHandlerThread.getLooper()) { 95 | @Override 96 | public void handleMessage(Message msg) { 97 | super.handleMessage(msg); 98 | if (msg.what == 1) { 99 | final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna); 100 | testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() { 101 | @Override 102 | public void onGLDraw(ICanvasGL canvasGL, List producedTextures, List consumedTextures) { 103 | 104 | TestVideoEncoder.drawCnt++; 105 | 106 | if (TestVideoEncoder.drawCnt == 19 || TestVideoEncoder.drawCnt == 39) { 107 | canvasGL.drawBitmap(bitmap, 0, 0); 108 | } 109 | TestVideoEncoder.drawRect(canvasGL, TestVideoEncoder.drawCnt); 110 | 111 | if (TestVideoEncoder.drawCnt >= 60) { 112 | TestVideoEncoder.drawCnt = 0; 113 | } 114 | Log.i("TestVideoEncoder", "gl draw"); 115 | } 116 | 117 | }); 118 | testVideoEncoder.start(); 119 | for (int i = 0; i < 120; i++) { 120 | produceTextureView.requestRenderAndWait(); 121 | testVideoEncoder.encodeAFrame(); 122 | outputVideoHandler.sendEmptyMessage(0); 123 | try { 124 | Thread.sleep(16); 125 | } catch (InterruptedException e) { 126 | e.printStackTrace(); 127 | } 128 | } 129 | } 130 | } 131 | }; 132 | 133 | } 134 | 135 | 136 | @Override 137 | protected void onResume() { 138 | super.onResume(); 139 | } 140 | 141 | @Override 142 | protected void onPause() { 143 | super.onPause(); 144 | testVideoEncoder.stop(); 145 | inputHandlerThread.quit(); 146 | outputVideoThread.quit(); 147 | } 148 | 149 | @Override 150 | protected void onDestroy() { 151 | super.onDestroy(); 152 | testVideoEncoder.destroy(); 153 | } 154 | 155 | public void clickStartTest(View view) { 156 | handler.sendEmptyMessage(1); 157 | 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /app/src/main/java/com/chillingvan/instantvideo/sample/util/ScreenUtil.java: -------------------------------------------------------------------------------- 1 | package com.chillingvan.instantvideo.sample.util; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by Chilling on 2018/8/25. 7 | */ 8 | public class ScreenUtil { 9 | 10 | public static float dpToPx(Context context, float dp) { 11 | if (context == null) { 12 | return -1; 13 | } 14 | return dp * context.getResources().getDisplayMetrics().density; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-nodpi/baboon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChillingVan/AndroidInstantVideo/7088bcecadc8e1d72d37ee02f74fdc00a0f11ba1/app/src/main/res/drawable-nodpi/baboon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-nodpi/lenna.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChillingVan/AndroidInstantVideo/7088bcecadc8e1d72d37ee02f74fdc00a0f11ba1/app/src/main/res/drawable-nodpi/lenna.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_camera_and_video.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 |