-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminicap.py
More file actions
116 lines (97 loc) · 3.64 KB
/
minicap.py
File metadata and controls
116 lines (97 loc) · 3.64 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import binascii
import socket
import time
from PySide.QtCore import QThread, QByteArray, Signal
MINICAP_CMD = ['LD_LIBRARY_PATH=/data/local/tmp/minicap-devel', '/data/local/tmp/minicap-devel/minicap']
class MiniServer(QThread):
def __init__(self, device):
super(MiniServer, self).__init__()
self._device = device
self._device.cmd.adb(['forward', 'tcp:1717', 'localabstract:minicap'])
self.__rotate = 0
def run(self):
self.__rotate = self.getCurrentRotation()
if not self.isActive():
displayInfo = self._device.getCurrDisplay()
size = str(displayInfo['width']) + 'x' + str(displayInfo['height'])
param = size + '@' + size + '/0'
self._device.cmd.shell(MINICAP_CMD + ['-P', param], shell=True)
print 'kill'
def isActive(self):
p = self._device.cmd.popen(['ps'])
stdout, stderr = p.communicate()
for line in stdout.split('\r\n'):
if line.find(MINICAP_CMD[1])!=-1:
return True
return False
def pid(self):
p = self._device.cmd.popen(['ps'])
stdout, stderr = p.communicate()
for line in stdout.split('\r\n'):
if line.find(MINICAP_CMD[1]) != -1:
return line.split()[1]
def stop(self):
pid = self.pid()
if pid:
self._device.cmd.shell(['kill', pid])
def reStart(self):
self.stop()
self.wait()
self.start()
def needRestart(self):
if self.__rotate != self.getCurrentRotation():
return True
else:
return False
def getCurrentRotation(self):
return self._device.getCurrDisplay()['orientation']
class MiniReader(QThread):
PORT = 1717
HOST = 'localhost'
isStop = False
picData = ''
picEvent = Signal(QByteArray)
def __init__(self, device):
super(MiniReader, self).__init__()
self._device = device
def getDisplay(self):
picData = ''
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.HOST, self.PORT))
data = s.recv(24)
# print self.parseBanner(data)
data = s.recv(4)
size = self.parsePicSize(data)
# print 'size = ' + str(size)
while True:
if size >= 4096:
data = s.recv(4096)
picData += data
size -= len(data)
elif 0 < size < 4096:
data = s.recv(size)
picData += data
size -= len(data)
elif size <= 0:
break
except ValueError:
print 'ValueError: getDisplay'
finally:
s.close()
return picData
def parsePicSize(self, data):
return int(binascii.hexlify(data[::-1]), 16)
def parseBanner(self, data):
if len(data) == 24:
banner = dict()
banner['version'] = int(binascii.hexlify(data[0]), 16)
banner['length'] = int(binascii.hexlify(data[1]), 16)
banner['pid'] = int(binascii.hexlify(data[2:5][::-1]), 16)
banner['real.width'] = int(binascii.hexlify(data[6:9][::-1]), 16)
banner['real.height'] = int(binascii.hexlify(data[7:13][::-1]), 16)
banner['virtual.width'] = int(binascii.hexlify(data[14:17][::-1]), 16)
banner['virtual.height'] = int(binascii.hexlify(data[18:21][::-1]), 16)
banner['orient'] = int(binascii.hexlify(data[22]), 16)
banner['policy'] = int(binascii.hexlify(data[23]), 16)
return banner