python timers and COM/directshow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sayanan Sivaraman

    python timers and COM/directshow

    Hey all,

    So I've written a simple video player using directshow/COM in VC++,
    and I'm in the process of translating it to python. For example, when
    the avi starts playing, I have a call media_control.R un() , etc.

    I'm wondering how I should go about updating my gtk.Hscale widget as a
    trackbar for the avi player.

    In C++, I have the following callbacks that update the scrollbar and
    video position with a timer.

    void CvideoDlg::OnNM Releasedcapture Slider1(NMHDR *pNMHDR, LRESULT
    *pResult)
    { //Slider event handler
    LONGLONG lPos = 0;
    LONGLONG lDuration = 0;
    KillTimer(101);

    g_pSeek->GetDuration(&l Duration);
    g_pSeek->GetCurrentPosi tion(&lPos);


    Videopos= m_slider.GetPos (); //Sets new video position to that of
    the slider, which user inputs
    lPos = ((long)Videopos * (lDuration/num_of_frames)) ;


    g_pSeek->SetPositions(& lPos, AM_SEEKING_Abso lutePositioning ,NULL,
    AM_SEEKING_NoPo sitioning);
    *pResult = 0;
    }
    void CvideoDlg::OnTi mer(UINT nIDEvent)
    { //Timer event handler.
    LONGLONG lPos = 0;
    LONGLONG lDuration = 0;

    g_pSeek->GetDuration(&l Duration);
    g_pSeek->GetCurrentPosi tion(&lPos);
    Videopos = (int)(lPos * num_of_frames/ lDuration);
    m_slider.SetPos (Videopos);
    if (Videopos==(int )(last_frame)) //If we get to the end of the
    selection, the video pauses
    pause();
    else{
    UpdateData(); //Updates the slider controller and position
    CDialog::OnTime r(nIDEvent);}

    }

    I'm wondering how I would implement similar callbacks in Python for a
    gtk.Hscale, and some sort of time [I'm not familiar with Pythons
    timers/threading at all].
  • Tim Golden

    #2
    Re: python timers and COM/directshow

    Sayanan Sivaraman wrote:
    So I've written a simple video player using directshow/COM in VC++,
    and I'm in the process of translating it to python. For example, when
    the avi starts playing, I have a call media_control.R un() , etc.
    >
    I'm wondering how I should go about updating my gtk.Hscale widget as a
    trackbar for the avi player.
    >
    In C++, I have the following callbacks that update the scrollbar and
    video position with a timer.
    [... snip callbacks ...]
    >
    I'm wondering how I would implement similar callbacks in Python for a
    gtk.Hscale, and some sort of time [I'm not familiar with Pythons
    timers/threading at all].

    You'd help your cause a lot here if you posted *Python*
    code to indicate what's calling what back where. Also if
    you stated whether you were using, eg, the GTK toolkit which
    your description suggests, or some other GUI toolkit. Because
    they tend to vary as to how they arrange their callbacks.

    In geeneral, Python callbacks are trivial: you create the
    function to do whatever and then pass the function as an
    object into the calling-back function call. Something
    like this (invented GUI toolkit):

    <code>
    def handle_lbutton_ click (event):
    #
    # do stuff with lbutton click
    #

    def handle_widget_s lide (event):
    #
    # do stuff with widget slide
    #


    handle_event ("lbutton_click ", handle_lbutton_ click)
    widget.attach_e vent ("slide", handle_widget_s lide)

    </code>

    But the details will obviously depend on the toolkit you
    use.

    TJG
    TJG

    Comment

    • Sayanan Sivaraman

      #3
      Re: python timers and COM/directshow

      On Sep 23, 4:24 am, Tim Golden <m...@timgolden .me.ukwrote:
      Sayanan Sivaraman wrote:
      So I've written a simple video player using directshow/COM in VC++,
      and I'm in the process of translating it to python.  For example, when
      the avi starts playing, I have a call media_control.R un() , etc.
      >
      I'm wondering how I should go about updating my gtk.Hscale widget as a
      trackbar for the avi player.
      >
      In C++, I have the following callbacks that update the scrollbar and
      video position with a timer.
      >
      [... snip callbacks ...]
      >
      >
      >
      I'm wondering how I would implement similar callbacks in Python for a
      gtk.Hscale, and some sort of time [I'm not familiar with Pythons
      timers/threading at all].
      >
      You'd help your cause a lot here if you posted *Python*
      code to indicate what's calling what back where. Also if
      you stated whether you were using, eg, the GTK toolkit which
      your description suggests, or some other GUI toolkit. Because
      they tend to vary as to how they arrange their callbacks.
      >
      In geeneral, Python callbacks are trivial: you create the
      function to do whatever and then pass the function as an
      object into the calling-back function call. Something
      like this (invented GUI toolkit):
      >
      <code>
      def handle_lbutton_ click (event):
        #
        # do stuff with lbutton click
        #
      >
      def handle_widget_s lide (event):
        #
        # do stuff with widget slide
        #
      >
      handle_event ("lbutton_click ", handle_lbutton_ click)
      widget.attach_e vent ("slide", handle_widget_s lide)
      >
      </code>
      >
      But the details will obviously depend on the toolkit you
      use.
      >
      TJG
      TJG
      Sorry, you make a very good point. I am using gtk. I don't have a
      problem with callbacks for the gtk widgets. My question is about
      timers and their callbacks. The reason I used c++ code is that
      Microsoft's COM interface is natively in C++, and Python uses "import
      comtypes" to access this functionality and the dll's.[ie
      GetModule('quar tz.dll')]

      Essentially what I would like to have [as evidenced by the c++ code]
      is something like the following:

      def timer_callback( args):
      while timer is active
      update scrollbar position based on video progress

      #here I am using microsoft's COM interface, so the function would
      be something like
      scrollbar.set_v alue(media_cont rol.CurrentPosi tion)

      def scrollbar_callb ack :
      when the scrollbar is moved, update this video position
      #this I understand. It would be something like
      media_control.C urrentPosition= scrollbar.get_v alue()

      def pauser :
      media_control.P ause()
      *somehow kill timer*

      def player:
      media_control.R un()
      timer.run() #timer.run() would call timer_callback


      So I would like to know how to construct and implement a timer that
      would do the above, a la the first callback. In addition, the timer
      has to be able to be paused/killed if I pause the video, and
      implemented again if I play the video ie:


      Thanks,
      sayanan

      Comment

      • Sayanan Sivaraman

        #4
        Re: python timers and COM/directshow

        You're right. Let me be more specific. Firstly, the reason I
        included c++ code is because I'm using Microsoft COM, which is
        natively in c++, and in fact, to access them through Python I use the
        comtypes module [import comtypes] and then GetModule('quar tz.dll') to
        access the dll's.

        I am using the gtk GUI widgets. I have a gtk.Hscale scrollbar which I
        would like to be a trackbar for the video playback. To coordinate
        this in Python, much like in c++, I would like to have a timer thread
        synchronizing the scrollbar update. ie:

        def scrollbar_callb ack(args):
        media_control.C urrentPosition= scrollbar.get_v alue()

        def timer_callback( args):
        #code to update the scrollbar based on video position, something
        like
        scrollbar.set_v alue(media_cont rol.CurrentPosi tion)
        >>>*And, I would like to be able to kill, run the timer based on whether the video is playing or paused, ie
        def player(args):
        media_control.R un() #plays video
        timer.run()

        def pauser(args):
        media_control.P ause()
        timer.kill

        Any tips?

        -sayanan




        Comment

        • Sayanan Sivaraman

          #5
          Re: python timers and COM/directshow

          Ok, so I actually found a solution to this out there, and decided I'd
          post back here and share it.

          import pygtk
          pygtk.require(' 2.0')
          import gtk
          import ctypes
          from ctypes import *
          from comtypes import client
          from ctypes.wintypes import *
          import gobject

          def delete_event(wi dget,event,data =None):
          global filter_builder, filter_graph, media_control, vseek
          media_control.S top
          vseek.Release
          filter_builder. Release
          GUIDATA.win1.Re lease
          media_control.R elease
          filter_graph.Re lease
          del GUIDATA.win1
          del filter_graph
          del vseek
          del filter_builder
          del media_control
          del all
          sys.exit([status])
          os._exit(status )
          gtk.main_quit()
          exit()
          return False

          def pauser(widget,d ata=None):
          global media_control,p laying, scrollbar, vseek
          if GUIDATA.data_lo aded==0:
          return 0
          scrollbar.set_v alue(vseek.Curr entPosition*30)
          media_control.P ause()
          playing=0
          return 0

          def player(widget,d ata=None):
          global media_control, vseek, scrollbar,playi ng
          if GUIDATA.data_lo aded==0:
          return 0
          media_control.R un()
          playing=1
          gobject.timeout _add(1,on_timer ,scrollbar)

          def scrollbar_callb ack(widget,scro ll, data=None):
          global media_control, vseek
          vseek.CurrentPo sition= scrollbar.get_v alue()/30
          return 0
          def on_timer(data=N one):
          global scrollbar, vseek, playing, media_control
          if (playing==1)and
          (vseek.CurrentP osition*30)<int (frame2.get_tex t()) :
          g= vseek.CurrentPo sition
          scrollbar.set_v alue(g*30)
          f= "%d" %(vseek.Current Position*30)
          curframe.set_te xt(f)
          print "update"
          return True




          win = gtk.Window()
          win.connect("de stroy", lambda x: gtk.main_quit() )
          win.set_default _size(500,800)
          win.set_title(" LISA GUI")

          filename= LPCWSTR("mymovi e.avi")
          #importing quartz.dll and qedit.dll for filtergraph construction
          qedit = client.GetModul e('qedit.dll') # DexterLib
          quartz= client.GetModul e('quartz.dll')

          CLSID_FilterGra ph = '{e436ebb3-524f-11ce-9f53-0020af0ba770}'
          filter_graph =
          client.CreateOb ject(CLSID_Filt erGraph,interfa ce=qedit.IFilte rGraph)
          filter_builder = filter_graph.Qu eryInterface(qe dit.IGraphBuild er)
          media_control = filter_builder. QueryInterface( quartz.IMediaCo ntrol)
          GUIDATA.win1= filter_builder. QueryInterface( quartz.IVideoWi ndow)
          filter_builder. RenderFile(GUID ATA.video, None)
          GUIDATA.win1.Se tWindowPosition (512, 0, 512, 400)
          vseek=filter_gr aph.QueryInterf ace(interface=q uartz.IMediaPos ition)



          adj= gtk.Adjustment( 1,1,30*vseek.Du ration+1,1,1.0, 1.0)
          scrollbar = gtk.HScale(adj)
          scrollbar.set_u pdate_policy(gt k.UPDATE_CONTIN UOUS)
          scrollbar.conne ct("change_valu e",scrollbar_ca llback)
          scrollbar.show( )
          hbox6=gtk.HBox( False,0)
          hbox6.pack_star t(scrollbar,Tru e,True)
          hbox6.show()
          vbox.pack_end(h box6,False,True )



          play_video= gtk.Button("Pla y")
          play_video.conn ect("clicked",p layer)
          play_video.show ()

          pause_video= gtk.Button("Pau se")
          pause_video.con nect("clicked", pauser)
          pause_video.sho w()

          hbox4= gtk.HBox(False, 0)
          hbox4.pack_star t(play_video,Tr ue,True)
          hbox4.pack_star t(pause_video,T rue,True)
          hbox4.show()
          vbox.pack_end(h box4,False,True )
          vbox.show()
          win.add(vbox)
          win.show_all()
          gtk.main()

          Comment

          Working...