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投射的視頻了。如圖所示:

MediaProjection MediaProjection Surface Surface MediaCodec MediaCodec EasyPusher EasyPusher 投射屏幕 提供視頻源 編碼 編碼數據通過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里的手機實時屏幕圖像

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

獲取更多信息

郵件:support@easydarwin.org

WEB:www.EasyDarwin.org

Copyright © EasyDarwin.org 2012-2016

EasyDarwin



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM