fix jsut committed crasher by rewriting startDrag action
[swfdec.git] / libswfdec / swfdec_net_stream_as.c
blob5aff00b8112efe32e4050fbeceab90fa92c3c647
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_net_stream.h"
25 #include "swfdec_as_context.h"
26 #include "swfdec_as_native_function.h"
27 #include "swfdec_as_strings.h"
28 #include "swfdec_debug.h"
29 #include "swfdec_internal.h"
30 #include "swfdec_player_internal.h"
32 static void
33 swfdec_net_stream_close (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
35 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
37 swfdec_net_stream_set_loader (stream, NULL);
38 swfdec_net_stream_set_playing (stream, TRUE);
41 static void
42 swfdec_net_stream_play (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
44 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
45 const char *url;
47 url = swfdec_as_value_to_string (cx, &argv[0]);
48 swfdec_net_stream_set_url (stream, url);
49 swfdec_net_stream_set_playing (stream, TRUE);
52 static void
53 swfdec_net_stream_pause (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
55 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
56 gboolean playing;
58 if (argc == 0) {
59 playing = !swfdec_net_stream_get_playing (stream);
60 } else {
61 playing = !swfdec_as_value_to_boolean (cx, &argv[0]);
63 SWFDEC_LOG ("%s stream %p", playing ? "playing" : "pausing", stream);
64 swfdec_net_stream_set_playing (stream, playing);
67 static void
68 swfdec_net_stream_setBufferTime (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
70 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
71 double d;
73 d = swfdec_as_value_to_number (cx, &argv[0]);
74 swfdec_net_stream_set_buffer_time (stream, d);
77 static void
78 swfdec_net_stream_do_seek (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
80 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
81 double d;
83 d = swfdec_as_value_to_number (cx, &argv[0]);
84 swfdec_net_stream_seek (stream, d);
87 static void
88 swfdec_net_stream_construct (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
90 SwfdecNetStream *stream = SWFDEC_NET_STREAM (obj);
91 SwfdecNetConnection *conn;
93 if (!swfdec_as_context_is_constructing (cx)) {
94 SWFDEC_FIXME ("What do we do if not constructing?");
95 return;
97 if (!SWFDEC_AS_VALUE_IS_OBJECT (&argv[0]) ||
98 !SWFDEC_IS_NET_CONNECTION ((conn = (SwfdecNetConnection *) SWFDEC_AS_VALUE_GET_OBJECT (&argv[0])))) {
99 SWFDEC_WARNING ("no connection passed to NetStream ()");
100 return;
102 stream->conn = conn;
105 void
106 swfdec_net_stream_init_context (SwfdecPlayer *player, guint version)
108 SwfdecAsContext *context;
109 SwfdecAsObject *stream, *proto;
110 SwfdecAsValue val;
112 g_return_if_fail (SWFDEC_IS_PLAYER (player));
114 context = SWFDEC_AS_CONTEXT (player);
115 proto = swfdec_as_object_new_empty (context);
116 if (proto == NULL)
117 return;
118 stream = SWFDEC_AS_OBJECT (swfdec_as_object_add_constructor (context->global,
119 SWFDEC_AS_STR_NetStream, SWFDEC_TYPE_NET_STREAM, SWFDEC_TYPE_NET_STREAM,
120 swfdec_net_stream_construct, 1, proto));
121 if (stream == NULL)
122 return;
123 /* set the right properties on the NetStream.prototype object */
124 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_pause, SWFDEC_TYPE_NET_STREAM,
125 swfdec_net_stream_pause, 0);
126 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_play, SWFDEC_TYPE_NET_STREAM,
127 swfdec_net_stream_play, 1);
128 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_close, SWFDEC_TYPE_NET_STREAM,
129 swfdec_net_stream_close, 0);
130 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_seek, SWFDEC_TYPE_NET_STREAM,
131 swfdec_net_stream_do_seek, 1);
132 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_setBufferTime, SWFDEC_TYPE_NET_STREAM,
133 swfdec_net_stream_setBufferTime, 1);
134 SWFDEC_AS_VALUE_SET_OBJECT (&val, stream);
135 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR_constructor,
136 &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
137 SWFDEC_AS_VALUE_SET_OBJECT (&val, context->Object_prototype);
138 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR___proto__, &val,
139 SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);