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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*! Sciter video rendering.

Host application can render custom video streams using `<video>` infrastructure.

*/


use capi::sctypes::{UINT, LPCBYTE, LPCSTR};
use capi::scom::som_passport_t;

/// A type alias for Sciter functions that return `bool`.
pub type Result<T> = ::std::result::Result<T, ()>;


/// Color space for video frame.
#[repr(C)]
pub enum COLOR_SPACE {
	Unknown,

	Yv12,
	/// I420
	Iyuv,
	Nv12,
	Yuy2,

	Rgb24,
	Rgb555,
	Rgb565,
	Rgb32,
}

macro_rules! cppcall {
	// self.func()
	($this:ident . $func:ident ()) => {
		unsafe {
			((*$this.vtbl).$func)($this as *mut _)
		}
	};
  (const $this:ident . $func:ident ()) => {
    unsafe {
      ((*$this.vtbl).$func)($this as *const _)
    }
  };

	// self.func(args...)
	($this:ident . $func:ident ( $( $arg:expr ),* )) => {
		unsafe {
			((*$this.vtbl).$func)($this as *mut _, $($arg),* )
		}
	};
  (const $this:ident . $func:ident ( $( $arg:expr ),* )) => {
    unsafe {
      ((*$this.vtbl).$func)($this as *const _, $($arg),* )
    }
  };
}

macro_rules! cppresult {
	( $( $t:tt )* ) => {
		if cppcall!( $($t)* ) {
			Ok(())
		} else {
			Err(())
		}
	}
}

#[doc(hidden)]
pub trait NamedInterface {
	fn get_interface_name() -> &'static [u8];

	fn query_interface(from: &mut iasset) -> Option<* mut iasset> {
		let mut out: *mut iasset = ::std::ptr::null_mut();
		from.get_interface(Self::get_interface_name().as_ptr() as LPCSTR, &mut out as *mut _);
		if !out.is_null() {
			Some(out)
		} else {
			None
		}
	}
}

impl NamedInterface for video_source {
	fn get_interface_name() -> &'static [u8] {
		b"source.video.sciter.com\0"
	}
}

impl NamedInterface for video_destination {
	fn get_interface_name() -> &'static [u8] {
		b"destination.video.sciter.com\0"
	}
}

impl NamedInterface for fragmented_video_destination {
	fn get_interface_name() -> &'static [u8] {
		b"fragmented.destination.video.sciter.com\0"
	}
}


/// COM `IUnknown` alike thing.
#[repr(C)]
struct iasset_vtbl {
	/// Increments the reference count for an interface on an object.
	pub add_ref: extern "C" fn(this: *mut iasset) -> i32,

	/// Decrements the reference count for an interface on an object.
	pub release: extern "C" fn(this: *mut iasset) -> i32,

	/// Retrieves pointers to the supported interfaces on an object.
	pub get_interface: extern "C" fn(this: *mut iasset, name: LPCSTR, out: *mut *mut iasset) -> bool,

	/// Retrieves a pointer to the passport declaration of an object.
	pub get_passport: extern "C" fn(thing: *mut iasset) -> *const som_passport_t,
}

/// COM `IUnknown` alike thing.
#[repr(C)]
pub struct iasset {
	vtbl: *const iasset_vtbl,
}

impl iasset {
	/// Increments the reference count for an interface on an object.
	fn add_ref(&mut self) -> i32 {
		cppcall!(self.add_ref())
	}

	/// Decrements the reference count for an interface on an object.
	fn release(&mut self) -> i32 {
		cppcall!(self.release())
	}

	/// Retrieves pointers to the supported interfaces on an object.
	pub fn get_interface(&mut self, name: LPCSTR, out: *mut *mut iasset) -> bool {
		cppcall!(self.get_interface(name, out))
	}
}


/// Video source interface, used by engine to query video state.
#[repr(C)]
struct video_source_vtbl {
	// <-- iasset:
	/// Increments the reference count for an interface on an object.
	pub add_ref: extern "C" fn(this: *mut video_source) -> i32,

	/// Decrements the reference count for an interface on an object.
	pub release: extern "C" fn(this: *mut video_source) -> i32,

	/// Retrieves pointers to the supported interfaces on an object.
	pub get_interface: extern "C" fn(this: *mut video_source, name: *const u8, out: *mut *mut iasset) -> bool,

	/// Retrieves a pointer to the passport declaration of an object.
	pub get_passport: extern "C" fn(thing: *mut iasset) -> *const som_passport_t,
	// -->

	// <-- video_source
	pub play: extern "C" fn(this: *mut video_source) -> bool,
	pub pause: extern "C" fn(this: *mut video_source) -> bool,
	pub stop: extern "C" fn(this: *mut video_source) -> bool,

	pub get_is_ended: extern "C" fn(this: *const video_source, is_end: *mut bool) -> bool,

	pub get_position: extern "C" fn(this: *const video_source, seconds: *mut f64) -> bool,
	pub set_position: extern "C" fn(this: *mut video_source, seconds: f64) -> bool,

	pub get_duration: extern "C" fn(this: *const video_source, seconds: *mut f64) -> bool,

	pub get_volume: extern "C" fn(this: *const video_source, volume: *mut f64) -> bool,
	pub set_volume: extern "C" fn(this: *mut video_source, volume: f64) -> bool,

	pub get_balance: extern "C" fn(this: *const video_source, balance: *mut f64) -> bool,
	pub set_balance: extern "C" fn(this: *mut video_source, balance: f64) -> bool,
	// -->
}

/// Video source interface to query video state.
#[repr(C)]
pub struct video_source {
	vtbl: *const video_source_vtbl,
}

impl video_source {
	/// Starts playback from the current position.
	pub fn play(&mut self) -> Result<()> {
		cppresult!(self.play())
	}

	/// Pauses playback.
	pub fn pause(&mut self) -> Result<()> {
		cppresult!(self.pause())
	}

	/// Stops playback.
	pub fn stop(&mut self) -> Result<()> {
		cppresult!(self.stop())
	}

	/// Whether playback has reached the end of the video.
	pub fn is_ended(&self) -> Result<bool> {
		let mut r = false;
		cppresult!(const self.get_is_ended(&mut r as *mut _)).map(|_| r)
	}

	/// Reports the current playback position.
	pub fn get_position(&self) -> Result<f64> {
		let mut r = 0f64;
		cppresult!(const self.get_position(&mut r as *mut _)).map(|_| r)
	}

	/// Sets the current playback position.
	pub fn set_position(&mut self, seconds: f64) -> Result<()> {
		cppresult!(self.set_position(seconds))
	}

	/// Reports the duration of the video in seconds.
	///
	/// If duration is not available, returns `0`.
	pub fn get_duration(&self) -> Result<f64> {
		let mut r = 0f64;
		cppresult!(const self.get_duration(&mut r as *mut _)).map(|_| r)
	}

	/// Reports the current volume level of an audio track of the movie.
	///
	/// `1.0` corresponds to `0db`, `0.0` (mute) to `-100db`.
	pub fn get_volume(&self) -> Result<f64> {
		let mut r = 0f64;
		cppresult!(const self.get_volume(&mut r as *mut _)).map(|_| r)
	}

	/// Sets the current volume level between `0.0` (mute) and `1.0` (`0db`).
	pub fn set_volume(&mut self, volume: f64) -> Result<()> {
		cppresult!(self.set_volume(volume))
	}

	/// Reports the current stereo balance.
	pub fn get_balance(&self) -> Result<f64> {
		let mut r = 0f64;
		cppresult!(const self.get_balance(&mut r as *mut _)).map(|_| r)
	}

	/// Sets a new value of the stereo balance.
	pub fn set_balance(&mut self, balance: f64) -> Result<()> {
		cppresult!(self.set_balance(balance))
	}
}


/// Video destination interface, represents video rendering site.
#[repr(C)]
struct video_destination_vtbl {
	// <-- iasset:
	/// Increments the reference count for an interface on an object.
	pub add_ref: extern "C" fn(this: *mut video_destination) -> i32,

	/// Decrements the reference count for an interface on an object.
	pub release: extern "C" fn(this: *mut video_destination) -> i32,

	/// Retrieves pointers to the supported interfaces on an object.
	pub get_interface: extern "C" fn(this: *mut video_destination, name: *const u8, out: *mut *mut iasset) -> bool,

	/// Retrieves a pointer to the passport declaration of an object.
	pub get_passport: extern "C" fn(thing: *mut iasset) -> *const som_passport_t,
	// -->

	// <-- video_destination
	/// Whether this instance of `video_renderer` is attached to a DOM element and is capable of playing.
	pub is_alive: extern "C" fn(this: *const video_destination) -> bool,

	/// Start streaming/rendering.
	pub start_streaming: extern "C" fn(this: *mut video_destination, frame_width: i32, frame_height: i32, color_space: COLOR_SPACE, src: *const video_source) -> bool,

	/// Stop streaming.
	pub stop_streaming: extern "C" fn(this: *mut video_destination) -> bool,

	/// Render the next frame.
	pub render_frame: extern "C" fn(this: *mut video_destination, data: LPCBYTE, size: UINT) -> bool,
	// -->
}

/// Video destination interface, represents video rendering site.
#[repr(C)]
pub struct video_destination {
	vtbl: *const video_destination_vtbl,
}

impl video_destination {

	/// Whether this instance of `video_renderer` is attached to a DOM element and is capable of playing.
	pub fn is_alive(&self) -> bool {
		cppcall!(const self.is_alive())
	}

	/// Start streaming/rendering.
	///
	/// * `frame_size` - the width and the height of the video frame.
	/// * `color_space` - the color space format of the video frame.
	/// * `src` - an optional custom [`video_source`](struct.video_source.html) interface implementation, provided by the application.
	pub fn start_streaming(&mut self, frame_size: (i32, i32), color_space: COLOR_SPACE, src: Option<&video_source>) -> Result<()> {
		let src_ptr = if let Some(ptr) = src { ptr as *const _ } else { ::std::ptr::null() };
		cppresult!(self.start_streaming(frame_size.0, frame_size.1, color_space, src_ptr))
	}

	/// Stop streaming.
	pub fn stop_streaming(&mut self) -> Result<()> {
		cppresult!(self.stop_streaming())
	}

	/// Render the next frame.
	pub fn render_frame(&mut self, data: &[u8]) -> Result<()> {
		cppresult!(self.render_frame(data.as_ptr(), data.len() as UINT))
	}
}


/// Fragmented destination interface, used for partial updates.
#[repr(C)]
struct fragmented_video_destination_vtbl {
	// <-- iasset:
	/// Increments the reference count for an interface on an object.
	pub add_ref: extern "C" fn(this: *mut fragmented_video_destination) -> i32,

	/// Decrements the reference count for an interface on an object.
	pub release: extern "C" fn(this: *mut fragmented_video_destination) -> i32,

	/// Retrieves pointers to the supported interfaces on an object.
	pub get_interface: extern "C" fn(this: *mut fragmented_video_destination, name: *const u8, out: *mut *mut iasset) -> bool,

	/// Retrieves a pointer to the passport declaration of an object.
	pub get_passport: extern "C" fn(thing: *mut iasset) -> *const som_passport_t,
	// -->

	// <-- video_destination
	/// Whether this instance of `video_renderer` is attached to a DOM element and is capable of playing.
	pub is_alive: extern "C" fn(this: *const fragmented_video_destination) -> bool,

	/// Start streaming/rendering.
	pub start_streaming: extern "C" fn(this: *mut fragmented_video_destination, frame_width: i32, frame_height: i32, color_space: COLOR_SPACE, src: *const video_source) -> bool,

	/// Stop streaming.
	pub stop_streaming: extern "C" fn(this: *mut fragmented_video_destination) -> bool,

	/// Render the next frame.
	pub render_frame: extern "C" fn(this: *mut fragmented_video_destination, data: LPCBYTE, size: UINT) -> bool,
	// -->

	// <-- fragmented_video_destination
	/// Render the specified part of the current frame.
	pub render_frame_part: extern "C" fn(this: *mut fragmented_video_destination, data: LPCBYTE, size: UINT, x: i32, y: i32, width: i32, height: i32) -> bool,
	// -->
}

/// Fragmented destination interface, used for partial updates.
#[repr(C)]
pub struct fragmented_video_destination {
	vtbl: *const fragmented_video_destination_vtbl,
}

impl fragmented_video_destination {

	/// Whether this instance of `video_renderer` is attached to a DOM element and is capable of playing.
	pub fn is_alive(&self) -> bool {
		cppcall!(const self.is_alive())
	}

	/// Start streaming/rendering.
	///
	/// * `frame_size` - the width and the height of the video frame.
	/// * `color_space` - the color space format of the video frame.
	/// * `src` - an optional custom [`video_source`](struct.video_source.html) interface implementation, provided by the application.
	pub fn start_streaming(&mut self, frame_size: (i32, i32), color_space: COLOR_SPACE, src: Option<&video_source>) -> Result<()> {
		let src_ptr = if let Some(ptr) = src { ptr as *const _ } else { ::std::ptr::null() };
		cppresult!(self.start_streaming(frame_size.0, frame_size.1, color_space, src_ptr))
	}

	/// Stop streaming.
	pub fn stop_streaming(&mut self) -> Result<()> {
		cppresult!(self.stop_streaming())
	}

	/// Render the next frame.
	pub fn render_frame(&mut self, data: &[u8]) -> Result<()> {
		cppresult!(self.render_frame(data.as_ptr(), data.len() as UINT))
	}

	/// Render the specified part of the current frame.
	///
	/// * `update_point` - X and Y coordinates of the update portion.
	/// * `update_size` - width and height of the update portion.
	pub fn render_frame_part(&mut self, data: &[u8], update_point: (i32, i32), update_size: (i32, i32)) -> Result<()> {
		cppresult!(self.render_frame_part(data.as_ptr(), data.len() as UINT, update_point.0, update_point.1, update_size.0, update_size.1))
	}
}

/// A managed `iasset` pointer.
pub struct AssetPtr<T> {
	ptr: *mut T,
}

/// It's okay to transfer video pointers between threads.
unsafe impl<T> Send for AssetPtr<T> {}

use ::std::ops::{Deref, DerefMut};

impl Deref for AssetPtr<video_destination> {
	type Target = video_destination;

	fn deref(&self) -> &Self::Target {
		unsafe { &*self.ptr }
	}
}

impl DerefMut for AssetPtr<video_destination> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		unsafe { &mut *self.ptr }
	}
}

impl Deref for AssetPtr<fragmented_video_destination> {
	type Target = fragmented_video_destination;

	fn deref(&self) -> &Self::Target {
		unsafe { &*self.ptr }
	}
}

impl DerefMut for AssetPtr<fragmented_video_destination> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		unsafe { &mut *self.ptr }
	}
}

/// Decrements the reference count of a managed pointer.
impl<T> Drop for AssetPtr<T> {
	fn drop(&mut self) {
		self.get().release();
	}
}

impl<T> AssetPtr<T> {
	/// Attach to an existing `iasset` pointer without reference increment.
	fn attach(lp: *mut T) -> Self {
		assert!(!lp.is_null());
		Self {
			ptr: lp
		}
	}

	/// Attach to an `iasset` pointer and increment its reference count.
	pub fn adopt(lp: *mut T) -> Self {
		let mut me = Self::attach(lp);
		me.get().add_ref();
		me
	}

	/// Get as an `iasset` type.
	fn get(&mut self) -> &mut iasset {
		let ptr = self.ptr as *mut iasset;
		unsafe { &mut *ptr }
	}
}


/// Attach to an `iasset` pointer.
impl<T> From<*mut T> for AssetPtr<T> {
	/// Attach to a pointer and increment its reference count.
	fn from(lp: *mut T) -> Self {
		AssetPtr::adopt(lp)
	}
}


/// Attempt to construct `Self` via a conversion.
impl<T: NamedInterface> AssetPtr<T> {

	/// Retrieve a supported interface of the managed pointer.
	///
	/// Example:
	///
	/// ```rust,no_run
	/// # use sciter::video::{AssetPtr, iasset, video_source};
	/// # let external_ptr: *mut iasset = ::std::ptr::null_mut();
	/// let mut site = AssetPtr::adopt(external_ptr);
	/// let source = AssetPtr::<video_source>::try_from(&mut site);
	/// assert!(source.is_ok());
	/// ```
	pub fn try_from<U>(other: &mut AssetPtr<U>) -> Result<Self> {
		let me = T::query_interface(other.get());
		me.map(|p| AssetPtr::adopt(p as *mut T)).ok_or(())
	}
}