2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package org.webrtc;
|
|
|
|
|
|
|
|
|
|
/** Java wrapper for a C++ MediaSourceInterface. */
|
|
|
|
|
public class MediaSource {
|
|
|
|
|
/** Tracks MediaSourceInterface.SourceState */
|
2017-12-12 12:52:54 +01:00
|
|
|
public enum State {
|
|
|
|
|
INITIALIZING,
|
|
|
|
|
LIVE,
|
|
|
|
|
ENDED,
|
|
|
|
|
MUTED;
|
|
|
|
|
|
|
|
|
|
@CalledByNative("State")
|
|
|
|
|
static State fromNativeIndex(int nativeIndex) {
|
|
|
|
|
return values()[nativeIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2020-02-03 16:09:45 +01:00
|
|
|
private final RefCountDelegate refCountDelegate;
|
2018-09-28 14:38:21 +02:00
|
|
|
private long nativeSource;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
public MediaSource(long nativeSource) {
|
2020-02-03 16:09:45 +01:00
|
|
|
refCountDelegate = new RefCountDelegate(() -> JniCommon.nativeReleaseRef(nativeSource));
|
2013-07-10 00:45:36 +00:00
|
|
|
this.nativeSource = nativeSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public State state() {
|
2018-09-28 14:38:21 +02:00
|
|
|
checkMediaSourceExists();
|
2017-12-20 15:12:10 +01:00
|
|
|
return nativeGetState(nativeSource);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-12 23:26:21 +00:00
|
|
|
public void dispose() {
|
2018-09-28 14:38:21 +02:00
|
|
|
checkMediaSourceExists();
|
2020-02-03 16:09:45 +01:00
|
|
|
refCountDelegate.release();
|
2018-09-28 14:38:21 +02:00
|
|
|
nativeSource = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns a pointer to webrtc::MediaSourceInterface. */
|
|
|
|
|
protected long getNativeMediaSource() {
|
|
|
|
|
checkMediaSourceExists();
|
|
|
|
|
return nativeSource;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 16:09:45 +01:00
|
|
|
/**
|
|
|
|
|
* Runs code in {@code runnable} holding a reference to the media source. If the object has
|
|
|
|
|
* already been released, does nothing.
|
|
|
|
|
*/
|
|
|
|
|
void runWithReference(Runnable runnable) {
|
|
|
|
|
if (refCountDelegate.safeRetain()) {
|
|
|
|
|
try {
|
|
|
|
|
runnable.run();
|
|
|
|
|
} finally {
|
|
|
|
|
refCountDelegate.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 14:38:21 +02:00
|
|
|
private void checkMediaSourceExists() {
|
|
|
|
|
if (nativeSource == 0) {
|
|
|
|
|
throw new IllegalStateException("MediaSource has been disposed.");
|
|
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 15:12:10 +01:00
|
|
|
private static native State nativeGetState(long pointer);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|