# Program which extracts audio pids, video pids and transport stream rate from the output generated by ffmpeg command.It also calculates symbol rate. On running the program one can browse through files and select the desired stream to play


#!/usr/bin/python 

import pygtk
pygtk.require("2.0")
import gtk
import sys
import string
import re
import subprocess
import time
import os
import signal

x=""
y=""
def changed_cb_disc():
	dialog= = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
	dialog.set_default_response(gtk.RESPONSE_OK)
	filter = gtk.FileFilter()
	filter.set_name("All files")
	filter.add_pattern("*")
	dialog.add_filter(filter)
	response = dialog.run()
    #chooser.set_current_folder("/home/stream/Streams/")
    #chooser.set_filename(filename)
    #filename = response.get_filename()
	#filename = raw_input("Enter the file name  ")
	#comm.append('PLAY ' + filename)
	#print "\n",comm
	if response == gtk.RESPONSE_OK:
		filename=dialog.get_filename()
		print filename
		print dialog.get_filename(), 'selected' 
		dialog.destroy()
		return filename
	elif response == gtk.RESPONSE_CANCEL:
		print 'Closed, no files selected'
	dialog.destroy()
	



def main(argv):
    filename=argv
    rc="/home/kdhara00/python/ffmpeg -i "+ str(filename)
    print rc
    p1+	= subprocess.Popen(args=rc,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    msg1=p1.communicate()[1]
    return msg1
    

filename1=changed_cb_disc()
asd=filename1.split('/')
asd1=asd[4].split('.ts')
msg=main(filename1)
k=msg.split()
b=-1;
pidv=[]
for i in k:
    b=b+1
    if i=="Video:":
      pidv.append(k[b-1])
a=0;
print "these are video pids"
for j in pidv:
  a=a+1
  l=j.split('[')
  m=l[1].split(']')
  p=int(m[0],16)
  print "%d. %s" %(a,p)
  x=str(p)

   
print "\n"
c=-1;
pida=[]


for i in k:
    c=c+1
    if i=="Audio:":
      pida.append(k[c-1])
e=0
print "these are audio pids"
for j in pida:
  e=e+1
  l=j.split('[')
  m=l[1].split(']') 
  p=int(m[0],16)
  print "%d. %s" %(e,p)
  y=str(p)  
print "\n"	
d=-1;
btr=0.0
for i in k:
    d=d+1
    if i=="bitrate:":
      btr=k[d+1]
print "Transport stream rate=%s kbps" %(btr) 
btr=int(btr)

if btr<33000:
   cr=float(3)/float(4)
   cr2="3/4" 
   cr1="3_4"
else:
   cr=float(5)/float(6)
   cr2="5/6" 
   cr1="5_6"

   
sr=int((btr*204)/(2*(cr)*188))
print "Symbol rate=%f kbps" %(sr)
sr=str(sr)

def delay(seconds): #sending the delay
    time.sleep(seconds)

def execution_engine1():
        play1=str()
        filename2=asdf[0]						
        remote_command1="S1:"+str(filename2)+":10760:h:1:"+sr+":FEC_"+str(cr1)+":QPSK:ROLL_OFF_AUTO:PILOT_AUTO:"+x+":"+y+":"+x+":8:6:NONE:NONE"
	print remote_command1


def execution_engine():
        play=str()
        remote_command="/home/kdhara00/dektec/DtPlay/DtPlay /home/kdhara00/test/"+str(asd1[0]) +".ts -r "+ str(btr) + "000 -mt DVBS -mf 1010.0 -mc " + str(cr2) + " -l 0"
	print remote_command
        execution_engine1()
     
        try:
	    b=1
	    while b<=1600:
            	print"\n"
            	play= = subprocess.Popen(args=remote_command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
            	time.sleep(111600)
                b=b+1 
        except:        
            print "Bye"    
asd=filename1.split('/')
asdf=asd[4].split('.ts')

def signal_handler(signum, frame):#keyboard interrupt handler i.e. CTRL+C will be handled
   print 'Ctrl+C received'
   sys.exit(0)
signal.signal(signal.SIGINT, signal_handler) #SIGINT is for CTRL+C


execution_engine()
win=gtk.Window()
win.show_all()
win.connect("destroy", lambda wid: gtk.main_quit())
gtk.main()
