Index: media/audio/audio_output_controller.h |
=================================================================== |
--- media/audio/audio_output_controller.h (revision 114012) |
+++ media/audio/audio_output_controller.h (working copy) |
@@ -8,6 +8,7 @@ |
#include "base/callback.h" |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
#include "base/synchronization/lock.h" |
#include "base/time.h" |
#include "media/audio/audio_buffers_state.h" |
@@ -22,8 +23,8 @@ |
// audio operations like play, pause, stop, etc. on a separate thread, |
// namely the audio controller thread. |
// |
-// All the public methods of AudioOutputController are non-blocking except |
-// close, the actual operations are performed on the audio controller thread. |
+// All the public methods of AudioOutputController are non-blocking. |
+// The actual operations are performed on the audio thread. |
// |
// Here is a state diagram for the AudioOutputController for default low |
// latency mode; in normal latency mode there is no "starting" or "paused when |
@@ -53,6 +54,16 @@ |
// In this mode a DataSource object is given to the AudioOutputController |
// and AudioOutputController reads from it synchronously. |
// |
+// The audio thread itself is owned by the AudioManager that the |
+// AudioOutputController holds a reference to. When performing tasks on the |
+// audio thread, the controller must not add or release references to the |
+// AudioManager or itself (since it in turn holds a reference to the manager), |
+// for delayed tasks as it can slow down or even prevent normal shut down. |
+// So, for tasks on the audio thread, the controller uses WeakPtr which enables |
+// us to safely cancel pending polling tasks. |
+// The owner of the audio thread, AudioManager, will take care of properly |
+// shutting it down. |
+// |
#include "media/base/media_export.h" |
namespace media { |
@@ -115,6 +126,7 @@ |
// will be created on the audio controller thread and when that is done |
// event handler will receive a OnCreated() call. |
static scoped_refptr<AudioOutputController> Create( |
+ AudioManager* audio_manager, |
EventHandler* event_handler, |
const AudioParameters& params, |
// Soft limit for buffer capacity in this controller. This parameter |
@@ -123,6 +135,7 @@ |
// Factory method for creating a low latency audio stream. |
static scoped_refptr<AudioOutputController> CreateLowLatency( |
+ AudioManager* audio_manager, |
EventHandler* event_handler, |
const AudioParameters& params, |
// External synchronous reader for audio controller. |
@@ -186,7 +199,8 @@ |
static const int kPollNumAttempts; |
static const int kPollPauseInMilliseconds; |
- AudioOutputController(EventHandler* handler, |
+ AudioOutputController(AudioManager* audio_manager, |
+ EventHandler* handler, |
uint32 capacity, SyncReader* sync_reader); |
// The following methods are executed on the audio controller thread. |
@@ -208,6 +222,7 @@ |
// Helper method that stops, closes, and NULLs |*stream_|. |
void StopCloseAndClearStream(); |
+ scoped_refptr<AudioManager> audio_manager_; |
// |handler_| may be called only if |state_| is not kClosed. |
EventHandler* handler_; |
AudioOutputStream* stream_; |
@@ -239,9 +254,15 @@ |
// Number of times left. |
int number_polling_attempts_left_; |
+ // Used to post delayed tasks to ourselves that we can cancel. |
+ // We don't want the tasks to hold onto a reference as it will slow down |
+ // shutdown and force it to wait for the most delayed task. |
+ // Also, if we're shutting down, we do not want to poll for more data. |
+ base::WeakPtrFactory<AudioOutputController> weak_this_; |
+ |
DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
}; |
} // namespace media |
-#endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
+#endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |