1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkMediaMuxer"
19
20
21 #include <media/NdkMediaMuxer.h>
22 #include <media/NdkMediaCodec.h>
23 #include <media/NdkMediaErrorPriv.h>
24 #include <media/NdkMediaFormatPriv.h>
25
26
27 #include <utils/Log.h>
28 #include <utils/StrongPointer.h>
29 #include <media/stagefright/foundation/ABuffer.h>
30 #include <media/stagefright/foundation/AMessage.h>
31 #include <media/stagefright/MediaMuxer.h>
32 #include <media/IMediaHTTPService.h>
33 #include <android_util_Binder.h>
34
35 #include <jni.h>
36
37 using namespace android;
38
39 struct AMediaMuxer {
40 sp<MediaMuxer> mImpl;
41
42 };
43
44 extern "C" {
45
46 EXPORT
AMediaMuxer_new(int fd,OutputFormat format)47 AMediaMuxer* AMediaMuxer_new(int fd, OutputFormat format) {
48 ALOGV("ctor");
49 AMediaMuxer *mData = new AMediaMuxer();
50 mData->mImpl = new MediaMuxer(fd, (android::MediaMuxer::OutputFormat)format);
51 return mData;
52 }
53
54 EXPORT
AMediaMuxer_delete(AMediaMuxer * muxer)55 media_status_t AMediaMuxer_delete(AMediaMuxer *muxer) {
56 ALOGV("dtor");
57 delete muxer;
58 return AMEDIA_OK;
59 }
60
61 EXPORT
AMediaMuxer_setLocation(AMediaMuxer * muxer,float latitude,float longtitude)62 media_status_t AMediaMuxer_setLocation(AMediaMuxer *muxer, float latitude, float longtitude) {
63 return translate_error(muxer->mImpl->setLocation(latitude * 10000, longtitude * 10000));
64 }
65
66 EXPORT
AMediaMuxer_setOrientationHint(AMediaMuxer * muxer,int degrees)67 media_status_t AMediaMuxer_setOrientationHint(AMediaMuxer *muxer, int degrees) {
68 return translate_error(muxer->mImpl->setOrientationHint(degrees));
69 }
70
71 EXPORT
AMediaMuxer_addTrack(AMediaMuxer * muxer,const AMediaFormat * format)72 ssize_t AMediaMuxer_addTrack(AMediaMuxer *muxer, const AMediaFormat *format) {
73 sp<AMessage> msg;
74 AMediaFormat_getFormat(format, &msg);
75 ssize_t ret = muxer->mImpl->addTrack(msg);
76 return (ret >= 0) ? ret : translate_error(ret);
77 }
78
79 EXPORT
AMediaMuxer_start(AMediaMuxer * muxer)80 media_status_t AMediaMuxer_start(AMediaMuxer *muxer) {
81 return translate_error(muxer->mImpl->start());
82 }
83
84 EXPORT
AMediaMuxer_stop(AMediaMuxer * muxer)85 media_status_t AMediaMuxer_stop(AMediaMuxer *muxer) {
86 return translate_error(muxer->mImpl->stop());
87 }
88
89 EXPORT
AMediaMuxer_writeSampleData(AMediaMuxer * muxer,size_t trackIdx,const uint8_t * data,const AMediaCodecBufferInfo * info)90 media_status_t AMediaMuxer_writeSampleData(AMediaMuxer *muxer,
91 size_t trackIdx, const uint8_t *data, const AMediaCodecBufferInfo *info) {
92 sp<ABuffer> buf = new ABuffer((void*)(data + info->offset), info->size);
93 return translate_error(
94 muxer->mImpl->writeSampleData(buf, trackIdx, info->presentationTimeUs, info->flags));
95 }
96
97
98 } // extern "C"
99
100