├── .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