@@ -76,12 +76,14 @@ public LifecycleManager(AppConfig appConfig, BaseTransportConfig config, Lifecyc
7676 void initialize () {
7777 super .initialize ();
7878
79- if (_transportConfig != null && _transportConfig .getTransportType ().equals (TransportType .MULTIPLEX )) {
80- this .session = new SdlSession (sdlSessionListener , (MultiplexTransportConfig ) _transportConfig );
81- } else if (_transportConfig != null && _transportConfig .getTransportType ().equals (TransportType .TCP )) {
82- this .session = new SdlSession (sdlSessionListener , (TCPTransportConfig ) _transportConfig );
83- } else {
84- DebugTool .logError (TAG , "Unable to create session for transport type" );
79+ synchronized (SESSION_LOCK ) {
80+ if (_transportConfig != null && _transportConfig .getTransportType ().equals (TransportType .MULTIPLEX )) {
81+ this .session = new SdlSession (sdlSessionListener , (MultiplexTransportConfig ) _transportConfig );
82+ } else if (_transportConfig != null && _transportConfig .getTransportType ().equals (TransportType .TCP )) {
83+ this .session = new SdlSession (sdlSessionListener , (TCPTransportConfig ) _transportConfig );
84+ } else {
85+ DebugTool .logError (TAG , "Unable to create session for transport type" );
86+ }
8587 }
8688 }
8789
@@ -93,11 +95,13 @@ void cycle(SdlDisconnectedReason disconnectedReason) {
9395 //We don't want to alert higher if we are just cycling for legacy bluetooth
9496 onClose ("Sdl Proxy Cycled" , new SdlException ("Sdl Proxy Cycled" , SdlExceptionCause .SDL_PROXY_CYCLED ), disconnectedReason );
9597 }
96- if (session != null ) {
97- try {
98- session .startSession ();
99- } catch (SdlException e ) {
100- e .printStackTrace ();
98+ synchronized (SESSION_LOCK ) {
99+ if (session != null ) {
100+ try {
101+ session .startSession ();
102+ } catch (SdlException e ) {
103+ e .printStackTrace ();
104+ }
101105 }
102106 }
103107 }
@@ -163,16 +167,18 @@ void onTransportDisconnected(String info, boolean availablePrimary, BaseTranspor
163167 */
164168 @ Override
165169 void startVideoService (boolean isEncrypted , VideoStreamingParameters parameters , boolean afterPendingRestart ) {
166- if (session == null ) {
167- DebugTool .logWarning (TAG , "SdlSession is not created yet." );
168- return ;
169- }
170- if (!session .getIsConnected ()) {
171- DebugTool .logWarning (TAG , "Connection is not available." );
172- return ;
173- }
170+ synchronized (SESSION_LOCK ) {
171+ if (session == null ) {
172+ DebugTool .logWarning (TAG , "SdlSession is not created yet." );
173+ return ;
174+ }
175+ if (!session .getIsConnected ()) {
176+ DebugTool .logWarning (TAG , "Connection is not available." );
177+ return ;
178+ }
174179
175- session .setDesiredVideoParams (parameters );
180+ session .setDesiredVideoParams (parameters );
181+ }
176182 tryStartVideoStream (isEncrypted , parameters , afterPendingRestart );
177183 }
178184
@@ -185,9 +191,11 @@ void startVideoService(boolean isEncrypted, VideoStreamingParameters parameters,
185191 * @param parameters VideoStreamingParameters that are desired. Does not guarantee this is what will be accepted.
186192 */
187193 private void tryStartVideoStream (boolean isEncrypted , VideoStreamingParameters parameters , boolean afterPendingRestart ) {
188- if (session == null ) {
189- DebugTool .logWarning (TAG , "SdlSession is not created yet." );
190- return ;
194+ synchronized (SESSION_LOCK ) {
195+ if (session == null ) {
196+ DebugTool .logWarning (TAG , "SdlSession is not created yet." );
197+ return ;
198+ }
191199 }
192200 if (getProtocolVersion () != null && getProtocolVersion ().getMajor () >= 5 && !systemCapabilityManager .isCapabilitySupported (SystemCapabilityType .VIDEO_STREAMING )) {
193201 DebugTool .logWarning (TAG , "Module doesn't support video streaming." );
@@ -198,17 +206,20 @@ private void tryStartVideoStream(boolean isEncrypted, VideoStreamingParameters p
198206 return ;
199207 }
200208
209+ synchronized (SESSION_LOCK ) {
210+ if (afterPendingRestart || !videoServiceStartResponseReceived || !videoServiceStartResponse //If we haven't started the service before
211+ || (videoServiceStartResponse && isEncrypted && !session .isServiceProtected (SessionType .NAV ))) { //Or the service has been started but we'd like to start an encrypted one
212+ if (session != null ) {
213+ session .setDesiredVideoParams (parameters );
214+ }
215+ videoServiceStartResponseReceived = false ;
216+ videoServiceStartResponse = false ;
201217
202- if (afterPendingRestart || !videoServiceStartResponseReceived || !videoServiceStartResponse //If we haven't started the service before
203- || (videoServiceStartResponse && isEncrypted && !session .isServiceProtected (SessionType .NAV ))) { //Or the service has been started but we'd like to start an encrypted one
204- session .setDesiredVideoParams (parameters );
205-
206- videoServiceStartResponseReceived = false ;
207- videoServiceStartResponse = false ;
208-
209- addVideoServiceListener ();
210- session .startService (SessionType .NAV , isEncrypted );
211-
218+ addVideoServiceListener ();
219+ if (session != null ) {
220+ session .startService (SessionType .NAV , isEncrypted );
221+ }
222+ }
212223 }
213224 }
214225
@@ -237,20 +248,27 @@ public void onServiceError(SdlSession session, SessionType type, String reason)
237248 videoServiceStartResponse = false ;
238249 }
239250 };
240- session .addServiceListener (SessionType .NAV , videoServiceListener );
251+
252+ synchronized (SESSION_LOCK ) {
253+ if (session != null ) {
254+ session .addServiceListener (SessionType .NAV , videoServiceListener );
255+ }
256+ }
241257 }
242258 }
243259
244260 @ Override
245261 void startAudioService (boolean isEncrypted ) {
246- if (session == null ) {
247- DebugTool .logWarning (TAG , "SdlSession is not created yet." );
248- return ;
249- }
250- if (!session .getIsConnected ()) {
251- DebugTool .logWarning (TAG , "Connection is not available." );
252- return ;
262+ synchronized (SESSION_LOCK ) {
263+ if (session == null ) {
264+ DebugTool .logWarning (TAG , "SdlSession is not created yet." );
265+ return ;
266+ }
267+ if (!session .getIsConnected ()) {
268+ DebugTool .logWarning (TAG , "Connection is not available." );
269+ return ;
270+ }
271+ session .startService (SessionType .PCM , isEncrypted );
253272 }
254- session .startService (SessionType .PCM , isEncrypted );
255273 }
256274}
0 commit comments