2

I'm trying to stream android screen using python + aiortc. I have a POC to obtain the device screen using adb + screenrecord. The provided code reads the raw output (h264) from the execution of screenrecord and shows it using ffmpeg and opencv.

import subprocess as sp
import cv2
import array
import numpy as np

adbCmd = ['adb', 'exec-out', 'screenrecord', '--output-format=h264', '-']
stream = sp.Popen(adbCmd, stdout= sp.PIPE, universal_newlines = True)

ffmpegCmd = ['ffmpeg', '-i', '-', '-f', 'rawvideo', '-vcodec', 'bmp', '-vf', 'fps=5', '-']
ffmpeg = sp.Popen(ffmpegCmd, stdin=stream.stdout, stdout=sp.PIPE)

while True:
    fileSizeBytes = ffmpeg.stdout.read(6)
    fileSize = 0
    for i in range(4):
        fileSize += fileSizeBytes[i + 2] * 256 ** i
    bmpData = fileSizeBytes + ffmpeg.stdout.read(fileSize - 6)
    image = cv2.imdecode(np.fromstring(bmpData, dtype=np.uint8), 1)
    cv2.imshow("im", image)
    cv2.waitKey(25)

Currently, I'm trying to plug the output from ffmpeg or adb exec-out to an aiortc media streamer. Based on this example I replaced the recv method with the fallowing code

async def recv(self):
    fileSizeBytes = ffmpeg.stdout.read(6)
    fileSize = 0
    for i in range(4):
        fileSize += fileSizeBytes[i + 2] * 256 ** i
    bmpData = fileSizeBytes + ffmpeg.stdout.read(fileSize - 6)
    image = cv2.imdecode(numpy.fromstring(bmpData, dtype=numpy.uint8), 1)

    frame = VideoFrame.from_ndarray(
        image, format="bgr24"
    )

    pts, time_base = await self.next_timestamp()
    frame.pts = pts
    frame.time_base = time_base
    self.counter += 1
    return frame

But this code is not able to stream the correct video from device screen and no error happen. I'm looking for solutions for this problem. I was thinking to use the direct output from adbCmd but it also didn't worked.

bottega
  • 123
  • 1
  • 13
  • I would really like to know this answer too! – user199858 Dec 09 '19 at 14:47
  • Hy i am working on a similar thing. (i want to feed the screen from my phone to tensorflow model) the ADB Command outputs data. But ffmpeg doesn't give any thing except empty byte strings? – maxbit89 Oct 26 '20 at 16:39

0 Answers0