diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-05 19:45:03 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-05 19:45:03 +0800 |
commit | bddd5b547d7361670234375512d0a7982ca70033 (patch) | |
tree | 11d0047f0263b5bf3c3e845ed82dc1654ad671c5 | |
parent | 657fe06256630ecf936b18f2ed8c1d20c7e4556d (diff) |
increase version to 0.0.170.0.17
The totem plugin and libppswrapper have been removed from gst-plugins-pps repo
-rw-r--r-- | Makefile.am | 2 | ||||
-rwxr-xr-x | autoregen.sh | 2 | ||||
-rw-r--r-- | configure.ac | 106 | ||||
-rw-r--r-- | po/POTFILES.in | 3 | ||||
-rw-r--r-- | src/Makefile.am | 7 | ||||
-rw-r--r-- | totem/Makefile.am | 1 | ||||
-rw-r--r-- | totem/plugin/Makefile.am | 13 | ||||
-rw-r--r-- | totem/plugin/PPSVideoList.py | 133 | ||||
-rw-r--r-- | totem/plugin/ppstream-config.ui | 108 | ||||
-rw-r--r-- | totem/plugin/ppstream.py | 1477 | ||||
-rw-r--r-- | totem/plugin/ppstream.totem-plugin | 11 | ||||
-rw-r--r-- | totem/plugin/ppstream.ui | 226 | ||||
-rw-r--r-- | wrapper/Makefile.am | 35 | ||||
-rw-r--r-- | wrapper/ppsdemo-simple.c | 276 | ||||
-rw-r--r-- | wrapper/ppsvod-list.c | 128 | ||||
-rw-r--r-- | wrapper/ppswrapper-client.c | 1407 | ||||
-rw-r--r-- | wrapper/ppswrapper-common.c | 769 | ||||
-rw-r--r-- | wrapper/ppswrapper-msgget.c | 34 | ||||
-rwxr-xr-x | wrapper/ppswrapper-run-server.in | 7 | ||||
-rw-r--r-- | wrapper/ppswrapper-server-main.c | 69 | ||||
-rw-r--r-- | wrapper/ppswrapper-server.c | 1282 | ||||
-rw-r--r-- | wrapper/ppswrapper.c | 356 | ||||
-rw-r--r-- | wrapper/ppswrapper.h | 399 |
23 files changed, 40 insertions, 6811 deletions
diff --git a/Makefile.am b/Makefile.am index 09713bc..8cfaf92 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ -SUBDIRS = m4 wrapper src totem po +SUBDIRS = m4 wrapper src po EXTRA_DIST = autogen.sh gst-autogen.sh diff --git a/autoregen.sh b/autoregen.sh index 59b804a..096f24e 100755 --- a/autoregen.sh +++ b/autoregen.sh @@ -1,2 +1,2 @@ #!/bin/sh -./autogen.sh --prefix=/usr --libdir=/usr/lib64 $@ +./autogen.sh --prefix=/usr --libdir=/usr/lib64/ $@ diff --git a/configure.ac b/configure.ac index 11a375b..8971504 100644 --- a/configure.ac +++ b/configure.ac @@ -1,13 +1,17 @@ # -*- Autoconf -*- m4_define([gst_pps_major], 0) m4_define([gst_pps_minor], 0) -m4_define([gst_pps_micro], 16) +m4_define([gst_pps_micro], 17) m4_define([gst_pps_nano], 0) -AC_INIT([gst-plugins-pps], gst_pps_major.gst_pps_minor.gst_pps_micro.gst_pps_nano) +m4_if(gst_pps_nano, 0, + [m4_define(gst_pps_version, gst_pps_major.gst_pps_minor.gst_pps_micro)], + [m4_define(gst_pps_version, gst_pps_major.gst_pps_minor.gst_pps_micro.gst_pps_nano)]) +AC_INIT([gst-plugins-pps], gst_pps_version) dnl AC_INIT([$PACKAGE], [$VERSION]) AC_CONFIG_SRCDIR([config.h.in]) AC_CONFIG_HEADERS([config.h]) +AC_CONFIG_MACRO_DIR([m4]) dnl AM_MAINTAINER_MODE provides the option to enable maintainer mode dnl AM_MAINTAINER_MODE @@ -25,11 +29,10 @@ AC_PROG_CPP AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET -AC_PROG_RANLIB AC_PROG_LIBTOOL dnl Add the languages which your application supports to po/LINGUAS -GETTEXT_PACKAGE=ppstream +GETTEXT_PACKAGE=gst-plugins-pps AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, ["$GETTEXT_PACKAGE"], [Define to the Gettext package name]) AC_SUBST(GETTEXT_PACKAGE) AM_GLIB_GNU_GETTEXT @@ -148,92 +151,55 @@ AC_LANG(C++) PPS_DEP_LIBS='-lppsbase -lppssg -lppsvodres -lppsfds -lppsvodnet' LIBS="-lppsapi $PPS_DEP_LIBS $LIBS" PPS_LIBS= -PPS32_LIBS= -WRAPPER_LDFLAGS="-pthread -lppsapi $PPS_DEP_LIBS" -WRAPPER_CFLAGS="-pthread" -WRAPPER_PRELOAD_LDFLAGS="-avoid-version -ldl" -WRAPPER_PRELOAD_CFLAGS= + AC_MSG_CHECKING([whether pps libraris is available]) AC_LINK_IFELSE(AC_LANG_CALL([], ppsvod_create), [AC_MSG_RESULT(yes) HAVE_PPS="yes"], [AC_MSG_RESULT(no) HAVE_PPS="no"]) -if test "x$HAVE_PPS" = "xyes"; then - PPS_LIBS="-lppsapi $PPS_DEP_LIBS" -# AC_DEFINE_UNQUOTED(HAVE_LIBPPS) -else - AC_MSG_CHECKING([whether pps 32bit libraris is available]) - saved32LDFLAGS=$LDFLAGS - LDFLAGS="-m32 $LDFLAGS" - AC_LINK_IFELSE(AC_LANG_CALL([], ppsvod_destroy), - [ HAVE_PPS32="yes" - AC_MSG_RESULT(yes)], - [ HAVE_PPS32="no" - AC_MSG_RESULT(no)]) - if test "x$HAVE_PPS32" = "xyes"; then - PPS32_LIBS="-lppsapi $PPS_DEP_LIBS" - else - AC_MSG_ERROR(['couldn't find pps libraries.]) - fi - LDFLAGS=$saved32LDFLAGS - WRAPPER_CFLAGS="-m32 $WRAPPER_CFLAGS" - WRAPPER_LDFLAGS="-m32 $WRAPPER_LDFLAGS" - WRAPPER_PRELOAD_CFLAGS="-m32 $WRAPPER_PRELOAD_CFLAGS" - WRAPPER_PRELOAD_LDFLAGS="-m32 $WRAPPER_PRELOAD_LDFLAGS" - use_wrapper=yes -# AC_DEFINE_UNQUOTED(HAVE_LIBPPS32) -fi LDFLAGS=$savedLDFLAGS LIBS=$savedLIB +savedLDFLAGS=$LDFLAGS +savedLIB=$LIBS +LIBS="-lppswrapper" +AC_MSG_CHECKING([whether ppswrapper libraris is available]) +AC_LINK_IFELSE(AC_LANG_CALL([], ppsvod_create), + [AC_MSG_RESULT(yes) + HAVE_PPSWRAPPER="yes"], + [AC_MSG_RESULT(no) + HAVE_PPSWRAPPER="no"]) +LDFLAGS=$savedLDFLAGS +LIBS=$savedLIB + +if test "x$HAVE_PPS" = "xno"; then + if test "x$HAVE_PPSWRAPPER" = "xno" -o "x$use_wrapper" = "xno";then + AC_MSG_ERROR("libpps and libppswrapper aren't available") + else + use_wrapper=yes + fi +fi + +if test "x$HAVE_PPSWRAPPER" = "xno";then + use_wrapper=no + PPS_LIBS="-lppsapi $PPS_DEP_LIBS" +else + PPS_LIBS="-lppswrapper" +fi + AC_LANG(C) AC_SUBST(PPS_LIBS) -AC_SUBST(PPS32_LIBS) -AC_SUBST(WRAPPER_LDFLAGS) -AC_SUBST(WRAPPER_CFLAGS) -AC_SUBST(WRAPPER_PRELOAD_LDFLAGS) -AC_SUBST(WRAPPER_PRELOAD_CFLAGS) - -AM_CONDITIONAL(USE_PPSWRAPPER, [test "x$use_wrapper" != "xno"]) - -dnl ================================================================ -dnl Totem plugins -dnl ================================================================ -PLUGINDIR='${libdir}/totem/plugins' -AC_SUBST(PLUGINDIR) - -AC_MSG_CHECKING([whether Totem plugin support is requested]) -AC_ARG_ENABLE([totem], - AS_HELP_STRING([--enable-totem],[Enable totem support]), - [enable_totem=$enableval have_totem=$enableval], - [enable_totem=autodetect have_totem=yes]) -AC_MSG_RESULT([$enable_totem]) - -if test "x$have_totem" != "xno"; then - AM_PATH_PYTHON([2.3],[],[have_totem=no]) - if test "x$PYTHON" = "x:"; then - have_totem=no - fi -fi AC_OUTPUT(Makefile m4/Makefile src/Makefile po/Makefile.in - wrapper/Makefile - wrapper/ppswrapper-run-server - totem/Makefile - totem/plugin/Makefile) + wrapper/Makefile) if test x$use_wrapper = xyes;then AC_MSG_NOTICE([** Using pps wrapper]) else AC_MSG_NOTICE([** Not using pps wrapper, ppssrc may can only be used by root]) fi -if test x$have_totem = xyes;then - AC_MSG_NOTICE([** Totem plugin enabled]) -else - AC_MSG_NOTICE([ Totem plugin disabled]) -fi diff --git a/po/POTFILES.in b/po/POTFILES.in index 7979e42..137cd9c 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -2,7 +2,4 @@ # Please keep this file sorted alphabetically. [encoding: UTF-8] src/gstppssrc.c -totem/plugin/ppstream.py -totem/plugin/ppstream.ui -totem/plugin/ppstream-config.ui diff --git a/src/Makefile.am b/src/Makefile.am index b13da1c..b31fc8c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,13 +2,8 @@ pps_LTLIBRARIES = libgstppssrc.la libgstppssrc_la_SOURCES = gstppssrc.c -if USE_PPSWRAPPER -PPSLIBS = $(top_builddir)/wrapper/libppswrapper.la -else -PPSLIBS = $(PPS_LIBS) -endif libgstppssrc_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(PPS_CFLAGS) -I$(top_srcdir)/wrapper -libgstppssrc_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(PPSLIBS) +libgstppssrc_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(PPS_LIBS) libgstppssrc_la_LDFLAGS = $(GST_PPS_LDFLAGS) libgstppssrc_la_LIBTOOLFLAGS = --tag=disable-static diff --git a/totem/Makefile.am b/totem/Makefile.am deleted file mode 100644 index 3f7beb2..0000000 --- a/totem/Makefile.am +++ /dev/null @@ -1 +0,0 @@ -SUBDIRS = plugin
\ No newline at end of file diff --git a/totem/plugin/Makefile.am b/totem/plugin/Makefile.am deleted file mode 100644 index 318a585..0000000 --- a/totem/plugin/Makefile.am +++ /dev/null @@ -1,13 +0,0 @@ -plugindir = $(PLUGINDIR)/ppstream -uidir = $(plugindir) -plugin_PYTHON = ppstream.py PPSVideoList.py - -plugin_files = ppstream.totem-plugin -plugin_DATA = ppstream.totem-plugin -ui_DATA = ppstream.ui ppstream-config.ui - -EXTRA_DIST = $(ui_DATA) ppstream.py PPSVideoList.py ppstream.totem-plugin - -#CLEANFILES = $(plugin_DATA) -#DISTCLEANFILES = $(plugin_DATA) - diff --git a/totem/plugin/PPSVideoList.py b/totem/plugin/PPSVideoList.py deleted file mode 100644 index 3179f81..0000000 --- a/totem/plugin/PPSVideoList.py +++ /dev/null @@ -1,133 +0,0 @@ -# Copyright (C) 2009 Luo Jinghua <sunmoon1997@gmail.com>. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or (at -# your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -import pygtk -pygtk.require('2.0') - -import sys -import gobject -import gtk -import totem - -class PPSVideoList (gtk.TreeView): - __gproperties__ = { - 'title-column' : (gobject.TYPE_INT, "title-column", - "title-column", -1, gobject.G_MAXINT, -1, - gobject.PARAM_READWRITE), - 'tooltip-column' : (gobject.TYPE_INT, 'tooltip-column', - 'tooltip-column', -1, gobject.G_MAXINT, -1, - gobject.PARAM_READWRITE), - 'mrl-column' : (gobject.TYPE_INT, 'mrl-column', 'mrl-column', - -1, gobject.G_MAXINT, -1, gobject.PARAM_READWRITE), - "totem": (gobject.TYPE_OBJECT, "Totem", - "Totem", gobject.PARAM_READWRITE), - "plugin": (gobject.TYPE_OBJECT, "plugin", - "plugin", gobject.PARAM_READWRITE), - } - __gsignals__ = { 'starting-video': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN, - (gobject.TYPE_OBJECT, gobject.TYPE_PYOBJECT )), - } - - __gtype_name__ = 'PPSVideoList' - - def __init__ (self): - self.plugin = None - self.totem = None - self.builder = None - gtk.TreeView.__init__ (self) - - self.set_property ("has-tooltip", True) - self.connect ("row-activated", self.row_activated_cb) - self.connect ("query-tooltip", self.query_tooltip_cb) - self.connect ("button-press-event", self.button_pressed_cb) - self.connect ("popup-menu", self.popup_menu_cb) - - selection = self.get_selection () - selection.connect ("changed", self.selection_changed_cb) - selection.set_mode (gtk.SELECTION_MULTIPLE) - - def do_set_property(self, pspec, value): - setattr(self, pspec.name, value) - - def do_get_property(self, pspec): - return getattr(self, pspec.name) - - def row_activated_cb (self, widget, path, column): - #mrl_column = widget.get_property ("mrl-column") - if self.get_property ("mrl-column") == -1: - return - - play_video = self.emit ("starting-video", self, path) - if not play_video: - return - - model = widget.get_model () - iter = model.get_iter (path) - mrl = model.get (iter, self.get_property ("mrl-column"))[0] - title = model.get (iter, self.get_property ("title-column"))[0] - if hasattr(self.totem, "add_to_playlist_and_play"): - self.totem.add_to_playlist_and_play (mrl, title, False) - else: - self.totem.action_remote (totem.REMOTE_COMMAND_REPLACE, mrl) - - if not hasattr (gtk.TreeView, 'get_tooltip_context'): - def get_tooltip_context (self, x, y, keyboard_mode): - model = self.get_model () - if keyboard_mode: - # Keyboard mode - ret = self.get_cursor () - if not ret[0]: - return None - path = ret[0] - else: - coords = self.convert_widget_to_bin_window_coords (x, y) - - # Mouse mode - path = self.get_path_at_pos (coords[0], coords[1]) - if not path: - return None - path = path[0] - return model, path, model.get_iter (path) - - def query_tooltip_cb (self, widget, x, y, keyboard_mode, tooltip): - if not widget.get_tooltip_context (x, y, keyboard_mode): - return False - - model, path, iter = widget.get_tooltip_context (x, y, keyboard_mode) - mrl_column = self.get_property ("mrl-column") - if mrl_column == -1: - text = model.get (iter, self.get_property ("tooltip-column"))[0] - tooltip.set_text (text) - else: - text = model.get (iter, self.get_property ("tooltip-column"))[0] - text += '\n' + model.get (iter, self.get_property ("mrl-column"))[0] - tooltip.set_text (text) - widget.set_tooltip_row (tooltip, path) - return True - - def button_pressed_cb (self, widget, event): - pass - - def selection_changed_cb (self, widget): - self.trigger_tooltip_query () - - def popup_menu_cb (self, treeview, widget): - return False - - def get_ui_manager (self): - return self.ui_manager - -gobject.type_register (PPSVideoList) diff --git a/totem/plugin/ppstream-config.ui b/totem/plugin/ppstream-config.ui deleted file mode 100644 index ddb8543..0000000 --- a/totem/plugin/ppstream-config.ui +++ /dev/null @@ -1,108 +0,0 @@ -<?xml version="1.0"?> -<interface> - <!-- interface-requires gtk+ 2.12 --> - <!-- interface-naming-policy project-wide --> - <object class="GtkDialog" id="pps_config_dialog"> - <property name="border_width">5</property> - <property name="title" translatable="yes">Configure PPStream</property> - <property name="type_hint">normal</property> - <property name="has_separator">False</property> - <child internal-child="vbox"> - <object class="GtkVBox" id="dialog-vbox1"> - <property name="visible">True</property> - <property name="orientation">vertical</property> - <property name="spacing">2</property> - <child> - <object class="GtkFrame" id="frame1"> - <property name="visible">True</property> - <property name="label_xalign">0</property> - <property name="shadow_type">none</property> - <child> - <object class="GtkAlignment" id="alignment1"> - <property name="visible">True</property> - <property name="left_padding">12</property> - <child> - <object class="GtkVBox" id="vbox1"> - <property name="visible">True</property> - <property name="orientation">vertical</property> - <child> - <object class="GtkCheckButton" id="pps_config_show_posters"> - <property name="label" translatable="yes">Show posters</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">False</property> - <property name="draw_indicator">True</property> - </object> - <packing> - <property name="position">0</property> - </packing> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - </object> - </child> - </object> - </child> - <child type="label"> - <object class="GtkLabel" id="label1"> - <property name="visible">True</property> - <property name="label" translatable="yes"><b>PPStream</b></property> - <property name="use_markup">True</property> - </object> - </child> - </object> - <packing> - <property name="position">1</property> - </packing> - </child> - <child internal-child="action_area"> - <object class="GtkHButtonBox" id="dialog-action_area1"> - <property name="visible">True</property> - <property name="layout_style">end</property> - <child> - <object class="GtkButton" id="pps_config_ok_button"> - <property name="label">gtk-ok</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">True</property> - <property name="use_stock">True</property> - </object> - <packing> - <property name="expand">False</property> - <property name="fill">False</property> - <property name="position">0</property> - </packing> - </child> - <child> - <object class="GtkButton" id="pps_config_cancel_button"> - <property name="label">gtk-cancel</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">True</property> - <property name="use_stock">True</property> - </object> - <packing> - <property name="expand">False</property> - <property name="fill">False</property> - <property name="position">1</property> - </packing> - </child> - </object> - <packing> - <property name="expand">False</property> - <property name="pack_type">end</property> - <property name="position">0</property> - </packing> - </child> - </object> - </child> - <action-widgets> - <action-widget response="0">pps_config_ok_button</action-widget> - <action-widget response="1">pps_config_cancel_button</action-widget> - </action-widgets> - </object> -</interface> diff --git a/totem/plugin/ppstream.py b/totem/plugin/ppstream.py deleted file mode 100644 index 38e7beb..0000000 --- a/totem/plugin/ppstream.py +++ /dev/null @@ -1,1477 +0,0 @@ -import totem -import gobject, gtk, gconf -gobject.threads_init() -import urllib -import httplib -import threading -import time -import re -import os -import random - -from xml.dom import minidom - -import PPSVideoList - -try: - from hashlib import md5 - - def url_digest (url): - m = md5 () - m.update (url) - return m.hexdigest () -except ImportError: - import md5 - - def url_digest (url): - m = md5.new () - m.update (url) - return m.hexdigest () - -gconf_key = '/apps/totem/plugins/ppstream' -pps_cache_dir = os.path.expanduser ('~/.local/share/totem/plugin/ppstream') - - -pps_xmls_dir = os.path.join (pps_cache_dir, "xmls") -pps_images_dir = os.path.join (pps_cache_dir, "images") -for p in [ pps_xmls_dir, pps_images_dir ]: - if not os.path.exists (p): - os.makedirs (p) - -def xml_node_value (node): - return node.firstChild.nodeValue - -def xml_child_value (node, id): - child = node.getElementsByTagName(id)[0].firstChild; - if child: - return child.nodeValue - return None - -class PPSClass: - def __init__ (self): - self.id = 0 - self.title = "" - self.type = 1 - self.contentnum = 0 - self.subclasses = [] - self.films = [] - self.fetching_films = False - self.films_fetched = False - self.last_films = '' - - def __str__(self): - return '<' + str (self.id) + ' ' + self.title + '>' - - def parse (self, node): - self.id = int(xml_child_value (node, 'ID')) - self.title = str(xml_child_value (node, 'Title')) - self.type = int(xml_child_value (node, 'Type')) - self.contentnum = int(xml_child_value (node, 'ContentNum')) - self.subclasses = [] - self.films = [] - self.fetching_films = False - self.films_fetched = False - self.last_films = '' - return self - -class PPSSubClass: - def __str__(self): - return '<' + str (self.subid) + ' ' + self.title + '>' - - def parse (self, node): - self.subid = int(xml_child_value (node, 'SubID') or 0) - self.title = str(xml_child_value (node, 'Title')) - self.filmtitle = str(xml_child_value (node, 'FilmTitle')) - self.director = str(xml_child_value (node, 'Dirt')) - self.actor = str(xml_child_value (node, 'Actor')) - self.bigimage = str(xml_child_value (node, 'BIUrl')) - self.smallimage = str(xml_child_value (node, 'SIUrl')) - self.contentnum = int(xml_child_value (node, 'ContentNum')) - self.contents = [] - self.films = [] - self.fetching_films = False - self.films_fetched = False - self.last_films = '' - return self - -class PPSFile: - elements = [ ('ID', 'id'), ('CI', 'ci'), - ('Size', 'size'), ('Form', 'format'), - ('Rate', 'rate'), ('DUrl', 'url'), - ('VF', 'vipflag')] - - def __str__ (self): - return '<' + str (self.id) + ' ' + self.url + '>' - - def __eq__ (self, other): - for i in PPSFile.elements: - if getattr (self, i[1]) != getattr (other, i[1]): - return False - return True - - def parse (self, node): - self.id = int(xml_child_value (node, 'ID')) - self.ci = int(xml_child_value (node, 'CI')) - self.size = int(xml_child_value (node, 'Size')) - self.format = str(xml_child_value (node, 'Form')) - self.rate = int(xml_child_value (node, 'Rate')) - self.url = str(xml_child_value (node, 'DUrl')) - self.vipflag = int(xml_child_value (node, 'VF')) - return self - - def to_cdata_element (self, dom, tag, data): - if type (data) is not str: - data = str (data) - - ele = dom.createElement (tag) - child = dom.createCDATASection (data) - ele.childNodes.append (child) - return ele - - def toxml (self, dom): - ele = dom.createElement ('Url') - for child in PPSFile.elements: - childele = self.to_cdata_element (dom, child[0], - getattr (self, child[1])) - ele.childNodes.append (childele) - return ele - -class PPSFilm: - elements = [ ('ID', 'id'), ('Title', 'title'), - ('Dirt', 'director'), ('Actor', 'actor'), - ('Area', 'area'), ('Size', 'size'), - ('PT', 'pubtime'), ('LT', 'length'), - ('Lang', 'lang'), ('Score', 'score'), - ('Desc', 'desc'), ('Ldesc', 'longdesc'), - ('BIUrl', 'bigimage'), ('SIUrl', 'smallimage'), - ('CN', 'cn')] - def __str__(self): - return '<' + str (self.id) + ' ' + self.title + '>' - - def __eq__ (self, other): - for i in PPSFilm.elements: - if getattr (self, i[1]) != getattr (other, i[1]): - return False - return self.files == other.files - - def parse (self, node): - self.id = int(xml_child_value (node, 'ID')) - self.title = str(xml_child_value (node, 'Title')) - self.director = str(xml_child_value (node, 'Dirt')) - self.actor = str(xml_child_value (node, 'Actor')) - self.area = str(xml_child_value (node, 'Area')) - self.size = int(xml_child_value (node, 'Size')) - self.pubtime = str(xml_child_value (node, 'PT')) - self.length = str(xml_child_value (node, 'LT')) - self.lang = str(xml_child_value (node, 'Lang') or '') - self.score = float(xml_child_value (node, 'Score')) - self.desc = str(xml_child_value (node, 'Desc')) - self.longdesc = str(xml_child_value (node, 'Ldesc')) - self.bigimage = str(xml_child_value (node, 'BIUrl')) - self.smallimage = str(xml_child_value (node, 'SIUrl')) - self.cn = int(xml_child_value (node, 'CN')) - self.files = [] - self.pixbuf = None - - down = node.getElementsByTagName ('Down') - if not down: - return - files = down[0].getElementsByTagName ('Url') - for f in files: - self.files += [PPSFile ().parse (f)] - return self - - def to_cdata_element (self, dom, tag, data): - if not type (data) is str: - if data is None: - print 'tag ', tag, 'is None' - data = '' - else: - data = str (data) - - ele = dom.createElement (tag) - child = dom.createCDATASection (data) - ele.childNodes.append (child) - return ele - - def toxml (self, dom): - ele = dom.createElement ('Film') - for child in PPSFilm.elements: - childele = self.to_cdata_element (dom, child[0], - getattr (self, child[1])) - ele.childNodes.append (childele) - childele = dom.createElement ('Down') - for f in self.files: - childele.childNodes.append (f.toxml (dom)) - ele.childNodes.append (childele) - return ele - -class ProcessThread (threading.Thread): - def __init__ (self, data, process, callback, *args, **kwargs): - self.data = data - self.process = process - self.callback = callback - self.args = args - self.kwargs = kwargs - threading.Thread.__init__ (self) - - def run (self): - try: - res = self.process (self.data, *self.args, **self.kwargs) - except Exception, e: - print "Couldn't process data: ", e - res = None - - gobject.idle_add (self.publish_results, res) - - def publish_results(self, res): - self.callback (res, *self.args, **self.kwargs) - return False - -class DownloadThread (threading.Thread): - def __init__ (self, url, callback, *args, **kwargs): - self.url = url - self.callback = callback - self.args = args - self.kwargs = kwargs - threading.Thread.__init__ (self) - - def run (self): - for i in range (3): - try: - res = urllib.urlopen (self.url).read () - except Exception, e: - print "Coudn't open url: ", e - res = None - if res: - break - gobject.idle_add (self.publish_results, res) - - def publish_results(self, res): - self.callback (res, *self.args, **self.kwargs) - return False - -class RetrieveImageTask: - pass - -class RetrieveImage (threading.Thread): - def __init__ (self): - self.queue = [] - self._cache = {} - self._lock = threading.Lock () - self._done = False - threading.Thread.__init__ (self) - - def retrieve (self, url, callback, *args): - task = RetrieveImageTask () - task.url = url - task.callback = callback - task.args = args - self._lock.acquire () - self.queue.append (task) - self._lock.release () - - def cancel_tasks (self): - self._lock.acquire (True) - self.queue = [] - self._cache = {} - self._lock.release () - - def cancel (self): - self._done = True - self._lock.acquire (True) - self.queue = [] - self._cache = {} - self._lock.release () - - def is_busy (self): - self._lock.acquire (True) - busy = len (self.queue) != 0 - self._lock.release () - - return busy - - def image_name_from_url (self, url): - ext = url.rfind('.') - if ext >= 0: - ext = url[ext:] - else: - ext = '' - filename = url_digest (url) + ext - return os.path.join (pps_cache_dir, "images", filename) - - def load_image_from_disk (self, url): - filename = self.image_name_from_url (url) - if not os.access (filename, os.R_OK): - return None - - #print 'loading image', filename - try: - pixbuf = gtk.gdk.pixbuf_new_from_file (filename) - except gobject.GError: - return None - return pixbuf - - def store_image_to_disk (self, url, origin): - filename = self.image_name_from_url (url) - df = None - sf = None - try: - df = file (filename, 'wb') - sf = file (origin, 'rb') - while True: - buf = sf.read (4096) - if not buf: - break - df.write (buf) - except Exception, e: - print 'Failed to copy ', origin, ' to ', filename - print e - finally: - if df: - df.close () - if sf: - sf.close () - - def load_pixbuf (self, url): - pixbuf = self.load_image_from_disk (url) - if pixbuf: - return pixbuf - - try: - filename, headers = urllib.urlretrieve (url) - except IOError: - return None - - try: - pixbuf = gtk.gdk.pixbuf_new_from_file (filename) - except gobject.GError: - return None - - self.store_image_to_disk (url, filename) - os.unlink (filename) - return pixbuf - - def load_from_cache (self, url): - pixbuf = None - self._lock.acquire (True) - if url in self._cache: - pixbuf = self._cache [url] - self._lock.release () - return pixbuf - - def put_in_cache (self, url, pixbuf): - self._lock.acquire (True) - self._cache[url] = pixbuf - self._lock.release () - - def run (self): - import time - while not self.done: - task = None - self._lock.acquire (True) - if len (self.queue): - #print 'pending tasks:', len(self.queue) - task = self.queue.pop (0) - self._lock.release () - - if not task: - time.sleep (0.1) - continue - - pixbuf = self.load_from_cache (task.url) - if not pixbuf: - #print 'loading:', task.url - pixbuf = self.load_pixbuf (task.url) - if pixbuf: - self.put_in_cache (task.url, pixbuf) - time.sleep (0.10) - - if not pixbuf: - continue - - #print 'image loaded:', task.url - gobject.timeout_add (50, self.publish_result, pixbuf, task) - - def publish_result (self, pixbuf, task): - task.callback (pixbuf, *task.args) - return False - - @property - def done (self): - self._lock.acquire (True) - res = self._done - self._lock.release () - return res - -class PPStream (totem.Plugin): - def __init__ (self): - totem.Plugin.__init__ (self) - self.debug = False - self.gstreamer_plugins_present = True - - self.gconf = gconf.client_get_default () - - self.max_results = 20 - self.button_down = False - - self.categories_terms = "" - self.ppstream_id = "" - - self.start_index = {} - self.results = {} # This is just the number of results from the last pagination query - self.entry = {} - - self.current_treeview_name = "" - self.notebook_pages = [] - - self.vadjust = {} - self.liststore = {} - self.treeview = {} - - self.classes = [] - self.subclass_index = 0 - - def activate (self, totem_object): - """Check for the availability of the ppssrc GStreamer plugins""" - bvw_name = totem_object.get_video_widget_backend_name () - - if bvw_name.find ("GStreamer") != -1: - try: - import pygst - pygst.require ("0.10") - import gst - - registry = gst.registry_get_default () - if registry.find_plugin ("pps") == None: - """This means an error will be displayed when they try to play anything""" - self.gstreamer_plugins_present = False - except ImportError: - """Do nothing; either it's using xine or python-gstreamer isn't installed""" - - """Continue loading the plugin as before""" - self.builder = self.load_interface ("ppstream.ui", True, totem_object.get_main_window (), self) - self.config_builder = self.load_interface ("ppstream-config.ui", True, totem_object.get_main_window (), self) - self.totem = totem_object - - self.search_entry = self.builder.get_object ("pps_search_entry") - self.search_entry.connect ("activate", self.on_search_entry_activated) - self.refresh_button = self.builder.get_object ("pps_refresh_button") - self.refresh_button.connect ("clicked", self.on_refresh_button_clicked) - self.progress_bar = self.builder.get_object ("pps_progress_bar") - - self.notebook = self.builder.get_object ("pps_notebook") - self.notebook.connect ("switch-page", self.on_notebook_page_changed) - - self.notebook_pages = ["categories", "films", "files"] - self.current_treeview_name = "categories" - self.load_config () - self.setup_favourites () - self.setup_categories () - self.setup_films () - self.setup_files () - self.setup_config_dialog () - - self.vbox = self.builder.get_object ("pps_vbox") - self.vbox.show_all () - totem_object.add_sidebar_page ("ppstream", _("PPStream"), self.vbox) - - self.retrieveimage = RetrieveImage () - self.retrieveimage.start () - - self.classes = [] - self.fetch_classes () - - def deactivate (self, totem): - self.clear_ui () - self.categories_count +=1 - self.films_count += 1 - self.classes = [] - self.retrieveimage.cancel () - self.retrieveimage.join () - - totem.remove_sidebar_page ("ppstream") - - def is_configurable (self): - return True - - def create_configure_dialog (self): - dialog = self.config_builder.get_object ("pps_config_dialog") - show_posters = self.config_builder.get_object ("pps_config_show_posters") - show_posters.set_active (self.configs['show_posters']) - return dialog - - def fetch_classes (self, force = False): - if force: - self.classes = [] - if self.classes == []: - self.films_ids = (-1, -1) - self.subclass_index = 0 - self.categories_count += 1 - self.films_count += 1 - self.clear_ui () - self.retrieveimage.cancel_tasks () - - ### show favourites - self.subclass_index += 1 - self.classes = [self.favourites] - - url = 'http://playlist.pps.tv/classfortv.php' - thread = DownloadThread (url, self.classes_fetched, - self.categories_count) - thread.start() - - def classes_fetched (self, res, count): - if res: - ### check if this is a canceled task - if self.categories_count == count: - self.parse_class (res) - else: - print "Couldn't load ppstream media list" - - def subclasses_fetched (self, res, *args): - if not self.classes: - return - ### parse the xml - self.parse_subclass (res) - - ### fill categories view - self.fill_categories (self.classes[self.subclass_index].id) - - ### fetch next subclass - self.fetch_next_subclass (True) - - def threaded_parse_films (self, res, clsid, subclsid, count): - if not res: - return None - try: - result = self.parse_films (res, clsid, subclsid) - except Exception, e: - print "Couldn't parse xml", clsid, subclsid, e - result = None - - return result - - def films_fetched (self, res, clsid, subclsid, count, new = True): - if self.films_count != count: - return - - ### check if the server repeats the last page - cls, subcls = self.get_class_pair (clsid, subclsid) - if not subcls: - subcls = cls - - if res == subcls.last_films: - subcls.fetching_films = False - subcls.films_fetched = True - return - - if res is None: - subcls.fetching_films = False - return - - subcls.last_films = res - - if new: - self.store_films_to_disk (clsid, subclsid, res) - ### If the xml is larger then 4k, parse it in another thread. - ### Blocking the main thread is bad. - if res and len(res) > 1024 * 4: - process = ProcessThread (res, self.threaded_parse_films, - self.add_parsed_films, - clsid, subclsid, count) - process.start () - return - - result = self.threaded_parse_films (res, clsid, subclsid, count) - self.add_parsed_films (result, clsid, subclsid,count) - - def add_parsed_films (self, result, clsid, subclsid, count): - if self.films_count != count: - return - cls, subcls = self.get_class_pair (clsid, subclsid) - if not subcls: - subcls = cls - - if result: - subcls.films += result - if len (subcls.films) >= subcls.contentnum: - finished = True - subcls.fetching_films = False - subcls.films_fetched = True - else: - finished = False - - if result: - self.fill_films (result, clsid, subclsid) - if not finished: - self.fetch_films (clsid, subclsid) - else: - subcls.fetching_films = False - - def parse_class (self, res): - xmldoc = minidom.parseString (res) - ch = xmldoc.getElementsByTagName ('PPSChannels') - if not ch: - return - ch = ch[0] - cls = ch.getElementsByTagName ('Class') - for cl in cls: - self.classes += [PPSClass ().parse (cl)] - - if cls: - self.fetch_subclass (0, True) - - def fetch_subclass (self, index, show = False): - self.subclass_index = index - if self.classes[index].type == 1: - if show: - self.fill_categories (self.classes[index].id) - self.fetch_next_subclass (show) - return - url = 'http://playlist.pps.tv/subclassfortv.php?class=' - classid = str(self.classes[index].id); - thread = DownloadThread (url + classid, self.subclasses_fetched) - thread.start() - - def fetch_next_subclass (self, show = False): - if self.subclass_index < len(self.classes) - 1: - self.fetch_subclass (self.subclass_index + 1, show) - - def parse_subclass (self, res): - if not res: - return - xmldoc = minidom.parseString (res) - clses = xmldoc.getElementsByTagName ('PPSSClasses') - if not clses: - return - cls = clses[0].getElementsByTagName ('SubClass') - curclass = self.classes[self.subclass_index] - for cl in cls: - curclass.subclasses += [PPSSubClass ().parse (cl)] - - def get_class_index (self, clsid): - i = 0 - for cls in self.classes: - if cls.id == clsid: - return i - i += 1 - return -1 - - def get_subclass_index (self, clsindex, subclsid): - i = 0 - for cls in self.classes[clsindex].subclasses: - if cls.subid == subclsid: - return i - i += 1 - return -1 - - def parse_films (self, res, clsid, subclsid): - if not res: - return [] - xmldoc = minidom.parseString (res) - data = xmldoc.getElementsByTagName ('FilmData') - if not data: - return [] - films = data[0].getElementsByTagName ('Film') - if not films: - return [] - - - result = [] - for film in films: - result += [PPSFilm ().parse (film)] - - return result - - def get_class_pair (self, clsid, subclsid): - clsindex = self.get_class_index (clsid) - cls = self.classes[clsindex] - if cls.type == 0: - subclsindex = self.get_subclass_index (clsindex, subclsid) - - if cls.type == 0: - subcls = cls.subclasses[subclsindex] - else: - subcls = None - return cls, subcls - - def films_disk_name (self, clsid, subclsid): - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - filename = 'films' + '-' + str(clsid) + '-' + str(subclsid) - filename += '-' + str(subcls.contentnum) + '-' + str(subcls.page) - filename += '.xml' - return os.path.join (pps_cache_dir, "xmls", filename) - - def store_films_to_disk (self, clsid, subclsid, res): - filename = self.films_disk_name (clsid, subclsid) - #print 'writing', filename - f = file (filename, 'wb') - f.write (res) - f.close () - - def load_films_from_disk (self, url, callback, clsid, subclsid, count): - filename = self.films_disk_name (clsid, subclsid) - if not os.access (filename, os.R_OK): - return False - - #print 'reading', filename - try: - st = os.stat (filename) - now = time.time () - if now - st.st_ctime > 60 * 60 * 24: - os.unlink (filename) - return None - except OSError: - return None - try: - res = file(filename, 'rb').read () - except: - return False - callback (res, clsid, subclsid, count, False) - return True - - def purge_cached_films (self, clsid, subclsid): - import glob - - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - filename = 'films' + '-' + str(clsid) + '-' + str(subclsid) - filename += '-*' + '.xml' - pattern = os.path.join (pps_cache_dir, "xmls", filename) - for f in glob.glob (pattern): - os.unlink (f) - - def download_films (self, url, callback, clsid, subclsid, count): - if self.load_films_from_disk (url, callback, clsid, subclsid, - count): - return - - thread = DownloadThread (url, callback, clsid, subclsid, - count) - thread.start() - - def fetch_films (self, clsid, subclsid): - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - if len (subcls.films) >= subcls.contentnum: - return - - url = 'http://playlist.pps.tv/channelsfortv.php?' - url += 'class=' + str(clsid) - if cls.type == 0: - url += '&subclass=' + str(subclsid) - if not len (subcls.films): - subcls.page = 0 - elif subcls.fetching_films: - subcls.page += 1 - #print len(subcls.films), subcls.films, subcls.contentnum - url += '&page=' + str(subcls.page + 1) - #print 'fetchfilm url = ', url, len(subcls.films), subcls.contentnum - subcls.fetching_films = True - - self.download_films (url, self.films_fetched, clsid, subclsid, - self.films_count) - - def on_cat_treeview_row_activated(self, treeview, path, view_column, data=None): - model, rows = treeview.get_selection ().get_selected_rows () - iter = model.get_iter (rows[0]) - title = model.get_value (iter, 3) - classid = model.get_value (iter, 0) - subclassid = model.get_value (iter, 2) - - #print title, classid, subclassid - self.show_films (classid, subclassid) - self.notebook.set_current_page (1) - - def setup_categories (self): - treeview_name = 'categories' - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - renderer = gtk.CellRendererText() - renderer.set_property("xalign", 0.0) - - column = gtk.TreeViewColumn(_("Name"), renderer, text = 3) - column.set_clickable(True) - treeview.append_column(column) - - column = gtk.TreeViewColumn(_("Number"), renderer, text = 4) - treeview.append_column(column) - - self.vadjust[treeview_name] = treeview.get_vadjustment () - self.vadjust[treeview_name].connect ("value-changed", self.on_value_changed) - vscroll = self.builder.get_object ("pps_scrolled_window_" + treeview_name).get_vscrollbar () - vscroll.connect ("button-press-event", self.on_button_press_event) - vscroll.connect ("button-release-event", self.on_button_release_event) - - self.liststore[treeview_name] = self.builder.get_object ("pps_liststore_" + treeview_name) - self.treeview[treeview_name] = treeview - treeview.set_model (self.liststore[treeview_name]) - - treeview.connect("row_activated", self.on_cat_treeview_row_activated) - - self.categories_count = 0 - - def on_add_to_favourites (self, button): - selection = self.treeview['films'].get_selection () - model, rows = selection.get_selected_rows () - if not rows: - return - iter = model.get_iter (rows[0]) - clsid = model.get_value (iter, 4) - subclsid = model.get_value (iter, 5) - - for row in rows: - iter = model.get_iter (row) - clsid = model.get_value (iter, 4) - subclsid = model.get_value (iter, 5) - self.add_favourites (clsid, subclsid, row[0], False) - self.save_favourites () - self.refresh_favourites () - - def on_remove_from_favourites (self, button): - selection = self.treeview['films'].get_selection () - model, rows = selection.get_selected_rows () - if not rows: - return - rows.reverse () - for row in rows: - iter = model.get_iter (row) - clsid = model.get_value (iter, 4) - subclsid = model.get_value (iter, 5) - self.remove_favourites (clsid, subclsid, row[0], False) - self.save_favourites () - self.refresh_favourites () - - def setup_favourites (self): - button = self.builder.get_object ('pps_add_to_favourites_button') - self.add_to_favourites_button = button - button.set_sensitive (False) - button.connect ('clicked', self.on_add_to_favourites) - button = self.builder.get_object ('pps_remove_from_favourites_button') - button.set_sensitive (False) - button.connect ('clicked', self.on_remove_from_favourites) - self.remove_from_favourites_button = button - self.load_favourites () - - def load_favourites (self): - treeview_name = 'categories' - filename = os.path.join (pps_cache_dir, 'favourites.xml') - - self.favourites = PPSClass () - self.favourites.title = _('Favourites') - self.favourites.films_fetched = True - - if not os.path.exists (filename): - return - try: - res = file (filename, 'rb').read () - films = self.parse_films (res, 0, 0) - except Exception, e: - print "Couldn't load favourites.xml:", e - return - #print 'loaded favourite films', films - if not films: - return - self.favourites.films = films - self.favourites.contentnum = len (films) - - def save_favourites (self): - treeview_name = 'categories' - filename = os.path.join (pps_cache_dir, 'favourites.xml') - fp = None - try: - fp = file (filename, 'w') - except: - return - impl = minidom.getDOMImplementation () - doc = impl.createDocument(None, "FilmData", None) - filmdata = doc.documentElement - for film in self.favourites.films: - filmdata.childNodes.append (film.toxml (doc)) - fp.write (doc.toxml (encoding = 'utf-8')) - fp.close () - - def add_favourites (self, clsid, subclsid, index, update = True): - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - film = subcls.films[index] - if film in self.favourites.films: - return - self.favourites.films.append (film) - self.favourites.contentnum = len (self.favourites.films) - - if not update: - return - self.refresh_favourites () - self.save_favourites () - - def remove_favourites (self, clsid, subclsid, index, update = False): - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - film = subcls.films[index] - #print self.favourites.films, film - if not film in self.favourites.films: - print "Couldn't find ", film, ' in favourites' - return - self.favourites.films.remove (film) - self.favourites.contentnum = len (self.favourites.films) - - if not update: - return - self.refresh_favourites () - self.save_favourites () - - def refresh_favourites (self): - treeview_name = 'categories' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - if not len (treeview): - return - - iter = treeview.get_iter (0) - treeview.set (iter, 4, self.favourites.contentnum) - - clsid, subclsid = self.films_ids - if (clsid, subclsid) != (0, 0): - return - - self.films_ids = (-1, -1) - self.show_films (clsid, subclsid, False, True) - - def fill_all_categories (self): - treeview_name = 'categories' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - treeview.clear () - for cls in self.classes: - iter = treeview.append(None) - treeview.set(iter, - 0, cls.id, - 1, cls.type, - 2, 0, - 3, cls.title, - 4, cls.contentnum) - - # add children - for subcls in cls.subclasses: - child_iter = treeview.append(iter); - treeview.set(child_iter, - 0, cls.id, - 1, cls.type, - 2, subcls.subid, - 3, subcls.title, - 4, subcls.contentnum) - - def fill_categories (self, clsid): - treeview_name = 'categories' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - for cls in self.classes: - if cls.id != clsid: - continue - - iter = treeview.append(None) - treeview.set(iter, - 0, cls.id, - 1, cls.type, - 2, 0, - 3, cls.title, - 4, cls.contentnum) - - # add children - for subcls in cls.subclasses: - child_iter = treeview.append(iter); - treeview.set(child_iter, - 0, cls.id, - 1, cls.type, - 2, subcls.subid, - 3, subcls.title, - 4, subcls.contentnum) - break - - def gettipfromfilm (self, film, f): - tip = film.title - tip += '\n' + _("Director: ") + film.director - tip += '\n' + _("Actor: ") + film.actor - tip += '\n' + _("Area: ") + film.area - tip += '\n' + _("Size: ") + str(film.size) + _('MB') - tip += '\n' + _("Pubtime: ") + film.pubtime - tip += '\n' + _("Length: ") + str(film.length) + _('Min') - tip += '\n' + _("Score: ") + str(film.score) - tip += '\n' + _("Episodes: ") + str(film.cn) - tip += '\n' + _("Description: ") + film.desc - tip += '\n' + _("Long Description: ") + film.longdesc - - return tip - - def retrieveimage_cb (self, pixbuf, film, iter, clsid, subclsid, count): - treeview_name = 'films' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - film.pixbuf = pixbuf - - try: - ### check whether the treeview has been cleared. - if self.films_ids == (clsid, subclsid) and self.films_count == count: - if self.configs['show_posters']: - treeview.set (iter, 0, pixbuf) - else: - print 'not showing posters' - except: - pass - - def storelist_append_film (self, treeview, film, clsid, subclsid): - if not film.files: - return - f = film.files[0] - tip = self.gettipfromfilm (film, f) - pixbuf = film.pixbuf - if not self.configs['show_posters']: - pixbuf = None - it = treeview.append ([pixbuf, film.title, f.url, tip, clsid, subclsid]) - if not film.pixbuf and film.smallimage and self.configs['show_posters']: - self.retrieveimage.retrieve (film.smallimage, self.retrieveimage_cb, - film, it, clsid, subclsid, - self.films_count) - - def clear_files (self): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - treeview.clear () - - def fill_files (self, film): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - treeview.clear () - for f in film.files: - no = str(f.ci) + '/' + str(film.cn) - it = treeview.append ([no, film.title, f.url]) - - def fill_all_films (self, clsid, subclsid): - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - treeview_name = 'films' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - for film in subcls.films: - self.storelist_append_film (treeview, film, clsid, subclsid) - - def fill_films (self, films, clsid, subclsid): - if self.films_ids != (clsid, subclsid): - return - treeview_name = 'films' - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - for film in films: - self.storelist_append_film (treeview, film, clsid, subclsid) - - def refetch_films (self, clsid = None, subclsid = None): - if clsid is None and subclsid is None: - clsid, subclsid = self.films_ids - - self.purge_cached_films (clsid, subclsid) - self.retrieveimage.cancel_tasks () - self.films_ids = (-1, -1) - self.show_films (clsid, subclsid, True) - - def show_films (self, clsid, subclsid, force = False, cancel = True): - ### FIXME what to do when the subclasses is empty? - if self.films_ids == (clsid, subclsid): - return - self.films_ids = (clsid, subclsid) - - if cancel: - self.films_count += 1 - if self.films_ids != (-1, -1): - cls, subcls = self.get_class_pair (*self.films_ids) - if cls.type == 1: - subcls = cls - subcls.fetching_films = False - - self.films_ids = (clsid, subclsid) - - if not self.configs['show_posters']: - self.retrieveimage.cancel_tasks () - - self.clear_pages (['films', 'files']) - - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - if force and (clsid, subclsid) != (0, 0): - subcls.films = [] - subcls.films_fetched = False - subcls.last_films = '' - - if cancel or (not subcls.fetching_films and not self.retrieveimage.is_busy ()): - self.add_progress_callback (clsid, subclsid, self.films_count) - - if subcls.films_fetched: - self.fill_all_films (clsid, subclsid) - return - if not subcls.fetching_films: - self.fetch_films (clsid, subclsid) - else: - print 'fetching films list for', clsid, subclsid, 'is already in progress' - self.fill_all_films (clsid, subclsid) - - def setup_films (self): - treeview_name = 'films' - self.start_index[treeview_name] = 1 - self.results[treeview_name] = 0 - self.entry[treeview_name] = None - self.films_ids = (-1, -1) - self.films_count = 0 - - """This is done here rather than in the UI file, - because UI files parsed in C and GObjects created in - Python apparently don't mix.""" - renderer = totem.CellRendererVideo (use_placeholder = self.configs['show_posters']) - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - treeview.set_property ("totem", self.totem) - treeview.connect ("row-activated", self.on_films_row_activated) - treeview.connect_after ("starting-video", self.on_films_starting_video) - treeview.insert_column_with_attributes (0, _("Videos"), renderer, thumbnail=0, title=1) - self.cell_video_renderer = renderer - - self.vadjust[treeview_name] = treeview.get_vadjustment () - self.vadjust[treeview_name].connect ("value-changed", self.on_value_changed) - vscroll = self.builder.get_object ("pps_scrolled_window_" + treeview_name).get_vscrollbar () - vscroll.connect ("button-press-event", self.on_button_press_event) - vscroll.connect ("button-release-event", self.on_button_release_event) - - self.liststore[treeview_name] = self.builder.get_object ("pps_liststore_" + treeview_name) - self.treeview[treeview_name] = treeview - treeview.set_model (self.liststore[treeview_name]) - - selection = treeview.get_selection () - selection.connect ("changed", self.on_films_selection_changed_cb) - - def add_to_playlist_cb (self, treeview): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - selection = treeview.get_selection () - model = treeview.get_model () - model, rows = selection.get_selected_rows () - - for row in rows: - iter = model.get_iter (row) - mrl = model.get_value (iter, 2) - self.totem.action_remote (totem.REMOTE_COMMAND_ENQUEUE, mrl) - - def copy_location_cb (self, action): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - selection = treeview.get_selection () - model = treeview.get_model () - model, rows = selection.get_selected_rows () - iter = model.get_iter (rows[0]) - mrl = model.get_value (iter, 2) - - clip = gtk.Clipboard () - clip.set_text (mrl) - clip = gtk.Clipboard (display = gtk.gdk.display_get_default(), - selection = "PRIMARY") - clip.set_text (mrl) - - - def setup_files_action (self): - self.files_uimanager = gtk.UIManager () - action_group = gtk.ActionGroup ("pps-files-action-group") - action_group.add_actions ([('add-to-list', gtk.STOCK_ADD, _('_Add to Playlist'), None, - _("Add the video to playlist"), self.add_to_playlist_cb), - ('copy-location', gtk.STOCK_COPY, _('_Copy Location'), None, - _("Copy location to the clipboard"), self.copy_location_cb)]) - self.files_uimanager.insert_action_group(action_group, 0) - ui = """ - <popup name="pps-file-list-group"> - <menuitem name="add-to-list" action="add-to-list"/> - <menuitem name="copy-location" action="copy-location"/> - </popup>""" - self.files_uimanager.add_ui_from_string (ui) - - def files_show_popup_menu (self, event = None): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - selection = treeview.get_selection () - - if event: - button = event.button - time = event.time - path = treeview.get_path_at_pos (int(event.x), int(event.y)) - if not path or not selection.path_is_selected (path[0]): - selection.unselect_all () - else: - time = gtk.get_current_event_time () - button = None - - count = selection.count_selected_rows () - if not count: - return False - - action_group = self.files_uimanager.get_action_groups () [0] - action = action_group.get_action ("copy-location") - action.set_sensitive (count == 1) - - menu = self.files_uimanager.get_widget ("/pps-file-list-group") - menu.select_first (False) - menu.popup (None, None, None, button, time) - - return True - - def files_popup_menu_cb (self): - print 'popup menu' - self.files_show_popup_menu () - - def files_button_pressed_cb (self, widget, event, user_data = None): - if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3: - return self.files_show_popup_menu (event) - - return False - - def setup_files (self): - treeview_name = 'files' - treeview = self.builder.get_object ("pps_treeview_" + treeview_name) - renderer = gtk.CellRendererText() - renderer.set_property("xalign", 0.0) - - column = gtk.TreeViewColumn(_("No"), renderer, text = 0) - column.set_clickable(True) - treeview.append_column(column) - - column = gtk.TreeViewColumn(_("Title"), renderer, text = 1) - treeview.append_column(column) - - column = gtk.TreeViewColumn(_("Mrl"), renderer, text = 2) - treeview.append_column(column) - - self.vadjust[treeview_name] = treeview.get_vadjustment () - self.vadjust[treeview_name].connect ("value-changed", self.on_value_changed) - vscroll = self.builder.get_object ("pps_scrolled_window_" + treeview_name).get_vscrollbar () - vscroll.connect ("button-press-event", self.on_button_press_event) - vscroll.connect ("button-release-event", self.on_button_release_event) - - self.liststore[treeview_name] = self.builder.get_object ("pps_liststore_" + treeview_name) - self.treeview[treeview_name] = treeview - treeview.set_model (self.liststore[treeview_name]) - - treeview.connect("row_activated", self.on_files_treeview_row_activated) - - self.setup_files_action () - treeview.connect ("popup-menu", self.files_popup_menu_cb) - treeview.connect ("button-press-event", self.files_button_pressed_cb) - - selection = treeview.get_selection () - selection.set_mode (gtk.SELECTION_MULTIPLE) - - def on_files_treeview_row_activated(self, treeview, path, view_column, data=None): - model, rows = treeview.get_selection ().get_selected_rows () - try: - iter = model.get_iter (rows[0]) - except IndexError: - print 'activated invalid index:', rows[0] - return - - title = model.get_value (iter, 1) - mrl = model.get_value (iter, 2) - #print title, mrl - if hasattr(self.totem, "add_to_playlist_and_play"): - self.totem.add_to_playlist_and_play (mrl, title, False) - else: - self.totem.action_remote (totem.REMOTE_COMMAND_REPLACE, mrl) - - def on_films_selection_changed_cb (self, selection): - model, rows = selection.get_selected_rows () - if not rows: - return - iter = model.get_iter (rows[0]) - clsid = model.get_value (iter, 4) - subclsid = model.get_value (iter, 5) - - cls, subcls = self.get_class_pair (clsid, subclsid) - if cls.type == 1: - subcls = cls - - #print clsid, subclsid, cls, subcls, rows, len(subcls.films) - if subcls.films: - self.fill_files (subcls.films[rows[0][0]]) - - def on_notebook_page_changed (self, notebook, notebook_page, page_num): - self.current_treeview_name = self.notebook_pages[page_num] - self.add_to_favourites_button.set_sensitive (False) - self.remove_from_favourites_button.set_sensitive (False) - if self.current_treeview_name == 'files': - self.refresh_button.set_sensitive (False) - elif self.current_treeview_name == 'films': - if self.films_ids == (-1, -1): - self.refresh_button.set_sensitive (False) - elif self.films_ids == (0, 0): - self.remove_from_favourites_button.set_sensitive (True) - else: - self.refresh_button.set_sensitive (True) - self.add_to_favourites_button.set_sensitive (True) - - else: - self.refresh_button.set_sensitive (True) - - def update_progress_bar (self): - if self.films_ids == (-1, -1): - self.progress_bar.set_fraction (0.0) - self.progress_bar.set_text ("") - - return True - - cls, subcls = self.get_class_pair (*self.films_ids) - if not subcls: - subcls = cls - if subcls.fetching_films: - progress = float(len (subcls.films)) / subcls.contentnum - if progress > 1.0: - progress = 1.0 - self.progress_bar.set_fraction (progress) - progressstr = str(len (subcls.films)) + '/' + str(subcls.contentnum) - self.progress_bar.set_text ("Loading film list(" + progressstr + ')...') - return True - elif self.retrieveimage.is_busy (): - self.progress_bar.set_text ('loading posters...') - self.progress_bar.pulse () - else: - self.progress_bar.set_fraction (0.0) - self.progress_bar.set_text ("") - - return False - - return True - - def update_progress_bar_cb (self, clsid, subclsid, count): - if self.films_ids == (clsid, subclsid) and self.films_count == count: - return self.update_progress_bar () - return False - - def add_progress_callback (self, clsid, subclsid, count): - gobject.timeout_add (350, self.update_progress_bar_cb, - clsid, subclsid, count) - - def on_films_row_activated (self, treeview, path, column): - pass - - def on_films_starting_video (self, treeview, path, user_data): - """Display an error if the required GStreamer plugins aren't installed""" - if self.gstreamer_plugins_present == False: - self.totem.interface_error_with_link (_("Totem cannot play this type of media (%s) because you do not have the appropriate plugins to handle it.") % _("Ppstream"), - _("Please install the necessary plugins and restart Totem to be able to play this media."), - "http://www.gnome.org/projects/totem/#codecs", - _("More information about media plugins"), - self.totem.get_main_window ()) - return False - - return True - - def on_button_press_event (self, widget, event): - self.button_down = True - - def on_button_release_event (self, widget, event): - self.button_down = False - self.on_value_changed (self.vadjust[self.current_treeview_name]) - - def on_value_changed (self, adjustment): - """Load more results when we get near the bottom of the treeview""" - pass - - def clear_ui (self, what = None): - #window = self.vbox.window - #window.set_cursor (None) - self.progress_bar.set_fraction (0.0) - self.progress_bar.set_text ("") - self.clear_pages (what) - - def clear_pages (self, what = None): - if what is None: - what = self.notebook_pages - if not type(what) is list: - what = [what] - - for treeview_name in what: - treeview = self.builder.get_object ("pps_liststore_" + treeview_name) - treeview.clear () - - def refresh_categories (self): - self.fetch_classes (True) - - def refresh_films (self): - if self.films_ids != (-1, -1): - self.refetch_films () - - def set_show_posters (self, show = True): - self.cell_video_renderer.set_property ('use_placeholder', show) - if self.films_ids == (-1, -1): - return - clsid, subclsid = self.films_ids - self.films_ids = (-1, -1) - self.show_films (clsid, subclsid, False, False) - - def sync_config (self): - for key in self.input_configs.keys (): - keyname = gconf_key + '/' + key - value = self.input_configs [key] - if type (value) is bool: - self.gconf.set_bool (keyname, value) - elif type (value) is int: - self.gconf.set_int (keyname, value) - elif type (value) is float: - self.gconf.set_float (keyname, value) - else: - self.gconf.set_string (keyname, value) - if self.input_configs['show_posters'] == self.configs['show_posters']: - return - - self.configs['show_posters'] = self.input_configs['show_posters'] - self.set_show_posters (self.configs['show_posters']) - - def on_refresh_button_clicked (self, button): - search_terms = self.search_entry.get_text () - if self.current_treeview_name == 'categories': - self.refresh_categories () - elif self.current_treeview_name == 'films': - self.refresh_films () - - def on_search_entry_activated (self, entry): - search_terms = self.search_entry.get_text () - - def on_config_dialog_delete (self, dialog, event): - dialog.hide () - return True - - def on_config_dialog_close (self, dialog): - dialog.hide () - - def on_config_dialog_response (self, dialog, response_id): - dialog.hide () - if response_id == gtk.RESPONSE_DELETE_EVENT: - return - elif response_id == 1: - return - - self.sync_config () - - def on_show_posers_toggle (self, button): - self.input_configs['show_posters'] = button.get_active () - - def load_config (self): - ### current configs - self.configs = {} - self.configs['show_posters'] = self.gconf.get_bool ('%s/show_posters' % gconf_key) - - ### user input configs - self.input_configs = self.configs.copy () - - def setup_config_dialog (self): - dialog = self.config_builder.get_object ("pps_config_dialog") - dialog.connect ("response", self.on_config_dialog_response) - dialog.connect ("close", self.on_config_dialog_close) - dialog.connect ("delete-event", self.on_config_dialog_delete) - - show_posters = self.config_builder.get_object ("pps_config_show_posters") - show_posters.set_active (self.configs['show_posters']) - show_posters.connect ("toggled", self.on_show_posers_toggle) diff --git a/totem/plugin/ppstream.totem-plugin b/totem/plugin/ppstream.totem-plugin deleted file mode 100644 index 502bce2..0000000 --- a/totem/plugin/ppstream.totem-plugin +++ /dev/null @@ -1,11 +0,0 @@ -[Totem Plugin] -Loader=python -Module=ppstream -IAge=1 -Name=PPStream browser -Name[zh_CN]=PPStream 浏览器 -Description=A plugin to let you browse PPStream videos. -Description[zh_CN]=一个可以浏览 PPStream 视频的插件 -Authors=Luo Jinghua <sunmoon1997@gmail.com> -Copyright=Copyright © 2009 Luo Jinghua -Website=http://www.gnome.org/projects/totem/ diff --git a/totem/plugin/ppstream.ui b/totem/plugin/ppstream.ui deleted file mode 100644 index bdf9615..0000000 --- a/totem/plugin/ppstream.ui +++ /dev/null @@ -1,226 +0,0 @@ -<?xml version="1.0"?> -<interface> - <!-- interface-requires gtk+ 2.12 --> - <!-- interface-naming-policy project-wide --> - <object class="GtkTreeStore" id="pps_liststore_categories"> - <columns> - <!-- column-name Class --> - <column type="gint"/> - <!-- column-name Type --> - <column type="gint"/> - <!-- column-name Subclass --> - <column type="gint"/> - <!-- column-name Title --> - <column type="gchararray"/> - <!-- column-name Content --> - <column type="gint"/> - </columns> - </object> - <object class="GtkListStore" id="pps_liststore_files"> - <columns> - <!-- column-name No. --> - <column type="gchararray"/> - <!-- column-name Title --> - <column type="gchararray"/> - <!-- column-name MRL --> - <column type="gchararray"/> - </columns> - </object> - <object class="GtkListStore" id="pps_liststore_films"> - <columns> - <!-- column-name Poster --> - <column type="GdkPixbuf"/> - <!-- column-name Title --> - <column type="gchararray"/> - <!-- column-name MRL --> - <column type="gchararray"/> - <!-- column-name gchararray1 --> - <column type="gchararray"/> - <!-- column-name Class --> - <column type="gint"/> - <!-- column-name Subclass --> - <column type="gint"/> - </columns> - </object> - <object class="GtkVBox" id="pps_vbox"> - <property name="visible">True</property> - <property name="orientation">vertical</property> - <property name="spacing">4</property> - <child> - <object class="GtkHBox" id="hbox1"> - <property name="visible">True</property> - <child> - <object class="GtkEntry" id="pps_search_entry"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="invisible_char">●</property> - </object> - <packing> - <property name="padding">0</property> - <property name="expand">True</property> - <property name="fill">True</property> - <property name="position">0</property> - </packing> - </child> - <child> - <object class="GtkButton" id="pps_refresh_button"> - <property name="label">gtk-refresh</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">True</property> - <property name="use_stock">True</property> - </object> - <packing> - <property name="padding">0</property> - <property name="expand">False</property> - <property name="fill">True</property> - <property name="position">1</property> - </packing> - </child> - </object> - <packing> - <property name="expand">False</property> - <property name="position">0</property> - </packing> - </child> - <child> - <object class="GtkNotebook" id="pps_notebook"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <child> - <object class="GtkScrolledWindow" id="pps_scrolled_window_categories"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="hscrollbar_policy">automatic</property> - <property name="vscrollbar_policy">automatic</property> - <child> - <object class="GtkTreeView" id="pps_treeview_categories"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="headers-visible">False</property> - <property name="fixed-height-mode">False</property> - <property name="rules-hint">True</property> - <property name="model">pps_liststore_films</property> - </object> - </child> - </object> - </child> - <child type="tab"> - <object class="GtkLabel" id="label1"> - <property name="visible">True</property> - <property name="label" translatable="yes">Categories</property> - </object> - <packing> - <property name="tab_fill">False</property> - </packing> - </child> - <child> - <object class="GtkScrolledWindow" id="pps_scrolled_window_films"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="hscrollbar_policy">automatic</property> - <property name="vscrollbar_policy">automatic</property> - <child> - <object class="PPSVideoList" id="pps_treeview_films"> - <property name="headers-visible">False</property> - <property name="fixed-height-mode">False</property> - <property name="title-column">1</property> - <property name="tooltip-column">3</property> - <property name="mrl-column">2</property> - </object> - </child> - </object> - <packing> - <property name="position">1</property> - </packing> - </child> - <child type="tab"> - <object class="GtkLabel" id="label2"> - <property name="visible">True</property> - <property name="label" translatable="yes">Films</property> - </object> - <packing> - <property name="position">1</property> - <property name="tab_fill">False</property> - </packing> - </child> - <child> - <object class="GtkScrolledWindow" id="pps_scrolled_window_files"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="hscrollbar_policy">automatic</property> - <property name="vscrollbar_policy">automatic</property> - <child> - <object class="GtkTreeView" id="pps_treeview_files"> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="headers-visible">False</property> - <property name="fixed-height-mode">False</property> - <property name="rules-hint">True</property> - <property name="model">pps_liststore_categories</property> - </object> - </child> - </object> - <packing> - <property name="position">2</property> - </packing> - </child> - <child type="tab"> - <object class="GtkLabel" id="label3"> - <property name="visible">True</property> - <property name="label" translatable="yes">Files</property> - </object> - <packing> - <property name="position">2</property> - <property name="tab_fill">False</property> - </packing> - </child> - </object> - <packing> - <property name="position">1</property> - </packing> - </child> - <child> - <object class="GtkProgressBar" id="pps_progress_bar"> - <property name="visible">True</property> - </object> - <packing> - <property name="expand">False</property> - <property name="position">2</property> - </packing> - </child> - <child> - <object class="GtkHBox" id="hbox2"> - <property name="visible">True</property> - <child> - <object class="GtkButton" id="pps_add_to_favourites_button"> - <property name="label" translatable="yes">Add to favourites</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">True</property> - </object> - <packing> - <property name="position">0</property> - <property name="expand">False</property> - </packing> - </child> - <child> - <object class="GtkButton" id="pps_remove_from_favourites_button"> - <property name="label" translatable="yes">Remove from favourites</property> - <property name="visible">True</property> - <property name="can_focus">True</property> - <property name="receives_default">True</property> - </object> - <packing> - <property name="position">1</property> - <property name="expand">False</property> - </packing> - </child> - </object> - <packing> - <property name="expand">False</property> - <property name="position">3</property> - </packing> - </child> - </object> -</interface> diff --git a/wrapper/Makefile.am b/wrapper/Makefile.am index da2df68..3b3cfed 100644 --- a/wrapper/Makefile.am +++ b/wrapper/Makefile.am @@ -1,34 +1 @@ -lib_LTLIBRARIES = libppswrapper.la libppswrapper-preload.la -libexec_PROGRAMS = ppswrapper-server -libexec_SCRIPTS = ppswrapper-run-server -noinst_PROGRAMS = ppsdemo-simple ppsvod-list - -ppswrapper_server_SOURCES = ppswrapper-server.c ppswrapper-common.c ppswrapper-server-main.c -ppswrapper_server_CFLAGS = @WRAPPER_CFLAGS@ -pthread -ppswrapper_server_LDFLAGS = @WRAPPER_LDFLAGS@ -pthread - -libppswrapper_preload_la_SOURCES = ppswrapper-msgget.c -libppswrapper_preload_la_CFLAGS = @WRAPPER_PRELOAD_CFLAGS@ -pthread -libppswrapper_preload_la_LDFLAGS = @WRAPPER_PRELOAD_LDFLAGS@ -pthread - -libppswrapper_la_SOURCES = ppswrapper.c ppswrapper-client.c ppswrapper-common.c - -libppswrapper_la_CFLAGS = -pthread -fPIC -DLIBEXEC_DIR="\"$(libexecdir)\"" -libppswrapper_la_LIBADD = - -ppsdemo_simple_SOURDES = ppsdemo-simple.c -ppsdemo_simple_CFLAGS = -pthread -ppsdemo_simple_LDADD = libppswrapper.la -ppsdemo_simple_LDFLAGS = -pthread - -ppsvod_list_SOURDES = ppsvod-list.c -ppsvod_list_CFLAGS = -pthread -ppsvod_list_LDADD = libppswrapper.la -ppsvod_list_LDFLAGS = -pthread - -noinst_HEADERS = ppswrapper.h pps.h pps-fixed.h - -install-exec-hook: -# -chmod u-s $(DESTDIR)$(libexecdir)/ppswrapper-server - chmod a+x $(DESTDIR)$(libexecdir)/ppswrapper-run-server - +noinst_HEADERS = pps.h pps-fixed.h diff --git a/wrapper/ppsdemo-simple.c b/wrapper/ppsdemo-simple.c deleted file mode 100644 index df43861..0000000 --- a/wrapper/ppsdemo-simple.c +++ /dev/null @@ -1,276 +0,0 @@ -/* - * WARNING: Sample code from ppstream.com. This file is not a part of - * ppswrapper. - */ -#include <stdio.h> -#include <string.h> -#include <sys/time.h> -#include <time.h> -#include <unistd.h> -#include <stdlib.h> - -#include "pps-fixed.h" - -static int taskindex = 0; - -int pps_event_handler(int index, pps_event event) -{ - unsigned int wparam = event.wparam; - - switch (event.id) - { - case EPPS_SERVER_TIMEOUT: - printf("[pps_callback]: server connecting timeout.\n"); - if (taskindex >= 0) - { - ppsvod_stop_item(taskindex); - ppsvod_remove_item(taskindex); - } - break; - case EPPS_NO_SERVER: - printf("[pps_callback]: can not find server. \n"); - break; - case EPPS_SERVER_NO_RESPONSE: - printf("[pps_callback]: no response from server. \n"); - break; - case EPPS_SERVER_INVALID: - printf("[pps_callback]: server invalid. \n"); - break; - case EPPS_VERSION_LOW: - printf("[pps_callback]: client version too low. \n"); - break; - case EPPS_CLIENT_NAME: - printf("[pps_callback]: client name error.\n"); - break; - case EPPS_URL_INVALID: - printf("[pps_callback]: url not found.\n"); - break; - case EPPS_NO_SERVICE: - printf("[pps_callback]: service does not exist.\n"); - break; - case EPPS_OUT_OF_SERVICE: - printf("[pps_callback]: out of service area.\n"); - break; - case EPPS_MEDIA_INFO_ERROR: - printf("[pps_callback]: loading media info error.\n"); - break; - case EPPS_INDEX_INFO_ERROR: - printf("[pps_callback]: index info error.\n"); - break; - case EPPS_NO_INDEX: - printf("[pps_callback]: no index.\n"); - break; - case EPPS_NO_MEIDIA: - printf("[pps_callback]: no media found.\n"); - break; - case EPPS_MULTI_INSTANCE: - printf("[pps_callback]: multi instance running.\n"); - break; - case EPPS_PREPARE_MEDIA_INFO: - printf("[pps_callback]: preparing media info %d%%.\n", wparam); - break; - case EPPS_GETTING_MEDIA_INFO: - printf("[pps_callback]: requesting media info %d%%.\n", wparam); - break; - case EPPS_GETTING_INDEX_INFO: - printf("[pps_callback]: requesting index info %d%%.\n", wparam); - break; - case EPPS_PLAYING: - printf("[pps_callback]: playing.\n"); - break; - case EPPS_BUFFERING: - printf("[pps_callback]: buffering %d%%...\n", wparam); - break; - case EPPS_CONNECTING: - printf("[pps_callback]: connecting to server...\n"); - break; - case EPPS_MEDIA_READY: - printf("[pps_callback]: media is ready.\n"); - break; - case EPPS_PARSING_SERVER: - printf("[pps_callback]: parsing server info.\n"); - break; - case EPPS_GET_MEDIA_INFO: - printf("[pps_callback]: get media info successfully.\n"); - break; - case EPPS_BUFFERED: - printf("[pps_callback]: buffing done.\n"); - break; - case EPPS_PREPARE_MEDIA: - printf("[pps_callback]: prepare media.\n"); - break; - case EPPS_STORAGE_ERROR: - printf("[pps_callback]: access storage failed.\n"); - break; - case EPPS_DATA_TIMEOUT: - printf("[pps_callback]: Wait data timeout!\n"); - if (taskindex >= 0) - { - ppsvod_stop_item(taskindex); - ppsvod_remove_item(taskindex); - } - break; - case EPPS_UPDATING: - printf("[pps_callback]: updating %d%%...\n", wparam); - break; - case EPPS_UPDATE_OK: - printf("[pps_callback]: updating successed\n"); - break; - case EPPS_UPDATE_FAILED: - printf("[pps_callback]: updating failed\n"); - if (taskindex >= 0) - { - ppsvod_stop_item(taskindex); - ppsvod_remove_item(taskindex); - } - break; - case EPPS_QUIT: - printf("[pps_callback]: Quit progress!\n"); - if (taskindex >= 0) - { - ppsvod_stop_item(taskindex); - ppsvod_remove_item(taskindex); - } - break; - default: - printf("[pps_callback]: unknown event.\n"); - break; - } - return 0; -} - -int main(int argc, char** argv) -{ - FILE *fpout = NULL; - ppsvod_iteminfo *piteminfo; - int ret = 0; - int filepos = 0; - char outbuf[2048]; -// char outbuf[64 * 1024]; - struct timeval block_time; - unsigned long long pos = 0; - char* url; - - printf("------PPS Vod start...\n"); -//again: - taskindex = 0; - ret = 0; - filepos = 0; - - if (argc > 1) - url = argv[1]; - else - url = "pps://hwhna3gqea6y5udq2aqa.pps/%D6%D0%B9%FA%CA%BD%C0%EB%BB%E9-01.rmvb"; - - /* initialization */ - if (ppsvod_create("ppstream", "demo", "ppsdemo001", pps_event_handler) < 0) - { - printf("------PPS initialization failed!\n"); - return -1; - } - - if ((fpout = fopen("file.out", "w+")) == NULL) - { - printf("------Open output file failed\n"); - return -1; - } - - printf("------Adding task...\n"); - /* NOTE: This is just a demo url. - * In real world, you should get xml file from ppstream.com, - * retrieve urls from that xml file, then pass it to pps_add_task. - */ - if ((taskindex = ppsvod_add_item(url, 0)) < 0) - { - printf("------Add task failed!\n"); - fclose(fpout); - return -1; - } - printf("--file length: %d\n", ppsvod_get_file_length(taskindex)); - - printf("------Starting task...\n"); - if (ppsvod_play_item(taskindex, NULL) < 0) - { - printf("------Can not start task %d!\n", taskindex); - fclose(fpout); - ppsvod_remove_item(taskindex); - return -1; - } - - printf("--file length: %d\n", ppsvod_get_file_length(taskindex)); - printf("------Start to play...\n"); - while(1) - { - if ((piteminfo = ppsvod_get_item_info(taskindex)) == NULL) - { - printf("------Can not get task info!\n"); - } - else - { -#if 0 - printf("------[index %d]:Filelen:%lld,Downspeed:%d B/s, Upspeed:%d B/s,PeerCount:%d,Progress:%d%%\r", - piteminfo->index, - piteminfo->file_size, - piteminfo->down_speed, - piteminfo->up_speed, - piteminfo->peer_count, - piteminfo->progress); -#else - pos = (unsigned long long)filepos; - printf("------[index %d]:Filelen:%u,Downspeed:%d B/s, Upspeed:%d B/s,PeerCount:%d,Progress:%d%%\r", - piteminfo->index, - piteminfo->file_size, - piteminfo->down_speed, - piteminfo->up_speed, - piteminfo->peer_count, - (unsigned int)((pos * 100) / (unsigned long long)piteminfo->file_size)); -#endif - - - ppsvod_free_item_info(piteminfo); - } - - /* "block_time.tv_sec = 30" means pps_read_file() will read in block mode, - * if no data can be got in 30 seconds, pps_read_file() will return -1. - * Set block_time to NULL will cause pps_read_file() block endlessly util data is read. - */ - block_time.tv_sec = 40; - block_time.tv_usec = 0; -// printf("------pps reading....\n"); -// ret = ppsvod_read(taskindex, outbuf, 64 * 1024, filepos, &block_time); - ret = ppsvod_read(taskindex, outbuf, 2 * 1024, filepos, &block_time); - if (ret < 0) - { - perror("------pps read failed!\n"); - break; - } - else if(ret == 0) - { - printf("------pps read file done!\n"); - break; - } - -// printf("------pps read ok, len:%d....\n", ret); - /* write data to a temporary file */ - int wret = 0; - if ((wret = fwrite(outbuf, 1, ret, fpout)) < ret) - { - printf("------pps write file failed\n"); - } - - filepos += ret; - - usleep(20000); /* 20ms */ - } - printf("------pps read finished...\n"); - ppsvod_stop_item(taskindex); - printf("------pps removing task...\n"); - ppsvod_remove_item(taskindex); - fclose(fpout); - - printf("------pps uninitianlizing...\n"); - ppsvod_destroy(); - printf("Demo end, exiting...\n"); - - return 0; -} diff --git a/wrapper/ppsvod-list.c b/wrapper/ppsvod-list.c deleted file mode 100644 index bbf55e9..0000000 --- a/wrapper/ppsvod-list.c +++ /dev/null @@ -1,128 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <sys/time.h> -#include <time.h> -#include <unistd.h> -#include <stdlib.h> - -#include "pps-fixed.h" - -enum display_mask { - DISPLAY_CATEGORY = 1 << 0, - DISPLAY_SUBCATEGORY = 1 << 1, - DISPLAY_FILMS = 1 << 2, - DISPLAY_FILES = 1 << 3, - DISPLAY_ALL = 0xffff -}; - -static void dump_channel (ppscategory *cat, ppscategory *subcat, int mask) -{ - ppschannel* ch; - ppsitem* item; - - ch = pps_vodlist_getchannel(cat->id, subcat ? subcat->id : 0, - subcat ? subcat->subcatnum : cat->subcatnum, 1); - if (!ch) - return; - - for (; ch; ch = ch->next) - { - printf ("\t\tfilm name: %s\n", ch->name); - printf ("\t\tfilm director: %s\n", ch->director); - printf ("\t\tactor: %s\n", ch->actor); - printf ("\t\tarea: %s\n", ch->area); - printf ("\t\tpubtime: %s\n", ch->pubtime); - printf ("\t\tlang: %s\n", ch->lang); - printf ("\t\tdesc: %s\n", ch->desc); - - if (mask & DISPLAY_FILES) - { - for (item = ch->pitems; item; item = item->next) - { - printf ("\t\t\turl: %s\n", item->ppsurl); - printf ("\t\t\tvip_flag: %d\n", item->vipflag); - printf ("\t\t\tformat: %s\n", item->format); - printf ("\t\t\tsize: %dMB\n", item->size); - printf ("\t\t\tduration: %dM\n", item->duration); - printf ("\t\t\tbitrate: %d\n\n", item->bitrate); - } - } - } -} - -static void dump_category (ppscategory *cat, int mask) -{ - ppscategory *subcat; - - printf ("cat id: %d\n", cat->id); - printf ("cat name: %s\n", cat->name); - printf ("cat type: %d\n", cat->type); - printf ("subcat num: %d\n", cat->subcatnum); - - if (cat->type == 0) - { - if (mask & DISPLAY_SUBCATEGORY) - { - for (subcat = cat->psubcat; subcat; subcat = subcat->next) - { - printf ("\tsubcat id: %d\n", subcat->id); - printf ("\tsubcat name: %s\n", subcat->name); - printf ("\tsubcat type: %d\n", subcat->type); - printf ("\tsubcat num: %d\n", subcat->subcatnum); - - if (mask & DISPLAY_FILMS) - dump_channel (cat, subcat, mask); - - printf ("\n"); - } - } - } - else - { - if (mask & DISPLAY_FILMS) - dump_channel (cat, NULL, mask); - } -} - -static void dump_vod_list(int mask) -{ - ppscategory *cat, *cur, *subcur; - int catnum; - - cat = pps_vodlist_getcat(&catnum); - if (!cat) - { - printf ("----Failed to get categories.\n"); - return; - } - - if (mask & DISPLAY_CATEGORY) - { - for (cur = cat; cur; cur = cur->next) - dump_category (cur, mask); - } -} - -int main(int argc, char** argv) -{ - int ret = 0; - int mask = DISPLAY_ALL; - - if (argc > 1) - mask = atoi (argv[1]); - - printf ("--Intialize vod list--\n"); - ret = pps_vodlist_init(); - if (ret < 0) - { - printf ("--Failed to intialize vod list--\n"); - return -1; - } - - dump_vod_list(mask); - - printf("------pps vod list uninitianlizing...\n"); - pps_vodlist_uninit(); - - return 0; -} diff --git a/wrapper/ppswrapper-client.c b/wrapper/ppswrapper-client.c deleted file mode 100644 index 6026704..0000000 --- a/wrapper/ppswrapper-client.c +++ /dev/null @@ -1,1407 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <errno.h> - -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/wait.h> -#include <netinet/in.h> -#include <arpa/inet.h> - -#include "pps-fixed.h" -#include "ppswrapper.h" - -static void -ppsw_client_clear_bufs (ppsw_client_t *self); - -static int -ppsw_client_spawn_server(ppsw_client_t *self, int *fds, int port) -{ - self->pid = fork(); - if (self->pid == 0) { - char port_arg[10]; - char fd_arg[10]; - int ret; - int max; - int i; - - if (fds) - close (fds[0]); - - max = sysconf(_SC_OPEN_MAX); - for (i = 3; i < max; i++) - if (!fds || i != fds[1]) - close (i); - - snprintf (port_arg, sizeof(port_arg), "%d", port); - snprintf (fd_arg, sizeof(fd_arg), "%d", fds ? fds[1] : -1); - - printf ("Invoking server: %s %s %s %s %s\n", - LIBEXEC_DIR "/ppswrapper-run-server", - "-p", port_arg, "-f", fd_arg); - - ret = execl(LIBEXEC_DIR "/ppswrapper-run-server", LIBEXEC_DIR "/ppswrapper-run-server", - "-p", port_arg, "-f", fd_arg, (char *) NULL); - ret = execl("./ppswrapper-run-server", "./ppswrapper-run-server", - "-p", port_arg, "-f", fd_arg, (char *) NULL); - perror("execute server failed " LIBEXEC_DIR "/ppswrapper-run-server "); - } else if (self->pid < 0) { - return -1; - } - - sleep(1); - return 0; -} - -static int -ppsw_client_connect_server(ppsw_client_t *self, int port) -{ - struct sockaddr_in sockaddr; - struct linger linger; - - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sin_family = PF_INET; - sockaddr.sin_port = htons(port); - inet_aton("127.0.0.1", &sockaddr.sin_addr); - if (connect(self->base.sock, (const struct sockaddr*)&sockaddr, - sizeof(sockaddr)) < 0) - return -1; - - linger.l_onoff = 1; - linger.l_linger = 1; - setsockopt(self->base.sock, SOL_SOCKET, SO_LINGER, - &linger, sizeof (linger)); - return 0; -} - -static void -free_categories(ppscategory* cat); - -static void -ppsw_free_channel(ppschannel* ch); - -ppsw_t* -ppsw_client_create(int port) -{ - ppsw_client_t *self = malloc (sizeof (ppsw_client_t)); - int fds[2]; - - if (!self) - return NULL; - - if (port >= 0 && ppsw_client_spawn_server(self, NULL, port) < 0) { - free (self); - return NULL; - } - - self->base.seq = 0; - if (port >= 0) { - self->base.sock = socket (AF_INET, SOCK_STREAM, 0); - if (self->base.sock < 0) - goto create_sock_failed; - - if (ppsw_client_connect_server(self, port) < 0) - goto spawn_server_failed; - } else { - int ret; - - ret = socketpair (AF_UNIX, SOCK_STREAM, 0, fds); - if (ret < 0) { - perror ("Fail to a socket pair"); - goto create_sock_failed; - } - - if (ppsw_client_spawn_server (self, fds, -1) < 0) { - close (fds[0]); - close (fds[1]); - goto create_sock_failed; - } - self->base.sock = fds[0]; - close (fds[1]); - } - - ppsw_init (&self->base); - - self->buf = NULL; - self->categories = NULL; - self->channel = NULL; - self->callback = NULL; - self->configdir = NULL; - self->has_thread = 0; - self->evt_quit = 0; - self->error = 0; - - pthread_mutex_init (&self->mutex, NULL); - - return &self->base; - spawn_server_failed: - close(self->base.sock); - create_sock_failed: - free (self); - return NULL; -} - -static void* -ppsw_client_event_loop(void* data) -{ - ppsw_client_t *self = data; - - while (!self->evt_quit) { - pps_event event; - int index; - - usleep(20000); - - while (!self->evt_quit) { - int ret; - - ret = ppsw_client_send_get_event(&self->base, &index, &event); - - if (ret == 1 && self->callback) - self->callback(index, event); - if (ret == 0) { - break; - } else if (ret < 0) { - fprintf (stderr, "Couldn't get a event, quiting the event thread.\n"); - self->evt_quit = 1; - } - } - - } - return NULL; -} - -static void -ppsw_client_start_event_thread(ppsw_t* super) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - - self->evt_quit = 0; - if (pthread_create(&self->evt_thread, NULL, - ppsw_client_event_loop, self) == 0) - self->has_thread = 1; -} - -static void -ppsw_client_stop_event_thread(ppsw_t* super) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - - if (!self->has_thread) - return; - - self->evt_quit = 1; - pthread_join(self->evt_thread, NULL); - self->has_thread = 0; -} - -void -ppsw_client_destroy(ppsw_t* super) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - int status; - - if (!self) - return; - - ppsw_client_send_close (super); - close (self->base.sock); - - kill (self->pid, SIGTERM); - waitpid (self->pid, &status, 0); - free (self->configdir); - - ppsw_client_stop_event_thread (super); - pthread_mutex_destroy (&self->mutex); - - free_categories (self->categories); - ppsw_free_channel (self->channel); - - ppsw_client_clear_bufs (self); - ppsw_fini (&self->base); - free (self); -} - -static void -ppsw_client_clear_bufs (ppsw_client_t *self) -{ - ppsw_buf_t *buf, *next; - - for (buf = self->buf; buf; buf = next) { - next = buf->next; - ppsw_packet_fini (&buf->packet); - free (buf); - } -} - -static void -ppsw_packet_copy (ppsw_packet_t *dst, ppsw_packet_t *src) -{ - *dst = *src; - if (src->data == src->local) - dst->data = dst->local; -} - -static int -ppsw_client_check_local (ppsw_t *super, uint16_t seq, - ppsw_msg_t *retmsg, - ppsw_packet_t *retp) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_buf_t *buf, *prev = NULL; - - for (buf = self->buf; buf; buf = buf->next, prev = buf) { - if (buf->msg.seq == seq) { - if (!prev) - self->buf = buf->next; - else - prev->next = buf->next; - *retmsg = buf->msg; - ppsw_packet_copy (retp, &buf->packet); - free (buf); - - return 0; - } - } - - return -1; -} - -static int -ppsw_client_push_buf (ppsw_t *super, - ppsw_msg_t *msg, - ppsw_packet_t *packet) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_buf_t *buf = malloc (sizeof (*buf)); - ppsw_buf_t *prev = NULL; - - if (!buf) - return -1; - - buf->msg = *msg; - ppsw_packet_copy (&buf->packet, packet); - buf->next = NULL; - - if (!self->buf) { - self->buf = buf; - return 0; - } - - for (prev = self->buf; prev; prev = prev->next) { - if (!prev->next) { - prev->next = buf; - break; - } - } - - return 0; -} - -static int -ppsw_client_recv_packet (ppsw_t *super, uint16_t id, - ppsw_msg_t *retmsg, - ppsw_packet_t *retp) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsw_packet_t p; - uint16_t packet_id; - fd_set rfs; - fd_set efs; - int ret; - struct timeval tv; - - //printf ("wait for a message from server\n"); - pthread_mutex_lock (&self->mutex); - - for (;;) { - if (self->error) { - ret = -1; - break; - } - - if (!ppsw_client_check_local (super, id, retmsg, retp)) { - ret = 0; - break; - } - - FD_ZERO(&rfs); - FD_SET(self->base.sock, &rfs); - FD_ZERO(&efs); - FD_SET(self->base.sock, &efs); - - memset (&p, 0, sizeof(p)); - - tv.tv_sec = 0; - tv.tv_usec = 500000; - - ret = select(self->base.sock + 1, &rfs, NULL, &efs, &tv); - if (ret < 0 && errno == EINTR) - continue; - - if (FD_ISSET (self->base.sock, &efs)) { - self->error = 1; - ret = -1; - break; - } - - if (ret <= 0) - continue; - - if (!FD_ISSET (self->base.sock, &rfs)) - continue; - - if (ppsw_recv_packet (super, &msg, &p) < 0) { - self->error = 1; - ret = -1; - break; - } - - if (msg.seq == id) { - ret = 0; - *retmsg = msg; - ppsw_packet_copy (retp, &p); - break; - } - - /* others packet, push it in queue */ - if (ppsw_client_push_buf (super, &msg, &p) < 0) { - self->error = 1; - ret = -1; - break; - } - pthread_mutex_unlock (&self->mutex); - - /* ugly */ - usleep (1000); - if (self->error) { - ret = -1; - break; - } - - pthread_mutex_lock (&self->mutex); - } - - done: - pthread_mutex_unlock (&self->mutex); - return ret; -} - -int -ppsw_client_read_reply_msg(ppsw_t* super, uint16_t id, - ppsw_reply_msg_t* msg) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_packet_t p; - - if (ppsw_client_recv_packet(super, id, &msg->base, &p) < 0) - return -1; - if (ppsw_packet_readl(&p, &msg->retval) < 0) - return -1; - ppsw_packet_fini(&p); - return 0; -} - -int -ppsw_client_packet_recv(ppsw_t* super, uint16_t id, - ppsw_msg_t* msg, ppsw_packet_t *p) -{ - if (ppsw_client_recv_packet(super, id, msg, p) < 0) - return -1; - return 0; -} - -int ppsw_client_send_create(ppsw_t* super, - const char *oem_name, - const char *terminal_type, - const char *device_id, - pps_callback pcallback) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_CREATE, - ppsw_strlen(oem_name) + ppsw_strlen(terminal_type) + - ppsw_strlen(device_id) + 3 * 2) < 0) - return -1; - if (ppsw_packet_write_str(&p, oem_name, -1) < 0) - return -1; - if (ppsw_packet_write_str(&p, terminal_type, -1) < 0) - return -1; - if (ppsw_packet_write_str(&p, device_id, -1) < 0) - return -1; - - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - self->callback = pcallback; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - if (msg.retval == 0) - ppsw_client_start_event_thread(super); - - return msg.retval; -} - -int ppsw_client_send_destroy(ppsw_t* super) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - ppsw_client_stop_event_thread(super); - - if (ppsw_packet_init(&p, PPSW_DESTROY, 0)) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_close(ppsw_t* super) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_CLOSE, 0)) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_add_item(ppsw_t* super, - const char* url, - int flags) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_ADD_ITEM, - ppsw_strlen(url) + 2 + 4) < 0) - return -1; - if (ppsw_packet_write_str(&p, url, -1) < 0) - return -1; - if (ppsw_packet_writel(&p, flags) < 0) - return -1; - - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_remove_item(ppsw_t* super, - int fd) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_REMOVE_ITEM, 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_play_item(ppsw_t* super, - int fd, - const char* valid_code) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_PLAY_ITEM, - ppsw_strlen(valid_code) + 2 + 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_packet_write_str(&p, valid_code, -1) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_stop_item(ppsw_t* super, - int fd) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_STOP_ITEM, 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_get_file_length(ppsw_t* super, - int fd) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_FILE_LENGTH, 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_seek(ppsw_t* super, - int fd, unsigned int position) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_SEEK, 8) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_packet_writel(&p, position) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -ppsvod_iteminfo *ppsw_client_send_get_item_info(ppsw_t* super, - int fd) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsvod_iteminfo * info; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_ITEM_INFO, 4) < 0) - return NULL; - if (ppsw_packet_writel(&p, fd) < 0) - return NULL; - if (ppsw_send_packet(super, &msg, &p) < 0) - return NULL; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return NULL; - if (msg.id != PPSW_REPLY_ITEM_INFO || msg.len == 0) - goto fail; - - info = malloc(sizeof(ppsvod_iteminfo)); - if (!info) - goto fail; - - if (ppsw_read_item_info(&p, info) < 0) { - fprintf (stderr, "read item info failed\n"); - free (info); - return NULL; - } - - ppsw_packet_fini(&p); - return info; - fail: - ppsw_packet_fini(&p); - return NULL; -} - -int ppsw_client_send_read(ppsw_t* super, int fd, char *buffer, - unsigned int length, unsigned int position, - struct timeval *wait_time) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - uint32_t retval; - int ret; - int error; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_READ, 5 * 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_packet_writel(&p, length) < 0) - return -1; - if (ppsw_packet_writel(&p, position) < 0) - return -1; - - if (ppsw_packet_writel(&p, wait_time ? wait_time->tv_sec : 0xffffffff) < 0) - return -1; - if (ppsw_packet_writel(&p, wait_time ? wait_time->tv_usec : 0xffffffff) < 0) - return -1; - - if (ppsw_send_packet(super, &msg, &p) < 0) - return -1; - - ret = -1; - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) { - perror ("Couldn't receive reply packet"); - return -1; - } - if (msg.id != PPSW_REPLY_READ) - goto done; - - if (ppsw_packet_readl(&p, &retval) < 0) { - fprintf (stderr, "Couldn't read return value.\n"); - return -1; - } - ret = retval; - if (ppsw_packet_readl(&p, &retval) < 0) { - fprintf (stderr, "Couldn't read errno value.\n"); - return -1; - } - error = retval; - - if (ret > 0 && ppsw_packet_read(&p, (uint8_t*)buffer, ret) < 0) { - return -1; - } else if (ret < 0) { - if (error == PPSW_EAGAIN) - errno = EAGAIN; - } - - done: - ppsw_packet_fini(&p); - return ret; -} - -int ppsw_client_send_drop_read(ppsw_t* super, - int fd) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_DROP_READ, 4) < 0) - return -1; - if (ppsw_packet_writel(&p, fd) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - return msg.retval; -} - -int ppsw_client_send_get_event(ppsw_t* super, - int* index, pps_event* event) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - uint32_t val; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_EVENT, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg, &p) < 0) - return -1; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) { - perror("Couldn't receive reply packet"); - return -1; - } - - if (msg.id != PPSW_REPLY_EVENT) { - perror("bad event type, expect REPLY_EVENT"); - ppsw_packet_fini(&p); - return -1; - } - - if (msg.len == 0) { - ppsw_packet_fini(&p); - return 0; - } - - if (ppsw_packet_readl(&p, &val) < 0) - return -1; - *index = val; - - if (ppsw_packet_readl(&p, &val) < 0) - return -1; - event->id = val; - - if (ppsw_packet_readl(&p, &val) < 0) - return -1; - event->wparam = val; - - if (ppsw_packet_readl(&p, &val) < 0) - return -1; - event->lparam = val; - - if (ppsw_packet_read(&p, (uint8_t*)event->extra, PPS_EXTRA_MSG_LEN) < 0) - return -1; - - ppsw_packet_fini(&p); - - return 1; -} - -int ppsw_client_send_get_time(ppsw_t* super, - struct timeval* tv) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - uint32_t sec; - uint32_t usec; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_TIME, 4) < 0) - return -1; - if (ppsw_send_packet(super, &msg, &p) < 0) - return -1; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return -1; - if (msg.id != PPSW_REPLY_TIME) { - ppsw_packet_fini(&p); - return -1; - } - - if (ppsw_packet_readl(&p, &sec) < 0) - return -1; - if (ppsw_packet_readl(&p, &usec) < 0) - return -1; - - tv->tv_sec = sec; - tv->tv_usec = usec; - - ppsw_packet_fini(&p); - - return 0; -} - -char* ppsw_client_send_get_config_dir(ppsw_t* super) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_CONFIG_DIR, 0) < 0) - return NULL; - if (ppsw_send_packet(super, &msg, &p) < 0) - return NULL; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return NULL; - if (msg.id != PPSW_REPLY_DIR) { - ppsw_packet_fini(&p); - return NULL; - } - - free (self->configdir); - self->configdir = NULL; - - if (ppsw_packet_read_str(&p, &self->configdir, NULL) < 0) - return NULL; - - ppsw_packet_fini(&p); - - return self->configdir; -} - -int ppsw_client_send_default_config_dir(ppsw_t* super) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_DEFAULT_CONFIG_DIR, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_save_config_dir(ppsw_t* super, - const char* path) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_SAVE_CONFIG_DIR, - ppsw_strlen(path) + 2) < 0) - return -1; - if (ppsw_packet_write_str(&p, path, -1) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_vodlist_init(ppsw_t* super) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_VODLIST_INIT, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_vodlist_uninit(ppsw_t* super) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_VODLIST_UNINIT, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -static void -free_categories(ppscategory* cat) -{ - if (!cat) - return; - if (cat->psubcat) - free_categories(cat->psubcat); - if (cat->next) - free_categories(cat->next); - free (cat->name); - free (cat); -} - -static ppscategory* -ppsw_read_single_category(ppsw_packet_t* p, - int* hassubcat) -{ - ppscategory* cat; - uint32_t val; - - cat = malloc(sizeof(ppscategory)); - if (!cat) { - ppsw_packet_fini(p); - return NULL; - } - - if (ppsw_packet_readl(p, &val) < 0) - goto fail; - cat->id = val; - if (ppsw_packet_read_str(p, &cat->name, NULL) < 0) - goto read_name_fail; - if (ppsw_packet_readl(p, &val) < 0) - goto read_name_fail; - cat->type = val; - if (ppsw_packet_readl(p, &val) < 0) - goto read_name_fail; - cat->subcatnum = val; - if (ppsw_packet_readl(p, &val) < 0) - goto read_name_fail; - *hassubcat = val; - cat->psubcat = NULL; - cat->next = NULL; - return cat; - read_name_fail: - free (cat->name); - fail: - free (cat); - return NULL; -} - -static ppscategory* -ppsw_read_category(ppsw_packet_t* p, int catnum) -{ - ppscategory* cat, *head, *last; - int i; - - head = last = NULL; - for (i = 0; i < catnum; i++) { - int hassubcat = 0; - cat = ppsw_read_single_category(p, &hassubcat); - if (!cat) { - free_categories(head); - return NULL; - } - if (cat->subcatnum && hassubcat) { - cat->psubcat = ppsw_read_category(p, cat->subcatnum); - if (!cat->psubcat) { - free_categories(cat); - free_categories(head); - return NULL; - } - } - if (!head) - head = cat; - else if (last) - last->next = cat; - last = cat; - } - - return head; -} - -ppscategory* ppsw_client_send_vodlist_getcat(ppsw_t* super, int *catnum) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsw_packet_t p; - uint32_t val; - ppscategory* cat; - - if (ppsw_packet_init(&p, PPSW_VODLIST_GET_CAT, 0) < 0) - return NULL; - if (ppsw_send_packet(super, &msg, &p) < 0) - return NULL; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return NULL; - - if (msg.id != PPSW_REPLY_GET_CAT) { - ppsw_packet_fini(&p); - return NULL; - } - - if (ppsw_packet_readl(&p, &val) < 0) - return NULL; - *catnum = val; - - cat = NULL; - if (val) { - cat = ppsw_read_category(&p, val); - if (!cat) - return NULL; - } - - free_categories(self->categories); - self->categories = cat; - - ppsw_packet_fini(&p); - return cat; -} - -static void -ppsw_free_items(ppsitem* item) -{ - if (!item) - return; - - ppsw_free_items(item->next); - free(item->ppsurl); - free(item->format); - free(item); -} - -static ppsitem* -ppsw_read_a_item(ppsw_packet_t* p) -{ - ppsitem* item; - uint32_t val; - - item = malloc(sizeof(*item)); - item->next = NULL; - if (!item) { - ppsw_packet_fini(p); - return NULL; - } - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - item->id = val; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - item->index = val; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - item->size = val; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - item->duration = val; - - if (ppsw_packet_read_str(p, &item->format, NULL) < 0) - goto error; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - item->bitrate = val; - - if (ppsw_packet_read_str(p, &item->ppsurl, NULL) < 0) - goto read_url_error; - - if (ppsw_packet_readl(p, &val) < 0) - goto read_vip_flag; - item->vipflag = val; - - return item; - read_vip_flag: - free(item->ppsurl); - read_url_error: - free(item->format); - error: - free (item); - return NULL; -} - -static ppsitem* -ppsw_read_items(ppsw_packet_t* p, int itemnum) -{ - ppsitem* item, *head, *last; - - last = head = NULL; - for (; itemnum; itemnum--) { - item = ppsw_read_a_item(p); - if (!item) { - ppsw_packet_fini(p); - ppsw_free_items(head); - return NULL; - } - - if (!head) - head = item; - else if (last) - last->next = item; - last = item; - } - - return item; -} - -static void -ppsw_free_channel(ppschannel* ch) -{ - if (!ch) - return; - - ppsw_free_items(ch->pitems); - ppsw_free_channel(ch->next); - free(ch->name); - free(ch->director); - free(ch->actor); - free(ch->area); - free(ch->pubtime); - free(ch->lang); - free(ch->desc); - free(ch->BImgUrl); - free(ch->SImgUrl); - free(ch); -} - -static ppschannel* -ppsw_read_a_channel(ppsw_packet_t* p) -{ - ppschannel* ch; - uint32_t val; - - ch = calloc(sizeof(*ch), 1); - if (!ch) { - ppsw_packet_fini(p); - return NULL; - - } - if (ppsw_packet_read_str(p, &ch->name, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->director, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->actor, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->area, NULL) < 0) - goto error; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - ch->size = val; - - if (ppsw_packet_read_str(p, &ch->pubtime, NULL) < 0) - goto error; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - ch->duration = val; - - if (ppsw_packet_read_str(p, &ch->lang, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->desc, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->BImgUrl, NULL) < 0) - goto error; - if (ppsw_packet_read_str(p, &ch->SImgUrl, NULL) < 0) - goto error; - - if (ppsw_packet_readl(p, &val) < 0) - goto error; - ch->itemnum = val; - - if (ch->itemnum) { - ch->pitems = ppsw_read_items(p, ch->itemnum); - if (!ch->pitems) - goto error; - } - return ch; - error: - ppsw_free_channel(ch); - return NULL; -} - -static ppschannel* -ppsw_read_channel(ppsw_packet_t* p, int chnum) -{ - ppschannel* ch, *head, *last; - int i; - - head = last = NULL; - for (i = 0; i < chnum; i++) { - ch = ppsw_read_a_channel(p); - if (!ch) { - ppsw_free_channel(head); - return NULL; - } - if (!head) - head = ch; - else if (last) - last->next = ch; - last = ch; - } - - return head; -} - -ppschannel* ppsw_client_send_vodlist_getchannel(ppsw_t* super, int catid, int subcatid, - int page_size, int page_num) -{ - ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsw_packet_t p; - ppschannel* ch; - uint32_t val; - - if (ppsw_packet_init(&p, PPSW_VODLIST_GET_CHANNEL, 4 * 4) < 0) - return NULL; - if (ppsw_packet_writel(&p, catid) < 0) - return NULL; - if (ppsw_packet_writel(&p, subcatid) < 0) - return NULL; - if (ppsw_packet_writel(&p, page_size) < 0) - return NULL; - if (ppsw_packet_writel(&p, page_num) < 0) - return NULL; - if (ppsw_send_packet(super, &msg, &p) < 0) - return NULL; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return NULL; - - if (msg.id != PPSW_REPLY_GET_CHANNEL) { - ppsw_packet_fini(&p); - return NULL; - } - - if (ppsw_packet_readl(&p, &val) < 0) - return NULL; - - ch = NULL; - if (val) { - ch = ppsw_read_channel(&p, val); - if (!ch) - return NULL; - } - - ppsw_free_channel(self->channel); - self->channel = ch; - - ppsw_packet_fini(&p); - return ch; -} - -int ppsw_client_send_get_account_type(ppsw_t* super) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_GET_ACCOUNT_TYPE, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_get_vip_expire_time(ppsw_t* super, char* time) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_msg_t msg; - ppsw_packet_t p; - char* expire_time; - uint32_t retval; - - if (ppsw_packet_init(&p, PPSW_GET_VIP_EXPIRE_TIME, 0) < 0) - return -1; - if (ppsw_send_packet(super, &msg, &p) < 0) - return -1; - - if (ppsw_client_recv_packet(super, msg.seq, &msg, &p) < 0) - return -1; - - if (msg.id != PPSW_REPLY_EXPIRE_TIME || msg.len > 1023) { - ppsw_packet_fini(&p); - return -1; - } - - if (ppsw_packet_readl(&p, &retval) < 0) - return -1; - if (ppsw_packet_read_str(&p, &expire_time, NULL) < 0) - return -1; - - strcpy (time, expire_time); - free (expire_time); - - ppsw_packet_fini (&p); - return retval; -} - -int ppsw_client_send_charge(ppsw_t* super, const char* password) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_CHARGE, - ppsw_strlen(password) + 2) < 0) - return -1; - if (ppsw_packet_write_str(&p, password, -1) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} - -int ppsw_client_send_check_url(ppsw_t* super, const char* url) -{ - //ppsw_client_t *self = (ppsw_client_t*)super; - ppsw_reply_msg_t msg; - ppsw_packet_t p; - - if (ppsw_packet_init(&p, PPSW_CHECK_URL, - ppsw_strlen(url) + 2) < 0) - return -1; - if (ppsw_packet_write_str(&p, url, -1) < 0) - return -1; - if (ppsw_send_packet(super, &msg.base, &p) < 0) - return -1; - - if (ppsw_client_read_reply_msg(super, msg.base.seq, &msg) < 0) - return -1; - if (msg.base.id != PPSW_REPLY) - return -1; - - return msg.retval; -} diff --git a/wrapper/ppswrapper-common.c b/wrapper/ppswrapper-common.c deleted file mode 100644 index 5609303..0000000 --- a/wrapper/ppswrapper-common.c +++ /dev/null @@ -1,769 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#include <assert.h> -#include <stdlib.h> -#include <stdio.h> -#include <ctype.h> -#include <string.h> -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> - -#include "pps-fixed.h" -#include "ppswrapper.h" - -static ssize_t -safe_read(int fildes, void *buf, size_t nbytes) -{ - ssize_t ret; - int error; - size_t left = nbytes; - - do { - ret = read(fildes, (char*)buf + nbytes - left, left); - if (ret > 0 && ret < left) { - //printf ("partial read: %zd left %zd\n", ret, left); - left -= ret; - ret = -1; - errno = EINTR; - } else if (ret == left) { - left = 0; - break; - } else if (ret == 0) { - error = errno; - //printf ("read zero bytes left: %zd\n", left); - errno = error; - } - } while (ret < 0 && (errno == EINTR || errno == EAGAIN)); - - if (ret < 0) { - error = errno; - fprintf (stderr, "read error: errno %d(%s)\n", error, strerror(errno)); - errno = error; - } - - if (ret < 0) - return ret; - return nbytes - left; -} - -static ssize_t -safe_write(int fildes, const void *buf, size_t nbytes) -{ - ssize_t ret; - size_t left = nbytes; - int error; - - do { - ret = write(fildes, (const char*)buf + nbytes - left, left); - if (ret > 0 && ret < left) { - //printf ("partial write: %zd left %zd\n", ret, left); - left -= ret; - ret = -1; - errno = EINTR; - } else if (ret == left) { - left = 0; - break; - } else if (ret == 0) { - error = errno; - //printf ("write zero bytes left: %zd\n", left); - errno = error; - } - } while (ret < 0 && (errno == EINTR || errno == EAGAIN || errno == EINPROGRESS)); - - if (ret < 0) { - error = errno; - fprintf (stderr, "write error: errno: %d(%s)\n", error, strerror(errno)); - errno = error; - } - - if (ret < 0) - return ret; - return nbytes - left; -} - -int -ppsw_read(ppsw_t* self, - uint8_t* retval, - size_t len) -{ - int ret; - - ret = safe_read(self->sock, retval, len); - if (ret != len) - return -1; - - return ret; -} - -int -ppsw_write(ppsw_t* self, - const uint8_t* retval, - size_t len) -{ - int ret; - - ret = safe_write(self->sock, retval, len); - if (ret != len) - return -1; - return ret; -} - -int -ppsw_readl(ppsw_t* self, - uint32_t* retval) -{ - uint8_t val[4]; - int ret; - - ret = ppsw_read(self, val, 4); - if (ret != 4) - return -1; - - *retval = - val[0] << 24 | val[1] << 16 | - val[2] << 8 | val[3]; - - return ret; -} - -int -ppsw_writel(ppsw_t* self, - uint32_t retval) -{ - uint8_t val[4]; - int ret; - - val[0] = (retval >> 24) & 0xff; - val[1] = (retval >> 16) & 0xff; - val[2] = (retval >> 8) & 0xff; - val[3] = (retval >> 0) & 0xff; - - ret = ppsw_write(self, val, 4); - if (ret != 4) - return -1; - return ret; -} - -int -ppsw_reads(ppsw_t* self, - uint16_t* retval) -{ - uint8_t val[2]; - int ret; - - ret = ppsw_read(self, val, 2); - if (ret != 2) - return -1; - - *retval = val[0] << 8 | val[1]; - - return ret; -} - -int -ppsw_writes(ppsw_t* self, - uint16_t retval) -{ - uint8_t val[2]; - int ret; - - val[0] = (retval >> 8) & 0xff; - val[1] = (retval >> 0) & 0xff; - - ret = ppsw_write(self, val, 2); - if (ret != 2) - return -1; - return ret; -} - -int -ppsw_readb(ppsw_t* self, - uint8_t* retval) -{ - uint8_t val; - int ret; - - ret = ppsw_read(self, &val, 1); - if (ret != 1) { - perror("read error"); - return -1; - } - - *retval = val; - return ret; -} - -int -ppsw_writeb(ppsw_t* self, - uint8_t retval) -{ - int ret; - - ret = ppsw_write(self, &retval, 1); - if (ret != 1) - return -1; - return ret; -} - -int -ppsw_read_str(ppsw_t* self, - char** retval, - uint16_t* retlen) -{ - int ret; - uint16_t len; - char* val; - - ret = ppsw_reads(self, &len); - if (ret < 0) - return -1; - - if (len == 0) { - *retval = NULL; - return 2; - } - - val = malloc(len); - if (!val) - return -1; - - ret = ppsw_read(self, (uint8_t*)val, len); - if (ret != len) { - free (val); - return -1; - } - - *retval = val; - if (retlen) - *retlen = len; - return 2 + len; -} - -int -ppsw_write_str(ppsw_t* self, - const char* retval, - int len) -{ - int ret; - - if (len < 0) - len = retval ? strlen (retval) + 1 : 0; - if (ppsw_writes (self, len) != 2) - return -1; - if (!len) - return 2; - ret = ppsw_write (self, (const uint8_t*)retval, len); - if (ret != len) - return -1; - return 2 + len; -} - - -int -ppsw_read_msg(ppsw_t* self, - ppsw_msg_t* msg) -{ - if (ppsw_readb(self, &msg->sync) < 0) { - perror("read message sync error"); - return -1; - } - if (msg->sync != PPSW_SYNC) { - fprintf(stderr, "%d unexpect sync: %d exepected: %d\n", - getpid(), msg->sync, PPSW_SYNC); - //__asm__ ("int $3"); - return -1; - } - if (ppsw_reads(self, &msg->id) < 0) { - perror("read message id error"); - return -1; - } - if (ppsw_reads(self, &msg->seq) < 0) { - perror("read message seqno error"); - return -1; - } - if (ppsw_readl(self, &msg->len) < 0) { - perror("read message length error"); - return -1; - } - - return 0; -} - -int -ppsw_read_reply_msg(ppsw_t* self, - ppsw_reply_msg_t* msg) -{ - ppsw_packet_t p; - - if (ppsw_recv_packet(self, &msg->base, &p) < 0) - return -1; - if (ppsw_packet_readl(&p, &msg->retval) < 0) - return -1; - ppsw_packet_fini(&p); - return 0; -} - -int -ppsw_send_reply_msg(ppsw_t* self, - ppsw_reply_msg_t* msg) -{ - ppsw_packet_t p; - - if (ppsw_packet_init(&p, msg->base.id, 4) < 0) - return -1; - if (ppsw_packet_writel(&p, msg->retval) < 0) - return -1; - if (ppsw_reply_packet(self, msg->base.seq, &p) < 0) - return -1; - return 0; -} - -size_t -ppsw_strlen(const char* s) -{ - if (!s) - return 0; - return strlen(s) + 1; -} - -int -ppsw_write_item_info(ppsw_packet_t* self, - ppsvod_iteminfo* info) -{ - if (ppsw_packet_writel(self, info->index) < 0) - return -1; - if (ppsw_packet_write_str(self, info->hash_id, HASH_VALUE_LENGTH) < 0) - return -1; - if (ppsw_packet_write_str(self, info->file_name, -1) < 0) - return -1; - if (ppsw_packet_writel(self, info->file_size) < 0) - return -1; - if (ppsw_packet_writel(self, info->progress) < 0) - return -1; - if (ppsw_packet_writel(self, info->down_speed) < 0) - return -1; - if (ppsw_packet_writel(self, info->up_speed) < 0) - return -1; - if (ppsw_packet_writel(self, info->peer_count) < 0) - return -1; - if (ppsw_packet_writel(self, info->status) < 0) - return -1; - return 0; -} - -int -ppsw_read_item_info(ppsw_packet_t* self, - ppsvod_iteminfo* info) -{ - char *hash_id; - uint32_t val; - - if (ppsw_packet_readl(self, &val) < 0) - return -1; - info->index = val; - - if (ppsw_packet_read_str(self, &hash_id, NULL) < 0) - return -1; - memcpy(info->hash_id, hash_id, sizeof(info->hash_id)); - free (hash_id); - - if (ppsw_packet_read_str(self, &info->file_name, NULL) < 0) - goto failed; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->file_size = val; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->progress = val; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->down_speed = val; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->up_speed = val; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->peer_count = val; - - if (ppsw_packet_readl(self, &val) < 0) - goto failed; - info->status = val; - return 0; - failed: - free (info->file_name); - return -1; -} - -int -ppsw_packet_readl(ppsw_packet_t* self, - uint32_t* retval) -{ - uint8_t val[4]; - int ret; - - ret = ppsw_packet_read(self, val, 4); - if (ret != 4) { - ppsw_packet_fini(self); - return -1; - } - - *retval = - val[0] << 24 | val[1] << 16 | - val[2] << 8 | val[3]; - - return 4; -} - -int -ppsw_packet_writel(ppsw_packet_t* self, - uint32_t retval) -{ - uint8_t val[4]; - int ret; - - val[0] = (retval >> 24) & 0xff; - val[1] = (retval >> 16) & 0xff; - val[2] = (retval >> 8) & 0xff; - val[3] = (retval >> 0) & 0xff; - - ret = ppsw_packet_write(self, val, 4); - if (ret < 0) { - ppsw_packet_fini(self); - return -1; - } - return ret; -} - -int -ppsw_packet_reads(ppsw_packet_t* self, - uint16_t* retval) -{ - uint8_t val[2]; - int ret; - - ret = ppsw_packet_read(self, val, 2); - if (ret != 2) { - ppsw_packet_fini(self); - return -1; - } - - *retval = val[0] << 8 | val[1]; - return 2; -} - -int -ppsw_packet_writes(ppsw_packet_t* self, - uint16_t retval) -{ - uint8_t val[2]; - int ret; - - val[0] = (retval >> 8) & 0xff; - val[1] = (retval >> 0) & 0xff; - - ret = ppsw_packet_write(self, val, 2); - if (ret < 0) { - ppsw_packet_fini(self); - return -1; - } - return 2; -} - -int -ppsw_packet_readb(ppsw_packet_t* self, - uint8_t* retval) -{ - uint8_t val; - int ret; - - ret = ppsw_packet_read(self, &val, 1); - if (ret != 1) { - perror("read error"); - ppsw_packet_fini(self); - errno = ENOSPC; - return -1; - } - - *retval = val; - return ret; -} - -int -ppsw_packet_writeb(ppsw_packet_t* self, - uint8_t retval) -{ - int ret; - - ret = ppsw_packet_write(self, &retval, 1); - if (ret < 0) { - ppsw_packet_fini(self); - return -1; - } - return 1; -} - -int -ppsw_packet_read(ppsw_packet_t* self, - uint8_t* retval, - uint32_t len) -{ - if (self->len - self->offset < len) { - ppsw_packet_fini(self); - errno = ENOSPC; - return -1; - } - - memcpy(retval, &self->data[self->offset], len); - self->offset += len; - return len; -} - -int -ppsw_packet_write(ppsw_packet_t* self, - const uint8_t* val, - uint32_t len) -{ - if (self->len - self->offset < len) { - ppsw_packet_fini(self); - return -1; - } - - memcpy(&self->data[self->offset], val, len); - self->offset += len; - return len; -} - -int -ppsw_packet_read_str(ppsw_packet_t* self, - char** retval, - uint16_t* retlen) -{ - int ret; - uint16_t len; - char* val; - - ret = ppsw_packet_reads(self, &len); - if (ret < 0) - return -1; - - if (len == 0) { - *retval = NULL; - return 2; - } - - val = malloc(len); - if (!val) { - ppsw_packet_fini(self); - return -1; - } - - ret = ppsw_packet_read(self, (uint8_t*)val, len); - if (ret != len) { - free (val); - return -1; - } - - *retval = val; - if (retlen) - *retlen = len; - return 2 + len; -} - -int -ppsw_packet_write_str(ppsw_packet_t* self, - const char* retval, - int len) -{ - int ret; - - if (len < 0) - len = retval ? strlen (retval) + 1 : 0; - assert (len < 0xffff); - if (ppsw_packet_writes (self, len) < 0) - return -1; - if (!len) - return 2; - ret = ppsw_packet_write (self, (const uint8_t*)retval, len); - if (ret < 0) - return -1; - return 2 + len; -} - -int -ppsw_packet_init(ppsw_packet_t* p, - uint16_t id, - uint32_t len) -{ - if (len > 0xffffff) - return -1; - - p->offset = 0; - p->len = PPSW_MSG_HEADER_LEN + len; - if (PPSW_MSG_HEADER_LEN + len < 64) - p->data = p->local; - else - p->data = malloc(PPSW_MSG_HEADER_LEN + len); - if (!p->data) - return -1; - - ppsw_packet_writeb(p, PPSW_SYNC); - ppsw_packet_writes(p, id); - /* skip seqno */ - p->offset += 2; - ppsw_packet_writel(p, len); - return 0; -} - -void -ppsw_packet_fini(ppsw_packet_t* p) -{ - if (p->data != p->local) - free (p->data); - p->data = NULL; -} - -int -ppsw_packet_reinit(ppsw_packet_t* p, - uint16_t id, - uint32_t len) -{ - ppsw_packet_fini(p); - return ppsw_packet_init(p, id, len); -} - -int -ppsw_send_packet(ppsw_t* self, - ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - - pthread_mutex_lock (&self->packet_mutex); - - p->offset = 1 + 2; - if (msg) - msg->seq = self->seq; - ppsw_packet_writes(p, self->seq++); - - ret = ppsw_write(self, p->data, p->len); - -#if 0 - fprintf (stderr, "%d: send packet: id %d seqno %d -> %d\n", - getpid(), p->data[1] << 8 | p->data[2], self->seq - 1, self->seq); -#endif - - pthread_mutex_unlock (&self->packet_mutex); - - ppsw_packet_fini(p); - - return ret; -} - -int -ppsw_reply_packet(ppsw_t* self, - uint16_t seq, - ppsw_packet_t* p) -{ - int ret; - - pthread_mutex_lock (&self->packet_mutex); - - p->offset = 1 + 2; - ppsw_packet_writes(p, seq); - - ret = ppsw_write(self, p->data, p->len); - -#if 0 - fprintf (stderr, "%d: reply packet: id %d seqno %d\n", - getpid(), p->data[1] << 8 | p->data[2], seq); -#endif - - pthread_mutex_unlock (&self->packet_mutex); - - ppsw_packet_fini(p); - - return ret; -} - -int -ppsw_recv_packet(ppsw_t* self, - ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - - pthread_mutex_lock (&self->packet_mutex); - - - if (ppsw_read_msg(self, msg) < 0) { - ret = -1; - goto done; - } - - if (ppsw_packet_init(p, msg->id, msg->len) < 0) { - int i; - uint8_t val; - - perror("failed to initialize packet"); - for (i = 0; i < msg->len; i++) - if (ppsw_read(self, &val, 1) < 0) - break; - - ret = -1; - goto done; - } - - p->offset = 1 + 2; - ppsw_packet_writes(p, msg->seq); - p->offset = PPSW_MSG_HEADER_LEN; - - ret = ppsw_read(self, &p->data[p->offset], msg->len); - if (ret < 0) { - perror("failed to recv packet content"); - //__asm__ ("int $3"); - ppsw_packet_fini(p); - ret = -1; - goto done; - } - -#if 0 - fprintf (stderr, "%d: recv packet: id %d seqno %d\n", - getpid(), msg->id, msg->seq); -#endif - - done: - pthread_mutex_unlock (&self->packet_mutex); - - return ret; -} - -void -ppsw_init(ppsw_t* self) -{ - pthread_mutex_init (&self->packet_mutex, NULL); - self->seq = 0; -} - -void -ppsw_fini(ppsw_t* self) -{ - pthread_mutex_destroy (&self->packet_mutex); -} diff --git a/wrapper/ppswrapper-msgget.c b/wrapper/ppswrapper-msgget.c deleted file mode 100644 index ecf975b..0000000 --- a/wrapper/ppswrapper-msgget.c +++ /dev/null @@ -1,34 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#define _GNU_SOURCE -#include <dlfcn.h> - -#include <sys/types.h> -#include <sys/ipc.h> -#include <sys/msg.h> - -#include <stdio.h> - -int msgget (key_t key, int msgflg) -{ - static int (*old_msgget)(key_t, int); - - if (!old_msgget) - old_msgget = dlsym (RTLD_NEXT, "msgget"); - - //printf ("msgget(0x%x, 0x%x)\n", key, msgflg); - //printf ("changed to msgget(0x%x, 0x%x)\n", key, msgflg | 0600); - return old_msgget (key, msgflg | 0600); -} diff --git a/wrapper/ppswrapper-run-server.in b/wrapper/ppswrapper-run-server.in deleted file mode 100755 index cb17b79..0000000 --- a/wrapper/ppswrapper-run-server.in +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -prefix=@prefix@ -exec_prefix=@exec_prefix@ -libdir=@libdir@ -libexecdir=@libexecdir@ -LD_PRELOAD=${libdir}/libppswrapper-preload.so ${LD_PRELOAD:+:$LD_PRELOAD} exec ${libexecdir}/ppswrapper-server "$@" diff --git a/wrapper/ppswrapper-server-main.c b/wrapper/ppswrapper-server-main.c deleted file mode 100644 index b7c984b..0000000 --- a/wrapper/ppswrapper-server-main.c +++ /dev/null @@ -1,69 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#include <sys/time.h> -#include <stdio.h> -#include <string.h> -#include <signal.h> -#include <stdlib.h> -#include <unistd.h> - -#include "pps-fixed.h" -#include "ppswrapper.h" - -int main(int argc, char **argv) -{ - ppsw_t* ppsw; - int ret; - int c; - int fd = -1; - int port = -1; - int infinit = 0; - extern char *optarg; - extern int optind, optopt; - - while ((c = getopt (argc, argv, "f:p:d")) != -1) { - switch(c) { - case 'f': - fd = atoi (optarg); - break; - case 'p': - port = atoi (optarg); - break; - case 'd': - infinit = 1; - default: - break; - } - } - - if (fd < 0 && port < 0) { - fprintf(stderr, "expect a port or a fd\n"); - return -1; - } - - signal (SIGPIPE, SIG_IGN); - ret = chdir ("/tmp/"); - - ppsw = ppsw_server_create(fd, port); - if (!ppsw) { - fprintf(stderr, "Create server failed\n"); - return -1; - } - - ppsw_server_run(ppsw, infinit); - ppsw_server_destroy(ppsw); - - return 0; -} diff --git a/wrapper/ppswrapper-server.c b/wrapper/ppswrapper-server.c deleted file mode 100644 index 444ead4..0000000 --- a/wrapper/ppswrapper-server.c +++ /dev/null @@ -1,1282 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#include <stdlib.h> -#include <string.h> -#include <ctype.h> -#include <errno.h> -#include <unistd.h> - -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/time.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> - -#include "pps-fixed.h" -#include "ppswrapper.h" - -static int -ppsw_server_push_play_request (ppsw_server_t *self, - uint16_t seq, - int fd, const char *valid_code); - -static ppsw_server_t* server = NULL; - -ppsw_t* -ppsw_server_create(int fd, int port) -{ - ppsw_server_t *self; - - if (server) - return NULL; - - printf ("Creating new server: pid %d fd %d port %d \n", - getpid (), fd, port); - - self = malloc (sizeof (ppsw_server_t)); - if (!self) { - if (fd >= 0) - close (fd); - return NULL; - } - - self->base.seq = 0; - if (fd < 0) { - self->srv_sock = socket (AF_INET, SOCK_STREAM, 0); - if (self->srv_sock < 0) { - perror("Create server socket failed"); - goto create_sock_failed; - } - - if (ppsw_server_listen (&self->base, port) < 0) - goto listen_failed; - } else { - self->srv_sock = -1; - self->base.sock = fd; - } - - ppsw_init (&self->base); - pthread_mutex_init (&self->mutex, NULL); - pthread_mutex_init (&self->bufmutex, NULL); - pthread_mutex_init (&self->play_queue_mutex, NULL); - - self->bufs = NULL; - self->play_reqs = NULL; - server = self; - - return &self->base; - listen_failed: - close (self->srv_sock); - create_sock_failed: - free (self); - return NULL; -} - -void -ppsw_server_destroy(ppsw_t* super) -{ - ppsw_server_t* self = (ppsw_server_t*)super; - - if (!self) - return; - - if (self->srv_sock >= 0) - close (self->srv_sock); - close (self->base.sock); - pthread_mutex_destroy (&self->mutex); - pthread_mutex_destroy (&self->bufmutex); - pthread_mutex_destroy (&self->play_queue_mutex); - - ppsw_fini (&self->base); - free (self); - - server = NULL; -} - -int -ppsw_server_listen(ppsw_t* super, - int port) -{ - ppsw_server_t* self = (ppsw_server_t*)super; - struct sockaddr_in sockaddr; - unsigned long op = 1; - - setsockopt(self->srv_sock, SOL_SOCKET, SO_REUSEADDR, - (const void *)&op, sizeof(unsigned long)); - - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sin_family = PF_INET; - sockaddr.sin_port = htons(port); - inet_aton ("127.0.0.1", &sockaddr.sin_addr); - - if (bind (self->srv_sock, (const struct sockaddr*)&sockaddr, - sizeof(sockaddr)) < 0) { - perror ("bind server socket failed"); - return -1; - } - if (listen (self->srv_sock, 1) < 0) { - perror ("listen server socket failed"); - return -1; - } - return 0; -} - -static ppsw_event_buf_t* -ppsw_server_get_event_buf(ppsw_server_t* self, - int index) -{ - ppsw_event_buf_t* buf; - - for (buf = self->bufs; buf; buf = buf->next) { - if (buf->index == index) - return buf; - } - - buf = malloc(sizeof(ppsw_event_buf_t)); - if (!buf) - return NULL; - - buf->wr_pos = 0; - buf->rd_pos = 0; - buf->index = index; - buf->next = NULL; - - if (!self->bufs) { - self->bufs = buf; - } else { - buf->next = self->bufs; - self->bufs = buf; - } - return buf; -} - -static void -ppsw_server_remove_event_buf(ppsw_server_t* self, - int index) -{ - ppsw_event_buf_t* buf, *prev = NULL; - - for (buf = self->bufs; buf; buf = buf->next) { - if (buf->index == index) - break; - } - - if (!buf) - return; - - if (prev) - prev->next = buf->next; - else - self->bufs = buf->next; - free (buf); -} - -static void -ppsw_server_push_event(ppsw_server_t* self, - int index, pps_event* event) -{ - ppsw_event_buf_t* buf; - - pthread_mutex_lock(&server->bufmutex); - - buf = ppsw_server_get_event_buf(self, index); - if (buf && buf->rd_pos != (buf->wr_pos + 1) % (PPSW_EVTBUF_SZ + 1)) { - buf->events[buf->wr_pos++] = *event; - buf->wr_pos %= (PPSW_EVTBUF_SZ + 1); - } - - pthread_mutex_unlock(&server->bufmutex); -} - -static int -ppsw_server_pop_event(ppsw_server_t* self, - int* index, pps_event* event) -{ - int ret = 0; - ppsw_event_buf_t* buf; - - pthread_mutex_lock(&server->bufmutex); - - for (buf = self->bufs; buf; buf = buf->next) { - if (buf->rd_pos != buf->wr_pos) { - *index = buf->index; - *event = buf->events[buf->rd_pos++]; - buf->rd_pos %= (PPSW_EVTBUF_SZ + 1); - ret = 1; - - //printf ("poped event: %d-%d\n", *index, event->id); - break; - } - } - - pthread_mutex_unlock(&server->bufmutex); - - return ret; -} - -static int -ppsw_server_clear_event_bufs(ppsw_server_t* self) -{ - ppsw_event_buf_t* buf, *next; - - for (buf = self->bufs; buf; buf = next) { - next = buf->next; - free (buf); - } - - self->bufs = NULL; -} - -static int -ppsw_callback(int index, pps_event event) -{ - if (server) - ppsw_server_push_event(server, index, &event); - - return 0; -} - -static int -send_reply(ppsw_server_t* self, - uint16_t id, - int retval) -{ - ppsw_reply_msg_t msg; - - msg.base.id = PPSW_REPLY; - msg.retval = retval; - msg.base.seq = id; - - return ppsw_send_reply_msg(&self->base, &msg); -} - -static int -send_item_info_reply(ppsw_server_t* self, - uint16_t id, - ppsvod_iteminfo * info) -{ - int len; - int ret; - ppsw_packet_t p; - - if (info) - len = ppsw_strlen(info->file_name) + 7 * 4 + HASH_VALUE_LENGTH + 2 * 2; - else - len = 0; - - if (ppsw_packet_init(&p, PPSW_REPLY_ITEM_INFO, len) < 0) - return -1; - - ret = ppsw_write_item_info(&p, info); - ppsvod_free_item_info(info); - - return ppsw_reply_packet(&self->base, id, &p); -} - -static int -send_dir_reply(ppsw_server_t* self, - uint16_t id, - char* dir) -{ - int len; - ppsw_packet_t p; - - if (dir) - len = ppsw_strlen(dir) + 2; - else - len = 0; - - if (ppsw_packet_init(&p, PPSW_REPLY_DIR, len) < 0) - return -1; - - if (dir && ppsw_packet_write_str(&p, dir, -1) < 0) - return -1; - - return ppsw_reply_packet(&self->base, id, &p); -} - -static int -send_read_reply(ppsw_server_t* self, - uint16_t id, - char* buffer, int length, - int retval, int error) -{ - int len; - int ret = -1; - ppsw_packet_t p; - - //printf ("read length: %d\n", length); - - if (buffer && retval > 0) - len = length + 4 * 2; - else - len = 4 * 2; - - if (ppsw_packet_init(&p, PPSW_REPLY_READ, len) < 0) - return -1; - - if (ppsw_packet_writel(&p, retval) < 0) - goto done; - - if (ppsw_packet_writel(&p, error) < 0) - goto done; - - if (buffer && retval > 0) - ret = ppsw_packet_write(&p, (uint8_t*)buffer, length); - else - ret = 0; - - if (ret >= 0) - ret = ppsw_reply_packet(&self->base, id, &p); - - done: - free (buffer); - return ret; -} - -static int -send_event_reply(ppsw_server_t* self, uint16_t id, - int index, pps_event* event) -{ - int len; - ppsw_packet_t p; - - //printf ("reply event: index %d id: %d\n", index, event ? event->id : -1); - - if (event) - len = 4 + 3 * 4 + PPS_EXTRA_MSG_LEN; - else - len = 0; - - if (ppsw_packet_init(&p, PPSW_REPLY_EVENT, len) < 0) - return -1; - - if (event) { - if (ppsw_packet_writel(&p, index) < 0) - return -1; - if (ppsw_packet_writel(&p, event->id) < 0) - return -1; - if (ppsw_packet_writel(&p, event->wparam) < 0) - return -1; - if (ppsw_packet_writel(&p, event->lparam) < 0) - return -1; - if (ppsw_packet_write(&p, (uint8_t*)event->extra, PPS_EXTRA_MSG_LEN) < 0) - return -1; - } - - return ppsw_reply_packet(&self->base, id, &p); -} - -static size_t -ppsw_cal_single_category_size(ppscategory* cat) -{ - return 4 + 2 + ppsw_strlen(cat->name) + 4 + 4 + 4; -} - -static size_t -ppsw_cal_category_size(ppscategory* cat, int catnum) -{ - int i; - size_t size = 0; - - for (i = 0; i < catnum; i++, cat = cat->next) { - size += ppsw_cal_single_category_size(cat); - if (cat->subcatnum && cat->psubcat) - size += ppsw_cal_category_size(cat->psubcat, cat->subcatnum); - } - return size; -} - -static int -ppsw_write_single_category(ppsw_packet_t* p, ppscategory* cat) -{ - if (ppsw_packet_writel(p, cat->id) < 0) - return -1; - if (ppsw_packet_write_str(p, cat->name, -1) < 0) - return -1; - if (ppsw_packet_writel(p, cat->type) < 0) - return -1; - if (ppsw_packet_writel(p, cat->subcatnum) < 0) - return -1; - if (ppsw_packet_writel(p, cat->psubcat ? 1 : 0) < 0) - return -1; - return 0; -} - -static int -ppsw_write_categories(ppsw_packet_t* p, ppscategory* cat, int catnum) -{ - int i; - - for (i = 0; i < catnum; i++, cat = cat->next) { - if (ppsw_write_single_category(p, cat) < 0) - return -1; - if (cat->subcatnum && cat->psubcat && - ppsw_write_categories(p, cat->psubcat, - cat->subcatnum) < 0) - return -1; - } - - return 0; -} - -static int -send_cat_reply(ppsw_server_t* self, - uint16_t id, - ppscategory* cat, - int catnum) -{ - ppsw_packet_t p; - size_t len; - - if (catnum && cat) - len = 4 + ppsw_cal_category_size(cat, catnum); - else - len = 4; - - if (ppsw_packet_init(&p, PPSW_REPLY_GET_CAT, len) < 0) - return -1; - - if (ppsw_packet_writel(&p, catnum) < 0) - return -1; - if (cat && catnum && ppsw_write_categories(&p, cat, catnum) < 0) - return -1; - return ppsw_reply_packet(&self->base, id, &p); -} - -static size_t -ppsw_cal_item_size(ppsitem* item) -{ - size_t size = 0; - - for (; item; item = item->next) - size += - 6 * 4 + 2 + ppsw_strlen(item->format) + - 2 + ppsw_strlen(item->ppsurl); - - return size; -} - -static size_t -ppsw_cal_channel_size(ppschannel* ch) -{ - size_t size = 0; - - for (; ch; ch = ch->next) { - size += 2 * 9 + 3 * 4; - size += ppsw_strlen(ch->name); - size += ppsw_strlen(ch->director); - size += ppsw_strlen(ch->actor); - size += ppsw_strlen(ch->area); - size += ppsw_strlen(ch->pubtime); - size += ppsw_strlen(ch->lang); - size += ppsw_strlen(ch->desc); - size += ppsw_strlen(ch->BImgUrl); - size += ppsw_strlen(ch->SImgUrl); - - if (ch->pitems && ch->itemnum) - size += ppsw_cal_item_size(ch->pitems); - } - return size; -} - -static int -ppsw_write_items(ppsw_packet_t* p, ppsitem* item) -{ - for (; item; item = item->next) { - if (ppsw_packet_writel(p, item->id) < 0) - return -1; - if (ppsw_packet_writel(p, item->index) < 0) - return -1; - if (ppsw_packet_writel(p, item->size) < 0) - return -1; - if (ppsw_packet_writel(p, item->duration) < 0) - return -1; - if (ppsw_packet_write_str(p, item->format, -1) < 0) - return -1; - if (ppsw_packet_writel(p, item->bitrate) < 0) - return -1; - if (ppsw_packet_write_str(p, item->ppsurl, -1) < 0) - return -1; - if (ppsw_packet_writel(p, item->vipflag) < 0) - return -1; - } - - return 0; -} - -static int -ppsw_write_channel(ppsw_packet_t* p, ppschannel* ch) -{ - for (; ch; ch = ch->next) { - if (ppsw_packet_write_str(p, ch->name, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->director, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->actor, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->area, -1) < 0) - return -1; - if (ppsw_packet_writel(p, ch->size) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->pubtime, -1) < 0) - return -1; - if (ppsw_packet_writel(p, ch->duration) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->lang, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->desc, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->BImgUrl, -1) < 0) - return -1; - if (ppsw_packet_write_str(p, ch->SImgUrl, -1) < 0) - return -1; - if (ppsw_packet_writel(p, ch->itemnum) < 0) - return -1; - if (ch->itemnum && ch->pitems && ppsw_write_items(p, ch->pitems) < 0) - return -1; - } - - return 0; -} - -static int -send_channel_reply(ppsw_server_t* self, - uint16_t id, - ppschannel* ch) -{ - int len; - int numch; - ppsw_packet_t p; - ppschannel* tmp = ch; - - if (ch) - len = 4 + ppsw_cal_channel_size(ch); - else - len = 4; - - if (ppsw_packet_init(&p, PPSW_REPLY_GET_CHANNEL, len) < 0) - return -1; - - for (numch = 0; tmp; tmp = tmp->next) - numch++; - if (ppsw_packet_writel(&p, numch) < 0) - return -1; - - printf ("ch:%p numch: %d size: %d\n", ch, numch, len); - if (ch && ppsw_write_channel(&p, ch) < 0) - return -1; - - printf ("here\n"); - return ppsw_reply_packet(&self->base, id, &p); -} - -static int -send_expire_time_reply(ppsw_server_t* self, uint16_t id, - char* buffer, int retval) -{ - int len; - ppsw_packet_t p; - - //printf ("read length: %d\n", length); - - if (retval == 0) - len = 4 + ppsw_strlen(buffer); - else - len = 4; - - if (ppsw_packet_init(&p, PPSW_REPLY_EXPIRE_TIME, len) < 0) - return -1; - - if (ppsw_packet_writel(&p, retval) < 0) - return -1; - - if (retval == 0 && ppsw_packet_write_str(&p, buffer, -1) < 0) - return -1; - - return ppsw_reply_packet(&self->base, id, &p); -} - -static int -process_create(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - char *oem_name; - char *terminal_type; - char *device_id; - int ret; - - if (ppsw_packet_read_str(p, &oem_name, NULL) < 0) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_read_str(p, &terminal_type, NULL) < 0) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_read_str(p, &device_id, NULL) < 0) - return send_reply(self, msg->seq, -1); - - printf ("ppsvod_create(%s, %s, %s)\n", oem_name, terminal_type, device_id); - ret = ppsvod_create(oem_name, terminal_type, device_id, ppsw_callback); - - free (oem_name); - free (terminal_type); - free (device_id); - return send_reply(self, msg->seq, ret); -} - -static int -process_destroy(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - - printf ("pps_vode_destroy\n"); - ret = ppsvod_destroy(); - return send_reply(self, msg->seq, ret); -} - -static int -process_close(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - printf ("close connection\n"); - return send_reply(self, msg->seq, 0); -} - -static int -process_add_item(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - char *url; - uint32_t flags; - int ret; - - if (ppsw_packet_read_str(p, &url, NULL) < 0) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &flags) < 0) - return send_reply(self, msg->seq, -1); - - printf ("add_item(%s, %d)\n", url, flags); - ret = ppsvod_add_item(url, flags); - - free (url); - return send_reply(self, msg->seq, ret); -} - -static int -process_remove_item(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - uint32_t fd; - - if (msg->len != 4) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - - printf ("remove_item(%d)\n", fd); - - ret = ppsvod_remove_item(fd); - - ppsw_server_remove_event_buf(self, fd); - - printf ("result: %d\n", ret); - return send_reply(self, msg->seq, ret); -} - -static int -process_play_item(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - char *valid_code; - uint32_t fd; - int ret; - - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_read_str(p, &valid_code, NULL) < 0) - return send_reply(self, msg->seq, -1); - - printf ("play_item(%d, %s)\n", fd, valid_code); - ret = ppsw_server_push_play_request (self, msg->seq, fd, valid_code); - free (valid_code); - return ret; -} - -static int -process_stop_item(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - uint32_t fd; - - if (msg->len != 4) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - - printf ("stop_item(%d)\n", fd); - - ret = ppsvod_stop_item(fd); - return send_reply(self, msg->seq, ret); -} - -static int -process_read(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - int error; - uint32_t fd; - uint32_t length; - uint32_t position; - uint32_t sec; - uint32_t usec; - struct timeval tv; - char* buffer; - - if (ppsw_packet_readl(p, &fd) < 0) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - if (ppsw_packet_readl(p, &length) < 0) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - if (ppsw_packet_readl(p, &position) < 0) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - if (ppsw_packet_readl(p, &sec) < 0) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - if (ppsw_packet_readl(p, &usec) < 0) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - - buffer = malloc(length); - if (!buffer) - return send_read_reply(self, msg->seq, NULL, 0, -1, 0); - - tv.tv_sec = sec; - tv.tv_usec = usec; - - //printf ("ppsvod_read(%d, %d, %d, %d.%d)\n", fd, length, position, sec, usec); - ret = ppsvod_read(fd, buffer, length, position, &tv); - error = errno; - if (ret < 0 && errno != EAGAIN) - perror ("Failed to read data"); - //printf ("read result: %d\n", ret); - return send_read_reply(self, msg->seq, buffer, length, ret, error); -} - -static int -process_drop_read(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - uint32_t fd; - - if (msg->len != 4) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - - printf ("drop_read(%d)\n", fd); - - ppsvod_drop_read(fd); - return send_reply(self, msg->seq, 0); -} - -static int -process_seek(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - uint32_t fd; - uint32_t pos; - - if (msg->len != 8) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &pos) < 0) - return send_reply(self, msg->seq, -1); - - //printf ("seek_item(%d, %d)\n", fd, pos); - - ret = ppsvod_seek(fd, pos); - return send_reply(self, msg->seq, ret); -} - -static int -process_get_file_length(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - uint32_t fd; - - if (msg->len != 4) - return send_reply(self, msg->seq, -1); - if (ppsw_packet_readl(p, &fd) < 0) - return send_reply(self, msg->seq, -1); - - ret = ppsvod_get_file_length(fd); - printf ("length: %d\n", ret); - return send_reply(self, msg->seq, ret); -} - -static int -process_get_item_info(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - uint32_t fd; - ppsvod_iteminfo * info; - - if (msg->len != 4) - return send_item_info_reply(self, msg->seq, NULL); - if (ppsw_packet_readl(p, &fd) < 0) - return send_item_info_reply(self, msg->seq, NULL); - - //printf ("get_item_info(%d)\n", fd); - - info = ppsvod_get_item_info(fd); - - return send_item_info_reply (self, msg->seq, info); -} - -static int -process_get_event(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int index; - pps_event event; - - //printf ("get_event()\n"); - - if (!ppsw_server_pop_event(self, &index, &event)) - return send_event_reply (self, msg->seq, 0, NULL); - return send_event_reply (self, msg->seq, index, &event); -} - -static int -process_save_config_dir(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - char* dir; - - if (ppsw_packet_read_str(p, &dir, NULL) < 0) - return send_reply(self, msg->seq, -1); - ret = ppsvod_save_config_dir(dir); - return send_reply (self, msg->seq, ret); -} - -static int -process_get_config_dir(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - char* dir; - - dir = ppsvod_get_config_dir(); - return send_dir_reply (self, msg->seq, dir); -} - -static int -process_default_config_dir(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - ret = ppsvod_default_config_dir(); - return send_reply (self, msg->seq, ret); -} - -static int -process_vod_list_init(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - ret = pps_vodlist_init(); - return send_reply (self, msg->seq, ret); -} - -static int -process_vod_list_uninit(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - ret = pps_vodlist_uninit(); - return send_reply (self, msg->seq, ret); -} - -static int -process_vod_list_get_cat(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int catnum = 0; - ppscategory* cat; - - cat = pps_vodlist_getcat(&catnum); - if (!cat) - perror ("Couldn't get category list"); - return send_cat_reply (self, msg->seq, cat, catnum); -} - -static int -process_get_account_type(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - - ret = ppsvod_get_account_type (); - return send_reply (self, msg->seq, ret); -} - -static int -process_get_vip_expire_time(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - char buffer[1024]; - - ret = ppsvod_get_vip_expire_time(buffer); - return send_expire_time_reply (self, msg->seq, buffer, ret); -} - -static int -process_charge (ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - char* password; - - if (ppsw_packet_read_str (p, &password, NULL) < 0) - return send_reply (self, msg->seq, -1); - - ret = ppsvod_charge (password); - free (password); - return send_reply (self, msg->seq, ret); -} - -static int -process_check_url (ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - int ret; - char* url; - - if (ppsw_packet_read_str (p, &url, NULL) < 0) - return send_reply (self, msg->seq, -1); - - ret = ppsvod_check_url (url); - free (url); - return send_reply (self, msg->seq, ret); -} - -static int -process_vod_list_get_channel(ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ - uint32_t catid; - uint32_t subcatid; - uint32_t page_size; - uint32_t page_num; - ppschannel* ch; - - if (ppsw_packet_readl(p, &catid) < 0) - return send_channel_reply (self, msg->seq, NULL); - if (ppsw_packet_readl(p, &subcatid) < 0) - return send_channel_reply (self, msg->seq, NULL); - if (ppsw_packet_readl(p, &page_size) < 0) - return send_channel_reply (self, msg->seq, NULL); - if (ppsw_packet_readl(p, &page_num) < 0) - return send_channel_reply (self, msg->seq, NULL); - ch = pps_vodlist_getchannel(catid, subcatid, page_size, page_num); - return send_channel_reply (self, msg->seq, ch); -} - -static int -ppsw_server_process_msg (ppsw_server_t* self, - const ppsw_msg_t* msg, - ppsw_packet_t* p) -{ -#if 0 - printf ("processing msg: id = %d len = %d seqno %d\n", - msg->id, msg->len, msg->seq); -#endif - - if (msg->id == PPSW_CREATE) - return process_create(self, msg, p); - else if (msg->id == PPSW_DESTROY) - return process_destroy(self, msg, p); - else if (msg->id == PPSW_CLOSE) - return process_close(self, msg, p); - else if (msg->id == PPSW_ADD_ITEM) - return process_add_item(self, msg, p); - else if (msg->id == PPSW_REMOVE_ITEM) - return process_remove_item(self, msg, p); - else if (msg->id == PPSW_PLAY_ITEM) - return process_play_item(self, msg, p); - else if (msg->id == PPSW_STOP_ITEM) - return process_stop_item(self, msg, p); - else if (msg->id == PPSW_READ) - return process_read(self, msg, p); - else if (msg->id == PPSW_DROP_READ) - return process_drop_read(self, msg, p); - else if (msg->id == PPSW_SEEK) - return process_seek(self, msg, p); - else if (msg->id == PPSW_GET_FILE_LENGTH) - return process_get_file_length(self, msg, p); - else if (msg->id == PPSW_GET_ITEM_INFO) - return process_get_item_info(self, msg, p); - else if (msg->id == PPSW_SAVE_CONFIG_DIR) - return process_save_config_dir(self, msg, p); - else if (msg->id == PPSW_GET_CONFIG_DIR) - return process_get_config_dir(self, msg, p); - else if (msg->id == PPSW_DEFAULT_CONFIG_DIR) - return process_default_config_dir(self, msg, p); - else if (msg->id == PPSW_GET_EVENT) - return process_get_event(self, msg, p); - else if (msg->id == PPSW_VODLIST_INIT) - return process_vod_list_init(self, msg, p); - else if (msg->id == PPSW_VODLIST_UNINIT) - return process_vod_list_uninit(self, msg, p); - else if (msg->id == PPSW_VODLIST_GET_CHANNEL) - return process_vod_list_get_channel(self, msg, p); - else if (msg->id == PPSW_VODLIST_GET_CAT) - return process_vod_list_get_cat(self, msg, p); - else if (msg->id == PPSW_GET_ACCOUNT_TYPE) - return process_get_account_type(self, msg, p); - else if (msg->id == PPSW_GET_VIP_EXPIRE_TIME) - return process_get_vip_expire_time(self, msg, p); - else if (msg->id == PPSW_CHARGE) - return process_charge(self, msg, p); - else if (msg->id == PPSW_CHECK_URL) - return process_check_url(self, msg, p); - return -1; -} - -static void -ppsw_play_request_free (ppsw_play_req_t *req) -{ - free (req->valid_code); - free (req); -} - -static int -ppsw_server_push_play_request (ppsw_server_t *self, - uint16_t seq, - int fd, const char *valid_code) -{ - ppsw_play_req_t *req, *cur; - - req = malloc (sizeof (ppsw_play_req_t)); - if (!req) - return -1; - - req->next = NULL; - req->fd = fd; - req->seq = seq; - if (valid_code) { - req->valid_code = malloc (strlen (valid_code) + 1); - if (!req->valid_code) { - free (req); - return -1; - } - } else { - req->valid_code = NULL; - } - - pthread_mutex_lock (&self->play_queue_mutex); - - if (!self->play_reqs) { - self->play_reqs = req; - } else { - for (cur = self->play_reqs; cur->next; cur = cur->next) - ; - cur->next = req; - } - pthread_mutex_unlock (&self->play_queue_mutex); - - return 0; -} - -static ppsw_play_req_t* -ppsw_server_pop_play_request (ppsw_server_t *self) -{ - ppsw_play_req_t *req; - - pthread_mutex_lock (&self->play_queue_mutex); - - req = self->play_reqs; - if (req) - self->play_reqs = req->next; - - pthread_mutex_unlock (&self->play_queue_mutex); - - return req; -} - -static void -ppsw_server_clear_play_requests (ppsw_server_t *self) -{ - ppsw_play_req_t *req; - - for (req = ppsw_server_pop_play_request (self); req; - req = ppsw_server_pop_play_request (self)) - ppsw_play_request_free (req); -} - -static void* -ppsw_server_play_loop (void *data) -{ - ppsw_server_t* self = data; - - while (!self->play_quit) { - ppsw_play_req_t *req; - int ret; - - req = ppsw_server_pop_play_request (self); - if (!req) { - usleep (50000); - continue; - } - - ret = ppsvod_play_item (req->fd, req->valid_code); - send_reply (self, req->seq, ret); - - ppsw_play_request_free (req); - } - - return 0; -} - -void -ppsw_server_run(ppsw_t* super, int infinit) -{ - ppsw_server_t* self = (ppsw_server_t*)super; - - self->done = 0; - while (!self->done) { - struct linger linger; - pthread_t play_thread; - int done = 0; - - if (self->srv_sock >= 0) { - printf ("wait for a client to come up\n"); - self->base.sock = accept(self->srv_sock, NULL, NULL); - if (self->base.sock < 0) - return; - - printf ("new client fd: %d\n", self->base.sock); - - linger.l_onoff = 1; - linger.l_linger = 1; - setsockopt(self->base.sock, SOL_SOCKET, SO_LINGER, - &linger, sizeof (linger)); - } - - self->play_quit = 0; - pthread_create (&play_thread, NULL, ppsw_server_play_loop, self); - - while (!done) { - ppsw_msg_t msg; - ppsw_packet_t p; - fd_set rfs; - fd_set efs; - int ret; - struct timeval tv; - - pthread_mutex_lock(&self->mutex); - - //printf ("wait for a message from client\n"); - - FD_ZERO(&rfs); - FD_SET(self->base.sock, &rfs); - FD_ZERO(&efs); - FD_SET(self->base.sock, &efs); - - memset(&p, 0, sizeof(p)); - - tv.tv_sec = 5; - tv.tv_usec = 0; - ret = select(self->base.sock + 1, &rfs, NULL, &efs, &tv); - if (ret < 0) { - if (errno != EINTR) - done = 1; - perror("select error"); - goto unlock; - } - - if (FD_ISSET(self->base.sock, &efs)) { - done = 1; - fprintf(stderr, "socket in error state.\n"); - goto unlock; - } - if (ret > 0 && FD_ISSET(self->base.sock, &rfs)) { - if (ppsw_recv_packet(super, &msg, &p) < 0) { - perror("read message from client error"); - done = 1; - goto unlock; - } - - if (ppsw_server_process_msg(self, &msg, &p) < 0) { - fprintf (stderr, "unhandled message(%d)\n", msg.id); - done = 1; - goto unlock; - } - - if (msg.id == PPSW_CLOSE) { - done = 1; - goto unlock; - } - } - - unlock: - ppsw_packet_fini(&p); - - pthread_mutex_unlock(&self->mutex); - } - printf ("disconnecting client(fd: %d)\n", self->base.sock); - - self->play_quit = 1; - pthread_join (play_thread, NULL); - ppsw_server_clear_event_bufs (self); - ppsw_server_clear_play_requests (self); - - close(self->base.sock); - self->base.sock = -1; - self->base.seq = 0; - - if (!infinit || self->base.sock < 0) - self->done = 1; - } -} - diff --git a/wrapper/ppswrapper.c b/wrapper/ppswrapper.c deleted file mode 100644 index 682f8d4..0000000 --- a/wrapper/ppswrapper.c +++ /dev/null @@ -1,356 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <sys/time.h> -#include <stdio.h> -#include <stdlib.h> -#include <pthread.h> - -#include "pps-fixed.h" -#include "ppswrapper.h" - -static struct { - pthread_mutex_t mutex; - - ppsw_t* client; - int refcnt; - int created; - int vodlist_inited; -} ppsw_state = { PTHREAD_MUTEX_INITIALIZER, NULL }; - -#if 1 -#undef PPSW_PORT -#define PPSW_PORT -1 -#endif - -static ppsw_t* -ppsw_client_get(void) -{ - const char *ppsw_port = getenv ("PPSW_PORT"); - int port = -1; - - if (ppsw_state.client) { - ppsw_state.refcnt++; - return ppsw_state.client; - } - - if (ppsw_port) - port = atoi (ppsw_port); - if (port >= 0 && port < 1024) - port = -1; - - ppsw_state.client = ppsw_client_create (port); - if (!ppsw_state.client) - return NULL; - - ppsw_state.refcnt = 1; - ppsw_state.created = 0; - ppsw_state.vodlist_inited = 0; - return ppsw_state.client; -} - -static void -ppsw_client_free(void) -{ - if (!ppsw_state.client) - return; - - if (ppsw_state.refcnt <= 0) - return; - if (--ppsw_state.refcnt > 0) - return; - - ppsw_client_destroy (ppsw_state.client); - ppsw_state.client = NULL; -} - -int ppsvod_create(const char *oem_name, - const char *terminal_type, - const char *device_id, - pps_callback pcallback) -{ - int ret; - - if (!ppsw_client_get()) - return -1; - - if (ppsw_state.created) - return -1; - - ret = ppsw_client_send_create(ppsw_state.client, oem_name, - terminal_type, device_id, - pcallback); - if (ret < 0) { - ppsw_client_free(); - return -1; - } - - ppsw_state.created++; - - return 0; -} - -int ppsvod_destroy(void) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - if (!ppsw_state.created) - return -1; - - ret = ppsw_client_send_destroy(ppsw_state.client); - - ppsw_state.created--; - ppsw_client_free(); - - return ret; -} - -int ppsvod_add_item(char *url, int vip_flag) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_add_item(ppsw_state.client, url, vip_flag); - return ret; -} - -int ppsvod_remove_item(int fd) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_remove_item(ppsw_state.client, fd); - - return ret; -} - -int ppsvod_play_item(int fd, const char * validcode) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_play_item(ppsw_state.client, fd, validcode); - - return ret; -} - -int ppsvod_stop_item(int fd) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_stop_item(ppsw_state.client, fd); - return ret; -} - -int ppsvod_read(int fd, char *buffer, unsigned int length, unsigned int position, struct timeval *wait_time) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_read(ppsw_state.client, fd, buffer, length, position, wait_time); - return ret; -} - -void ppsvod_drop_read(int fd) -{ - if (!ppsw_state.client) - return; - - ppsw_client_send_drop_read(ppsw_state.client, fd); -} - -int ppsvod_seek(int fd, unsigned int position) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_seek(ppsw_state.client, fd, position); - return ret; -} - -int ppsvod_get_file_length(int fd) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_get_file_length(ppsw_state.client, fd); - return ret; -} - -int ppsvod_check_url(const char* url) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_check_url(ppsw_state.client, url); - return -1; -} - -ppsvod_iteminfo *ppsvod_get_item_info(int fd) -{ - ppsvod_iteminfo * info; - - if (!ppsw_state.client) - return NULL; - - info = ppsw_client_send_get_item_info(ppsw_state.client, fd); - return info; -} - -void ppsvod_free_item_info(ppsvod_iteminfo *piteminfo) -{ - free (piteminfo->file_name); - free (piteminfo); -} - -int pps_vodlist_init(void) -{ - int ret; - - if (!ppsw_client_get()) - return -1; - - if (ppsw_state.vodlist_inited) - return -1; - - ret = ppsw_client_send_vodlist_init(ppsw_state.client); - ppsw_state.vodlist_inited++; - return ret; -} - -int pps_vodlist_uninit(void) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - if (!ppsw_state.vodlist_inited) - return -1; - - ret = ppsw_client_send_vodlist_uninit(ppsw_state.client); - ppsw_state.vodlist_inited--; - ppsw_client_free(); - return ret; -} - -ppscategory *pps_vodlist_getcat(int *catnum) -{ - ppscategory* cat; - - cat = ppsw_client_send_vodlist_getcat(ppsw_state.client, catnum); - return cat; -} - -ppschannel *pps_vodlist_getchannel(int catid, int subcatid, int page_size, int page_num) -{ - ppschannel* ch; - - ch = ppsw_client_send_vodlist_getchannel(ppsw_state.client, - catid, subcatid, page_size, - page_num); - return ch; -} - -int ppsvod_save_config_dir(const char * root_path) -{ - int ret; - - ret = ppsw_client_send_save_config_dir(ppsw_state.client, root_path); - return ret; -} - -char *ppsvod_get_config_dir(void) -{ - char* ret; - - ret = ppsw_client_send_get_config_dir(ppsw_state.client); - return ret; -} - -int ppsvod_default_config_dir(void) -{ - int ret; - - ret = ppsw_client_send_default_config_dir(ppsw_state.client); - return ret; -} - -int ppsvod_charge(const char* password) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_charge(ppsw_state.client, password); - return ret; -} - -int ppsvod_get_account_type(void) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_get_account_type(ppsw_state.client); - return ret; -} - -int ppsvod_get_vip_expire_time(char *time) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_get_vip_expire_time(ppsw_state.client, time); - return ret; -} - -int ppsvod_get_time(struct timeval *tv) -{ - int ret; - - if (!ppsw_state.client) - return -1; - - ret = ppsw_client_send_get_time(ppsw_state.client, tv); - return ret; -} diff --git a/wrapper/ppswrapper.h b/wrapper/ppswrapper.h deleted file mode 100644 index 2d6f5a8..0000000 --- a/wrapper/ppswrapper.h +++ /dev/null @@ -1,399 +0,0 @@ -/* PPSWrapper - - * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more - */ -#ifndef PPSWRAPPER_H -#define PPSWRAPPER_H - -#include <stdint.h> -#include <unistd.h> -#include <pthread.h> - -#define PPSW_PORT 11994 - -typedef enum { - PPSW_REPLY = 0, - PPSW_REPLY_ITEM_INFO, - PPSW_REPLY_READ, - PPSW_REPLY_EVENT, - PPSW_REPLY_TIME, - PPSW_REPLY_DIR, - PPSW_REPLY_GET_CAT, - PPSW_REPLY_GET_CHANNEL, - PPSW_REPLY_EXPIRE_TIME, - PPSW_HELLO, - PPSW_CLOSE, - PPSW_CREATE, - PPSW_DESTROY, - PPSW_ADD_ITEM, - PPSW_REMOVE_ITEM, - PPSW_PLAY_ITEM, - PPSW_STOP_ITEM, - PPSW_READ, - PPSW_DROP_READ, - PPSW_GET_FILE_LENGTH, - PPSW_GET_ITEM_INFO, - PPSW_SEEK, - PPSW_GET_EVENT, - PPSW_GET_TIME, - PPSW_SAVE_CONFIG_DIR, - PPSW_GET_CONFIG_DIR, - PPSW_DEFAULT_CONFIG_DIR, - PPSW_GET_ACCOUNT_TYPE, - PPSW_GET_VIP_EXPIRE_TIME, - PPSW_CHECK_URL, - PPSW_CHARGE, - PPSW_VODLIST_INIT, - PPSW_VODLIST_UNINIT, - PPSW_VODLIST_GET_CAT, - PPSW_VODLIST_GET_CHANNEL -} ppsw_msg_id_t; - -#define PPSW_SYNC 0x47 -#define PPSW_EAGAIN 1 - -typedef struct { - /*** sync ***/ - uint8_t sync; - /*** message id */ - uint16_t id; - /*** sequence number */ - uint16_t seq; - /*** payload length */ - uint32_t len; -} ppsw_msg_t; - -#define PPSW_MSG_HEADER_LEN (2 * 2 + 4 + 1) - -typedef struct { - uint32_t len; - uint32_t offset; - uint8_t *data; - - uint8_t local[64]; -} ppsw_packet_t; - -typedef struct { - ppsw_msg_t base; - uint32_t retval; - uint8_t* extra; -} ppsw_reply_msg_t; - -typedef struct { - ppsw_msg_t base; - char* oem_name; - char* termainal_type; - char* device_id; -} ppsw_create_msg_t; - -typedef struct { - ppsw_msg_t base; - char* url; - uint32_t flags; -} ppsw_add_item_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; -} ppsw_remove_item_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; - char* valid_code; -} ppsw_play_item_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; -} ppsw_stop_item_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; - uint32_t length; - uint32_t position; - uint32_t wait_time_sec; - uint32_t wait_time_usec; -} ppsw_read_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; -} ppsw_drop_read_msg_t; - -typedef struct { - ppsw_msg_t base; - uint32_t fd; - uint32_t position; -} ppsw_seek_msg_t; - -typedef struct { - int sock; - - volatile uint16_t seq; - - pthread_mutex_t packet_mutex; -} ppsw_t; - -typedef struct ppsw_buf { - ppsw_msg_t msg; - ppsw_packet_t packet; - - struct ppsw_buf *next; -} ppsw_buf_t; - -typedef struct { - ppsw_t base; - - pid_t pid; - pps_callback callback; - char* configdir; - char expire_time[1024]; - - ppscategory* categories; - ppschannel* channel; - - ppsw_buf_t *buf; - - pthread_mutex_t mutex; - pthread_mutex_t packet_mutex; - - int error; - - int has_thread; - int evt_quit; - pthread_t evt_thread; -} ppsw_client_t; - -#define PPSW_EVTBUF_SZ 256 -typedef struct ppsw_event_buf { - int index; - pps_event events[PPSW_EVTBUF_SZ + 1]; - int rd_pos; - int wr_pos; - - struct ppsw_event_buf* next; -} ppsw_event_buf_t; - -typedef struct ppsw_play_req { - uint16_t seq; - - int fd; - char* valid_code; - - struct ppsw_play_req* next; -} ppsw_play_req_t; - -typedef struct { - ppsw_t base; - int srv_sock; - int done; - - pthread_mutex_t mutex; - pthread_mutex_t bufmutex; - pthread_mutex_t play_queue_mutex; - - ppsw_event_buf_t * bufs; - ppsw_play_req_t* play_reqs; - int play_quit; -} ppsw_server_t; - -void -ppsw_init(ppsw_t* self); - -void -ppsw_fini(ppsw_t* self); - -ppsw_t* -ppsw_client_create(int port); - -void -ppsw_client_destroy(ppsw_t* super); - -ppsw_t* -ppsw_server_create(int fd, int port); - -void -ppsw_server_destroy(ppsw_t* super); - -void -ppsw_server_run(ppsw_t* super, int infinit); - -size_t -ppsw_strlen(const char* s); - -int -ppsw_client_send_create(ppsw_t *self, - const char *oem_name, - const char * terminal_type, - const char *device_id, - pps_callback pcallback); - -int -ppsw_client_send_destroy(ppsw_t* self); - -int ppsw_client_send_close(ppsw_t* super); - -int ppsw_client_send_add_item(ppsw_t* super, - const char* url, - int flags); - -int ppsw_client_send_remove_item(ppsw_t* super, - int fd); - -int ppsw_client_send_play_item(ppsw_t* super, - int fd, - const char* valid_code); - -int ppsw_client_send_stop_item(ppsw_t* super, - int fd); - -int ppsw_client_send_get_file_length(ppsw_t* super, - int fd); - -int ppsw_client_send_seek(ppsw_t* super, int fd, - unsigned int position); - -ppsvod_iteminfo *ppsw_client_send_get_item_info(ppsw_t* super, - int fd); - -int ppsw_client_send_read(ppsw_t* super, int fd, char *buffer, - unsigned int length, unsigned int position, - struct timeval *wait_time); - -int ppsw_client_send_drop_read(ppsw_t* super, - int fd); - -int ppsw_client_send_get_event(ppsw_t* super, - int* index, pps_event* event); - -int ppsw_client_send_get_time(ppsw_t* super, - struct timeval* tv); - -char* ppsw_client_send_get_config_dir(ppsw_t* super); - -int ppsw_client_send_default_config_dir(ppsw_t* super); - -int ppsw_client_send_save_config_dir(ppsw_t* super, - const char* path); - -int ppsw_client_send_vodlist_init(ppsw_t* super); - -int ppsw_client_send_vodlist_uninit(ppsw_t* super); - -ppscategory* ppsw_client_send_vodlist_getcat(ppsw_t* super, int *catnum); - -ppschannel* ppsw_client_send_vodlist_getchannel(ppsw_t* super, int catid, int subcatid, - int page_size, int page_num); - -int ppsw_client_send_get_account_type(ppsw_t* super); - -int ppsw_client_send_get_vip_expire_time(ppsw_t* super, char* time); - -int ppsw_client_send_charge(ppsw_t* super, const char* password); - -int ppsw_client_send_check_url(ppsw_t* super, const char* url); - -int -ppsw_write_item_info(ppsw_packet_t* self, - ppsvod_iteminfo* info); - -int -ppsw_read_item_info(ppsw_packet_t* p, - ppsvod_iteminfo* info); - -int -ppsw_server_listen(ppsw_t* super, - int port); - -int -ppsw_read_reply_msg(ppsw_t* self, - ppsw_reply_msg_t* msg); - -int -ppsw_send_reply_msg(ppsw_t* self, - ppsw_reply_msg_t* msg); - -int -ppsw_packet_readl(ppsw_packet_t* self, - uint32_t* retval); - -int -ppsw_packet_writel(ppsw_packet_t* self, - uint32_t retval); - -int -ppsw_packet_reads(ppsw_packet_t* self, - uint16_t* retval); - -int -ppsw_packet_writes(ppsw_packet_t* self, - uint16_t retval); - -int -ppsw_packet_readb(ppsw_packet_t* self, - uint8_t* retval); - -int -ppsw_packet_writeb(ppsw_packet_t* self, - uint8_t retval); - -int -ppsw_packet_read(ppsw_packet_t* self, - uint8_t* retval, - uint32_t len); - -int -ppsw_packet_write(ppsw_packet_t* self, - const uint8_t* val, - uint32_t len); - -int -ppsw_packet_read_str(ppsw_packet_t* self, - char** retval, - uint16_t* retlen); - -int -ppsw_packet_write_str(ppsw_packet_t* self, - const char* retval, - int len); - -int -ppsw_packet_init(ppsw_packet_t* p, - uint16_t id, - uint32_t len); - -void -ppsw_packet_fini(ppsw_packet_t* p); - -int -ppsw_packet_reinit(ppsw_packet_t* p, - uint16_t id, - uint32_t len); -int -ppsw_send_packet(ppsw_t* self, - ppsw_msg_t* msg, - ppsw_packet_t* p); - -int -ppsw_reply_packet(ppsw_t* self, - uint16_t seq, - ppsw_packet_t* p); - -int -ppsw_recv_packet(ppsw_t* self, - ppsw_msg_t* msg, - ppsw_packet_t* p); - -#endif |