Skip to content

Commit

Permalink
make ALWebRTC ready for android Pie
Browse files Browse the repository at this point in the history
  • Loading branch information
zeus64 committed Aug 10, 2019
1 parent dedb177 commit d8c36fa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
5 changes: 3 additions & 2 deletions source/ALAndroidWebRTCApi.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ interface
uses Androidapi.JNIBridge,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App;
Androidapi.JNI.App,
Androidapi.JNI.OpenGL;

type

Expand Down Expand Up @@ -280,7 +281,7 @@ TJALWebRTC_PeerConnectionParameters = class(TJavaGenericImport<JALWebRTC_PeerC
['{3F5C8CF8-CEB7-4C92-B6E4-FD43F27BF995}']
{class} procedure initializeLibrary(appContext: JContext); cdecl;
{class} procedure finalizeLibrary; cdecl;
{class} function init(appContext: JContext; eglContext: int64; iceServers: JList; peerConnectionParameters: JALWebRTC_PeerConnectionParameters): JALWebRTC; cdecl;
{class} function init(appContext: JContext; eglContext: Jopengl_EGLContext; iceServers: JList; peerConnectionParameters: JALWebRTC_PeerConnectionParameters): JALWebRTC; cdecl;
end;

{********************************************}
Expand Down
60 changes: 45 additions & 15 deletions source/ALVideoPlayer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ TALVideoListener = class(TJavaLocal, JSimpleExoPlayer_VideoListener)
procedure prepare(Const aDataSource: String);
procedure Start;
procedure Stop;
procedure seekTo(const msec: Integer);
procedure seekTo(const msec: Int64);
procedure setLooping(const looping: Boolean);
procedure setVolume(const Value: Single);
procedure setPlaybackSpeed(const Value: single);
property bitmap: TalTexture read fbitmap;
property OnError: TNotifyEvent read fOnErrorEvent write fOnErrorEvent;
property OnPrepared: TNotifyEvent read fOnPreparedEvent write fOnPreparedEvent;
Expand Down Expand Up @@ -270,9 +271,10 @@ TALNotificationsDelegate = class(TOCLocal, IALNotificationsDelegate)
procedure prepare(Const aDataSource: String);
procedure Start;
procedure Stop;
procedure seekTo(const msec: Integer);
procedure seekTo(const msec: Int64);
procedure setLooping(const looping: Boolean);
procedure setVolume(const Value: Single);
procedure setPlaybackSpeed(const Value: single);
property bitmap: TalTexture read fbitmap;
property OnError: TNotifyEvent read fOnErrorEvent write fOnErrorEvent;
property OnPrepared: TNotifyEvent read fOnPreparedEvent write fOnPreparedEvent;
Expand Down Expand Up @@ -308,9 +310,10 @@ TALWinVideoPlayer = class(Tobject)
procedure prepare(Const aDataSource: String);
procedure Start;
procedure Stop;
procedure seekTo(const msec: Integer);
procedure seekTo(const msec: Int64);
procedure setLooping(const looping: Boolean);
procedure setVolume(const Value: Single);
procedure setPlaybackSpeed(const Value: single);
property bitmap: TBitmap read fbitmap;
property OnError: TNotifyEvent read fOnErrorEvent write fOnErrorEvent;
property OnPrepared: TNotifyEvent read fOnPreparedEvent write fOnPreparedEvent;
Expand Down Expand Up @@ -346,7 +349,7 @@ TALMacOSVideoPlayer = class(Tobject)
procedure prepare(Const aDataSource: String);
procedure Start;
procedure Stop;
procedure seekTo(const msec: Integer);
procedure seekTo(const msec: Int64);
procedure setLooping(const looping: Boolean);
procedure setVolume(const Value: Single);
property bitmap: TBitmap read fbitmap;
Expand Down Expand Up @@ -410,9 +413,10 @@ TALVideoPlayer = class(TObject)
procedure prepare(Const aDataSource: String; const aAutoStartWhenPrepared: Boolean=False);
procedure Start;
procedure Stop;
procedure seekTo(const msec: Integer);
procedure seekTo(const msec: Int64);
procedure setLooping(const looping: Boolean);
procedure setVolume(const Value: Single);
procedure setPlaybackSpeed(const Value: single);
{$IF DEFINED(IOS) or DEFINED(ANDROID)}
property Bitmap: TALTexture read Getbitmap;
{$ELSE}
Expand Down Expand Up @@ -1010,8 +1014,8 @@ procedure TALAndroidVideoPlayer.stop;
{$ENDIF}
end;

{**********************************************************}
procedure TALAndroidVideoPlayer.seekTo(const msec: Integer);
{********************************************************}
procedure TALAndroidVideoPlayer.seekTo(const msec: Int64);
begin
if not (GetState in [vpsPrepared, vpsStarted, vpsPaused, vpsPlaybackCompleted]) then exit;
FSimpleExoPlayer.seekTo(msec); // The seek position in the current window, or TIME_UNSET to seek to the window's default position.
Expand Down Expand Up @@ -1062,6 +1066,15 @@ procedure TALAndroidVideoPlayer.setVolume(const Value: Single);
{$ENDIF}
end;

{********************************************************************}
procedure TALAndroidVideoPlayer.setPlaybackSpeed(const Value: single);
var aPlaybackParameters: JPlaybackParameters;
begin
aPlaybackParameters := tJPlaybackParameters.JavaClass.init(Value, 1);
FSimpleExoPlayer.setPlaybackParameters(aPlaybackParameters);
aPlaybackParameters := Nil;
end;

{***********************************************}
function TALAndroidVideoPlayer.getState: integer;
begin
Expand Down Expand Up @@ -1534,8 +1547,8 @@ procedure TALIOSVideoPlayer.Stop;
{$ENDIF}
end;

{******************************************************}
procedure TALIOSVideoPlayer.seekTo(const msec: Integer);
{****************************************************}
procedure TALIOSVideoPlayer.seekTo(const msec: Int64);
begin
if not (GetState in [vpsPrepared, vpsStarted, vpsPaused, vpsPlaybackCompleted]) then exit;
FPlayer.seekToTime(CMTimeMake(msec, 1));
Expand Down Expand Up @@ -1569,6 +1582,12 @@ procedure TALIOSVideoPlayer.setVolume(const Value: Single);
{$ENDIF}
end;

{****************************************************************}
procedure TALIOSVideoPlayer.setPlaybackSpeed(const Value: single);
begin
// not yet implemented
end;

{************************************************************}
procedure TALIOSVideoPlayer.doOnFrameRefresh(Sender: TObject);
var aPixelBuffer: CVPixelBufferRef;
Expand Down Expand Up @@ -1943,8 +1962,8 @@ procedure TALWinVideoPlayer.Stop;
begin
end;

{******************************************************}
procedure TALWinVideoPlayer.seekTo(const msec: Integer);
{****************************************************}
procedure TALWinVideoPlayer.seekTo(const msec: Int64);
begin
end;

Expand All @@ -1958,6 +1977,11 @@ procedure TALWinVideoPlayer.setVolume(const Value: Single);
begin
end;

{****************************************************************}
procedure TALWinVideoPlayer.setPlaybackSpeed(const Value: single);
begin
end;

{*******************************************}
function TALWinVideoPlayer.getState: integer;
begin
Expand Down Expand Up @@ -2039,8 +2063,8 @@ procedure TALMacOSVideoPlayer.Stop;
begin
end;

{********************************************************}
procedure TALMacOSVideoPlayer.seekTo(const msec: Integer);
{******************************************************}
procedure TALMacOSVideoPlayer.seekTo(const msec: Int64);
begin
end;

Expand Down Expand Up @@ -2166,8 +2190,8 @@ procedure TALVideoPlayer.prepare(Const aDataSource: String; const aAutoStartWhen
fVideoPlayerControl.prepare(aDataSource);
end;

{***************************************************}
procedure TALVideoPlayer.seekTo(const msec: Integer);
{*************************************************}
procedure TALVideoPlayer.seekTo(const msec: Int64);
begin
fVideoPlayerControl.seekTo(msec);
end;
Expand All @@ -2184,6 +2208,12 @@ procedure TALVideoPlayer.setVolume(const Value: Single);
fVideoPlayerControl.setVolume(Value);
end;

{*************************************************************}
procedure TALVideoPlayer.setPlaybackSpeed(const Value: single);
begin
fVideoPlayerControl.setPlaybackSpeed(Value);
end;

{*****************************}
procedure TALVideoPlayer.Start;
begin
Expand Down
4 changes: 2 additions & 2 deletions source/ALWebRTC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ implementation
Androidapi.JNI.GraphicsContentViewText,
Androidapi.jni,
Androidapi.jni.App,
Androidapi.JNI.OpenGL,
Androidapi.Helpers,
FMX.Context.GLES.Android,
{$ELSEIF defined(IOS)}
system.math,
Macapi.Helpers,
Expand Down Expand Up @@ -631,7 +631,7 @@ constructor TALWebRTC.Create(const aIceServers: TALWebRTCIceServers; const aPeer
aJPeerConnectionParameters.DataChannelId := aPeerConnectionParameters.DataChannelId;
//-----
fAndroidWebRTC := TJALWebRTC.Wrap(TJALWebRTC.JavaClass.init(TAndroidHelper.Context.getApplicationContext,
int64(TCustomAndroidContext(TContext3D.CurrentContext).SharedContext),
TJEGL14.JavaClass.eglGetCurrentContext,
aJListIceServers,
aJPeerConnectionParameters));
fAndroidWebRTCListener := TAndroidWebRTCListener.Create(Self);
Expand Down
49 changes: 36 additions & 13 deletions source/java/com/alcinoe/webrtc/ALWebRTC.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public class ALWebRTC {
private final SDPObserver mSDPObserver = new SDPObserver();
private final RemoteProxyVideoSink mRemoteProxyVideoSink = new RemoteProxyVideoSink();
private final LocalProxyVideoSink mLocalProxyVideoSink = new LocalProxyVideoSink();
private boolean mCanRestoreSavedAudioState = false;
private int mSavedAudioMode = AudioManager.MODE_INVALID;
private boolean mSavedIsSpeakerPhoneOn = false;
private boolean mSavedIsMicrophoneMute = false;

public static class PeerConnectionParameters {
public boolean videoCallEnabled = true; /* Enable video in a call. */
Expand Down Expand Up @@ -247,15 +251,15 @@ public void run() {

}

public ALWebRTC(Context appContext, long eglContext, List<PeerConnection.IceServer> iceServers, PeerConnectionParameters peerConnectionParameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
public ALWebRTC(Context appContext, android.opengl.EGLContext eglContext, List<PeerConnection.IceServer> iceServers, PeerConnectionParameters peerConnectionParameters) {

/* init local vars */
mAppContext = appContext;
mHandler = new Handler(mAppContext.getMainLooper());
mPeerConnectionParameters = peerConnectionParameters;
mPreferIsac = (mPeerConnectionParameters.audioCodec != null) &&
(mPeerConnectionParameters.audioCodec.equals(AUDIO_CODEC_ISAC));
mEglBase = EglBase.createEgl14(createSharedEGLContextObj(eglContext), EglBase.CONFIG_PLAIN);
mEglBase = EglBase.createEgl14(eglContext, EglBase.CONFIG_PLAIN);
mIceServers = iceServers;

}
Expand Down Expand Up @@ -419,15 +423,6 @@ public void setListener(Listener listener) {
mListener = listener;
}

private android.opengl.EGLContext createSharedEGLContextObj(long handle) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class<?> classType =Class.forName("android.opengl.EGLContext");
Class<?>[] types = new Class[] { long.class };
Constructor constructor=classType.getDeclaredConstructor(types);
constructor.setAccessible(true);
Object object=constructor.newInstance(handle);
return (android.opengl.EGLContext) object;
}

private @Nullable VideoCapturer createVideoCapturer() {
final VideoCapturer videoCapturer;
if (Camera2Enumerator.isSupported(mAppContext)) {
Expand Down Expand Up @@ -470,9 +465,25 @@ public boolean start() throws ClassNotFoundException, InstantiationException, Il
/* exit if already started */
if (mPeerConnectionFactory != null) { return mVideoCapturer != null; }

/* set the speaker on */
// init audioManager
AudioManager audioManager = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
if (!audioManager.isSpeakerphoneOn()) { audioManager.setSpeakerphoneOn(true); }

// Store current audio state so we can restore it when stop() is called.
mSavedAudioMode = audioManager.getMode();
mSavedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
mSavedIsMicrophoneMute = audioManager.isMicrophoneMute();
mCanRestoreSavedAudioState = true;

// By setting MODE_IN_COMMUNICATION as default audio mode. It is
// required to be in this mode when playout and/or recording starts for
// best possible VoIP performance.
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

// Always disable microphone mute during a WebRTC call.
if (mSavedIsMicrophoneMute) { audioManager.setMicrophoneMute(false); }

/* set the speaker on */
if (!mSavedIsSpeakerPhoneOn) { audioManager.setSpeakerphoneOn(true); }

/* create mPeerConnectionFactory */
mPeerConnectionFactory = createPeerConnectionFactory(new PeerConnectionFactory.Options());
Expand Down Expand Up @@ -592,6 +603,17 @@ public boolean start() throws ClassNotFoundException, InstantiationException, Il
}

public void stop() {

// Restore previously stored audio states.
if (mCanRestoreSavedAudioState) {
AudioManager audioManager = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(mSavedIsSpeakerPhoneOn);
audioManager.setMicrophoneMute(mSavedIsMicrophoneMute);
audioManager.setMode(mSavedAudioMode);
mCanRestoreSavedAudioState = false;
}

//free the objects
mListener = null;
if ((mPeerConnectionParameters.aecDump) &&
(mPeerConnectionFactory != null)) { mPeerConnectionFactory.stopAecDump(); }
Expand Down Expand Up @@ -628,6 +650,7 @@ public void stop() {
mPeerConnectionFactory = null;
}
mEglBase.release();

}

public void pauseVideoCapturer() {
Expand Down

0 comments on commit d8c36fa

Please sign in to comment.