MainActivity.cs 5.05 KB
Newer Older
1 2
using System;
using System.Runtime.InteropServices;
3
using System.Threading;
4 5 6 7 8 9 10 11

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

12 13 14
using Org.Linphone.Mediastream.Video;
using Org.Linphone.Mediastream.Video.Display;

15 16
namespace LinphoneXamarin.Droid
{
17 18
    [Activity(Label = "LinphoneXamarin", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity,  AndroidVideoWindowImpl.IVideoWindowListener
19
    {
20 21 22 23 24 25 26 27
        private const string LIBNAME = "liblinphone-armeabi-v7a";
        private LinphoneXamarin.App app;
        private System.Threading.Thread coreIterate;
        private GL2JNIView view1;
        private SurfaceView view2;
        private AndroidVideoWindowImpl androidVideo;

        [DllImport(LIBNAME)]
28 29
        static extern void ms_set_jvm_from_env(IntPtr jnienv);

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        [DllImport(LIBNAME)]
        static extern void setAndroidLogHandler();

        [DllImport(LIBNAME)]
        static extern void setMediastreamerAndroidContext(IntPtr jnienv, IntPtr context);

        [DllImport(LIBNAME)]
        static extern void linphone_core_iterate(IntPtr lc);

        [DllImport(LIBNAME)]
        static extern void linphone_core_set_native_video_window_id(IntPtr lc, IntPtr id);

        [DllImport(LIBNAME)]
        static extern void linphone_core_set_native_preview_window_id(IntPtr lc, IntPtr id);

        [DllImport(LIBNAME)]
        static extern void linphone_core_enable_video(IntPtr lc, int enable_c, int enable_d);

        [DllImport(LIBNAME)]
        static extern IntPtr linphone_core_create_call_params(IntPtr lc, IntPtr call);

        [DllImport(LIBNAME)]
        static extern void linphone_call_params_enable_video(IntPtr callp, int enabled);

        [DllImport(LIBNAME)]
        static extern IntPtr linphone_address_new(string to);

        [DllImport(LIBNAME)]
        static extern IntPtr linphone_core_invite_address_with_params(IntPtr lc, IntPtr to, IntPtr callp);

        void LinphoneCoreIterate()
        {
            while (true)
            {
                RunOnUiThread(() => linphone_core_iterate(app.corePtr));
                System.Threading.Thread.Sleep(50);
            }
        }

69 70 71 72 73 74 75 76 77 78 79 80 81
        protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			global::Xamarin.Forms.Forms.Init (this, bundle);

            Java.Lang.JavaSystem.LoadLibrary("bctoolbox-armeabi-v7a");
            Java.Lang.JavaSystem.LoadLibrary("ortp-armeabi-v7a");
            Java.Lang.JavaSystem.LoadLibrary("mediastreamer_base-armeabi-v7a");
            Java.Lang.JavaSystem.LoadLibrary("mediastreamer_voip-armeabi-v7a");
            Java.Lang.JavaSystem.LoadLibrary("linphone-armeabi-v7a");

            ms_set_jvm_from_env(Android.Runtime.JNIEnv.Handle);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            setAndroidLogHandler();
            setMediastreamerAndroidContext(Android.Runtime.JNIEnv.Handle, this.Handle);

            app = new LinphoneXamarin.App();
            LoadApplication(app);

            coreIterate = new Thread(LinphoneCoreIterate);
            coreIterate.IsBackground = false;
            coreIterate.Start();

            view1 = new GL2JNIView(this);
            view1.LayoutParameters = new LinearLayout.LayoutParams(500, 500);
            view2 = new SurfaceView(this);
            view2.LayoutParameters = new LinearLayout.LayoutParams(500, 500);
            LinearLayout ll = new LinearLayout(this);
            ll.Orientation = Orientation.Vertical;
            ll.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
            ll.AddView(view1);
            ll.AddView(view2);
            SetContentView(ll);
            view1.SetZOrderOnTop(false);
            view2.SetZOrderOnTop(true);

            androidVideo = new AndroidVideoWindowImpl(view1, view2, this);
        }

        protected override void OnResume()
        {
            base.OnResume();

            linphone_core_enable_video(app.corePtr, 1, 1);
            IntPtr callp = linphone_core_create_call_params(app.corePtr, IntPtr.Zero);
            linphone_call_params_enable_video(callp, 1);
            IntPtr to = linphone_address_new("sip:sylvain@sip.linphone.org");
            linphone_core_invite_address_with_params(app.corePtr, to, callp);
            linphone_core_set_native_video_window_id(app.corePtr, androidVideo.Handle);
        }

        public void OnVideoPreviewSurfaceDestroyed(AndroidVideoWindowImpl p0)
        {

        }

        public void OnVideoPreviewSurfaceReady(AndroidVideoWindowImpl p0, SurfaceView p1)
        {
            linphone_core_set_native_preview_window_id(app.corePtr, p1.Handle);
        }

        public void OnVideoRenderingSurfaceDestroyed(AndroidVideoWindowImpl p0)
        {

        }
134

135 136 137
        public void OnVideoRenderingSurfaceReady(AndroidVideoWindowImpl p0, SurfaceView p1)
        {
            linphone_core_set_native_video_window_id(app.corePtr, p0.Handle);
138
        }
139
    }
140 141
}