You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2012/05/19 10:53:24 UTC

wp7 commit: Fixed threading issue in audio, + Defect [CB-602], added more info to log statements, INFO/ERROR

Updated Branches:
  refs/heads/master 327e47fea -> e87a25834


Fixed threading issue in audio, + Defect [CB-602], added more info to log statements, INFO/ERROR


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/e87a2583
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/e87a2583
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/e87a2583

Branch: refs/heads/master
Commit: e87a2583465e0f2d242c918db8491f9ee799a787
Parents: 327e47f
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Sat May 19 01:52:05 2012 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Sat May 19 01:52:05 2012 -0700

----------------------------------------------------------------------
 framework/Cordova/Commands/AudioPlayer.cs |   18 ++++++++++++------
 framework/Cordova/Commands/BaseCommand.cs |    4 +++-
 framework/Cordova/Commands/Media.cs       |   16 +++++++++++++++-
 framework/Cordova/NativeExecution.cs      |   21 +++++++++++----------
 framework/Cordova/ScriptCallback.cs       |    1 +
 framework/CordovaView.xaml.cs             |   20 ++++++++++----------
 6 files changed, 52 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/Cordova/Commands/AudioPlayer.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/AudioPlayer.cs b/framework/Cordova/Commands/AudioPlayer.cs
index bbf28d4..d1fcb52 100644
--- a/framework/Cordova/Commands/AudioPlayer.cs
+++ b/framework/Cordova/Commands/AudioPlayer.cs
@@ -29,7 +29,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
     /// <summary>
     /// Implements audio record and play back functionality.
     /// </summary>
-    internal class AudioPlayer: IDisposable
+    internal class AudioPlayer : IDisposable
     {
         #region Constants
         
@@ -56,6 +56,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         private const int MediaErrorPauseState = 7;
         private const int MediaErrorStopState = 8;
 
+        //TODO: get rid of this callback, it should be universal
         private const string CallbackFunction = "CordovaMediaonStatus";
 
         #endregion
@@ -131,6 +132,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// </summary>
         public void Dispose()
         {
+            Debug.WriteLine("Dispose :: " + this.audioFile);
             if (this.player != null)
             {
                 this.stopPlaying();
@@ -225,7 +227,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
             }
 
 
-            if ((this.player == null) || (this.state == MediaStopped))
+            if (this.player == null) 
             {
                 try
                 {
@@ -241,7 +243,9 @@ namespace WP7CordovaClassLib.Cordova.Commands
                                 Grid grid = page.FindName("LayoutRoot") as Grid;
                                 if (grid != null)
                                 {
+                                    
                                     this.player = new MediaElement();
+                                    this.player.Name = "playerMediaElement";
                                     grid.Children.Add(this.player);
                                     this.player.Visibility = Visibility.Collapsed;
                                     this.player.MediaOpened += MediaOpened;
@@ -281,13 +285,13 @@ namespace WP7CordovaClassLib.Cordova.Commands
                 }
                 catch (Exception e)
                 {
-                    string s = e.Message;
+                    Debug.WriteLine("Error: " + e.Message);
                     this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback));
                 }
             }
             else
             {
-                if ((this.state == MediaPaused) || (this.state == MediaStarting))
+                if (this.state != MediaRunning)
                 {
                     this.player.Play();
                     this.SetState(MediaRunning);
@@ -340,8 +344,8 @@ namespace WP7CordovaClassLib.Cordova.Commands
         { 
             if (this.player != null)
             {
-                TimeSpan timeSpen = new TimeSpan(0, 0, 0, 0, milliseconds);
-                this.player.Position = timeSpen;
+                TimeSpan tsPos = new TimeSpan(0, 0, 0, 0, milliseconds);
+                this.player.Position = tsPos;
                 this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f));
             }
         }
@@ -370,6 +374,8 @@ namespace WP7CordovaClassLib.Cordova.Commands
             if ((this.state == MediaRunning) || (this.state == MediaPaused))
             {
                 this.player.Stop();
+
+                this.player.Position = new TimeSpan(0L);
                 this.SetState(MediaStopped);
             } else
             {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/Cordova/Commands/BaseCommand.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/BaseCommand.cs b/framework/Cordova/Commands/BaseCommand.cs
index 575d141..c2af744 100644
--- a/framework/Cordova/Commands/BaseCommand.cs
+++ b/framework/Cordova/Commands/BaseCommand.cs
@@ -15,6 +15,7 @@
 using System;
 using System.Reflection;
 using Microsoft.Phone.Shell;
+using System.Diagnostics;
 
 namespace WP7CordovaClassLib.Cordova.Commands
 {
@@ -75,7 +76,8 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             if (this.OnCustomScript != null)
             {
-                this.OnCustomScript(this, script);               
+                this.OnCustomScript(this, script);
+                this.OnCustomScript = null;
             }
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/Cordova/Commands/Media.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/Media.cs b/framework/Cordova/Commands/Media.cs
index 36b04f4..7b8de51 100644
--- a/framework/Cordova/Commands/Media.cs
+++ b/framework/Cordova/Commands/Media.cs
@@ -16,6 +16,7 @@ using System;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Windows;
+using System.Diagnostics;
 
 namespace WP7CordovaClassLib.Cordova.Commands
 {
@@ -244,6 +245,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
                 }
                 else
                 {
+                    Debug.WriteLine("INFO: startPlayingAudio could not find mediaPlayer for " + mediaOptions.Id);
                     audio = Media.players[mediaOptions.Id];
                 }
 
@@ -296,6 +298,10 @@ namespace WP7CordovaClassLib.Cordova.Commands
                             AudioPlayer audio = Media.players[mediaOptions.Id];
                             audio.seekToPlaying(mediaOptions.Milliseconds);
                         }
+                        else
+                        {
+                            Debug.WriteLine("ERROR: seekToAudio could not find mediaPlayer for " + mediaOptions.Id);
+                        }
 
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
                     }
@@ -338,6 +344,10 @@ namespace WP7CordovaClassLib.Cordova.Commands
                             AudioPlayer audio = Media.players[mediaOptions.Id];
                             audio.pausePlaying();
                         }
+                        else
+                        {
+                            Debug.WriteLine("ERROR: pausePlayingAudio could not find mediaPlayer for " + mediaOptions.Id);
+                        }
 
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
                     }
@@ -377,6 +387,10 @@ namespace WP7CordovaClassLib.Cordova.Commands
                             AudioPlayer audio = Media.players[mediaOptions.Id];
                             audio.stopPlaying();
                         }
+                        else
+                        {
+                            Debug.WriteLine("stopPlaying could not find mediaPlayer for " + mediaOptions.Id);
+                        }
 
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
                     }
@@ -415,7 +429,6 @@ namespace WP7CordovaClassLib.Cordova.Commands
                 if (Media.players.ContainsKey(mediaOptions.Id))
                 {
                     AudioPlayer audio = Media.players[mediaOptions.Id];
-
                     Deployment.Current.Dispatcher.BeginInvoke(() =>
                     {
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK, audio.getCurrentPosition()));
@@ -460,6 +473,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
                 }
                 else
                 {
+                    Debug.WriteLine("ERROR: getDurationAudio could not find mediaPlayer for " + mediaOptions.Id);
                     audio = new AudioPlayer(this, mediaOptions.Id);
                     Media.players.Add(mediaOptions.Id, audio);
                 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/Cordova/NativeExecution.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/NativeExecution.cs b/framework/Cordova/NativeExecution.cs
index 466c1d9..afc9381 100644
--- a/framework/Cordova/NativeExecution.cs
+++ b/framework/Cordova/NativeExecution.cs
@@ -18,6 +18,7 @@ using System.Threading;
 using Microsoft.Devices;
 using Microsoft.Phone.Controls;
 using WP7CordovaClassLib.Cordova.Commands;
+using System.Windows;
 
 namespace WP7CordovaClassLib.Cordova
 {
@@ -69,7 +70,6 @@ namespace WP7CordovaClassLib.Cordova
 
             try
             {
-
                 BaseCommand bc = CommandFactory.CreateByServiceName(commandCallParams.Service);
 
                 if (bc == null)
@@ -96,21 +96,19 @@ namespace WP7CordovaClassLib.Cordova
                 // TODO: alternative way is using thread pool (ThreadPool.QueueUserWorkItem) instead of 
                 // new thread for every command call; but num threads are not sufficient - 2 threads per CPU core
 
-
                 Thread thread = new Thread(func =>
                 {
-
                     try
                     {
                         bc.InvokeMethodNamed(commandCallParams.Action, commandCallParams.Args);
                     }
                     catch (Exception ex)
                     {
-                        Debug.WriteLine("Exception in ProcessCommand :: " + ex.Message);
+                        Debug.WriteLine("ERROR: Exception in ProcessCommand :: " + ex.Message);
                         bc.OnCommandResult -= OnCommandResultHandler;
                         bc.OnCustomScript -= OnCustomScriptHandler;
 
-                        Debug.WriteLine("failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service);
+                        Debug.WriteLine("ERROR: failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service);
 
                         this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION));
 
@@ -123,7 +121,7 @@ namespace WP7CordovaClassLib.Cordova
             catch (Exception ex)
             {
                 // ERROR
-                Debug.WriteLine(String.Format("Unable to execute command :: {0}:{1}:{3} ", 
+                Debug.WriteLine(String.Format("ERROR: Unable to execute command :: {0}:{1}:{3} ", 
                     commandCallParams.Service, commandCallParams.Action, ex.Message));
 
                 this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.ERROR));
@@ -142,13 +140,13 @@ namespace WP7CordovaClassLib.Cordova
             
             if (result == null)
             {
-                Debug.WriteLine("OnCommandResult missing result argument");
+                Debug.WriteLine("ERROR: OnCommandResult missing result argument");
                 return;
             }
 
             if (String.IsNullOrEmpty(callbackId))
             {
-                Debug.WriteLine("OnCommandResult missing callbackId argument");
+                Debug.WriteLine("ERROR: OnCommandResult missing callbackId argument");
                 return;
             }
 
@@ -188,17 +186,20 @@ namespace WP7CordovaClassLib.Cordova
                 throw new ArgumentNullException("ScriptName");
             }
 
+            //Debug.WriteLine("INFO:: About to invoke ::" + script.ScriptName + " with args ::" + script.Args[0]);
             this.webBrowser.Dispatcher.BeginInvoke((ThreadStart)delegate()
             {
+
                 try
                 {
-                    //Debug.WriteLine("InvokingScript::" + script.ScriptName + " with args ::" + script.Args[0] + ", " + script.Args[1] + ", " + script.Args[2]);
+                    //Debug.WriteLine("INFO:: InvokingScript::" + script.ScriptName + " with args ::" + script.Args[0]);
                     this.webBrowser.InvokeScript(script.ScriptName, script.Args);
                 }
                 catch (Exception ex)
                 {
-                    Debug.WriteLine("Exception in InvokeScriptCallback :: " + ex.Message);
+                    Debug.WriteLine("ERROR: Exception in InvokeScriptCallback :: " + ex.Message);
                 }
+               
             });
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/Cordova/ScriptCallback.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/ScriptCallback.cs b/framework/Cordova/ScriptCallback.cs
index 7178a77..5c50c26 100644
--- a/framework/Cordova/ScriptCallback.cs
+++ b/framework/Cordova/ScriptCallback.cs
@@ -17,6 +17,7 @@ using System.Windows.Media;
 using System.Windows.Media.Animation;
 using System.Windows.Shapes;
 using WP7CordovaClassLib.Cordova.JSON;
+using System.Diagnostics;
 
 namespace WP7CordovaClassLib.Cordova
 {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/e87a2583/framework/CordovaView.xaml.cs
----------------------------------------------------------------------
diff --git a/framework/CordovaView.xaml.cs b/framework/CordovaView.xaml.cs
index afd09d0..f9a6e0b 100644
--- a/framework/CordovaView.xaml.cs
+++ b/framework/CordovaView.xaml.cs
@@ -159,7 +159,7 @@ namespace WP7CordovaClassLib
 
         void AppDeactivated(object sender, DeactivatedEventArgs e)
         {
-            Debug.WriteLine("AppDeactivated");
+            Debug.WriteLine("INFO: AppDeactivated");
 
             try
             {
@@ -167,25 +167,25 @@ namespace WP7CordovaClassLib
             }
             catch (Exception)
             {
-                Debug.WriteLine("Pause event error");
+                Debug.WriteLine("ERROR: Pause event error");
             } 
         }
 
         void AppLaunching(object sender, LaunchingEventArgs e)
         {
-            Debug.WriteLine("AppLaunching");
+            Debug.WriteLine("INFO: AppLaunching");
         }
 
         void AppActivated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
         {
-            Debug.WriteLine("AppActivated");
+            Debug.WriteLine("INFO: AppActivated");
             try
             {
                 CordovaBrowser.InvokeScript("CordovaCommandResult", new string[] { "resume" });
             }
             catch (Exception)
             {
-                Debug.WriteLine("Resume event error");
+                Debug.WriteLine("ERROR: Resume event error");
             }  
         }
 
@@ -268,14 +268,14 @@ namespace WP7CordovaClassLib
 
                                     if(!appStorage.DirectoryExists(strBaseDir))
                                     {
-                                        //Debug.WriteLine("Creating Directory :: " + strBaseDir);
+                                        Debug.WriteLine("INFO: Creating Directory :: " + strBaseDir);
                                         appStorage.CreateDirectory(strBaseDir);
                                     }
 
                                     // This will truncate/overwrite an existing file, or 
                                     using (IsolatedStorageFileStream outFile = appStorage.OpenFile(AppRoot + file.path, FileMode.Create))
                                     {
-                                        Debug.WriteLine("Writing data for " + AppRoot + file.path + " and length = " + data.Length);
+                                        Debug.WriteLine("INFO: Writing data for " + AppRoot + file.path + " and length = " + data.Length);
                                         using (var writer = new BinaryWriter(outFile))
                                         {
                                             writer.Write(data);
@@ -285,7 +285,7 @@ namespace WP7CordovaClassLib
                             }
                             else
                             {
-                                Debug.WriteLine("Failed to write file :: " + file.path + " did you forget to add it to the project?");
+                                Debug.WriteLine("ERROR: Failed to write file :: " + file.path + " did you forget to add it to the project?");
                             }
                         }
                     }
@@ -297,7 +297,7 @@ namespace WP7CordovaClassLib
             }
             catch (Exception ex)
             {
-                Debug.WriteLine("Exception in GapBrowser_Loaded :: {0}", ex.Message);
+                Debug.WriteLine("ERROR: Exception in GapBrowser_Loaded :: {0}", ex.Message);
             }
         }
 
@@ -373,7 +373,7 @@ namespace WP7CordovaClassLib
         void GapBrowser_Navigating(object sender, NavigatingEventArgs e)
         {
             this.PageDidChange = true;
-            Debug.WriteLine("GapBrowser_Navigating to :: " + e.Uri.ToString());
+            // Debug.WriteLine("GapBrowser_Navigating to :: " + e.Uri.ToString());
             // TODO: tell any running plugins to stop doing what they are doing.
             // TODO: check whitelist / blacklist
             // NOTE: Navigation can be cancelled by setting :        e.Cancel = true;