blob: 9245e3e2eb2bc6171fcace026a7906d811971490 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011package org.webrtc;
12
13/** Java wrapper for a C++ MediaSourceInterface. */
14public class MediaSource {
15 /** Tracks MediaSourceInterface.SourceState */
Magnus Jedvert9060eb12017-12-12 12:52:54 +010016 public enum State {
17 INITIALIZING,
18 LIVE,
19 ENDED,
20 MUTED;
21
22 @CalledByNative("State")
23 static State fromNativeIndex(int nativeIndex) {
24 return values()[nativeIndex];
25 }
26 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
Sami Kalliomäki0f6bcd12020-02-03 16:09:45 +010028 private final RefCountDelegate refCountDelegate;
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020029 private long nativeSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31 public MediaSource(long nativeSource) {
Sami Kalliomäki0f6bcd12020-02-03 16:09:45 +010032 refCountDelegate = new RefCountDelegate(() -> JniCommon.nativeReleaseRef(nativeSource));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 this.nativeSource = nativeSource;
34 }
35
36 public State state() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020037 checkMediaSourceExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010038 return nativeGetState(nativeSource);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 }
40
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000041 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020042 checkMediaSourceExists();
Sami Kalliomäki0f6bcd12020-02-03 16:09:45 +010043 refCountDelegate.release();
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020044 nativeSource = 0;
45 }
46
47 /** Returns a pointer to webrtc::MediaSourceInterface. */
48 protected long getNativeMediaSource() {
49 checkMediaSourceExists();
50 return nativeSource;
51 }
52
Sami Kalliomäki0f6bcd12020-02-03 16:09:45 +010053 /**
54 * Runs code in {@code runnable} holding a reference to the media source. If the object has
55 * already been released, does nothing.
56 */
57 void runWithReference(Runnable runnable) {
58 if (refCountDelegate.safeRetain()) {
59 try {
60 runnable.run();
61 } finally {
62 refCountDelegate.release();
63 }
64 }
65 }
66
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020067 private void checkMediaSourceExists() {
68 if (nativeSource == 0) {
69 throw new IllegalStateException("MediaSource has been disposed.");
70 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 }
72
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010073 private static native State nativeGetState(long pointer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074}