Skip to content

Commit 49cd189

Browse files
authored
ndk/media_format: Implement copy()/clear() fns since API level 29 (#449)
Also provide the typical `try_clone()` method, and add missing doc aliases to all types and functions.
1 parent b7d83a8 commit 49cd189

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

ndk/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
- Move `MediaFormat` from `media::media_codec` to its own `media::media_format` module. (#442)
4+
- media_format: Expose `MediaFormat::copy()` and `MediaFormat::clear()` from API level 29. (#449)
45

56
# 0.8.0 (2023-10-15)
67

@@ -9,7 +10,7 @@
910
- asset: Use entire asset length when mapping buffer. (#387)
1011
- Bump MSRV to 1.66 for `raw-window-handle 0.5.1`, `num_enum`'s `catch_all` with arbitrary enum discriminants. (#388, #431)
1112
- Bump optional `jni` dependency for doctest example from `0.19` to `0.21`. (#390)
12-
- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-TODO). (#370)
13+
- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-2023-10-15). (#370)
1314
- **Breaking:** Upgrade `bitflags` crate from `1` to `2`. (#394)
1415
- bitmap: Add `try_format()` to `AndroidBitmapInfo` to handle unexpected formats without panicking. (#395)
1516
- Add `Font` bindings. (#397)

ndk/src/media/media_format.rs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@ use std::{
99
slice,
1010
};
1111

12-
use crate::media_error::MediaError;
12+
use crate::media_error::{MediaError, Result};
1313

1414
/// A native [`AMediaFormat *`]
1515
///
1616
/// [`AMediaFormat *`]: https://developer.android.com/ndk/reference/group/media#amediaformat
1717
#[derive(Debug)]
18+
#[doc(alias = "AMediaFormat")]
1819
pub struct MediaFormat {
1920
inner: NonNull<ffi::AMediaFormat>,
2021
}
2122

2223
impl fmt::Display for MediaFormat {
24+
/// Human readable representation of the format.
25+
#[doc(alias = "AMediaFormat_toString")]
2326
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2427
let c_str = unsafe { CStr::from_ptr(ffi::AMediaFormat_toString(self.as_ptr())) };
2528
f.write_str(c_str.to_str().unwrap())
2629
}
2730
}
2831

2932
impl Default for MediaFormat {
33+
#[doc(alias = "AMediaFormat_new")]
3034
fn default() -> Self {
3135
Self::new()
3236
}
@@ -45,12 +49,14 @@ impl MediaFormat {
4549
self.inner.as_ptr()
4650
}
4751

52+
#[doc(alias = "AMediaFormat_new")]
4853
pub fn new() -> Self {
4954
Self {
5055
inner: NonNull::new(unsafe { ffi::AMediaFormat_new() }).unwrap(),
5156
}
5257
}
5358

59+
#[doc(alias = "AMediaFormat_getInt32")]
5460
pub fn i32(&self, key: &str) -> Option<i32> {
5561
let name = CString::new(key).unwrap();
5662
let mut out = 0;
@@ -61,6 +67,7 @@ impl MediaFormat {
6167
}
6268
}
6369

70+
#[doc(alias = "AMediaFormat_getInt64")]
6471
pub fn i64(&self, key: &str) -> Option<i64> {
6572
let name = CString::new(key).unwrap();
6673
let mut out = 0;
@@ -71,6 +78,7 @@ impl MediaFormat {
7178
}
7279
}
7380

81+
#[doc(alias = "AMediaFormat_getFloat")]
7482
pub fn f32(&self, key: &str) -> Option<f32> {
7583
let name = CString::new(key).unwrap();
7684
let mut out = 0.0;
@@ -81,6 +89,7 @@ impl MediaFormat {
8189
}
8290
}
8391

92+
#[doc(alias = "AMediaFormat_getSize")]
8493
pub fn usize(&self, key: &str) -> Option<usize> {
8594
let name = CString::new(key).unwrap();
8695
let mut out = 0;
@@ -91,6 +100,7 @@ impl MediaFormat {
91100
}
92101
}
93102

103+
#[doc(alias = "AMediaFormat_getBuffer")]
94104
pub fn buffer(&self, key: &str) -> Option<&[u8]> {
95105
let name = CString::new(key).unwrap();
96106
let mut out_buffer = ptr::null_mut();
@@ -106,34 +116,40 @@ impl MediaFormat {
106116
.then(|| unsafe { slice::from_raw_parts(out_buffer.cast(), out_size) })
107117
}
108118

119+
#[doc(alias = "AMediaFormat_getString")]
109120
pub fn str(&self, key: &str) -> Option<&str> {
110121
let name = CString::new(key).unwrap();
111122
let mut out = ptr::null();
112123
unsafe { ffi::AMediaFormat_getString(self.as_ptr(), name.as_ptr(), &mut out) }
113124
.then(|| unsafe { CStr::from_ptr(out) }.to_str().unwrap())
114125
}
115126

127+
#[doc(alias = "AMediaFormat_setInt32")]
116128
pub fn set_i32(&self, key: &str, value: i32) {
117129
let name = CString::new(key).unwrap();
118-
unsafe { ffi::AMediaFormat_setInt32(self.as_ptr(), name.as_ptr(), value) };
130+
unsafe { ffi::AMediaFormat_setInt32(self.as_ptr(), name.as_ptr(), value) }
119131
}
120132

133+
#[doc(alias = "AMediaFormat_setInt64")]
121134
pub fn set_i64(&self, key: &str, value: i64) {
122135
let name = CString::new(key).unwrap();
123-
unsafe { ffi::AMediaFormat_setInt64(self.as_ptr(), name.as_ptr(), value) };
136+
unsafe { ffi::AMediaFormat_setInt64(self.as_ptr(), name.as_ptr(), value) }
124137
}
125138

139+
#[doc(alias = "AMediaFormat_setFloat")]
126140
pub fn set_f32(&self, key: &str, value: f32) {
127141
let name = CString::new(key).unwrap();
128-
unsafe { ffi::AMediaFormat_setFloat(self.as_ptr(), name.as_ptr(), value) };
142+
unsafe { ffi::AMediaFormat_setFloat(self.as_ptr(), name.as_ptr(), value) }
129143
}
130144

145+
#[doc(alias = "AMediaFormat_setString")]
131146
pub fn set_str(&self, key: &str, value: &str) {
132147
let name = CString::new(key).unwrap();
133148
let c_string = CString::new(value).unwrap();
134-
unsafe { ffi::AMediaFormat_setString(self.as_ptr(), name.as_ptr(), c_string.as_ptr()) };
149+
unsafe { ffi::AMediaFormat_setString(self.as_ptr(), name.as_ptr(), c_string.as_ptr()) }
135150
}
136151

152+
#[doc(alias = "AMediaFormat_setBuffer")]
137153
pub fn set_buffer(&self, key: &str, value: &[u8]) {
138154
let name = CString::new(key).unwrap();
139155
unsafe {
@@ -143,10 +159,11 @@ impl MediaFormat {
143159
value.as_ptr().cast(),
144160
value.len(),
145161
)
146-
};
162+
}
147163
}
148164

149165
#[cfg(feature = "api-level-28")]
166+
#[doc(alias = "AMediaFormat_getDouble")]
150167
pub fn f64(&self, key: &str) -> Option<f64> {
151168
let name = CString::new(key).unwrap();
152169
let mut out = 0.0;
@@ -159,6 +176,7 @@ impl MediaFormat {
159176

160177
/// Returns (left, top, right, bottom)
161178
#[cfg(feature = "api-level-28")]
179+
#[doc(alias = "AMediaFormat_getRect")]
162180
pub fn rect(&self, key: &str) -> Option<(i32, i32, i32, i32)> {
163181
let name = CString::new(key).unwrap();
164182
let mut left = 0;
@@ -182,29 +200,57 @@ impl MediaFormat {
182200
}
183201

184202
#[cfg(feature = "api-level-28")]
203+
#[doc(alias = "AMediaFormat_setDouble")]
185204
pub fn set_f64(&self, key: &str, value: f64) {
186205
let name = CString::new(key).unwrap();
187-
unsafe { ffi::AMediaFormat_setDouble(self.as_ptr(), name.as_ptr(), value) };
206+
unsafe { ffi::AMediaFormat_setDouble(self.as_ptr(), name.as_ptr(), value) }
188207
}
189208

190209
#[cfg(feature = "api-level-28")]
210+
#[doc(alias = "AMediaFormat_setRect")]
191211
pub fn set_rect(&self, key: &str, left: i32, top: i32, right: i32, bottom: i32) {
192212
let name = CString::new(key).unwrap();
193-
unsafe {
194-
ffi::AMediaFormat_setRect(self.as_ptr(), name.as_ptr(), left, top, right, bottom)
195-
};
213+
unsafe { ffi::AMediaFormat_setRect(self.as_ptr(), name.as_ptr(), left, top, right, bottom) }
196214
}
197215

198216
#[cfg(feature = "api-level-28")]
217+
#[doc(alias = "AMediaFormat_setSize")]
199218
pub fn set_usize(&self, key: &str, value: usize) {
200219
let name = CString::new(key).unwrap();
201-
unsafe { ffi::AMediaFormat_setSize(self.as_ptr(), name.as_ptr(), value) };
220+
unsafe { ffi::AMediaFormat_setSize(self.as_ptr(), name.as_ptr(), value) }
221+
}
222+
223+
/// Copy one [`MediaFormat`] to another.
224+
#[cfg(feature = "api-level-29")]
225+
#[doc(alias = "AMediaFormat_copy")]
226+
pub fn copy(&self, to: &mut Self) -> Result<()> {
227+
let status = unsafe { ffi::AMediaFormat_copy(to.as_ptr(), self.as_ptr()) };
228+
MediaError::from_status(status)
229+
}
230+
231+
/// Clones this [`MediaFormat`] into a [`MediaFormat::new()`] object using
232+
/// [`MediaFormat::copy()`].
233+
#[cfg(feature = "api-level-29")]
234+
#[doc(alias = "AMediaFormat_new")]
235+
#[doc(alias = "AMediaFormat_copy")]
236+
pub fn try_clone(&self) -> Result<Self> {
237+
let mut copy = Self::new();
238+
self.copy(&mut copy)?;
239+
Ok(copy)
240+
}
241+
242+
/// Remove all key/value pairs from this [`MediaFormat`].
243+
#[cfg(feature = "api-level-29")]
244+
#[doc(alias = "AMediaFormat_clear")]
245+
pub fn clear(&mut self) {
246+
unsafe { ffi::AMediaFormat_clear(self.as_ptr()) }
202247
}
203248
}
204249

205250
impl Drop for MediaFormat {
251+
#[doc(alias = "AMediaFormat_delete")]
206252
fn drop(&mut self) {
207253
let status = unsafe { ffi::AMediaFormat_delete(self.as_ptr()) };
208-
MediaError::from_status(status).unwrap();
254+
MediaError::from_status(status).unwrap()
209255
}
210256
}

0 commit comments

Comments
 (0)