2 * Copyright 2015, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaSync-JNI"
19 #include <utils/Log.h>
21 #include "android_media_MediaSync.h"
23 #include "android_media_AudioTrack.h"
24 #include "android_media_PlaybackParams.h"
25 #include "android_media_SyncParams.h"
26 #include "android_runtime/AndroidRuntime.h"
27 #include "android_runtime/android_view_Surface.h"
29 #include <nativehelper/JNIHelp.h>
31 #include <gui/Surface.h>
33 #include <media/AudioResamplerPublic.h>
34 #include <media/AudioTrack.h>
35 #include <media/stagefright/MediaClock.h>
36 #include <media/stagefright/MediaSync.h>
37 #include <media/stagefright/foundation/ADebug.h>
38 #include <media/stagefright/foundation/AString.h>
40 #include <nativehelper/ScopedLocalRef.h>
46 jfieldID mediaTimestampMediaTimeUsID;
47 jfieldID mediaTimestampNanoTimeID;
48 jfieldID mediaTimestampClockRateID;
51 static fields_t gFields;
52 static PlaybackParams::fields_t gPlaybackParamsFields;
53 static SyncParams::fields_t gSyncParamsFields;
55 ////////////////////////////////////////////////////////////////////////////////
57 JMediaSync::JMediaSync() {
58 mSync = MediaSync::create();
61 JMediaSync::~JMediaSync() {
64 status_t JMediaSync::setSurface(const sp<IGraphicBufferProducer> &bufferProducer) {
65 return mSync->setSurface(bufferProducer);
68 status_t JMediaSync::setAudioTrack(const sp<AudioTrack> &audioTrack) {
69 return mSync->setAudioTrack(audioTrack);
72 status_t JMediaSync::createInputSurface(
73 sp<IGraphicBufferProducer>* bufferProducer) {
74 return mSync->createInputSurface(bufferProducer);
77 sp<const MediaClock> JMediaSync::getMediaClock() {
78 return mSync->getMediaClock();
81 status_t JMediaSync::setPlaybackParams(const AudioPlaybackRate& rate) {
82 return mSync->setPlaybackSettings(rate);
85 void JMediaSync::getPlaybackParams(AudioPlaybackRate* rate /* nonnull */) {
86 mSync->getPlaybackSettings(rate);
89 status_t JMediaSync::setSyncParams(const AVSyncSettings& syncParams) {
90 return mSync->setSyncSettings(syncParams);
93 void JMediaSync::getSyncParams(AVSyncSettings* syncParams /* nonnull */) {
94 mSync->getSyncSettings(syncParams);
97 status_t JMediaSync::setVideoFrameRateHint(float rate) {
98 return mSync->setVideoFrameRateHint(rate);
101 float JMediaSync::getVideoFrameRate() {
102 return mSync->getVideoFrameRate();
105 void JMediaSync::flush() {
109 status_t JMediaSync::updateQueuedAudioData(
110 int sizeInBytes, int64_t presentationTimeUs) {
111 return mSync->updateQueuedAudioData(sizeInBytes, presentationTimeUs);
114 status_t JMediaSync::getPlayTimeForPendingAudioFrames(int64_t *outTimeUs) {
115 return mSync->getPlayTimeForPendingAudioFrames(outTimeUs);
118 } // namespace android
120 ////////////////////////////////////////////////////////////////////////////////
122 using namespace android;
124 static sp<JMediaSync> setMediaSync(JNIEnv *env, jobject thiz, const sp<JMediaSync> &sync) {
125 sp<JMediaSync> old = (JMediaSync *)env->GetLongField(thiz, gFields.context);
127 sync->incStrong(thiz);
130 old->decStrong(thiz);
133 env->SetLongField(thiz, gFields.context, (jlong)sync.get());
138 static sp<JMediaSync> getMediaSync(JNIEnv *env, jobject thiz) {
139 return (JMediaSync *)env->GetLongField(thiz, gFields.context);
142 static void android_media_MediaSync_release(JNIEnv *env, jobject thiz) {
143 setMediaSync(env, thiz, NULL);
146 static void throwExceptionAsNecessary(
147 JNIEnv *env, status_t err, const char *msg = NULL) {
153 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
157 case INVALID_OPERATION:
162 AString msgWithErrorCode(msg == NULL ? "" : msg);
163 msgWithErrorCode.append(" error:");
164 msgWithErrorCode.append(err);
165 jniThrowException(env, "java/lang/IllegalStateException", msgWithErrorCode.c_str());
170 static void android_media_MediaSync_native_setSurface(
171 JNIEnv *env, jobject thiz, jobject jsurface) {
172 ALOGV("android_media_MediaSync_setSurface");
174 sp<JMediaSync> sync = getMediaSync(env, thiz);
176 throwExceptionAsNecessary(env, INVALID_OPERATION);
180 sp<IGraphicBufferProducer> bufferProducer;
181 if (jsurface != NULL) {
182 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
183 if (surface != NULL) {
184 bufferProducer = surface->getIGraphicBufferProducer();
186 throwExceptionAsNecessary(env, BAD_VALUE, "The surface has been released");
191 status_t err = sync->setSurface(bufferProducer);
193 if (err == INVALID_OPERATION) {
194 throwExceptionAsNecessary(
195 env, INVALID_OPERATION, "Surface has already been configured");
196 } if (err != NO_ERROR) {
197 AString msg("Failed to connect to surface with error ");
199 throwExceptionAsNecessary(env, BAD_VALUE, msg.c_str());
203 static void android_media_MediaSync_native_setAudioTrack(
204 JNIEnv *env, jobject thiz, jobject jaudioTrack) {
205 ALOGV("android_media_MediaSync_setAudioTrack");
207 sp<JMediaSync> sync = getMediaSync(env, thiz);
209 throwExceptionAsNecessary(env, INVALID_OPERATION);
213 sp<AudioTrack> audioTrack;
214 if (jaudioTrack != NULL) {
215 audioTrack = android_media_AudioTrack_getAudioTrack(env, jaudioTrack);
216 if (audioTrack == NULL) {
217 throwExceptionAsNecessary(env, BAD_VALUE, "The audio track has been released");
222 status_t err = sync->setAudioTrack(audioTrack);
224 if (err == INVALID_OPERATION) {
225 throwExceptionAsNecessary(
226 env, INVALID_OPERATION, "Audio track has already been configured");
227 } if (err != NO_ERROR) {
228 AString msg("Failed to configure audio track with error ");
230 throwExceptionAsNecessary(env, BAD_VALUE, msg.c_str());
234 static jobject android_media_MediaSync_createInputSurface(
235 JNIEnv* env, jobject thiz) {
236 ALOGV("android_media_MediaSync_createInputSurface");
238 sp<JMediaSync> sync = getMediaSync(env, thiz);
240 throwExceptionAsNecessary(env, INVALID_OPERATION);
244 // Tell the MediaSync that we want to use a Surface as input.
245 sp<IGraphicBufferProducer> bufferProducer;
246 status_t err = sync->createInputSurface(&bufferProducer);
247 if (err != NO_ERROR) {
248 throwExceptionAsNecessary(env, INVALID_OPERATION);
252 // Wrap the IGBP in a Java-language Surface.
253 return android_view_Surface_createFromIGraphicBufferProducer(env,
257 static void android_media_MediaSync_native_updateQueuedAudioData(
258 JNIEnv *env, jobject thiz, jint sizeInBytes, jlong presentationTimeUs) {
259 sp<JMediaSync> sync = getMediaSync(env, thiz);
261 throwExceptionAsNecessary(env, INVALID_OPERATION);
265 status_t err = sync->updateQueuedAudioData(sizeInBytes, presentationTimeUs);
266 if (err != NO_ERROR) {
267 throwExceptionAsNecessary(env, err);
272 static jboolean android_media_MediaSync_native_getTimestamp(
273 JNIEnv *env, jobject thiz, jobject timestamp) {
274 sp<JMediaSync> sync = getMediaSync(env, thiz);
276 throwExceptionAsNecessary(env, INVALID_OPERATION);
280 sp<const MediaClock> mediaClock = sync->getMediaClock();
281 if (mediaClock == NULL) {
285 int64_t nowUs = ALooper::GetNowUs();
287 if (mediaClock->getMediaTime(nowUs, &mediaUs) != OK) {
291 env->SetLongField(timestamp, gFields.mediaTimestampMediaTimeUsID,
293 env->SetLongField(timestamp, gFields.mediaTimestampNanoTimeID,
294 (jlong)(nowUs * 1000));
295 env->SetFloatField(timestamp, gFields.mediaTimestampClockRateID,
296 (jfloat)mediaClock->getPlaybackRate());
300 static jlong android_media_MediaSync_native_getPlayTimeForPendingAudioFrames(
301 JNIEnv *env, jobject thiz) {
302 sp<JMediaSync> sync = getMediaSync(env, thiz);
304 throwExceptionAsNecessary(env, INVALID_OPERATION);
307 int64_t playTimeUs = 0;
308 status_t err = sync->getPlayTimeForPendingAudioFrames(&playTimeUs);
309 if (err != NO_ERROR) {
310 throwExceptionAsNecessary(env, err);
312 return (jlong)playTimeUs;
315 static jfloat android_media_MediaSync_setPlaybackParams(
316 JNIEnv *env, jobject thiz, jobject params) {
317 sp<JMediaSync> sync = getMediaSync(env, thiz);
319 throwExceptionAsNecessary(env, INVALID_OPERATION);
324 pbs.fillFromJobject(env, gPlaybackParamsFields, params);
325 ALOGV("setPlaybackParams: %d:%f %d:%f %d:%u %d:%u",
326 pbs.speedSet, pbs.audioRate.mSpeed,
327 pbs.pitchSet, pbs.audioRate.mPitch,
328 pbs.audioFallbackModeSet, pbs.audioRate.mFallbackMode,
329 pbs.audioStretchModeSet, pbs.audioRate.mStretchMode);
331 AudioPlaybackRate rate;
332 sync->getPlaybackParams(&rate);
333 bool updatedRate = false;
335 rate.mSpeed = pbs.audioRate.mSpeed;
339 rate.mPitch = pbs.audioRate.mPitch;
342 if (pbs.audioFallbackModeSet) {
343 rate.mFallbackMode = pbs.audioRate.mFallbackMode;
346 if (pbs.audioStretchModeSet) {
347 rate.mStretchMode = pbs.audioRate.mStretchMode;
351 status_t err = sync->setPlaybackParams(rate);
353 throwExceptionAsNecessary(env, err);
358 sp<const MediaClock> mediaClock = sync->getMediaClock();
359 if (mediaClock == NULL) {
363 return (jfloat)mediaClock->getPlaybackRate();
366 static jobject android_media_MediaSync_getPlaybackParams(
367 JNIEnv *env, jobject thiz) {
368 sp<JMediaSync> sync = getMediaSync(env, thiz);
370 throwExceptionAsNecessary(env, INVALID_OPERATION);
375 AudioPlaybackRate &audioRate = pbs.audioRate;
376 sync->getPlaybackParams(&audioRate);
377 ALOGV("getPlaybackParams: %f %f %d %d",
378 audioRate.mSpeed, audioRate.mPitch, audioRate.mFallbackMode, audioRate.mStretchMode);
382 pbs.audioFallbackModeSet = true;
383 pbs.audioStretchModeSet = true;
385 return pbs.asJobject(env, gPlaybackParamsFields);
388 static jfloat android_media_MediaSync_setSyncParams(
389 JNIEnv *env, jobject thiz, jobject params) {
390 sp<JMediaSync> sync = getMediaSync(env, thiz);
392 throwExceptionAsNecessary(env, INVALID_OPERATION);
397 scs.fillFromJobject(env, gSyncParamsFields, params);
398 ALOGV("setSyncParams: %d:%d %d:%d %d:%f %d:%f",
399 scs.syncSourceSet, scs.sync.mSource,
400 scs.audioAdjustModeSet, scs.sync.mAudioAdjustMode,
401 scs.toleranceSet, scs.sync.mTolerance,
402 scs.frameRateSet, scs.frameRate);
404 AVSyncSettings avsync;
405 sync->getSyncParams(&avsync);
406 bool updatedSync = false;
408 if (scs.syncSourceSet) {
409 avsync.mSource = scs.sync.mSource;
412 if (scs.audioAdjustModeSet) {
413 avsync.mAudioAdjustMode = scs.sync.mAudioAdjustMode;
416 if (scs.toleranceSet) {
417 avsync.mTolerance = scs.sync.mTolerance;
421 err = sync->setSyncParams(avsync);
424 if (scs.frameRateSet && err == OK) {
425 err = sync->setVideoFrameRateHint(scs.frameRate);
428 throwExceptionAsNecessary(env, err);
432 sp<const MediaClock> mediaClock = sync->getMediaClock();
433 if (mediaClock == NULL) {
437 return (jfloat)mediaClock->getPlaybackRate();
440 static jobject android_media_MediaSync_getSyncParams(JNIEnv *env, jobject thiz) {
441 sp<JMediaSync> sync = getMediaSync(env, thiz);
443 throwExceptionAsNecessary(env, INVALID_OPERATION);
448 sync->getSyncParams(&scs.sync);
449 scs.frameRate = sync->getVideoFrameRate();
451 ALOGV("getSyncParams: %d %d %f %f",
452 scs.sync.mSource, scs.sync.mAudioAdjustMode, scs.sync.mTolerance, scs.frameRate);
454 // sanity check params
455 if (scs.sync.mSource >= AVSYNC_SOURCE_MAX
456 || scs.sync.mAudioAdjustMode >= AVSYNC_AUDIO_ADJUST_MODE_MAX
457 || scs.sync.mTolerance < 0.f
458 || scs.sync.mTolerance >= AVSYNC_TOLERANCE_MAX) {
459 throwExceptionAsNecessary(env, INVALID_OPERATION);
463 scs.syncSourceSet = true;
464 scs.audioAdjustModeSet = true;
465 scs.toleranceSet = true;
466 scs.frameRateSet = scs.frameRate >= 0.f;
468 return scs.asJobject(env, gSyncParamsFields);
471 static void android_media_MediaSync_native_flush(JNIEnv *env, jobject thiz) {
472 sp<JMediaSync> sync = getMediaSync(env, thiz);
474 throwExceptionAsNecessary(env, INVALID_OPERATION);
481 static void android_media_MediaSync_native_init(JNIEnv *env) {
482 ScopedLocalRef<jclass> clazz(env, env->FindClass("android/media/MediaSync"));
483 CHECK(clazz.get() != NULL);
485 gFields.context = env->GetFieldID(clazz.get(), "mNativeContext", "J");
486 CHECK(gFields.context != NULL);
488 clazz.reset(env->FindClass("android/media/MediaTimestamp"));
489 CHECK(clazz.get() != NULL);
491 gFields.mediaTimestampMediaTimeUsID =
492 env->GetFieldID(clazz.get(), "mediaTimeUs", "J");
493 CHECK(gFields.mediaTimestampMediaTimeUsID != NULL);
495 gFields.mediaTimestampNanoTimeID =
496 env->GetFieldID(clazz.get(), "nanoTime", "J");
497 CHECK(gFields.mediaTimestampNanoTimeID != NULL);
499 gFields.mediaTimestampClockRateID =
500 env->GetFieldID(clazz.get(), "clockRate", "F");
501 CHECK(gFields.mediaTimestampClockRateID != NULL);
503 gSyncParamsFields.init(env);
504 gPlaybackParamsFields.init(env);
507 static void android_media_MediaSync_native_setup(JNIEnv *env, jobject thiz) {
508 sp<JMediaSync> sync = new JMediaSync();
510 setMediaSync(env, thiz, sync);
513 static void android_media_MediaSync_native_finalize(JNIEnv *env, jobject thiz) {
514 android_media_MediaSync_release(env, thiz);
517 static JNINativeMethod gMethods[] = {
518 { "native_setSurface",
519 "(Landroid/view/Surface;)V",
520 (void *)android_media_MediaSync_native_setSurface },
522 { "native_setAudioTrack",
523 "(Landroid/media/AudioTrack;)V",
524 (void *)android_media_MediaSync_native_setAudioTrack },
526 { "createInputSurface", "()Landroid/view/Surface;",
527 (void *)android_media_MediaSync_createInputSurface },
529 { "native_updateQueuedAudioData",
531 (void *)android_media_MediaSync_native_updateQueuedAudioData },
533 { "native_getTimestamp",
534 "(Landroid/media/MediaTimestamp;)Z",
535 (void *)android_media_MediaSync_native_getTimestamp },
537 { "native_getPlayTimeForPendingAudioFrames",
539 (void *)android_media_MediaSync_native_getPlayTimeForPendingAudioFrames },
541 { "native_flush", "()V", (void *)android_media_MediaSync_native_flush },
543 { "native_init", "()V", (void *)android_media_MediaSync_native_init },
545 { "native_setup", "()V", (void *)android_media_MediaSync_native_setup },
547 { "native_release", "()V", (void *)android_media_MediaSync_release },
549 { "native_setPlaybackParams", "(Landroid/media/PlaybackParams;)F",
550 (void *)android_media_MediaSync_setPlaybackParams },
552 { "getPlaybackParams", "()Landroid/media/PlaybackParams;",
553 (void *)android_media_MediaSync_getPlaybackParams },
555 { "native_setSyncParams", "(Landroid/media/SyncParams;)F",
556 (void *)android_media_MediaSync_setSyncParams },
558 { "getSyncParams", "()Landroid/media/SyncParams;",
559 (void *)android_media_MediaSync_getSyncParams },
561 { "native_finalize", "()V", (void *)android_media_MediaSync_native_finalize },
564 int register_android_media_MediaSync(JNIEnv *env) {
565 return AndroidRuntime::registerNativeMethods(
566 env, "android/media/MediaSync", gMethods, NELEM(gMethods));