Open connglli opened 3 years ago
very nice guideline. thank bro
my comment was making fun of the scammers :(
@JohnodonCode Sorry for deleting your comment bro. I could get you. I deleted it as it is related to the scam. I should have let you know; sorry, bro.
Nah I get it dont worry about it
It's ok man anything that a scam gotta go ✊✊
On Tue., 20 Feb. 2024, 1:25 pm John Bostick, @.***> wrote:
Nah I get it dont worry about it
— Reply to this email directly, view it on GitHub https://github.com/connglli/blog-notes/issues/115#issuecomment-1953385411, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWNWCUWAEYOIHOGCHN2Y4TLYUQCR3AVCNFSM4WCJ5OA2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJVGMZTQNJUGEYQ . You are receiving this because you were mentioned.Message ID: @.***>
Overview
scrcpy is used and extended by many manufactures, such as XIAOMI, which they call "多屏协同". This blog then introduces the basic scrcpy tool.
The approach for scrcpy is that, AS plays a role of server, and DS plays a role of client. In such C/S arch,
Therefore, AS opens three threads
DS opens four threads
Implementation
Estanblish Connections
AS/DS plays a server/client role conceptually, which may be not in implementation.
At before, scrcoy lets DS opens a server socket, adb reverses that socket to AS, and AS connects to that socket. That said, in implementation, AS plays a role of client, whereas DS a server. However, due to a bug of
adb reverse
, scrcpy enables a forward mode.Option 1: Forward Mode
In
adb forward
mode, the AS opens a server socket, whereas the DS uses a client socket to connectOption 2: Reverse Mode
In
adb reverse
mode, the AS opens a client socket, whereas the DS uses a server socket to connectAndroid side:
Desktop side:
Note that, after above, AS and DS are connected through a video socket and a control socket. Once connected, AS sends the device info (i.e., device name/width/height) to DS through video socket
Threading
After the connection is established, AS:
ScreenEncoder
for capturing and encoding video frames,On the other side, DS:
Video Streaming
Prepare main loop????
To stream video, scrcpy record current screen, and stream to the DS. To record, scrcpy first creates a logic (virtual) display with a custom surface. Then with help of Android framework, the display('s surface) would be rendered with content of current screen.
After obtaining the screen record (mirror), scrcpy feeds that to a codec for encoding, and stream it to DS through video socket.
Screen Record (Mirror)
Option1: MediaProjection
Typically, a virtual display can be created with help of
MediaProjection#createVirtualDisplay()
, which captures a screen projection to a givenSurface
.To see details of the above workflow, see ScreenRecorder or [MediaDemo]().
Option1: SurfaceControl
Alternatively, a virtual display can also be created via
SurfaceControl#createDisplay()
, and configured lately by methods ofSurfaceControl
.However, above
SurfaceControl
APIs are hidden by Android framework, and should be accessed by reflection. Furthermore, thelayerStack
is a field ofDisplayInfo
, however, this field is not only hidden by also sealed by the Android framework. Hence, to useDisplayInfo.layerStack
in a common app (not a system-level app like scrcpy), one need to unseal the reflection using library like tiann/FreeReflection. Additionally, thecreateDisplay()
method needsACCESS_SURFACE_FLINGER
permission that can only be granted to a system app.To see details of the above workflow, see scrcpy/ScreenRecorder.
Encoding
For streaming, we need to encode the screen mirror. Considering the
surface
used by above display can be filled bydisplay
, we can directly encode the surface.Contribute to the Andorid multimedia framework,
surface
can be directed created as the input buffer of a codec byMediaCodec#createInputSurface()
.Above code encodes the
Decoding
see stream.{h,c}, decoder.{h,c}, recorder.{h,c}
Control Thread and Message Thread
We do not introduce the implementation if these threads because they are much more easier to implement. See
Controller.java
,DeviceMessageSender.java
for AS, andcontroller.{c,h}
,receiver.{c,h}
for DS.