1. 程式人生 > >EasyPusher實現Android手機螢幕桌面直播,實時推送操作畫面,用於手遊直播等應用

EasyPusher實現Android手機螢幕桌面直播,實時推送操作畫面,用於手遊直播等應用

本文轉自EasyDarwin開源團隊成員John的部落格:http://blog.csdn.net/jyt0551/article/details/52651194

由於Android 5.0提供了捕獲當前螢幕畫面的SDK,基於該SDK,EasyPusher實現了實時推送手機螢幕畫面的功能。經測試,效果很不錯,延遲也比較低,畫面也比較流暢。該功能可運用到小型會議PPT演示、手遊直播等行業。

具體來說,MediaProjection 類可以將當前螢幕畫面採集到一個surface裡面,而MediaCodec可以從一個surface裡面獲取視訊資料來源。我們讓MediaProjection投射到MediaCodec建立的Surface,MediaCodec就可以獲取到MediaProjection投射的視訊了。如圖所示:

MediaProjectionMediaProjectionSurfaceSurfaceMediaCodecMediaCodecEasyPusherEasyPusher投射螢幕提供視訊源編碼編碼資料通過Pusher推送

在這裡就不再詳細描述程式碼的實現,主要介紹下兩個介面:

VirtualDisplay createVirtualDisplay (String name, 
                int width, 
                int height, 
                int dpi, 
                int flags, 
                Surface surface, 
                VirtualDisplay.Callback callback, 
                Handler handler)
Creates a VirtualDisplay to
capture the contents of the screen. Parameters name String: The name of the virtual display, must be non-empty. 要建立的投射器的名稱,非空 width int: The width of the virtual display in pixels. Must be greater than 0. 投射後視訊的寬度,這裡的寬度就是實際上後面MediaCodec初始化的寬度. height int: The height of the virtual display in pixels. Must be greater than
0. 投射後視訊的高度,這裡的寬度就是實際上後面MediaCodec初始化的高度. dpi int: The density of the virtual display in dpi. Must be greater than 0. 投射器的畫素密度,未理解啥意思,我們直接用DisplayMetrics的densityDpi即可. flags int: A combination of virtual display flags. See DisplayManager for the full list of flags. 我們傳 DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR|DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC|DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION 即可。 surface Surface: The surface to which the content of the virtual display should be rendered, or null if there is none initially. 投射器要投射到的Surface callback VirtualDisplay.Callback: Callback to call when the virtual display's state changes, or null if none. 投射器更改後的狀態回撥,我們這裡不需要,傳null即可。 handler Handler: The Handler on which the callback should be invoked, or null if the callback should be invoked on the calling thread's main Looper. 回撥函式將在該Handler所在的執行緒呼叫,我們也不需要,傳null即可。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
createInputSurface

Surface createInputSurface ()
Requests a Surface to use as the input to an encoder, in place of input buffers. 

該介面建立一個作為編碼器輸入的Surface。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然後,將後者得到的Surface作為第6個引數傳給前者,前者就可以獲取到螢幕資料了~

如下圖所示,在VLC訪問RTSP地址,即可看到螢幕直播。

VLC裡的手機實時螢幕影象

獲取更多資訊

Copyright © EasyDarwin.org 2012-2016

EasyDarwin