• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <android_util_Binder.h>
21 #include <jni.h>
22 #include <media/IMediaHTTPService.h>
23 #include <media/NdkMediaCodec.h>
24 #include <media/NdkMediaErrorPriv.h>
25 #include <media/NdkMediaFormatPriv.h>
26 #include <media/NdkMediaMuxer.h>
27 #include <media/stagefright/MediaAppender.h>
28 #include <media/stagefright/MediaMuxer.h>
29 #include <media/stagefright/foundation/ABuffer.h>
30 #include <media/stagefright/foundation/AMessage.h>
31 #include <utils/Log.h>
32 #include <utils/StrongPointer.h>
33 
34 using namespace android;
35 
36 struct AMediaMuxer {
37     sp<MediaMuxerBase> mImpl;
38 };
39 
40 extern "C" {
41 
42 EXPORT
AMediaMuxer_new(int fd,OutputFormat format)43 AMediaMuxer* AMediaMuxer_new(int fd, OutputFormat format) {
44     ALOGV("ctor");
45     AMediaMuxer *mData = new (std::nothrow) AMediaMuxer();
46     if (mData == nullptr) {
47         return nullptr;
48     }
49     mData->mImpl = new (std::nothrow) MediaMuxer(fd, (android::MediaMuxer::OutputFormat)format);
50     if (mData->mImpl == nullptr) {
51         delete mData;
52         return nullptr;
53     }
54     return mData;
55 }
56 
57 EXPORT
AMediaMuxer_delete(AMediaMuxer * muxer)58 media_status_t AMediaMuxer_delete(AMediaMuxer *muxer) {
59     ALOGV("dtor");
60     delete muxer;
61     return AMEDIA_OK;
62 }
63 
64 EXPORT
AMediaMuxer_setLocation(AMediaMuxer * muxer,float latitude,float longtitude)65 media_status_t AMediaMuxer_setLocation(AMediaMuxer *muxer, float latitude, float longtitude) {
66     return translate_error(muxer->mImpl->setLocation(latitude * 10000, longtitude * 10000));
67 }
68 
69 EXPORT
AMediaMuxer_setOrientationHint(AMediaMuxer * muxer,int degrees)70 media_status_t AMediaMuxer_setOrientationHint(AMediaMuxer *muxer, int degrees) {
71     return translate_error(muxer->mImpl->setOrientationHint(degrees));
72 }
73 
74 EXPORT
AMediaMuxer_addTrack(AMediaMuxer * muxer,const AMediaFormat * format)75 ssize_t AMediaMuxer_addTrack(AMediaMuxer *muxer, const AMediaFormat *format) {
76     sp<AMessage> msg;
77     AMediaFormat_getFormat(format, &msg);
78     ssize_t ret = muxer->mImpl->addTrack(msg);
79     return (ret >= 0) ? ret : translate_error(ret);
80 }
81 
82 EXPORT
AMediaMuxer_start(AMediaMuxer * muxer)83 media_status_t AMediaMuxer_start(AMediaMuxer *muxer) {
84     return translate_error(muxer->mImpl->start());
85 }
86 
87 EXPORT
AMediaMuxer_stop(AMediaMuxer * muxer)88 media_status_t AMediaMuxer_stop(AMediaMuxer *muxer) {
89     return translate_error(muxer->mImpl->stop());
90 }
91 
92 EXPORT
AMediaMuxer_writeSampleData(AMediaMuxer * muxer,size_t trackIdx,const uint8_t * data,const AMediaCodecBufferInfo * info)93 media_status_t AMediaMuxer_writeSampleData(AMediaMuxer *muxer,
94         size_t trackIdx, const uint8_t *data, const AMediaCodecBufferInfo *info) {
95     sp<ABuffer> buf = new ABuffer((void*)(data + info->offset), info->size);
96     return translate_error(
97             muxer->mImpl->writeSampleData(buf, trackIdx, info->presentationTimeUs, info->flags));
98 }
99 
100 EXPORT
AMediaMuxer_append(int fd,AppendMode mode)101 AMediaMuxer* AMediaMuxer_append(int fd, AppendMode mode) {
102     ALOGV("append");
103     AMediaMuxer* mData = new (std::nothrow) AMediaMuxer();
104     if (mData == nullptr) {
105         return nullptr;
106     }
107     mData->mImpl = MediaAppender::create(fd, (android::MediaAppender::AppendMode)mode);
108     if (mData->mImpl == nullptr) {
109         delete mData;
110         return nullptr;
111     }
112     return mData;
113 }
114 
115 EXPORT
AMediaMuxer_getTrackCount(AMediaMuxer * muxer)116 ssize_t AMediaMuxer_getTrackCount(AMediaMuxer* muxer) {
117     return muxer->mImpl->getTrackCount();
118 }
119 
120 EXPORT
AMediaMuxer_getTrackFormat(AMediaMuxer * muxer,size_t idx)121 AMediaFormat* AMediaMuxer_getTrackFormat(AMediaMuxer* muxer, size_t idx) {
122     sp<AMessage> format = muxer->mImpl->getTrackFormat(idx);
123     if (format != nullptr) {
124         return AMediaFormat_fromMsg(&format);
125     }
126     return nullptr;
127 }
128 
129 } // extern "C"
130 
131