Skip to main content

ferroui_media_video_android/
lib.rs

1//! Android Media3 native audio/timing with one GPU texture output stream.
2#![cfg(target_os = "android")]
3#![forbid(unsafe_code)]
4use ferroui_android::{interop::java::*, FerroAndroidApplication};
5#[cfg(feature = "controls")]
6pub use ferroui_media_android::NativeGlTextureImporter;
7use ferroui_media_android::{
8    native_error, poll_native, text, AndroidFrameStream, AndroidSurfaceSource,
9    NativeGlFrameProducer,
10};
11use ferroui_media_video::{frames::*, *};
12use std::{cell::Cell, path::PathBuf, rc::Rc, task::Poll, time::Duration};
13
14fn resource_directory() -> Result<PathBuf, MediaError> {
15    let directory = call_object(
16        &FerroAndroidApplication::context(),
17        "getCacheDir",
18        "()Ljava/io/File;",
19        &[],
20    )
21    .ok_or_else(|| native_error("No private resource cache"))?;
22    call_object(&directory, "getAbsolutePath", "()Ljava/lang/String;", &[])
23        .map(|v| PathBuf::from(string_of(&v)))
24        .ok_or_else(|| native_error("No cache path"))
25}
26pub struct AndroidVideoProvider {
27    pub frames: NativeGlFrameProducer,
28}
29pub fn native_diagnostics() -> String {
30    call_static_object(
31        &JavaClass::find("org/ferroui/media/video/VideoPlayer"),
32        "diagnostics",
33        "()Ljava/lang/String;",
34        &[],
35    )
36    .map(|v| string_of(&v))
37    .unwrap_or_default()
38}
39pub fn live_native_players() -> u32 {
40    call_static_int(
41        &JavaClass::find("org/ferroui/media/video/VideoPlayer"),
42        "livePlayers",
43        "()I",
44        &[],
45    )
46    .max(0) as u32
47}
48impl MediaPlayerProvider for AndroidVideoProvider {
49    fn open(
50        &self,
51        source: MediaSource,
52        id: u64,
53        generation: u64,
54        _: PresentationPolicy,
55    ) -> MediaFuture<Rc<dyn NativeMediaSession>> {
56        let producer = self.frames.clone();
57        let directory = if matches!(&source, MediaSource::Resource { .. }) {
58            resource_directory().map(Some)
59        } else {
60            Ok(None)
61        };
62        // Resolve custom classes on the application class loader, before GL jobs.
63        let class = JavaClass::find("org/ferroui/media/video/VideoPlayer");
64        Box::pin(async move {
65            let PreparedMediaSource {
66                location,
67                temporary: resource,
68            } = source.prepare_native(directory?).await?;
69            let source = match location {
70                NativeMediaLocation::Url(url) => url,
71                NativeMediaLocation::File(path) => path
72                    .to_str()
73                    .map(str::to_owned)
74                    .ok_or_else(|| native_error("Media path is not UTF-8"))?,
75            };
76            let surface = AndroidSurfaceSource::create(&producer).await?;
77            let java = new_object(
78                &class,
79                "(Landroid/content/Context;Lorg/ferroui/media/SurfaceFrames;Ljava/lang/String;)V",
80                &[
81                    JavaValue::Object(Some(&FerroAndroidApplication::context())),
82                    JavaValue::Object(Some(&surface.java())),
83                    JavaValue::String(&source),
84                ],
85            )
86            .to_global();
87            let mut session = Some(AndroidSession {
88                stream: AndroidFrameStream::new(producer, surface, id, generation)
89                    .with_frame_clock(java.clone()),
90                java,
91                _resource: resource,
92                seek: Cell::new(0),
93            });
94            poll_native(move || {
95                let value = session.as_ref().unwrap();
96                if let Err(error) = value.check() {
97                    return Poll::Ready(Err(error));
98                }
99                if call_int(&value.java, "state", "()I", &[]) == 0 {
100                    return Poll::Pending;
101                }
102                Poll::Ready(Ok(
103                    Rc::new(session.take().unwrap()) as Rc<dyn NativeMediaSession>
104                ))
105            })
106            .await
107        })
108    }
109}
110struct AndroidSession {
111    java: JavaObject,
112    stream: AndroidFrameStream,
113    _resource: Option<TemporaryMediaFile>,
114    seek: Cell<i64>,
115}
116impl AndroidSession {
117    fn check(&self) -> Result<(), MediaError> {
118        let error = text(&self.java, "error");
119        if error.is_empty() {
120            Ok(())
121        } else {
122            Err(if call_boolean(&self.java, "unsupported", "()Z", &[]) {
123                MediaError::new(MediaErrorKind::Unsupported, error)
124            } else {
125                native_error(error)
126            })
127        }
128    }
129}
130impl Drop for AndroidSession {
131    fn drop(&mut self) {
132        self.stream.stop();
133        call_void(&self.java, "close", "()V", &[]);
134    }
135}
136impl NativeMediaSession for AndroidSession {
137    fn has_pending_frame_work(&self) -> bool {
138        self.stream.has_pending_frame_work()
139    }
140    fn state(&self) -> Result<MediaPlaybackSession, MediaError> {
141        self.check()?;
142        let state = match call_int(&self.java, "state", "()I", &[]) {
143            0 => MediaPlaybackState::Opening,
144            1 => MediaPlaybackState::Paused,
145            2 => MediaPlaybackState::Playing,
146            3 => MediaPlaybackState::Buffering,
147            4 => MediaPlaybackState::Ended,
148            _ => MediaPlaybackState::Failed,
149        };
150        let duration = call_long(&self.java, "duration", "()J", &[]);
151        let width = call_int(&self.java, "naturalWidth", "()I", &[]).max(0) as u32;
152        let height = call_int(&self.java, "naturalHeight", "()I", &[]).max(0) as u32;
153        Ok(MediaPlaybackSession {
154            state,
155            position: Duration::from_millis(
156                call_long(&self.java, "position", "()J", &[]).max(0) as u64
157            ),
158            duration: (duration >= 0).then(|| Duration::from_millis(duration as u64)),
159            natural_size: (width > 0 && height > 0).then_some((width, height)),
160            seekable: call_boolean(&self.java, "seekable", "()Z", &[]),
161            decode: Acceleration::Unknown,
162            error: None,
163        })
164    }
165    fn play(&self) -> MediaFuture<()> {
166        call_void(&self.java, "play", "()V", &[]);
167        let result = self.check();
168        Box::pin(async move { result })
169    }
170    fn pause(&self) -> Result<(), MediaError> {
171        call_void(&self.java, "pause", "()V", &[]);
172        self.check()
173    }
174    fn stop(&self) -> Result<(), MediaError> {
175        self.stream.clear();
176        self.pause()
177    }
178    fn seek(&self, position: Duration) -> MediaFuture<Duration> {
179        self.stream.clear();
180        let request = call_long(
181            &self.java,
182            "seek",
183            "(J)J",
184            &[JavaValue::Long(
185                position.as_millis().min(i64::MAX as u128) as i64
186            )],
187        );
188        self.seek.set(request);
189        let java = self.java.clone();
190        poll_native(move || {
191            let error = text(&java, "error");
192            if !error.is_empty() {
193                return Poll::Ready(Err(native_error(error)));
194            }
195            if request == 0 {
196                return Poll::Ready(Err(native_error("Native source is not prepared")));
197            }
198            if call_long(&java, "completedSeek", "()J", &[]) < request {
199                return Poll::Pending;
200            }
201            Poll::Ready(Ok(Duration::from_millis(
202                call_long(&java, "position", "()J", &[]).max(0) as u64,
203            )))
204        })
205    }
206    fn set_audio(&self, volume: f64, muted: bool) -> Result<(), MediaError> {
207        call_void(
208            &self.java,
209            "audio",
210            "(FZ)V",
211            &[JavaValue::Float(volume as f32), JavaValue::Boolean(muted)],
212        );
213        self.check()
214    }
215    fn supports_playback_rate(&self) -> bool {
216        true
217    }
218    fn set_playback_rate(&self, rate: f64) -> Result<(), MediaError> {
219        self.check()?;
220        call_void(&self.java, "rate", "(F)V", &[JavaValue::Float(rate as f32)]);
221        Ok(())
222    }
223    fn tracks(&self, kind: MediaTrackKind) -> Vec<MediaTrack> {
224        let Some(list) = call_object(
225            &self.java,
226            "tracks",
227            "(I)Ljava/lang/String;",
228            &[track_kind(kind)],
229        ) else {
230            return Vec::new();
231        };
232        serde_json::from_str::<Vec<serde_json::Value>>(&string_of(&list))
233            .unwrap_or_default()
234            .into_iter()
235            .filter_map(|value| {
236                Some(MediaTrack {
237                    id: value["id"].as_u64()?.try_into().ok()?,
238                    label: value["label"].as_str()?.to_owned(),
239                    selected: value["selected"].as_bool()?,
240                })
241            })
242            .collect()
243    }
244    fn select_track(&self, kind: MediaTrackKind, id: Option<u32>) -> Result<(), MediaError> {
245        self.check()?;
246        let id = id
247            .map(|id| i32::try_from(id))
248            .transpose()
249            .map_err(|_| MediaError::new(MediaErrorKind::InvalidSource, "Invalid track ID"))?
250            .unwrap_or(-1);
251        if call_boolean(
252            &self.java,
253            "selectTrack",
254            "(II)Z",
255            &[track_kind(kind), JavaValue::Int(id)],
256        ) {
257            Ok(())
258        } else {
259            Err(MediaError::new(
260                MediaErrorKind::InvalidSource,
261                "The selected track is unavailable",
262            ))
263        }
264    }
265    fn caption_text(&self) -> Option<String> {
266        let captions = text(&self.java, "captions");
267        (!captions.is_empty()).then_some(captions)
268    }
269    fn take_frame(&self) -> Result<Option<FrameLease>, MediaError> {
270        self.check()?;
271        if self.seek.get() > call_long(&self.java, "completedSeek", "()J", &[]) {
272            return Ok(None);
273        }
274        self.stream.take_frame(
275            call_int(&self.java, "width", "()I", &[]).max(0) as u32,
276            call_int(&self.java, "height", "()I", &[]).max(0) as u32,
277        )
278    }
279}
280
281fn track_kind(kind: MediaTrackKind) -> JavaValue<'static> {
282    JavaValue::Int(match kind {
283        MediaTrackKind::Audio => 0,
284        MediaTrackKind::Captions => 1,
285    })
286}