coding utf-8 import gobject gobject threads_init from time import slee

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
import gobject
#gobject.threads_init()
from time import sleep
import gst
from logger import log
from bufferer import Buffer
class Source(gst.BaseSrc):
__gsttemplates__ = (
gst.PadTemplate("src",
gst.PAD_SRC,
gst.PAD_ALWAYS,
gst.caps_new_any()),
)
unlock = False
buf = None
def __init__(self, name):
gst.BaseSrc.__init__(self)
self.__gobject_init__()
self.set_name(name)
self.set_format(gst.FORMAT_BYTES)
def set_track(self, track):
if self.buf:
self.buf.terminate()
self.buf = Buffer(track)
self.buf.start()
def do_create(self, offset, size):
print 'called'
while not self.buf.started:
log.debug('Waiting for buffer to start')
if self.unlock:
self.unlock = False
return
sleep(0.001)
if self.buf.length and offset >= self.buf.length:
log.error('Requested offset (%s) is over file length (%s)' % (offset, self.buf.length))
return gst.FLOW_UNEXPECTED, None
while self.buf.pos < offset+size and not self.buf.finished:
if self.buf.failed:
log.error('Buffering failed, stopping player')
return gst.FLOW_UNEXPECTED, None
if self.unlock:
self.unlock = False
return
log.debug('Waiting 0.01 sec for buffer to settle...')
sleep(0.001)
data = self.buf.data[offset:offset+size]
if data:
return gst.FLOW_OK, gst.Buffer(data)
else:
return gst.FLOW_UNEXPECTED, None
def do_query (self, query):
if query.type == gst.QUERY_DURATION:
if query.parse_duration()[0] == gst.FORMAT_TIME:
while not self.buf.track.length:
if self.unlock:
self.unlock = False
return
log.debug('Waiting 0.01 sec for buffer to settle...')
sleep(0.001)
lent = self.buf.track.length*gst.SECOND
query.set_duration(gst.FORMAT_TIME, lent)
return True
elif query.parse_duration()[0] == gst.FORMAT_BYTES:
log.debug('Query (type duration) format BYTES')
while not self.buf.length:
if self.unlock:
self.unlock = False
return
log.debug('Waiting for buffer to settle...')
sleep(0.001)
log.debug('Relying query (type duration) format BYTES with %d' % self.buf.length)
query.set_duration(format=gst.FORMAT_BYTES, duration=int(self.buf.length))
return True
else:
log.debug('Unhandled query (type: duration) %s' % str(query.parse_duration()))
return False
return False
elif query.type == gst.QUERY_LATENCY:
log.debug('Query latency request')
query.set_latency(min_latency=0, live=True, max_latency=-1)
return True
else:
log.debug('Unhandled query, type: %s' % str(query.type))
return False
def do_unlock(self):
self.unlock = True
def do_is_seekable(self):
"Answers is the source seekable (this source is)"
return True
def do_check_get_range(self, *args):
"Do not check range when start playing"
return False
def destroy(self):
if self.buf:
if self.buf.isRunning():
self.buf.terminate()
gobject.type_register(Source)