
    %j                      (   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ  G d d	ej.                        Z G d
 de      Z G d de      Z G d d      Z G d de      Zy)z3Created on Sun Apr 07 2024 19:29:55 by codeskyblue
    N)retry)AdbError)adb_path)AdbConnectionNetwork)AbstractDevicec                       e Zd Zej                  defd       Zej                  defd       Zej                  defd       Z	ej                  d        Z
defdZd Zy	)
AbstractScreenrecordreturnc                      y)zreturn whether recordingN selfs    Y/var/www/oz-panel.ru/ozpay-pub/venv/lib/python3.12/site-packages/adbutils/screenrecord.pyis_recordingz!AbstractScreenrecord.is_recording           c                      y)zcheck if environment if validNr   r   s    r   	check_envzAbstractScreenrecord.check_env!   r   r   filenamec                      y Nr   r   r   s     r   _startzAbstractScreenrecord._start%       r   c                      y r   r   r   s    r   _stopzAbstractScreenrecord._stop)   r   r   c                 ^    | j                         rt        d       y | j                  |       y )Nzrecording already running)r   printr   r   s     r   start_recordingz$AbstractScreenrecord.start_recording-   s&    -.Hr   c                 \    | j                         st        d       y | j                          y )Nzrecording alreay stopped)r   r   r   r   s    r   stop_recordingz#AbstractScreenrecord.stop_recording3   s#      ",-

r   N)__name__
__module____qualname__abcabstractmethodboolr   r   strr   r   r    r"   r   r   r   r
   r
      s    'd ' ' 	,4 , , 	s   	  r   r
   c                   <    e Zd Zd ZdefdZd ZdefdZde	fdZ
y)	ScreenrecordExtensionc                     d | _         y r   )_record_clientr   s    r   __init__zScreenrecordExtension.__init__;   s
    "r   r   c                 B    | j                         j                  |       y)z\start video recording

        Raises:
            AdbError (when no record client)
        N)-_ScreenrecordExtension__get_screenrecord_implr    r   s     r   r    z%ScreenrecordExtension.start_recording>   s     	$$&66x@r   c                 >    | j                         j                         S )zstop video recording)r0   r"   r   s    r   r"   z$ScreenrecordExtension.stop_recordingF   s    ++-<<>>r   r   c                 >    | j                         j                         S )zis recording)r0   r   r   s    r   r   z"ScreenrecordExtension.is_recordingJ   s    ++-::<<r   c                     | j                   r| j                   S t        |       }|j                         r	|| _         |S t        |       }|j                         r	|| _         |S t	        d      )Nzno valid screenrecord client)r-   _ScrcpyScreenRecordr   _AdbScreenRecordr   )r   r1r2s      r   __get_screenrecord_implz-ScreenrecordExtension.__get_screenrecord_implN   sc    &&& &<<>"$DId#<<>"$DI566r   N)r#   r$   r%   r.   r)   r    r"   r(   r   r
   r0   r   r   r   r+   r+   :   s3    #A A?=d =7)= 7r   r+   c                   B    e Zd ZdefdZdefdZdefdZdefdZ	d Z
y	)
r4   dc                     || _         t        j                  dk(  rdnd}t        j                  |      | _        d | _        y )Nposixscrcpyz
scrcpy.exe)_dosnameshutilwhich_scrcpy_path_p)r   r:   bin_names      r   r.   z_ScrcpyScreenRecord.__init__^   s3    !ww'18|"LL2$(r   r   c                 h    t        | j                  xr | j                  j                         d u       S r   )r(   rD   pollr   s    r   r   z _ScrcpyScreenRecord.is_recordingd   s%    DGG6$ 677r   c                     | j                   d uS r   )rC   r   s    r   r   z_ScrcpyScreenRecord.check_envg   s      ,,r   r   c                    t         j                  j                         }t               |d<   | j                  j
                  |d<   t        j                  | j                  dddd|gt        j                  t        j                  |      | _
        t        j                  | j                  | j                  j                        | _        y )NADBANDROID_SERIALz--no-controlz--no-windowz--no-playbackz--record)stdinstdoutenv)r?   environcopyr   r>   serial
subprocessPopenrC   DEVNULLrD   weakreffinalizekill
_finalizer)r   r   rN   s      r   r   z_ScrcpyScreenRecord._startj   s    jjooZE
 $""PZ\de$$%%	
 "**477DGGLLAr   c                    | j                   j                          | j                  j                  t        j
                         	 | j                  j                  d      }|dk(  ry |dk(  rt        d      |dk(  rt        d      t        d|      # t        j                  $ r& | j                  j                          t        d	      w xY w)
N   timeoutr      zscrcpy error: start failure   z/scrcpy error: device disconnected while runningzscrcpy errorz!scrcpy not handled SIGINT, killed)rX   detachrD   send_signalsignalSIGINTwaitr   rR   TimeoutExpiredrW   )r   
returncodes     r   r   z_ScrcpyScreenRecord._stopv   s     FMM*	@a0JQq<==qPQQ~z::(( 	@GGLLN>??	@s   !B ',B 9CN)r#   r$   r%   r   r.   r(   r   r   r)   r   r   r   r   r   r4   r4   \   s:    ). )8d 8-4 -
Bs 
B@r   r4   c                       e Zd ZdZddedefdZdefdZdefd	Z	 e
ed
dd      dej                  fd       Zdej                  fdZdej                  dej                   fdZdefdZy)_ScrcpyJarScreenrecordu  
    # -y  overwrite output files
    ffmpeg -i "output.h264" -c:v copy -f mp4 -y "video.mp4"

    遗留问题：秒表视频，转化后的视频时长不对啊（本来5s的，转化后变成了20s）

    https://stackoverflow.com/questions/21263064/how-to-wrap-h264-into-a-mp4-container

    协议没有完全理解，Frame的pts也没有。还是需要多看看scrcpy的代码才行。
    Nr:   h264_filenamec                 `    || _         || _        d | _        t        j                         | _        y r   )r>   	_filename_conn	threadingEvent_done_event)r   r:   rh   s      r   r.   z_ScrcpyJarScreenrecord.__init__   s&    &$(
$??,r   r   c                 L    | j                   xr | j                   j                   S r   )rk   closedr   s    r   r   z#_ScrcpyJarScreenrecord.is_recording   s    zz3$**"3"333r   r   c                 l   || _         t        j                  t              j	                         j
                  }d}|j                  d      }|j                         sJ | j                  j                  j                  ||       g d}d|z   ddddd	g|z   }| j                  j                  |d
      }|}~|j                  j                  d      j                  d      }	t        d|	       || _        t#        j$                  d| j&                  |j                  fd
      j)                          t+        j,                  d       | j/                         }
t        j                  | j                         j1                  d      }t#        j$                  d| j2                  |
|fd
      j)                          y )Nz!/data/local/tmp/scrcpy-server.jarzbinaries/scrcpy-server-1.24.jar)zcontrol=falsezbit_rate=8000000ztunnel_forward=truezlock_video_orientation=-1zsend_dummy_byte=falsezsend_device_meta=falsezsend_frame_meta=truezdownsize_on_error=truez
CLASSPATH=app_process/z--nice-name=scrcpy-serverzcom.genymobile.scrcpy.Serverz1.24Tstreamd   utf-8zScrcpy:scrcpy_main)r@   targetargsdaemon皙?wbsocket_copy)rj   pathlibPath__file__absoluteparentjoinpathexistsr>   syncpushshellconnrecvdecoder   rk   rl   Thread
_copy2nullstarttimesleep_safe_dial_scrcpyopen
_copy2file)r   r   curdirdevice_jar_pathscrcpy_server_jar_pathoptscmd_ccmessagestream_sockfhs               r   r   z_ScrcpyJarScreenrecord._start   sq   !h'00299= "(1R!S%,,...0/B	
 ?*'*
  WW]]3t],&&++c"))'2i!
taffYt	

%'

3,,.\\$..)..t4??r"		

 %'r   
   r|   g{Gz?)triesdelayjitterc                 V    | j                   j                  t        j                  d      S )Nr=   )r>   create_connectionr   LOCAL_ABSTRACTr   s    r   r   z(_ScrcpyJarScreenrecord._safe_dial_scrcpy   s    ww(()?)?JJr   sc                     	 	 |j                  d      }|dk(  rt        d|j                  d             n	 5t        d       y #  Y xY w)Ni   r   zO:rw   zScrcpy mainThread stopped)r   r   r   )r   r   chunks      r   r   z!_ScrcpyJarScreenrecord._copy2null   sS    tC<$W 56    	)*s   1A Ar   c                     	 |j                  d      }|sn|j                  |       &|j                          t        dd       | j                  j                          y )NTi   zCopy h264 stream finished)flush)r   writecloser   rn   set)r   r   r   r   s       r   r   z!_ScrcpyJarScreenrecord._copy2file   sP    FF7OEHHUO	 
 	
)6r   c                     | j                   j                          | j                  j                  d       t	        j
                  d       | j                  j                          y )Ng      @r[   r]   )rk   r   rn   rc   r   r   clearr   s    r   r   z_ScrcpyJarScreenrecord._stop   sF    

c*

1 r   r   )r#   r$   r%   __doc__r   r)   r.   r(   r   r   r   r   socketr   r   typingBinaryIOr   r   r   r   r   rg   rg      s    	-. - -4d 4.s .` 82S6K6== K 7K	+FMM 	+FMM v !t !r   rg   c                   D    e Zd Zd
defdZdefdZdefdZdefdZ	d	 Z
y)r5   Nr:   c                 x    || _         |s#dt        t        j                         dz        z  }|| _        d| _        y)z#The maxium record time is 3 minutesz!/sdcard/adbutils-tmp-video-%d.mp4i  N)r>   intr   _remote_path_stream)r   r:   remote_path	autostarts       r   r.   z_AdbScreenRecord.__init__   s6    =DIIKRVDV@WWK'r   r   c                 Z    | j                   j                  ddg      }|j                  dk(  S )NrB   screenrecordr   )r>   shell2re   )r   rets     r   r   z_AdbScreenRecord.check_env   s(    ggnng~67~~""r   c                 ^    t        | j                  xr | j                  j                         S r   )r(   r   rp   r   s    r   r   z_AdbScreenRecord.is_recording   s#    DLL<)<)<%<==r   r   c                    || _         t        j                  d      j                  d      }| j                  j
                  j                  |d       | j                  j                  dd| j                  gd      | _	        y )Nz        #!/system/bin/sh
        # generate by adbutils
        screenrecord "$1" &
        PID=$!
        read ANY
        kill -INT $PID
        wait
        rw   z /sdcard/adbutils-screenrecord.shshTrt   )
rj   textwrapdedentencoder>   r   r   r   r   r   )r   r   script_contents      r   r   z_AdbScreenRecord._start   sr    !!

 &/ 	 	.*LM&*ggmm5t7H7HIRV '4 '
r   c                 `   | j                   j                  d       | j                   j                          | j                   j                          | j                  j
                  j                  | j                  | j                         | j                  j                  | j                         y )N   
)
r   sendread_until_closer   r>   r   pullr   rj   remover   s    r   r   z_AdbScreenRecord._stop  sl    % %%'$++T^^<t(()r   )NF)r#   r$   r%   r   r.   r(   r   r   r)   r   r   r   r   r   r5   r5      s7    . #4 #>d >
s 
$*r   r5   )r   r&   r?   r   rA   ra   r   rR   r   rl   r   r   rU   r   adbutils.errorsr   adbutils._utilsr   adbutils._adbr   r   adbutils._interfacesr   ABCr
   r+   r4   rg   r5   r   r   r   <module>r      s     	            $ $ 0 /377 <7N 7D)@. )@Xb! b!J)*+ )*r   