Exemple #1
0
PyObject *
PyNumber_Int(PyObject *o)
{
	PyNumberMethods *m;
	const char *buffer;
	int buffer_len;

	if (o == NULL)
		return null_error();
	if (PyInt_Check(o)) {
		Py_INCREF(o);
		return o;
	}
	if (PyString_Check(o))
		return int_from_string(PyString_AS_STRING(o), 
				       PyString_GET_SIZE(o));
	if (PyUnicode_Check(o))
		return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
					 PyUnicode_GET_SIZE(o),
					 10);
	m = o->ob_type->tp_as_number;
	if (m && m->nb_int)
		return m->nb_int(o);
	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
		return int_from_string((char*)buffer, buffer_len);

	return type_error("object can't be converted to int");
}
Exemple #2
0
/*
 * @data: a m3u8 playlist text data, taking ownership
 */
static gboolean
gst_m3u8_update (GstM3U8 * self, gchar * data, gboolean * updated)
{
    gint val, duration;
    gchar *title, *end;
//  gboolean discontinuity;
    GstM3U8 *list;

    g_return_val_if_fail (self != NULL, FALSE);
    g_return_val_if_fail (data != NULL, FALSE);
    g_return_val_if_fail (updated != NULL, FALSE);

    *updated = TRUE;

    /* check if the data changed since last update */
    if (self->last_data && g_str_equal (self->last_data, data)) {
        GST_DEBUG ("Playlist is the same as previous one");
        *updated = FALSE;
        g_free (data);
        return TRUE;
    }

    if (!g_str_has_prefix (data, "#EXTM3U")) {
        GST_WARNING ("Data doesn't start with #EXTM3U");
        g_free (data);
        return FALSE;
    }

    g_free (self->last_data);
    self->last_data = data;

    if (self->files) {
        g_list_foreach (self->files, (GFunc) gst_m3u8_media_file_free, NULL);
        g_list_free (self->files);
        self->files = NULL;
    }

    list = NULL;
    duration = -1;
    title = NULL;
    data += 7;
    while (TRUE) {
        end = g_utf8_strchr (data, -1, '\n');       /* FIXME: support \r\n */
        if (end)
            *end = '\0';

        if (data[0] != '#') {
            if (duration < 0 && list == NULL) {
                GST_LOG ("%s: got line without EXTINF or EXTSTREAMINF, dropping", data);
                goto next_line;
            }

            if (!gst_uri_is_valid (data)) {
                gchar *slash;
                if (!self->uri) {
                    GST_WARNING ("uri not set, can't build a valid uri");
                    goto next_line;
                }
                slash = g_utf8_strrchr (self->uri, -1, '/');
                if (!slash) {
                    GST_WARNING ("Can't build a valid uri");
                    goto next_line;
                }

                *slash = '\0';
                data = g_strdup_printf ("%s/%s", self->uri, data);
                *slash = '/';
            } else
                data = g_strdup (data);

            if (list != NULL) {
                if (g_list_find_custom (self->lists, data,
                                        (GCompareFunc) _m3u8_compare_uri)) {
                    GST_DEBUG ("Already have a list with this URI");
                    gst_m3u8_free (list);
                    g_free (data);
                } else {
                    gst_m3u8_set_uri (list, data);
                    self->lists = g_list_append (self->lists, list);
                }
                list = NULL;
            } else {
                GstM3U8MediaFile *file;
                file =
                    gst_m3u8_media_file_new (data, title, duration,
                                             self->mediasequence++);
                duration = -1;
                title = NULL;
                self->files = g_list_append (self->files, file);
            }

        } else if (g_str_has_prefix (data, "#EXT-X-ENDLIST")) {
            self->endlist = TRUE;
        } else if (g_str_has_prefix (data, "#EXT-X-VERSION:")) {
            if (int_from_string (data + 15, &data, &val))
                self->version = val;
        } else if (g_str_has_prefix (data, "#EXT-X-STREAM-INF:")) {
            gchar *v, *a;

            if (list != NULL) {
                GST_WARNING ("Found a list without a uri..., dropping");
                gst_m3u8_free (list);
            }

            list = gst_m3u8_new ();
            data = data + 18;
            while (data && parse_attributes (&data, &a, &v)) {
                if (g_str_equal (a, "BANDWIDTH")) {
                    if (!int_from_string (v, NULL, &list->bandwidth))
                        GST_WARNING ("Error while reading BANDWIDTH");
                } else if (g_str_equal (a, "PROGRAM-ID")) {
                    if (!int_from_string (v, NULL, &list->program_id))
                        GST_WARNING ("Error while reading PROGRAM-ID");
                } else if (g_str_equal (a, "CODECS")) {
                    g_free (list->codecs);
                    list->codecs = g_strdup (v);
                } else if (g_str_equal (a, "RESOLUTION")) {
                    if (!int_from_string (v, &v, &list->width))
                        GST_WARNING ("Error while reading RESOLUTION width");
                    if (!v || *v != '=') {
                        GST_WARNING ("Missing height");
                    } else {
                        v = g_utf8_next_char (v);
                        if (!int_from_string (v, NULL, &list->height))
                            GST_WARNING ("Error while reading RESOLUTION height");
                    }
                }
            }
        } else if (g_str_has_prefix (data, "#EXT-X-TARGETDURATION:")) {
            if (int_from_string (data + 22, &data, &val))
                self->targetduration = val;
        } else if (g_str_has_prefix (data, "#EXT-X-MEDIA-SEQUENCE:")) {
            if (int_from_string (data + 22, &data, &val))
                self->mediasequence = val;
        } else if (g_str_has_prefix (data, "#EXT-X-DISCONTINUITY")) {
            /* discontinuity = TRUE; */
        } else if (g_str_has_prefix (data, "#EXT-X-PROGRAM-DATE-TIME:")) {
            /* <YYYY-MM-DDThh:mm:ssZ> */
            GST_DEBUG ("FIXME parse date");
        } else if (g_str_has_prefix (data, "#EXT-X-ALLOW-CACHE:")) {
            g_free (self->allowcache);
            self->allowcache = g_strdup (data + 19);
        } else if (g_str_has_prefix (data, "#EXTINF:")) {
            if (!int_from_string (data + 8, &data, &val)) {
                GST_WARNING ("Can't read EXTINF duration");
                goto next_line;
            }
            duration = val;
            if (duration > self->targetduration)
                GST_WARNING ("EXTINF duration > TARGETDURATION");
            if (!data || *data != ',')
                goto next_line;
            data = g_utf8_next_char (data);
            if (data != end) {
                g_free (title);
                title = g_strdup (data);
            }
        } else {
            GST_LOG ("Ignored line: %s", data);
        }

next_line:
        if (!end)
            break;
        data = g_utf8_next_char (end);      /* skip \n */
    }

    /* redorder playlists by bitrate */
    if (self->lists)
        self->lists =
            g_list_sort (self->lists,
                         (GCompareFunc) gst_m3u8_compare_playlist_by_bitrate);

    return TRUE;
}