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

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


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

具體來講,MediaProjection 類能夠將當前屏幕畫面採集到一個surface裏面,而MediaCodec能夠從一個surface裏面獲取視頻數據源。咱們讓MediaProjection投射到MediaCodec建立的Surface,MediaCodec就能夠獲取到MediaProjection投射的視頻了。如圖所示:app

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個參數傳給前者,前者就能夠獲取到屏幕數據了~oop

以下圖所示,在VLC訪問RTSP地址,便可看到屏幕直播。測試

VLC裏的手機實時屏幕圖像

EasyPusher項目見:https://github.com/EasyDarwin/EasyPusherui

獲取更多信息

郵件:support@easydarwin.org編碼

WEB:www.EasyDarwin.orgspa

Copyright © EasyDarwin.org 2012-2016.net

EasyDarwin

相關文章
相關標籤/搜索