Skip to main content

dora_ssr/dora/
video_node.rs

1/* Copyright (c) 2016-2026 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn videonode_type() -> i32;
11	fn videonode_pause(slf: i64);
12	fn videonode_resume(slf: i64);
13	fn videonode_is_paused(slf: i64) -> i32;
14	fn videonode_new(filename: i64, looped: i32) -> i64;
15}
16use crate::dora::IObject;
17use crate::dora::ISprite;
18impl ISprite for VideoNode { }
19use crate::dora::INode;
20impl INode for VideoNode { }
21pub struct VideoNode { raw: i64 }
22crate::dora_object!(VideoNode);
23impl VideoNode {
24	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
25		(unsafe { videonode_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
26			match raw {
27				0 => None,
28				_ => Some(Box::new(VideoNode { raw: raw }))
29			}
30		})
31	}
32	/// Pauses the video playback.
33	pub fn pause(&mut self) {
34		unsafe { videonode_pause(self.raw()); }
35	}
36	/// Resumes the video playback.
37	pub fn resume(&mut self) {
38		unsafe { videonode_resume(self.raw()); }
39	}
40	/// Gets Whether the video is currently paused.
41	pub fn is_paused(&self) -> bool {
42		return unsafe { videonode_is_paused(self.raw()) != 0 };
43	}
44	/// Creates a new VideoNode object for playing a video.
45	///
46	/// # Arguments
47	///
48	/// * `filename` - The path to the video file. It should be a valid video file path with `.h264` suffix.
49	///     H.264 format requirements:
50	///       - Video codec: H.264 / AVC only (no H.265/HEVC, VP9, AV1, etc.).
51	///       - Bitstream format: Annex-B byte stream is required (NAL units separated by 0x000001 / 0x00000001 start codes).
52	///         MP4/FLV-style AVCC (length-prefixed NAL units) is NOT supported unless converted to Annex-B beforehand.
53	///       - Stream type: video-only elementary stream is recommended. Audio tracks (if any) are ignored.
54	///       - Profile/level constraints (recommended for maximum compatibility and performance):
55	///           * Baseline / Constrained Baseline profile is recommended.
56	///           * Progressive frames only (no interlaced/field-coded content).
57	///           * No B-frames is recommended (e.g., baseline) to avoid output reordering costs.
58	///       - Color format: YUV 4:2:0 (8-bit) is recommended; other chroma formats may be unsupported.
59	///       - Frame rate: Constant frame rate (CFR) is recommended. Variable frame rate streams may play with unstable timing.
60	///       - Resolution/performance notes:
61	///           * 4K and high-bitrate streams may be CPU intensive for software decoding.
62	///           * For smooth playback on mid-range devices, 720p/1080p and moderate bitrates are recommended.
63	///       - It is recommended to use the `ffmpeg` tool to convert the video file to H.264 format before using it.
64	/// * `looped` - (optional) Whether the video should loop. Default is false.
65	///
66	/// # Returns
67	///
68	/// * `VideoNode` - The created VideoNode instance. Returns `nil` if creation fails.
69	pub fn new(filename: &str, looped: bool) -> Option<VideoNode> {
70		unsafe { return VideoNode::from(videonode_new(crate::dora::from_string(filename), if looped { 1 } else { 0 })); }
71	}
72}