|
| 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.support.annotation.Nullable; |
| 30 | +import android.support.v7.app.AppCompatActivity; |
| 31 | +import android.view.View; |
| 32 | +import android.widget.TextView; |
| 33 | + |
| 34 | +import com.chillingvan.canvasgl.ICanvasGL; |
| 35 | +import com.chillingvan.canvasgl.Loggers; |
| 36 | +import com.chillingvan.canvasgl.glcanvas.BasicTexture; |
| 37 | +import com.chillingvan.canvasgl.glcanvas.RawTexture; |
| 38 | +import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter; |
| 39 | +import com.chillingvan.canvasgl.textureFilter.HueFilter; |
| 40 | +import com.chillingvan.canvasgl.textureFilter.TextureFilter; |
| 41 | +import com.chillingvan.instantvideo.sample.R; |
| 42 | +import com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView; |
| 43 | +import com.chillingvan.lib.camera.InstantVideoCamera; |
| 44 | +import com.chillingvan.lib.encoder.video.H264Encoder; |
| 45 | +import com.chillingvan.lib.muxer.MP4Muxer; |
| 46 | +import com.chillingvan.lib.publisher.CameraStreamPublisher; |
| 47 | +import com.chillingvan.lib.publisher.StreamPublisher; |
| 48 | + |
| 49 | +import java.io.IOException; |
| 50 | + |
| 51 | +public class TestMp4MuxerActivity extends AppCompatActivity { |
| 52 | + |
| 53 | + private CameraStreamPublisher streamPublisher; |
| 54 | + private CameraPreviewTextureView cameraPreviewTextureView; |
| 55 | + private InstantVideoCamera instantVideoCamera; |
| 56 | + private Handler handler; |
| 57 | + private HandlerThread handlerThread; |
| 58 | + private TextView outDirTxt; |
| 59 | + private String outputDir; |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void onCreate(Bundle savedInstanceState) { |
| 63 | + super.onCreate(savedInstanceState); |
| 64 | + outputDir = getExternalFilesDir(null) + "/test_mp4_encode.mp4"; |
| 65 | + setContentView(R.layout.activity_test_mp4_muxer); |
| 66 | + cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view); |
| 67 | + cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() { |
| 68 | + @Override |
| 69 | + public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { |
| 70 | + drawVideoFrame(canvasGL, surfaceTexture, rawTexture); |
| 71 | + } |
| 72 | + }); |
| 73 | + outDirTxt = (TextView) findViewById(R.id.output_dir_txt); |
| 74 | + outDirTxt.setText(outputDir); |
| 75 | + |
| 76 | + |
| 77 | + instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 640, 480); |
| 78 | +// instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720); |
| 79 | + |
| 80 | + handlerThread = new HandlerThread("StreamPublisherOpen"); |
| 81 | + handlerThread.start(); |
| 82 | + handler = new Handler(handlerThread.getLooper()) { |
| 83 | + @Override |
| 84 | + public void handleMessage(Message msg) { |
| 85 | + super.handleMessage(msg); |
| 86 | +// StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(); |
| 87 | +// StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(1080, 640, 9500 * 1000, 30, 1, 44100, 19200); |
| 88 | + StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(540, 750, 1500 * 1000, 30, 1, 44100, 19200); |
| 89 | + streamPublisherParam.outputFilePath = outputDir; |
| 90 | +// streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_mp4_encode.mp4"; |
| 91 | + streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() { |
| 92 | + @Override |
| 93 | + public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { |
| 94 | + drawVideoFrame(canvasGL, outsideSurfaceTexture, outsideTexture); |
| 95 | + |
| 96 | + Loggers.i("DEBUG", "gl draw"); |
| 97 | + } |
| 98 | + }); |
| 99 | + try { |
| 100 | + streamPublisher.startPublish(); |
| 101 | + } catch (IOException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + ((TextView)findViewById(R.id.test_camera_button)).setText("START"); |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + streamPublisher = new CameraStreamPublisher(new MP4Muxer(), cameraPreviewTextureView, instantVideoCamera); |
| 109 | + } |
| 110 | + |
| 111 | + private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) { |
| 112 | + // Here you can do video process |
| 113 | + // 此处可以视频处理,例如加水印等等 |
| 114 | + TextureFilter textureFilterLT = new BasicTextureFilter(); |
| 115 | + TextureFilter textureFilterRT = new HueFilter(180); |
| 116 | + int width = outsideTexture.getWidth(); |
| 117 | + int height = outsideTexture.getHeight(); |
| 118 | + canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, width /2, height /2, textureFilterLT); |
| 119 | + canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, height/2, width/2, height, textureFilterRT); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected void onResume() { |
| 125 | + super.onResume(); |
| 126 | + streamPublisher.resumeCamera(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected void onPause() { |
| 131 | + super.onPause(); |
| 132 | + streamPublisher.pauseCamera(); |
| 133 | + if (streamPublisher.isStart()) { |
| 134 | + streamPublisher.closeAll(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected void onDestroy() { |
| 140 | + super.onDestroy(); |
| 141 | + handlerThread.quitSafely(); |
| 142 | + } |
| 143 | + |
| 144 | + public void clickStartTest(View view) { |
| 145 | + TextView textView = (TextView) view; |
| 146 | + if (streamPublisher.isStart()) { |
| 147 | + streamPublisher.closeAll(); |
| 148 | + textView.setText("START"); |
| 149 | + } else { |
| 150 | + streamPublisher.resumeCamera(); |
| 151 | + handler.sendEmptyMessage(1); |
| 152 | + textView.setText("STOP"); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments