PageRenderTime 56ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/GroundWork.py

https://code.google.com/p/groundwork/
Python | 279 lines | 253 code | 13 blank | 13 comment | 106 complexity | 18b6a1965dcaccc5081f43ff37be2f64 MD5 | raw file
  1. #!/usr/bin/env python
  2. import os,sys,time,timeit,os.path,threading,string
  3. from subprocess import *
  4. from datetime import datetime
  5. class restart(threading.Thread):
  6. def run(self):
  7. output = Popen("ps aux | grep capture.sh", shell=True, stdout=PIPE).stdout
  8. output = output.read()
  9. output=output.strip()
  10. id=output.split(" ")
  11. i = 0
  12. killid=id[5]
  13. print killid
  14. print os.system('kill '+str(killid))
  15. print os.system('kill '+str(int(killid)+1))
  16. print os.system('kill '+str(int(killid)+2))
  17. os.system("sh ~/Pictures/capture.sh")
  18. class Monitor(threading.Thread):
  19. def run(self):
  20. while 1:
  21. output = Popen("ls -l ~/Pictures/", shell=True, stdout=PIPE).stdout
  22. s = output.readlines()
  23. for lines in s:
  24. if '0.raw' in lines:
  25. raw=lines[-12:-7]
  26. t=str(datetime.now())
  27. e=t[11:16]
  28. if raw==e:
  29. print 'pass'
  30. pass
  31. else:
  32. restart().start()
  33. time.sleep(15)
  34. class Start(threading.Thread):
  35. def run(self):
  36. print os.getpid()
  37. os.system("sh ~/Pictures/capture.sh")
  38. class Mirror(threading.Thread):
  39. def run(self):
  40. if mode=="1" and connect=="w" and compression=="n":
  41. ## rgb convert
  42. atrix="nc -l 9000 > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr32 -s 540x960 \-i - ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  43. #xperia x10 & g1 rgb convert
  44. xperia="nc -l 9000 > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb565 -s 480x854 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  45. ##Nexus S rgb convert
  46. nexuss="nc -l 9000 > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s 480x800 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  47. if device == "Atrix" or device=='atrix':
  48. standard=atrix
  49. if device == "X10" or device=='x10':
  50. standard=xperia
  51. if device == "NexusS" or "nexuss":
  52. standard=nexuss
  53. script="while true; do "+standard+";done"
  54. q=open("/home/"+user+"/Pictures/capture.sh",'w').write(script)
  55. os.system('touch ~/Pictures/0.png')
  56. Start().start()
  57. #Monitor().start()
  58. print "Opening Chrome, Refresh page if no image appears"
  59. time.sleep(5)
  60. g=os.system('google-chrome ~/Pictures/video.html')
  61. if g <= 0:
  62. print "Opening Firefox, Refresh page if no image appears"
  63. else:
  64. os.system('firefox video.html')
  65. if mode=="1" and connect=="w" and compression=="y":
  66. ##atrix rgb convert
  67. atrix="nc -1 9000 | zcat > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr32 -s 540x960 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  68. #xperia x10 & g1 rgb convert
  69. xperia="nc -1 9000 | zcat > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb565 -s 480x854 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  70. ##Nexus S rgb convert
  71. nexuss="nc -l 9000 | zcat > ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s 480x800 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  72. if device == "Atrix":
  73. standard=atrix
  74. if device == "atrix":
  75. standard=atrix
  76. if device == "X10":
  77. standard=xperia
  78. if device == "x10":
  79. standard=xperia
  80. if device == "NexusS" or "nexuss":
  81. standard=nexuss
  82. script="while true; do "+standard+";done"
  83. q=open("/home/"+user+"/Pictures/capture.sh",'w').write(script)
  84. os.system('touch ~/Pictures/0.png')
  85. Start().start()
  86. #Monitor().start()
  87. print "Opening Chrome, Refresh page if no image appears"
  88. time.sleep(5)
  89. g=os.system('google-chrome ~/Pictures/video.html')
  90. if g <= 0:
  91. print "Opening Firefox, Refresh page if no image appears"
  92. else:
  93. os.system('firefox video.html')
  94. if mode=="2" and connect=="w" and compression=="n":
  95. if device=="atrix" or device=="Atrix":
  96. standard="w=540:h=960:format=rgb32"
  97. if device=="X10" or device=="x10":
  98. xperia="w=480:h=854:i420:format=rgb32"
  99. if device=="nexuss" or device=="Nexuss":
  100. standard="w=480:h=800:i420:format=rgb32"
  101. os.system("(while true; do nc -l 9000; done) | mplayer -demuxer rawvideo -rawvideo "+standard+" -nosound - ")
  102. if mode=="2" and connect=="w" and compression=="y":
  103. if device=="atrix" or device=="Atrix":
  104. standard="w=540:h=960:i420:format=rgb32"
  105. if device=="X10" or device=="x10":
  106. xperia="w=480:h=854:i420:format=rgb32"
  107. if device=="nexuss" or device=="Nexuss":
  108. standard="w=480:h=800:i420:format=rgb32"
  109. s='480x800'
  110. os.system("while true; do nc -l 9000; done | zcat | mplayer -demuxer rawvideo -rawvideo "+standard+" -nosound - ")
  111. if mode=='3' and connect=='w' and compression=='n':
  112. if device=="atrix" or device=="Atrix":
  113. standard="bgr32 -s 540x960"
  114. if device=="X10" or device=="x10":
  115. xperia="rgb565 -s 480x854"
  116. if device=="nexuss" or device=="Nexuss":
  117. standard="rgb32 -s 480x800"
  118. os.system("while true; do nc -l 9000;done | ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt "+standard+" -i - -f mpeg2video - | vlc -")
  119. if mode=='3' and connect=='w' and compression=='y':
  120. if device=="atrix" or device=="Atrix":
  121. standard="bgr32 -s 540x960"
  122. if device=="X10" or device=="x10":
  123. xperia="rgb565 -s 480x854"
  124. if device=="nexuss" or device=="Nexuss":
  125. standard="rgb32 -s 480x800"
  126. os.system("while true; do nc -l 9000;done | zcat | ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt "+standard+" -i - -f mpeg2video - | vlc - ")
  127. if mode=='3' and connect=='a':
  128. if device=="atrix" or device=="Atrix":
  129. standard="bgr32 -s 540x960"
  130. if device=="X10" or device=="x10":
  131. xperia="rgb565 -s 480x854"
  132. if device=="nexuss" or device=="Nexuss":
  133. standard="rgb32 -s 480x800"
  134. os.system("while true; do /home/"+user+"/android-sdk-linux_86/platform-tools/adb shell fbread ;done | ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt "+standard+" -i - -f mpeg2video - | vlc -")
  135. if mode=="2" and connect=="a":
  136. if device=="atrix" or device=="Atrix":
  137. standard="w=540:h=960:format=rgb32"
  138. if device=="X10" or device=="x10":
  139. xperia="w=480:h=854:i420:format=rgb32"
  140. if device=="nexuss" or device=="Nexuss":
  141. standard="w=480:h=800:i420:format=rgb32"
  142. os.system("while true; do /home/"+user+"/android-sdk-linux_86/platform-tools/adb shell fbread ;done | mplayer -demuxer rawvideo -rawvideo "+standard+" -cache 4096 -fixed-vo -nosound - ")
  143. if mode=="1" and connect=="a":
  144. ##atrix rgb convert
  145. atrix="/home/"+user+"/android-sdk-linux_86/platform-tools/adb shell 'fbread > /sdcard/frame.raw';/home/"+user+"/android-sdk-linux_86/platform-tools/adb pull /sdcard/frame.raw /home/"+user+"/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr32 -s 540x960 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  146. #xperia x10 & g1 rgb convert
  147. xperia="/home/"+user+"/android-sdk-linux_86/platform-tools/adb shell 'cat /dev/graphics/fb0 > /sdcard/frame.raw';/home/"+user+"/android-sdk-linux_86/platform-tools/adb pull /sdcard/frame.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb565 -s 480x854 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  148. ##Nexus S rgb convert
  149. nexuss="/home/"+user+"/android-sdk-linux_86/platform-tools/adb shell 'cat /dev/graphics/fb0 > /sdcard/frame.raw';/home/"+user+"/android-sdk-linux_86/platform-tools/adb pull /sdcard/frame.raw ~/Pictures/0.raw && ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s 480x800 \-i ~/Pictures/0.raw -f image2 -vcodec png ~/Pictures/0.png"
  150. if device == "Atrix" or device=='atrix':
  151. standard=atrix
  152. if device == "X10" or device=='x10':
  153. standard=xperia
  154. if device == "NexusS" or device=="nexuss":
  155. standard=nexuss
  156. script="while true; do "+standard+";done"
  157. q=open("/home/"+user+"/Pictures/capture.sh",'w').write(script)
  158. os.system('touch ~/Pictures/0.png')
  159. Start().start()
  160. #Monitor().start()
  161. print "Opening Chrome, Refresh page if no image appears"
  162. time.sleep(5)
  163. g=os.system('google-chrome ~/Pictures/video.html')
  164. if g <= 0:
  165. print "Opening Firefox, Refresh page if no image appears"
  166. else:
  167. os.system('firefox video.html')
  168. class stream(threading.Thread):
  169. def run(self):
  170. os.system("nc -l 9003 | mplayer -fixed-vo - ")
  171. stream().start()
  172. sys.exit()
  173. class lxstream(threading.Thread):
  174. def run(self):
  175. config = Popen("nc -l 9001", shell=True, stdout=PIPE).stdout
  176. run = config.readlines()
  177. run=run[0].strip()
  178. print run
  179. os.system('while true; do nc '+phonewifi+' 9001; done | '+run)
  180. print "Make sure to start GroundWork on the device"
  181. lxstream().start()
  182. sys.exit()
  183. class type(threading.Thread):
  184. def run(self):
  185. n=0
  186. while True:
  187. os.system('nc -l 9001 > ground.work')
  188. try:
  189. status = open('ground.work','r').readlines()
  190. time.sleep(1)
  191. if status[0].strip()=='file':
  192. print "Streaming File"
  193. os.system('rm ground.work')
  194. type().start()
  195. stream()
  196. sys.exit()
  197. if status[0].strip()=='mirror':
  198. Mirror.start()
  199. except:
  200. n=n+1
  201. print 'exception',n
  202. time.sleep(5)
  203. def setup():
  204. global user,device,phonewifi,mode,connect,compression
  205. print "Please save your settings this time"
  206. person = Popen("whoami", shell=True, stdout=PIPE).stdout
  207. person = person.read()
  208. user=person.strip()
  209. phonewifi=raw_input('What is the Android/Meego device ip address: ')
  210. device=raw_input("Atrix or X10 or NexusS or N9: ")
  211. if device=='n9' or device=='N9':
  212. a=open("config.txt",'w').write(user+'\n'+phonewifi+'\n'+device)
  213. lxstream().start()
  214. else:
  215. mode=raw_input("Browser(1) or Mplayer(2) or VLC(3): ")
  216. connect=raw_input("Use Wifi(W) or usb(adb required to be installed and in path(A))")
  217. compression='n'
  218. compression=raw_input('Use Compression over wifi?(Use for slow wifi Networks) (y or n)')
  219. save=raw_input("Do you want to save your settings? (y or n) ")
  220. if save=="y" or "Y":
  221. a=open("config.txt",'w').write(user+'\n'+phonewifi+'\n'+device+'\n'+mode+'\n'+connect+'\n'+compression)
  222. reset=raw_input('Do you want to use saved settings? (y or n)')
  223. if reset=='n' or reset=='N':
  224. setup()
  225. else:
  226. b=open('config.txt','r').readlines()
  227. user=b[0].strip()
  228. phonewifi=b[1].strip()
  229. device=b[2].strip()
  230. if device=='n9' or device=='N9':
  231. lxstream().start()
  232. sys.exit()
  233. mode=b[3].strip()
  234. connect=b[4].strip()
  235. compression=b[5].strip()
  236. if mode=="1":
  237. print 'User: ',user +'\n','Wifi: ',phonewifi +'\n','Device: ',device +'\n','OutputMode: Browser'+'\n','Compression on ?: ',compression
  238. if mode=="2":
  239. print 'User: ',user +'\n','Wifi: ',phonewifi +'\n','Device: ',device +'\n','OutputMode: Mplayer'+'\n','Compression on ?: ',compression
  240. if mode=='3':
  241. print 'User: ',user +'\n','Wifi: ',phonewifi +'\n','Device: ',device +'\n','OutputMode: VLC'+'\n','Compression on ?: ',compression
  242. html="""
  243. <html>
  244. <head>
  245. </head>
  246. <body>
  247. <p>
  248. <img src="0.png" id="reloader" onload="setTimeout('document.getElementById(\'reloader\').src=\'0.png?\'+new Date().getMilliseconds()', 900)" />
  249. </p>
  250. </body>
  251. </html>
  252. """
  253. if device=='n9' or device=='N9':
  254. sys.exit()
  255. else:
  256. c = os.system("test -e video.html && echo 'testing for video file'")
  257. if c !=0:
  258. F=open('video.html','w').write(html)
  259. Mirror().start()
  260. stream().start()