1- //! Bindings for [`AMediaFormat`] and [` AMediaCodec`]
1+ //! Bindings for [`AMediaCodec`]
22//!
3- //! [`AMediaFormat`]: https://developer.android.com/ndk/reference/group/media#amediaformat
43//! [`AMediaCodec`]: https://developer.android.com/ndk/reference/group/media#amediacodec
54
5+ #[ deprecated = "MediaFormat should be referenced directly from the media_format module" ]
6+ pub use super :: media_format:: MediaFormat ;
67use crate :: media_error:: { MediaError , Result } ;
78use crate :: native_window:: NativeWindow ;
89use crate :: utils:: abort_on_panic;
910use std:: {
1011 ffi:: { c_char, c_void, CStr , CString } ,
11- fmt:: Display ,
12+ fmt,
1213 mem:: MaybeUninit ,
1314 pin:: Pin ,
1415 ptr:: { self , NonNull } ,
@@ -22,204 +23,6 @@ pub enum MediaCodecDirection {
2223 Encoder ,
2324}
2425
25- /// A native [`AMediaFormat *`]
26- ///
27- /// [`AMediaFormat *`]: https://developer.android.com/ndk/reference/group/media#amediaformat
28- #[ derive( Debug ) ]
29- pub struct MediaFormat {
30- inner : NonNull < ffi:: AMediaFormat > ,
31- }
32-
33- impl Display for MediaFormat {
34- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
35- let c_str = unsafe { CStr :: from_ptr ( ffi:: AMediaFormat_toString ( self . as_ptr ( ) ) ) } ;
36- f. write_str ( c_str. to_str ( ) . unwrap ( ) )
37- }
38- }
39-
40- impl Default for MediaFormat {
41- fn default ( ) -> Self {
42- Self :: new ( )
43- }
44- }
45-
46- impl MediaFormat {
47- /// Assumes ownership of `ptr`
48- ///
49- /// # Safety
50- /// `ptr` must be a valid pointer to an Android [`ffi::AMediaFormat`].
51- pub unsafe fn from_ptr ( ptr : NonNull < ffi:: AMediaFormat > ) -> Self {
52- Self { inner : ptr }
53- }
54-
55- fn as_ptr ( & self ) -> * mut ffi:: AMediaFormat {
56- self . inner . as_ptr ( )
57- }
58-
59- pub fn new ( ) -> Self {
60- Self {
61- inner : NonNull :: new ( unsafe { ffi:: AMediaFormat_new ( ) } ) . unwrap ( ) ,
62- }
63- }
64-
65- pub fn i32 ( & self , key : & str ) -> Option < i32 > {
66- let name = CString :: new ( key) . unwrap ( ) ;
67- let mut out = 0 ;
68- if unsafe { ffi:: AMediaFormat_getInt32 ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) } {
69- Some ( out)
70- } else {
71- None
72- }
73- }
74-
75- pub fn i64 ( & self , key : & str ) -> Option < i64 > {
76- let name = CString :: new ( key) . unwrap ( ) ;
77- let mut out = 0 ;
78- if unsafe { ffi:: AMediaFormat_getInt64 ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) } {
79- Some ( out)
80- } else {
81- None
82- }
83- }
84-
85- pub fn f32 ( & self , key : & str ) -> Option < f32 > {
86- let name = CString :: new ( key) . unwrap ( ) ;
87- let mut out = 0.0 ;
88- if unsafe { ffi:: AMediaFormat_getFloat ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) } {
89- Some ( out)
90- } else {
91- None
92- }
93- }
94-
95- pub fn usize ( & self , key : & str ) -> Option < usize > {
96- let name = CString :: new ( key) . unwrap ( ) ;
97- let mut out = 0 ;
98- if unsafe { ffi:: AMediaFormat_getSize ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) } {
99- Some ( out)
100- } else {
101- None
102- }
103- }
104-
105- pub fn buffer ( & self , key : & str ) -> Option < & [ u8 ] > {
106- let name = CString :: new ( key) . unwrap ( ) ;
107- let mut out_buffer = ptr:: null_mut ( ) ;
108- let mut out_size = 0 ;
109- unsafe {
110- ffi:: AMediaFormat_getBuffer (
111- self . as_ptr ( ) ,
112- name. as_ptr ( ) ,
113- & mut out_buffer,
114- & mut out_size,
115- )
116- }
117- . then ( || unsafe { slice:: from_raw_parts ( out_buffer. cast ( ) , out_size) } )
118- }
119-
120- pub fn str ( & self , key : & str ) -> Option < & str > {
121- let name = CString :: new ( key) . unwrap ( ) ;
122- let mut out = ptr:: null ( ) ;
123- unsafe { ffi:: AMediaFormat_getString ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) }
124- . then ( || unsafe { CStr :: from_ptr ( out) } . to_str ( ) . unwrap ( ) )
125- }
126-
127- pub fn set_i32 ( & self , key : & str , value : i32 ) {
128- let name = CString :: new ( key) . unwrap ( ) ;
129- unsafe { ffi:: AMediaFormat_setInt32 ( self . as_ptr ( ) , name. as_ptr ( ) , value) } ;
130- }
131-
132- pub fn set_i64 ( & self , key : & str , value : i64 ) {
133- let name = CString :: new ( key) . unwrap ( ) ;
134- unsafe { ffi:: AMediaFormat_setInt64 ( self . as_ptr ( ) , name. as_ptr ( ) , value) } ;
135- }
136-
137- pub fn set_f32 ( & self , key : & str , value : f32 ) {
138- let name = CString :: new ( key) . unwrap ( ) ;
139- unsafe { ffi:: AMediaFormat_setFloat ( self . as_ptr ( ) , name. as_ptr ( ) , value) } ;
140- }
141-
142- pub fn set_str ( & self , key : & str , value : & str ) {
143- let name = CString :: new ( key) . unwrap ( ) ;
144- let c_string = CString :: new ( value) . unwrap ( ) ;
145- unsafe { ffi:: AMediaFormat_setString ( self . as_ptr ( ) , name. as_ptr ( ) , c_string. as_ptr ( ) ) } ;
146- }
147-
148- pub fn set_buffer ( & self , key : & str , value : & [ u8 ] ) {
149- let name = CString :: new ( key) . unwrap ( ) ;
150- unsafe {
151- ffi:: AMediaFormat_setBuffer (
152- self . as_ptr ( ) ,
153- name. as_ptr ( ) ,
154- value. as_ptr ( ) . cast ( ) ,
155- value. len ( ) ,
156- )
157- } ;
158- }
159-
160- #[ cfg( feature = "api-level-28" ) ]
161- pub fn f64 ( & self , key : & str ) -> Option < f64 > {
162- let name = CString :: new ( key) . unwrap ( ) ;
163- let mut out = 0.0 ;
164- if unsafe { ffi:: AMediaFormat_getDouble ( self . as_ptr ( ) , name. as_ptr ( ) , & mut out) } {
165- Some ( out)
166- } else {
167- None
168- }
169- }
170-
171- /// Returns (left, top, right, bottom)
172- #[ cfg( feature = "api-level-28" ) ]
173- pub fn rect ( & self , key : & str ) -> Option < ( i32 , i32 , i32 , i32 ) > {
174- let name = CString :: new ( key) . unwrap ( ) ;
175- let mut left = 0 ;
176- let mut top = 0 ;
177- let mut right = 0 ;
178- let mut bottom = 0 ;
179- if unsafe {
180- ffi:: AMediaFormat_getRect (
181- self . as_ptr ( ) ,
182- name. as_ptr ( ) ,
183- & mut left,
184- & mut top,
185- & mut right,
186- & mut bottom,
187- )
188- } {
189- Some ( ( left, top, right, bottom) )
190- } else {
191- None
192- }
193- }
194-
195- #[ cfg( feature = "api-level-28" ) ]
196- pub fn set_f64 ( & self , key : & str , value : f64 ) {
197- let name = CString :: new ( key) . unwrap ( ) ;
198- unsafe { ffi:: AMediaFormat_setDouble ( self . as_ptr ( ) , name. as_ptr ( ) , value) } ;
199- }
200-
201- #[ cfg( feature = "api-level-28" ) ]
202- pub fn set_rect ( & self , key : & str , left : i32 , top : i32 , right : i32 , bottom : i32 ) {
203- let name = CString :: new ( key) . unwrap ( ) ;
204- unsafe {
205- ffi:: AMediaFormat_setRect ( self . as_ptr ( ) , name. as_ptr ( ) , left, top, right, bottom)
206- } ;
207- }
208-
209- #[ cfg( feature = "api-level-28" ) ]
210- pub fn set_usize ( & self , key : & str , value : usize ) {
211- let name = CString :: new ( key) . unwrap ( ) ;
212- unsafe { ffi:: AMediaFormat_setSize ( self . as_ptr ( ) , name. as_ptr ( ) , value) } ;
213- }
214- }
215-
216- impl Drop for MediaFormat {
217- fn drop ( & mut self ) {
218- let status = unsafe { ffi:: AMediaFormat_delete ( self . as_ptr ( ) ) } ;
219- MediaError :: from_status ( status) . unwrap ( ) ;
220- }
221- }
222-
22326/// A native [`AMediaCodec *`]
22427///
22528/// [`AMediaCodec *`]: https://developer.android.com/ndk/reference/group/media#amediacodec
@@ -256,8 +59,8 @@ pub struct AsyncNotifyCallback {
25659 pub on_error : Option < ErrorCallback > ,
25760}
25861
259- impl std :: fmt:: Debug for AsyncNotifyCallback {
260- fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
62+ impl fmt:: Debug for AsyncNotifyCallback {
63+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
26164 f. debug_struct ( "AsyncNotifyCallback" )
26265 . field (
26366 "on_input_available" ,
@@ -600,13 +403,13 @@ impl MediaCodec {
600403 pub fn input_format ( & self ) -> MediaFormat {
601404 let inner = NonNull :: new ( unsafe { ffi:: AMediaCodec_getInputFormat ( self . as_ptr ( ) ) } )
602405 . expect ( "AMediaCodec_getInputFormat returned NULL" ) ;
603- MediaFormat { inner }
406+ unsafe { MediaFormat :: from_ptr ( inner) }
604407 }
605408
606409 pub fn output_format ( & self ) -> MediaFormat {
607410 let inner = NonNull :: new ( unsafe { ffi:: AMediaCodec_getOutputFormat ( self . as_ptr ( ) ) } )
608411 . expect ( "AMediaCodec_getOutputFormat returned NULL" ) ;
609- MediaFormat { inner }
412+ unsafe { MediaFormat :: from_ptr ( inner) }
610413 }
611414
612415 #[ cfg( feature = "api-level-28" ) ]
@@ -774,7 +577,7 @@ impl OutputBuffer<'_> {
774577 ffi:: AMediaCodec_getBufferFormat ( self . codec . as_ptr ( ) , self . index )
775578 } )
776579 . expect ( "AMediaCodec_getBufferFormat returned NULL" ) ;
777- MediaFormat { inner }
580+ unsafe { MediaFormat :: from_ptr ( inner) }
778581 }
779582
780583 pub fn info ( & self ) -> & BufferInfo {
0 commit comments