Stream android screen

后端 未结 1 2150
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 09:40

I am trying to fallow Truiton ScreenCapture example, in order to record the device screen using MediaProjection

When saving the recording localy it works



        
相关标签:
1条回答
  • 2021-02-19 10:12

    You have to use LocalServerSocket, below is what partially worked for me, I have a MediaStreamer class which extends MediaRecorder.

    public class MediaStreamer extends MediaRecorder {
    
        private LocalServerSocket localServerSocket = null;
        private LocalSocket receiver, sender = null;
    
        public void prepare() throws IllegalStateException,IOException {
    
            receiver = new LocalSocket();
            try {
                localServerSocket = new LocalServerSocket("<your_socket_addr>");
                receiver.connect(new LocalSocketAddress("<your_socket_addr>"));
                receiver.setReceiveBufferSize(4096);
                receiver.setSendBufferSize(4096);
                sender = localServerSocket.accept();
                sender.setReceiveBufferSize(4096);
                sender.setSendBufferSize(4096); 
            } catch (IOException e1) {
                throw new IOException("Can't create local socket !");
            }
    
            setOutputFile(sender.getFileDescriptor());
    
            try {
                super.prepare();
            } catch (Exception e) {
                closeSockets();
                throw e;
            }           
        }
    
        public InputStream getInputStream() {
    
            InputStream out = null;
    
            try {
                out = receiver.getInputStream();
            } catch (IOException e) {
            }
    
            return out;
    
        }
    
    
        public void stop() {
            closeSockets();
            super.stop();
        }
    
        private void closeSockets() {
            if (localServerSocket !=null) {
                try {
                    localServerSocket.close();
                    sender.close();
                    receiver.close();
                }
                catch (IOException e) {
    
                }
                localServerSocket = null; 
                sender = null; 
                receiver = null;
            }
        }
    }
    

    For Recording

    video = new MediaStreamer();
    video.reset();
    
    video.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    video.setPreviewDisplay(holder.getSurface());
    video.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    video.setVideoFrameRate(VideoConstants.frameRate);
    video.setVideoEncodingBitRate(VideoConstants.bitRate*1000);
    video.setVideoSize(VideoConstants.resolationX, VideoConstants.resolationY);
    video.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    
    try {
       video.prepare();
    } catch (IOException e) {
       e.printStackTrace();
    }
    video.start();
    


    But the main problem is mp4 is not very easy to stream . The basic problem is that MP4 is not live, streamable format, so even though data is captured via the socket, the crucial file headers which are normally written at the conclusion of an audio or video capture, are missing (because sockets are not seekeable like local files) - hence the unplayable data (And so, why it works fine when saved as local files, is understandable).

    There is no easy way to perform post-processing on the data to manually add the file headers. So the solution is either don't use MP4 as the recording format, or, write a packetiser similar to what is used in the Spydroid project

    Hope this helps !

    0 讨论(0)
提交回复
热议问题