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.
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
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
)
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
);
48 swfdec_net_stream_play (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
49 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*rval
)
51 SwfdecNetStream
*stream
;
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
);
61 swfdec_net_stream_pause (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
62 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*rval
)
64 SwfdecNetStream
*stream
;
67 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM
, &stream
, "");
70 playing
= !swfdec_net_stream_get_playing (stream
);
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
)
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
)
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
)
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
)
104 swfdec_net_stream_setBufferTime (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
105 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*rval
)
107 SwfdecNetStream
*stream
;
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
)
117 swfdec_net_stream_get_checkPolicyFile (SwfdecAsContext
*cx
,
118 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
121 SWFDEC_STUB ("NetStream.checkPolicyFile (get)");
124 SWFDEC_AS_NATIVE (2101, 6, swfdec_net_stream_set_checkPolicyFile
)
126 swfdec_net_stream_set_checkPolicyFile (SwfdecAsContext
*cx
,
127 SwfdecAsObject
*object
, guint argc
, SwfdecAsValue
*argv
,
130 SWFDEC_STUB ("NetStream.checkPolicyFile (set)");
134 swfdec_net_stream_do_seek (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
135 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*rval
)
137 SwfdecNetStream
*stream
;
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
);
151 swfdec_net_stream_get_time (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
152 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
154 SwfdecNetStream
*stream
;
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);
163 if (msecs
>= stream
->current_time
)
166 msecs
= stream
->current_time
- msecs
;
167 *ret
= swfdec_as_value_from_number (cx
, msecs
/ 1000.);
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);
182 *ret
= swfdec_as_value_from_number (cx
, swfdec_loader_get_loaded (stream
->loader
));
186 swfdec_net_stream_get_bytesTotal (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
187 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
189 SwfdecNetStream
*stream
;
192 SWFDEC_AS_CHECK (SWFDEC_TYPE_NET_STREAM
, &stream
, "");
194 if (stream
->loader
== NULL
) {
197 bytes
= swfdec_loader_get_size (stream
->loader
);
199 bytes
= swfdec_loader_get_loaded (stream
->loader
);
201 *ret
= swfdec_as_value_from_number (cx
, bytes
);
205 swfdec_net_stream_get_bufferLength (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
206 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
208 SWFDEC_STUB ("Netstream.bufferLength (get)");
212 swfdec_net_stream_get_bufferTime (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
213 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
215 SWFDEC_STUB ("Netstream.bufferTime (get)");
219 swfdec_net_stream_get_audiocodec (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
220 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
222 SWFDEC_STUB ("Netstream.audiocodec (get)");
226 swfdec_net_stream_get_currentFps (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
227 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
229 SWFDEC_STUB ("Netstream.currentFps (get)");
233 swfdec_net_stream_get_decodedFrames (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
234 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
236 SWFDEC_STUB ("Netstream.decodedFrames (get)");
240 swfdec_net_stream_get_liveDelay (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
241 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
243 SWFDEC_STUB ("Netstream.liveDelay (get)");
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
)
255 swfdec_net_stream_setup (SwfdecAsContext
*cx
, SwfdecAsObject
*object
, guint argc
,
256 SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
);
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?");
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
);
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 ()");
304 SWFDEC_AS_VALUE_SET_OBJECT (rval
, obj
);
308 swfdec_net_stream_init_context (SwfdecPlayer
*player
)
310 SwfdecAsContext
*context
;
311 SwfdecAsObject
*stream
, *proto
;
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
);