1 /*
2 LinphoneCoreFactoryImpl.java
3 Copyright (C) 2010  Belledonne Communications, Grenoble, France
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License fo r more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 package org.linphone.core;
20 
21 import android.content.Context;
22 import android.os.Build;
23 
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 import org.linphone.mediastream.Log;
30 import org.linphone.mediastream.MediastreamerAndroidContext;
31 import org.linphone.mediastream.Version;
32 import org.linphone.tools.OpenH264DownloadHelper;
33 
34 public class LinphoneCoreFactoryImpl extends LinphoneCoreFactory {
loadOptionalLibrary(String s)35 	private static boolean loadOptionalLibrary(String s) {
36 		try {
37 			System.loadLibrary(s);
38 			return true;
39 		} catch (Throwable e) {
40 			android.util.Log.w("LinphoneCoreFactoryImpl", "Unable to load optional library " + s + ": " +e.getMessage());
41 		}
42 		return false;
43 	}
44 
45 	static {
46 		System.loadLibrary("gnustl_shared");
47 		loadOptionalLibrary("ffmpeg-linphone");
48 		System.loadLibrary("bctoolbox");
49 		System.loadLibrary("ortp");
50 		System.loadLibrary("mediastreamer_base");
51 		System.loadLibrary("mediastreamer_voip");
52 		System.loadLibrary("linphone");
Version.dumpCapabilities()53 		Version.dumpCapabilities();
54 	}
55 	@Override
createAuthInfo(String username, String password, String realm, String domain)56 	public LinphoneAuthInfo createAuthInfo(String username, String password,
57 			String realm, String domain) {
58 		return new LinphoneAuthInfoImpl(username, password, realm, domain);
59 	}
60 
61 	@Override
createLinphoneAddress(String username, String domain, String displayName)62 	public LinphoneAddress createLinphoneAddress(String username,
63 			String domain, String displayName) {
64 		return new LinphoneAddressImpl(username,domain,displayName);
65 	}
66 
67 	@Override
createLinphoneAddress(String identity)68 	public LinphoneAddress createLinphoneAddress(String identity) throws LinphoneCoreException {
69 		return new LinphoneAddressImpl(identity);
70 	}
71 
72 	@Override
createLpConfig(String file)73 	public LpConfig createLpConfig(String file) {
74 		return LpConfigImpl.fromFile(file);
75 	}
76 
createLpConfigFromString(String buffer)77 	public LpConfig createLpConfigFromString(String buffer) {
78 		return LpConfigImpl.fromBuffer(buffer);
79 	}
80 
loadingDownloadedOpenH264(Context context)81 	private boolean loadingDownloadedOpenH264(Context context) {
82 		File file = new File(context.getApplicationInfo().nativeLibraryDir+"/libmsopenh264.so");
83 
84 		if (!file.exists()) {
85 			Log.i("LinphoneCoreFactoryImpl"," libmsopenh264 not found, we disable the download of Openh264");
86 			return false;
87 		}
88 
89 		OpenH264DownloadHelper downloadHelper = new OpenH264DownloadHelper(context);
90 		if (downloadHelper.isCodecFound()) {
91 			Log.i("LinphoneCoreFactoryImpl"," Loading OpenH264 downloaded plugin:" + downloadHelper.getFullPathLib());
92 			System.load(downloadHelper.getFullPathLib());
93 		} else {
94 			Log.i("LinphoneCoreFactoryImpl"," Cannot load OpenH264 downloaded plugin");
95 		}
96 		return true;
97 	}
98 
99 	@Override
createLinphoneCore(LinphoneCoreListener listener, String userConfig, String factoryConfig, Object userdata, Object context)100 	public LinphoneCore createLinphoneCore(LinphoneCoreListener listener,
101 			String userConfig, String factoryConfig, Object userdata, Object context)
102 			throws LinphoneCoreException {
103 		try {
104 			fcontext = (Context)context;
105 			boolean openh264DownloadEnabled = false;
106 			if (context != null) openh264DownloadEnabled = loadingDownloadedOpenH264(fcontext);
107 			MediastreamerAndroidContext.setContext(context);
108 			File user = userConfig == null ? null : new File(userConfig);
109 			File factory = factoryConfig == null ? null : new File(factoryConfig);
110 			LinphoneCore lc = new LinphoneCoreImpl(listener, user, factory, userdata);
111 			lc.enableDownloadOpenH264(openh264DownloadEnabled);
112 			if (context != null) lc.setContext(context);
113 			return lc;
114 		} catch (IOException e) {
115 			throw new LinphoneCoreException("Cannot create LinphoneCore",e);
116 		}
117 	}
118 
119 	@Override
createLinphoneCore(LinphoneCoreListener listener, Object context)120 	public LinphoneCore createLinphoneCore(LinphoneCoreListener listener, Object context) throws LinphoneCoreException {
121 		try {
122 			fcontext = (Context)context;
123 			boolean openh264DownloadEnabled = false;
124 			if (context != null) openh264DownloadEnabled = loadingDownloadedOpenH264(fcontext);
125 			MediastreamerAndroidContext.setContext(context);
126 			LinphoneCore lc = new LinphoneCoreImpl(listener);
127 			lc.enableDownloadOpenH264(openh264DownloadEnabled);
128 			if (context != null) lc.setContext(context);
129 			return lc;
130 		} catch (IOException e) {
131 			throw new LinphoneCoreException("Cannot create LinphoneCore",e);
132 		}
133 	}
134 
135 	@Override
setDebugMode(boolean enable, String tag)136 	public native void setDebugMode(boolean enable, String tag);
137 
138 
_setLogHandler(Object handler)139 	private native void _setLogHandler(Object handler);
140 	@Override
setLogHandler(LinphoneLogHandler handler)141 	public void setLogHandler(LinphoneLogHandler handler) {
142 		_setLogHandler(handler);
143 	}
144 
145 	@Override
createOpenH264DownloadHelper()146 	public OpenH264DownloadHelper createOpenH264DownloadHelper() {
147 		if (fcontext == null) {
148 			new LinphoneCoreException("Cannot create OpenH264DownloadHelper");
149 			return null;//exception
150 		}
151 		return new OpenH264DownloadHelper(fcontext);
152 	}
153 
154 	@Override
155 	@Deprecated
createLinphoneFriend(String friendUri)156 	public LinphoneFriend createLinphoneFriend(String friendUri) {
157 		return new LinphoneFriendImpl(friendUri);
158 	}
159 
160 	@Override
161 	@Deprecated
createLinphoneFriend()162 	public LinphoneFriend createLinphoneFriend() {
163 		return createLinphoneFriend(null);
164 	}
165 
166 	@Override
enableLogCollection(boolean enable)167 	public native void enableLogCollection(boolean enable);
168 
169 	@Override
setLogCollectionPath(String path)170 	public native void setLogCollectionPath(String path);
171 
isArmv7()172 	public static boolean isArmv7()
173 	{
174 		return System.getProperty("os.arch").contains("armv7");
175 	}
176 
177 	@Override
createAuthInfo(String username, String userid, String passwd, String ha1, String realm, String domain)178 	public LinphoneAuthInfo createAuthInfo(String username, String userid,
179 			String passwd, String ha1, String realm, String domain) {
180 		return new LinphoneAuthInfoImpl(username, userid, passwd, ha1, realm, domain);
181 	}
182 
183 	@Override
createLinphoneContent(String type, String subType, byte [] data, String encoding)184 	public LinphoneContent createLinphoneContent(String type, String subType,
185 			byte [] data, String encoding) {
186 		return new LinphoneContentImpl(type,subType,data,encoding);
187 	}
188 
189 	@Override
createLinphoneContent(String type, String subType, String data)190 	public LinphoneContent createLinphoneContent(String type, String subType,
191 			String data) {
192 		return new LinphoneContentImpl(type,subType,data == null ? null : data.getBytes(), null);
193 	}
194 
195 	@Override
createPresenceActivity(PresenceActivityType type, String description)196 	public PresenceActivity createPresenceActivity(PresenceActivityType type, String description) {
197 		return new PresenceActivityImpl(type, description);
198 	}
199 
200 	@Override
createPresenceService(String id, PresenceBasicStatus status, String contact)201 	public PresenceService createPresenceService(String id, PresenceBasicStatus status, String contact) {
202 		return new PresenceServiceImpl(id, status, contact);
203 	}
204 
205 	@Override
createPresenceModel()206 	public PresenceModel createPresenceModel() {
207 		return new PresenceModelImpl();
208 	}
209 
210 	@Override
createPresenceModel(PresenceActivityType type, String description)211 	public PresenceModel createPresenceModel(PresenceActivityType type, String description) {
212 		return new PresenceModelImpl(type, description);
213 	}
214 
215 	@Override
createPresenceModel(PresenceActivityType type, String description, String note, String lang)216 	public PresenceModel createPresenceModel(PresenceActivityType type, String description, String note, String lang) {
217 		return new PresenceModelImpl(type, description, note, lang);
218 	}
219 
_createTunnelConfig()220 	private native Object _createTunnelConfig();
221 	@Override
createTunnelConfig()222 	public TunnelConfig createTunnelConfig() {
223 		return (TunnelConfig)_createTunnelConfig();
224 	}
225 
226 	@Override
createAccountCreator(LinphoneCore lc, String url)227 	public LinphoneAccountCreator createAccountCreator(LinphoneCore lc, String url) {
228 		return new LinphoneAccountCreatorImpl(lc, url);
229 	}
230 
getAllDialPlanNative()231 	private native DialPlan[] getAllDialPlanNative();
232 	@Override
getAllDialPlan()233 	public DialPlan[] getAllDialPlan(){
234 		return getAllDialPlanNative();
235 	}
236 
createErrorInfoNative()237 	private native long createErrorInfoNative();
238 	@Override
createErrorInfo()239 	public ErrorInfo createErrorInfo(){
240 		return new ErrorInfoImpl(createErrorInfoNative(), true);
241 	}
242 }
243