add blend mode tests
[swfdec.git] / swfdec / swfdec_net_stream_as.c
blob9167430e52d8eeb63c13d195e2420280dc051cb7
1 /* Swfdec
2 * Copyright (C) 2007-2008 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_internal.h"
27 #include "swfdec_as_native_function.h"
28 #include "swfdec_as_strings.h"
29 #include "swfdec_debug.h"
30 #include "swfdec_internal.h"
31 #include "swfdec_player_internal.h"
32 #include "swfdec_sandbox.h"
34 SWFDEC_AS_NATIVE (2101, 0, swfdec_net_stream_close)
35 void
36 swfdec_net_stream_close (SwfdecAsContext *cx, SwfdecAsObject *object,
37 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
39 SwfdecNetStream *stream;
41 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "");
43 swfdec_net_stream_set_loader (stream, NULL);
44 swfdec_net_stream_set_playing (stream, TRUE);
47 static void
48 swfdec_net_stream_play (SwfdecAsContext *cx, SwfdecAsObject *object,
49 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
51 SwfdecNetStream *stream;
52 const char *url;
54 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "s", &url);
56 swfdec_net_stream_set_url (stream, url);
57 swfdec_net_stream_set_playing (stream, TRUE);
60 static void
61 swfdec_net_stream_pause (SwfdecAsContext *cx, SwfdecAsObject *object,
62 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
64 SwfdecNetStream *stream;
65 gboolean playing;
67 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "");
69 if (argc == 0) {
70 playing = !swfdec_net_stream_get_playing (stream);
71 } else {
72 playing = !swfdec_as_value_to_boolean (cx, argv[0]);
74 SWFDEC_LOG ("%s stream %p", playing ? "playing" : "pausing", stream);
75 swfdec_net_stream_set_playing (stream, playing);
78 SWFDEC_AS_NATIVE (2101, 1, swfdec_net_stream_attachAudio)
79 void
80 swfdec_net_stream_attachAudio (SwfdecAsContext *cx, SwfdecAsObject *obj,
81 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
83 SWFDEC_STUB ("NetStream.attachAudio");
86 SWFDEC_AS_NATIVE (2101, 2, swfdec_net_stream_attachVideo)
87 void
88 swfdec_net_stream_attachVideo (SwfdecAsContext *cx, SwfdecAsObject *obj,
89 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
91 SWFDEC_STUB ("NetStream.attachVideo");
94 SWFDEC_AS_NATIVE (2101, 3, swfdec_net_stream_send)
95 void
96 swfdec_net_stream_send (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc,
97 SwfdecAsValue *argv, SwfdecAsValue *rval)
99 SWFDEC_STUB ("NetStream.send");
102 SWFDEC_AS_NATIVE (2101, 4, swfdec_net_stream_setBufferTime)
103 void
104 swfdec_net_stream_setBufferTime (SwfdecAsContext *cx, SwfdecAsObject *object,
105 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
107 SwfdecNetStream *stream;
108 double d;
110 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "n", &d);
112 swfdec_net_stream_set_buffer_time (stream, d);
115 SWFDEC_AS_NATIVE (2101, 5, swfdec_net_stream_get_checkPolicyFile)
116 void
117 swfdec_net_stream_get_checkPolicyFile (SwfdecAsContext *cx,
118 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
119 SwfdecAsValue *rval)
121 SWFDEC_STUB ("NetStream.checkPolicyFile (get)");
124 SWFDEC_AS_NATIVE (2101, 6, swfdec_net_stream_set_checkPolicyFile)
125 void
126 swfdec_net_stream_set_checkPolicyFile (SwfdecAsContext *cx,
127 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
128 SwfdecAsValue *rval)
130 SWFDEC_STUB ("NetStream.checkPolicyFile (set)");
133 static void
134 swfdec_net_stream_do_seek (SwfdecAsContext *cx, SwfdecAsObject *object,
135 guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
137 SwfdecNetStream *stream;
138 SwfdecSandbox *cur;
139 double d;
141 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "n", &d);
143 cur = swfdec_sandbox_get (SWFDEC_PLAYER (cx));
144 swfdec_sandbox_unuse (cur);
145 /* FIXME: perform security check if seeking is allowed here? */
146 swfdec_net_stream_seek (stream, d);
147 swfdec_sandbox_use (cur);
150 static void
151 swfdec_net_stream_get_time (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
152 SwfdecAsValue *argv, SwfdecAsValue *ret)
154 SwfdecNetStream *stream;
155 guint msecs;
157 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "");
159 if (stream->flvdecoder == NULL ||
160 !swfdec_flv_decoder_get_video_info (stream->flvdecoder, &msecs, NULL)) {
161 *ret = swfdec_as_value_from_integer (cx, 0);
162 } else {
163 if (msecs >= stream->current_time)
164 msecs = 0;
165 else
166 msecs = stream->current_time - msecs;
167 *ret = swfdec_as_value_from_number (cx, msecs / 1000.);
171 static void
172 swfdec_net_stream_get_bytesLoaded (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
173 SwfdecAsValue *argv, SwfdecAsValue *ret)
175 SwfdecNetStream *stream;
177 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "");
179 if (stream->loader == NULL)
180 *ret = swfdec_as_value_from_integer (cx, 0);
181 else
182 *ret = swfdec_as_value_from_number (cx, swfdec_loader_get_loaded (stream->loader));
185 static void
186 swfdec_net_stream_get_bytesTotal (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
187 SwfdecAsValue *argv, SwfdecAsValue *ret)
189 SwfdecNetStream *stream;
190 glong bytes;
192 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM, &stream, "");
194 if (stream->loader == NULL) {
195 bytes = 0;
196 } else {
197 bytes = swfdec_loader_get_size (stream->loader);
198 if (bytes < 0)
199 bytes = swfdec_loader_get_loaded (stream->loader);
201 *ret = swfdec_as_value_from_number (cx, bytes);
204 static void
205 swfdec_net_stream_get_bufferLength (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
206 SwfdecAsValue *argv, SwfdecAsValue *ret)
208 SWFDEC_STUB ("Netstream.bufferLength (get)");
211 static void
212 swfdec_net_stream_get_bufferTime (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
213 SwfdecAsValue *argv, SwfdecAsValue *ret)
215 SWFDEC_STUB ("Netstream.bufferTime (get)");
218 static void
219 swfdec_net_stream_get_audiocodec (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
220 SwfdecAsValue *argv, SwfdecAsValue *ret)
222 SWFDEC_STUB ("Netstream.audiocodec (get)");
225 static void
226 swfdec_net_stream_get_currentFps (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
227 SwfdecAsValue *argv, SwfdecAsValue *ret)
229 SWFDEC_STUB ("Netstream.currentFps (get)");
232 static void
233 swfdec_net_stream_get_decodedFrames (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
234 SwfdecAsValue *argv, SwfdecAsValue *ret)
236 SWFDEC_STUB ("Netstream.decodedFrames (get)");
239 static void
240 swfdec_net_stream_get_liveDelay (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
241 SwfdecAsValue *argv, SwfdecAsValue *ret)
243 SWFDEC_STUB ("Netstream.liveDelay (get)");
246 static void
247 swfdec_net_stream_get_videoCodec (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
248 SwfdecAsValue *argv, SwfdecAsValue *ret)
250 SWFDEC_STUB ("Netstream.videoCodec (get)");
253 SWFDEC_AS_NATIVE (2101, 200, swfdec_net_stream_setup)
254 void
255 swfdec_net_stream_setup (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
256 SwfdecAsValue *argv, SwfdecAsValue *ret)
258 if (object == NULL)
259 return;
261 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_time,
262 swfdec_net_stream_get_time, NULL);
263 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_bytesLoaded,
264 swfdec_net_stream_get_bytesLoaded, NULL);
265 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_bytesTotal,
266 swfdec_net_stream_get_bytesTotal, NULL);
267 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_bufferLength,
268 swfdec_net_stream_get_bufferLength, NULL);
269 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_bufferTime,
270 swfdec_net_stream_get_bufferTime, NULL);
271 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_audiocodec,
272 swfdec_net_stream_get_audiocodec, NULL);
273 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_currentFps,
274 swfdec_net_stream_get_currentFps, NULL);
275 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_decodedFrames,
276 swfdec_net_stream_get_decodedFrames, NULL);
277 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_liveDelay,
278 swfdec_net_stream_get_liveDelay, NULL);
279 swfdec_as_object_add_native_variable (object, SWFDEC_AS_STR_videoCodec,
280 swfdec_net_stream_get_videoCodec, NULL);
283 static void
284 swfdec_net_stream_construct (SwfdecAsContext *cx, SwfdecAsObject *obj, guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
286 SwfdecNetStream *stream;
287 SwfdecNetConnection *conn;
289 if (!swfdec_as_context_is_constructing (cx)) {
290 SWFDEC_FIXME ("What do we do if not constructing?");
291 return;
293 stream = g_object_new (SWFDEC_TYPE_NET_STREAM, "context", cx, NULL);
294 swfdec_as_object_set_relay (obj, SWFDEC_AS_RELAY (stream));
296 swfdec_net_stream_setup (cx, obj, 0, NULL, rval);
297 if (argc == 0 ||
298 !SWFDEC_AS_VALUE_IS_OBJECT (argv[0]) ||
299 !SWFDEC_IS_NET_CONNECTION ((conn = (SwfdecNetConnection *) SWFDEC_AS_VALUE_GET_OBJECT (argv[0])))) {
300 SWFDEC_WARNING ("no connection passed to NetStream ()");
301 return;
303 stream->conn = conn;
304 SWFDEC_AS_VALUE_SET_OBJECT (rval, obj);
307 void
308 swfdec_net_stream_init_context (SwfdecPlayer *player)
310 SwfdecAsContext *context;
311 SwfdecAsObject *stream, *proto;
312 SwfdecAsValue val;
314 g_return_if_fail (SWFDEC_IS_PLAYER (player));
316 context = SWFDEC_AS_CONTEXT (player);
317 proto = swfdec_as_object_new_empty (context);
318 stream = swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (
319 swfdec_as_object_add_function (context->global,
320 SWFDEC_AS_STR_NetStream, swfdec_net_stream_construct)));
321 /* set the right properties on the NetStream.prototype object */
322 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_pause, swfdec_net_stream_pause);
323 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_play, swfdec_net_stream_play);
324 swfdec_as_object_add_function (proto, SWFDEC_AS_STR_seek, swfdec_net_stream_do_seek);
325 SWFDEC_AS_VALUE_SET_OBJECT (&val, stream);
326 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR_constructor,
327 &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
328 swfdec_as_object_get_variable (context->global, SWFDEC_AS_STR_Object, &val);
329 if (SWFDEC_AS_VALUE_IS_OBJECT (val)) {
330 swfdec_as_object_get_variable (SWFDEC_AS_VALUE_GET_OBJECT (val),
331 SWFDEC_AS_STR_prototype, &val);
332 swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR___proto__, &val,
333 SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
334 SWFDEC_AS_VALUE_SET_OBJECT (&val, proto);
335 swfdec_as_object_set_variable_and_flags (stream, SWFDEC_AS_STR_prototype, &val,
336 SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);