Guest User

streamer python

a guest
Jul 7th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys, os
  4. import pango
  5. import pygtk, gtk, gobject
  6. import pygst
  7. pygst.require("0.10")
  8. import gst
  9. import subprocess
  10. class GTK_Main:
  11.  
  12. def __init__(self):
  13. window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  14.  
  15. window.set_title("Webcam Streamer")
  16. window.set_default_size(1000, 700)
  17.  
  18. window.connect("destroy", gtk.main_quit, "WM destroy")
  19. self.sstate = ('preparing')
  20. red=gtk.gdk.color_parse('grey')
  21. eb=gtk.EventBox()
  22. eb.modify_bg(gtk.STATE_NORMAL, red)
  23. window.add(eb)
  24.  
  25. windows = gtk.ScrolledWindow()
  26. windows.set_size_request(1000, 700)
  27. eb.add(windows)
  28.  
  29. hbox0 = gtk.HBox(True)
  30. windows.add_with_viewport(hbox0)
  31.  
  32. vbox = gtk.VBox()
  33. vbox.set_border_width(10)
  34. self.movie_window = gtk.DrawingArea()
  35. vbox.add(self.movie_window)
  36. hbox = gtk.HBox()
  37. vbox.pack_start(hbox, False)
  38. hbox.set_border_width(10)
  39. hbox.pack_start(gtk.Label())
  40. self.button = gtk.Button("Start Stream")
  41. self.button.connect("clicked", self.start_stop)
  42. hbox.pack_start(self.button, False)
  43. self.button2 = gtk.Button("Quit")
  44. self.button2.connect("clicked", self.exit)
  45. hbox.pack_start(self.button2, False)
  46. self.statuslabel = gtk.Label()
  47. self.statuslabel.modify_font(pango.FontDescription("sans bold 14"))
  48. self.statuslabel.set_justify(gtk.JUSTIFY_RIGHT);
  49. self.statuslabel.set_markup("<span background='blue'>Camera started...Ready to stream</span>")
  50. vbox.pack_start(self.statuslabel, False)
  51. hbox0.pack_start(vbox, False)
  52.  
  53. vbox2 = gtk.VBox()
  54. vbox2.set_border_width(10)
  55.  
  56. self.devlabel = gtk.Label("Input devices")
  57. self.devlabel.modify_font(pango.FontDescription("sans bold 10"))
  58. self.devlabel.set_justify(gtk.JUSTIFY_RIGHT);
  59. vbox2.pack_start(self.devlabel, False)
  60. vbox2.pack_start(gtk.HSeparator(), False)
  61.  
  62. hbox2 = gtk.HBox(True)
  63. vbox2.pack_start(hbox2, False)
  64. self.combodevice = gtk.combo_box_new_text()
  65. self.combodevice.append_text("/dev/video0")
  66. self.combodevice.append_text("/dev/video1")
  67. self.combodevice.append_text("/dev/video2")
  68. self.combodevice.append_text("/dev/video3")
  69. self.combodevice.set_active(0)
  70. hbox2.pack_start(gtk.Label("Video device : "), True)
  71. hbox2.pack_start(self.combodevice, True)
  72.  
  73. hbox21 = gtk.HBox(True)
  74. vbox2.pack_start(hbox21, False)
  75. self.vdevicetype = gtk.combo_box_new_text()
  76. self.vdevicetype.append_text("v4l")
  77. self.vdevicetype.append_text("v4l2")
  78. self.vdevicetype.set_active(1)
  79. hbox21.pack_start(gtk.Label("Driver type : "), True)
  80. hbox21.pack_start(self.vdevicetype, True)
  81.  
  82. hbox3 = gtk.HBox(True)
  83. vbox2.pack_start(hbox3, False)
  84. hbox3.pack_start(gtk.Label("Audio device : "), True)
  85. self.comboadevice = gtk.combo_box_new_text()
  86. self.comboadevice.append_text("") #geht zwar, ist aber nicht sauber durch das Leerzeichen davor
  87. self.comboadevice.append_text("")
  88. self.comboadevice.append_text("/dev/dsp1")
  89. self.comboadevice.append_text("/dev/dsp2")
  90. self.comboadevice.append_text("/dev/dsp3")
  91. self.comboadevice.set_active(0)
  92. hbox3.pack_start(self.comboadevice, True)
  93.  
  94. self.vparamslabel = gtk.Label("Video parameters")
  95. self.vparamslabel.set_justify(gtk.JUSTIFY_RIGHT);
  96. self.vparamslabel.modify_font(pango.FontDescription("sans bold 10"))
  97. vbox2.pack_start(self.vparamslabel, False)
  98. vbox2.pack_start(gtk.HSeparator(), False)
  99.  
  100. hbox4 = gtk.HBox(True)
  101. vbox2.pack_start(hbox4, False)
  102. self.combovsize = gtk.combo_box_new_text()
  103. self.combovsize.append_text("160x128")
  104. self.combovsize.append_text("320x240")
  105. self.combovsize.append_text("360x288")
  106. self.combovsize.append_text("640x480")
  107. self.combovsize.append_text("720x576")
  108. self.combovsize.set_active(1)
  109. hbox4.pack_start(gtk.Label("Video size : "), True)
  110. hbox4.pack_start(self.combovsize, True)
  111.  
  112. hbox5 = gtk.HBox(True)
  113. vbox2.pack_start(hbox5, False)
  114. hbox5.pack_start(gtk.Label("Framerate : "), True)
  115. self.comboframerate = gtk.combo_box_new_text()
  116. self.comboframerate.append_text("20:1")
  117. self.comboframerate.append_text("25:2")
  118. self.comboframerate.append_text("25:3")
  119. self.comboframerate.append_text("25:4")
  120. self.comboframerate.append_text("25:5")
  121. self.comboframerate.set_active(0)
  122. hbox5.pack_start(self.comboframerate, True)
  123.  
  124. hbox6 = gtk.HBox(True)
  125. vbox2.pack_start(hbox6, False)
  126. hbox6.pack_start(gtk.Label("Video quality : "), True)
  127. self.combovquality = gtk.combo_box_new_text()
  128. self.combovquality.append_text("0")
  129. self.combovquality.append_text("1")
  130. self.combovquality.append_text("2")
  131. self.combovquality.append_text("3")
  132. self.combovquality.append_text("4")
  133. self.combovquality.append_text("5")
  134. self.combovquality.append_text("6")
  135. self.combovquality.append_text("7")
  136. self.combovquality.append_text("8")
  137. self.combovquality.append_text("9")
  138. self.combovquality.append_text("10")
  139. self.combovquality.append_text("11")
  140. self.combovquality.append_text("12")
  141. self.combovquality.append_text("13")
  142. self.combovquality.append_text("14")
  143. self.combovquality.append_text("15")
  144. self.combovquality.append_text("16")
  145. self.combovquality.append_text("17")
  146. self.combovquality.append_text("18")
  147. self.combovquality.append_text("19")
  148. self.combovquality.append_text("20")
  149. self.combovquality.append_text("21")
  150. self.combovquality.append_text("22")
  151. self.combovquality.append_text("23")
  152. self.combovquality.append_text("24")
  153. self.combovquality.append_text("25")
  154. self.combovquality.append_text("26")
  155. self.combovquality.append_text("27")
  156. self.combovquality.append_text("28")
  157. self.combovquality.append_text("29")
  158. self.combovquality.append_text("30")
  159. self.combovquality.append_text("31")
  160. self.combovquality.append_text("32")
  161. self.combovquality.append_text("33")
  162. self.combovquality.append_text("34")
  163. self.combovquality.append_text("35")
  164. self.combovquality.append_text("36")
  165. self.combovquality.append_text("37")
  166. self.combovquality.append_text("38")
  167. self.combovquality.append_text("39")
  168. self.combovquality.append_text("40")
  169. self.combovquality.append_text("41")
  170. self.combovquality.append_text("42")
  171. self.combovquality.append_text("43")
  172. self.combovquality.append_text("44")
  173. self.combovquality.append_text("45")
  174. self.combovquality.append_text("46")
  175. self.combovquality.append_text("47")
  176. self.combovquality.append_text("48")
  177. self.combovquality.append_text("49")
  178. self.combovquality.append_text("50")
  179. self.combovquality.append_text("51")
  180. self.combovquality.append_text("52")
  181. self.combovquality.append_text("53")
  182. self.combovquality.append_text("54")
  183. self.combovquality.append_text("55")
  184. self.combovquality.append_text("56")
  185. self.combovquality.append_text("57")
  186. self.combovquality.append_text("58")
  187. self.combovquality.append_text("59")
  188. self.combovquality.append_text("60")
  189. self.combovquality.append_text("61")
  190. self.combovquality.append_text("62")
  191. self.combovquality.append_text("63")
  192. self.combovquality.set_active(16)
  193. hbox6.pack_start(self.combovquality, True)
  194.  
  195. self.aparamslabel = gtk.Label("Audio parameters")
  196. self.aparamslabel.set_justify(gtk.JUSTIFY_RIGHT);
  197. self.aparamslabel.modify_font(pango.FontDescription("sans bold 10"))
  198. vbox2.pack_start(self.aparamslabel, False)
  199. vbox2.pack_start(gtk.HSeparator(), False)
  200.  
  201. hbox7 = gtk.HBox(True)
  202. vbox2.pack_start(hbox7, False)
  203. self.comboaquality = gtk.combo_box_new_text()
  204. self.comboaquality.append_text("-0.1")
  205. self.comboaquality.append_text("0")
  206. self.comboaquality.append_text("0.1")
  207. self.comboaquality.append_text("0.2")
  208. self.comboaquality.append_text("0.3")
  209. self.comboaquality.append_text("0.4")
  210. self.comboaquality.append_text("0.5")
  211. self.comboaquality.append_text("0.6")
  212. self.comboaquality.append_text("0.7")
  213. self.comboaquality.append_text("0.8")
  214. self.comboaquality.append_text("0.9")
  215. self.comboaquality.append_text("1.0")
  216. self.comboaquality.set_active(3)
  217. hbox7.pack_start(gtk.Label("Audio quality : "), True)
  218. hbox7.pack_start(self.comboaquality, True)
  219.  
  220. hbox8 = gtk.HBox(True)
  221. vbox2.pack_start(hbox8, False)
  222. hbox8.pack_start(gtk.Label("Audio channels : "), True)
  223. self.comboachannels = gtk.combo_box_new_text()
  224. self.comboachannels.append_text("1")
  225. self.comboachannels.append_text("2")
  226. self.comboachannels.set_active(1)
  227. hbox8.pack_start(self.comboachannels, True)
  228.  
  229. hbox9 = gtk.HBox(True)
  230. vbox2.pack_start(hbox9, False)
  231. hbox9.pack_start(gtk.Label("Audio rate : "), True)
  232. self.comboarate = gtk.combo_box_new_text()
  233. self.comboarate.append_text("48000")
  234. self.comboarate.append_text("22050")
  235. self.comboarate.append_text("44100")
  236. self.comboarate.append_text("48000")
  237. self.comboarate.set_active(0)
  238. hbox9.pack_start(self.comboarate, True)
  239.  
  240. self.sconfiglabel = gtk.Label("Server Configuration")
  241. self.sconfiglabel.set_justify(gtk.JUSTIFY_RIGHT);
  242. self.sconfiglabel.modify_font(pango.FontDescription("sans bold 10"))
  243. vbox2.pack_start(self.sconfiglabel, False)
  244. vbox2.pack_start(gtk.HSeparator(), False)
  245.  
  246. hbox10 = gtk.HBox(True)
  247. vbox2.pack_start(hbox10, False)
  248. hbox10.pack_start(gtk.Label("Server : "), True)
  249. self.servername = gtk.Entry()
  250. self.servername.set_text("ip--domain")
  251. hbox10.pack_start(self.servername, True)
  252.  
  253. hbox11 = gtk.HBox(True)
  254. vbox2.pack_start(hbox11, False)
  255. hbox11.pack_start(gtk.Label("Port : "), True)
  256. self.portnumber = gtk.Entry()
  257. self.portnumber.set_text("xxxx")
  258. hbox11.pack_start(self.portnumber, True)
  259.  
  260. hbox12 = gtk.HBox(True)
  261. vbox2.pack_start(hbox12, False)
  262. hbox12.pack_start(gtk.Label("Mountpoint : "), True)
  263. self.mountpoint = gtk.Entry()
  264. self.mountpoint.set_text("test.ogg")
  265. hbox12.pack_start(self.mountpoint, True)
  266.  
  267. hbox13 = gtk.HBox(True)
  268. vbox2.pack_start(hbox13, False)
  269. hbox13.pack_start(gtk.Label("Password : "), True)
  270. self.password = gtk.Entry()
  271. self.password.set_text("xxxx")
  272. hbox13.pack_start(self.password, True)
  273.  
  274. self.smdatalabel = gtk.Label("Icecast Meta Data")
  275. self.smdatalabel.set_justify(gtk.JUSTIFY_RIGHT);
  276. self.smdatalabel.modify_font(pango.FontDescription("sans bold 10"))
  277. vbox2.pack_start(self.smdatalabel, False)
  278. vbox2.pack_start(gtk.HSeparator(), False)
  279.  
  280. hbox14 = gtk.HBox(True)
  281. vbox2.pack_start(hbox14, False)
  282. hbox14.pack_start(gtk.Label("Name : "), True)
  283. self.name = gtk.Entry()
  284. self.name.set_text("")
  285. hbox14.pack_start(self.name, True)
  286.  
  287. hbox15 = gtk.HBox(True)
  288. vbox2.pack_start(hbox15, False)
  289. hbox15.pack_start(gtk.Label("Description : "), True)
  290. self.description = gtk.Entry()
  291. self.description.set_text("")
  292. hbox15.pack_start(self.description, True)
  293.  
  294. hbox16 = gtk.HBox(True)
  295. vbox2.pack_start(hbox16, False)
  296. hbox16.pack_start(gtk.Label("Genre : "), True)
  297. self.genre = gtk.Entry()
  298. self.genre.set_text("")
  299. hbox16.pack_start(self.genre, True)
  300.  
  301. hbox17 = gtk.HBox(True)
  302. vbox2.pack_start(hbox17, False)
  303. hbox17.pack_start(gtk.Label("Url : "), True)
  304. self.url = gtk.Entry()
  305. self.url.set_text("")
  306. hbox17.pack_start(self.url, True)
  307.  
  308. self.srecordlabel = gtk.Label("Recording")
  309. self.srecordlabel.set_justify(gtk.JUSTIFY_RIGHT);
  310. self.srecordlabel.modify_font(pango.FontDescription("sans bold 10"))
  311. vbox2.pack_start(self.srecordlabel, False)
  312. vbox2.pack_start(gtk.HSeparator(), False)
  313.  
  314. hbox18 = gtk.HBox(True)
  315. vbox2.pack_start(hbox18, False)
  316. hbox18.pack_start(gtk.Label("Dump to file : "), True)
  317. self.recfile = gtk.Entry()
  318. self.recfile.set_text(".ogg")
  319. hbox18.pack_start(self.recfile, True)
  320. self.fcbutton = gtk.Button("Browse")
  321. self.fcbutton.connect("clicked", self.on_file_dialog)
  322. hbox18.pack_start(self.fcbutton, True)
  323.  
  324. hbox0.pack_start(vbox2, False)
  325.  
  326. window.show_all()
  327.  
  328. # Set up the gstreamer pipeline
  329. devname = self.combodevice.get_active_text()
  330. if self.vdevicetype.get_active_text() == "v4l":
  331. self.player = gst.parse_launch ("v4lsrc device="+devname+" ! video/x-raw-yuv,width=320,height=240 ! autovideosink")
  332. else:
  333. self.player = gst.parse_launch ("v4l2src device="+devname+" ! video/x-raw-yuv,width=320,height=240 ! autovideosink")
  334.  
  335. bus = self.player.get_bus()
  336. bus.add_signal_watch()
  337. bus.enable_sync_message_emission()
  338. bus.connect("message", self.on_message)
  339. bus.connect("sync-message::element", self.on_sync_message)
  340. self.player.set_state(gst.STATE_PLAYING)
  341.  
  342. def stop_stream(self):
  343. self.button.set_label("Start Stream")
  344. self.player.set_state(gst.STATE_NULL)
  345. devname = self.combodevice.get_active_text()
  346. if self.vdevicetype.get_active_text() == "v4l":
  347. self.player = gst.parse_launch ("v4lsrc device="+devname+" ! video/x-raw-yuv,width=320,height=240 ! autovideosink")
  348. else:
  349. self.player = gst.parse_launch ("v4l2src device="+devname+" ! video/x-raw-yuv,width=320,height=240 ! autovideosink")
  350. bus = self.player.get_bus()
  351. bus.add_signal_watch()
  352. bus.enable_sync_message_emission()
  353. bus.connect("message", self.on_message)
  354. bus.connect("sync-message::element", self.on_sync_message)
  355. self.player.set_state(gst.STATE_PLAYING)
  356.  
  357. def stop_cam(self):
  358. self.button.set_label("Start Stream")
  359. self.player.set_state(gst.STATE_NULL)
  360.  
  361. def start_stop(self, w):
  362. if self.button.get_label() == "Start Stream":
  363. self.button.set_label("Stop Stream")
  364. self.player.set_state(gst.STATE_NULL)
  365.  
  366. dumpfile = self.recfile.get_text()
  367. if dumpfile == ".ogg":
  368. print "No dump file"
  369. else:
  370. print "Dumping to :", dumpfile
  371. pipelist= []
  372. devname = self.combodevice.get_active_text()
  373. if self.vdevicetype.get_active_text() == "v4l":
  374. pipelist.append("v4lsrc device="+devname)
  375. else:
  376. pipelist.append("v4l2src device="+devname)
  377. pipelist.append("video/x-raw-yuv,width=320,height=240")
  378. pipelist.append("queue")
  379. pipelist.append("videorate")
  380. framerate=self.comboframerate.get_active_text()
  381. framerate=framerate.replace(":","/")
  382.  
  383. pipelist.append("ffmpegcolorspace")
  384. pipelist.append("tee name=tscreen")
  385. pipelist.append("queue")
  386. pipelist.append("autovideosink tscreen.")
  387. pipelist.append("queue")
  388. vquality = self.combovquality.get_active_text()
  389. pipelist.append("theoraenc quality="+vquality)
  390. pipelist.append("queue")
  391.  
  392. #audio input
  393. adevice=self.comboadevice.get_active_text()
  394. pipelist.append("oggmux name=mux jackaudiosrc connect=0")
  395. arate=self.comboarate.get_active_text()
  396. achannels=self.comboachannels.get_active_text()
  397. pipelist.append("audio/x-raw-float,channels=2")
  398. pipelist.append("queue")
  399. pipelist.append("audioconvert")
  400. aquality=self.comboaquality.get_active_text()
  401. pipelist.append("vorbisenc quality="+aquality)
  402. pipelist.append("queue")
  403. pipelist.append("mux. mux.")
  404. pipelist.append("queue")
  405.  
  406. #recording part
  407. if dumpfile != ".ogg":
  408. pipelist.append("tee name= tfile")
  409. pipelist.append("queue")
  410. pipelist.append("filesink location="+dumpfile+" tfile.")
  411. pipelist.append("queue")
  412.  
  413. #last but not least streaming
  414. server=self.servername.get_text()
  415. port=self.portnumber.get_text()
  416. mount=self.mountpoint.get_text()
  417. password=self.password.get_text()
  418. name=self.name.get_text()
  419. sdesc=self.description.get_text()
  420. sgenre=self.genre.get_text()
  421. surl=self.url.get_text()
  422. pipelist.append("shout2send ip="+server+" port="+port+" mount="+mount+" password="+password+" name="+name+" description="+sdesc+" genre="+sgenre+" url="+surl)
  423.  
  424. # ouf!
  425. pipeline = " ! ".join(pipelist)
  426. print "gstreamer command : ", pipeline
  427. self.player = gst.parse_launch (pipeline)
  428. bus = self.player.get_bus()
  429. bus.add_signal_watch()
  430. bus.enable_sync_message_emission()
  431. bus.connect("message", self.on_message)
  432. bus.connect("sync-message::element", self.on_sync_message)
  433. self.player.set_state(gst.STATE_PLAYING)
  434. self.statuslabel.set_markup("<span background='green'>Streaming at "+framerate+" frames per second</span>")
  435. self.sstate = 'running'
  436.  
  437. else:
  438. self.statuslabel.set_markup("<span background='blue'>Camera started...Ready to stream</span>")
  439. self.stop_stream()
  440. self.sstate = 'preparing'
  441.  
  442. def exit(self, widget, data=None):
  443. gtk.main_quit()
  444.  
  445. def on_file_dialog(self, w):
  446. self.fchooser = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_SAVE,
  447. buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
  448. self.fchooser.set_default_response(gtk.RESPONSE_OK)
  449. response = self.fchooser.run()
  450. if response == gtk.RESPONSE_OK:
  451. self.recfile.set_text(self.fchooser.get_filename())
  452. response = self.fchooser.destroy()
  453.  
  454. def on_message(self, bus, message):
  455. t = message.type
  456. if t == gst.MESSAGE_ERROR:
  457. if self.sstate == 'preparing':
  458. self.statuslabel.set_markup("<span background='red'>Error initializing camera...Check your devices</span>")
  459. self.stop_cam()
  460. if self.sstate == 'running':
  461. self.statuslabel.set_markup("<span background='red'>Error starting stream...Check your parameters</span>")
  462. self.stop_stream()
  463. self.sstate = 'preparing'
  464. err, debug = message.parse_error()
  465. print "Error: %s" % err, debug
  466.  
  467. def on_sync_message(self, bus, message):
  468. if message.structure is None:
  469. return
  470. message_name = message.structure.get_name()
  471. if message_name == "prepare-xwindow-id":
  472. # Assign the viewport
  473. imagesink = message.src
  474. imagesink.set_property("force-aspect-ratio", True)
  475. imagesink.set_xwindow_id(self.movie_window.window.xid)
  476.  
  477. GTK_Main()
  478. gtk.gdk.threads_init()
  479. gtk.main()
Add Comment
Please, Sign In to add comment