| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/pepper/ppb_audio_impl.h" | 5 #include "content/renderer/pepper/ppb_audio_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/renderer/pepper/pepper_platform_audio_output.h" | 8 #include "content/renderer/pepper/pepper_platform_audio_output.h" |
| 9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 10 #include "content/renderer/pepper/plugin_instance_throttler_impl.h" | 10 #include "content/renderer/pepper/plugin_instance_throttler_impl.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 using ppapi::thunk::EnterResourceNoLock; | 23 using ppapi::thunk::EnterResourceNoLock; |
| 24 using ppapi::thunk::PPB_Audio_API; | 24 using ppapi::thunk::PPB_Audio_API; |
| 25 using ppapi::thunk::PPB_AudioConfig_API; | 25 using ppapi::thunk::PPB_AudioConfig_API; |
| 26 using ppapi::TrackedCallback; | 26 using ppapi::TrackedCallback; |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 // PPB_Audio_Impl -------------------------------------------------------------- | 30 // PPB_Audio_Impl -------------------------------------------------------------- |
| 31 | 31 |
| 32 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance) | 32 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance) |
| 33 : Resource(ppapi::OBJECT_IS_IMPL, instance), audio_(NULL) {} | 33 : Resource(ppapi::OBJECT_IS_IMPL, instance), audio_(NULL) { |
| 34 } |
| 34 | 35 |
| 35 PPB_Audio_Impl::~PPB_Audio_Impl() { | 36 PPB_Audio_Impl::~PPB_Audio_Impl() { |
| 36 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and | 37 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and |
| 37 // releases the audio data associated with the pointer. Note however, that | 38 // releases the audio data associated with the pointer. Note however, that |
| 38 // until ShutDown returns, StreamCreated may still be called. This will be | 39 // until ShutDown returns, StreamCreated may still be called. This will be |
| 39 // OK since we'll just immediately clean up the data it stored later in this | 40 // OK since we'll just immediately clean up the data it stored later in this |
| 40 // destructor. | 41 // destructor. |
| 41 if (audio_) { | 42 if (audio_) { |
| 42 audio_->ShutDown(); | 43 audio_->ShutDown(); |
| 43 audio_ = NULL; | 44 audio_ = NULL; |
| 44 } | 45 } |
| 45 } | 46 } |
| 46 | 47 |
| 47 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { return this; } | 48 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { return this; } |
| 48 | 49 |
| 49 PP_Resource PPB_Audio_Impl::GetCurrentConfig() { | 50 PP_Resource PPB_Audio_Impl::GetCurrentConfig() { |
| 50 // AddRef on behalf of caller, while keeping a ref for ourselves. | 51 // AddRef on behalf of caller, while keeping a ref for ourselves. |
| 51 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); | 52 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); |
| 52 return config_; | 53 return config_; |
| 53 } | 54 } |
| 54 | 55 |
| 55 PP_Bool PPB_Audio_Impl::StartPlayback() { | 56 PP_Bool PPB_Audio_Impl::StartPlayback() { |
| 56 if (!audio_) | 57 if (!audio_) |
| 57 return PP_FALSE; | 58 return PP_FALSE; |
| 58 if (playing()) | 59 if (playing()) |
| 59 return PP_TRUE; | 60 return PP_TRUE; |
| 61 |
| 62 // If plugin is in power saver mode, defer audio IPC communication. |
| 63 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( |
| 64 PepperPluginInstance::Get(pp_instance())); |
| 65 if (instance->throttler() && instance->throttler()->power_saver_enabled()) { |
| 66 if (!IsMutedForPowerSaver()) { |
| 67 SetMutedForPowerSaver(true); |
| 68 instance->throttler()->AddObserver(this); |
| 69 } |
| 70 } |
| 71 |
| 60 SetStartPlaybackState(); | 72 SetStartPlaybackState(); |
| 61 return PP_FromBool(audio_->StartPlayback()); | 73 return PP_FromBool(audio_->StartPlayback()); |
| 62 } | 74 } |
| 63 | 75 |
| 64 PP_Bool PPB_Audio_Impl::StopPlayback() { | 76 PP_Bool PPB_Audio_Impl::StopPlayback() { |
| 65 if (!audio_) | 77 if (!audio_) |
| 66 return PP_FALSE; | 78 return PP_FALSE; |
| 67 if (!playing()) | 79 if (!playing()) |
| 68 return PP_TRUE; | 80 return PP_TRUE; |
| 69 if (!audio_->StopPlayback()) | 81 if (!audio_->StopPlayback()) |
| 70 return PP_FALSE; | 82 return PP_FALSE; |
| 71 SetStopPlaybackState(); | 83 SetStopPlaybackState(); |
| 84 |
| 85 if (IsMutedForPowerSaver()) { |
| 86 SetMutedForPowerSaver(false); |
| 87 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( |
| 88 PepperPluginInstance::Get(pp_instance())); |
| 89 if (instance && instance->throttler()) { |
| 90 instance->throttler()->RemoveObserver(this); |
| 91 } |
| 92 } |
| 93 |
| 72 return PP_TRUE; | 94 return PP_TRUE; |
| 73 } | 95 } |
| 74 | 96 |
| 75 int32_t PPB_Audio_Impl::Open(PP_Resource config, | 97 int32_t PPB_Audio_Impl::Open(PP_Resource config, |
| 76 scoped_refptr<TrackedCallback> create_callback) { | 98 scoped_refptr<TrackedCallback> create_callback) { |
| 77 // Validate the config and keep a reference to it. | 99 // Validate the config and keep a reference to it. |
| 78 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); | 100 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); |
| 79 if (enter.failed()) | 101 if (enter.failed()) |
| 80 return PP_ERROR_FAILED; | 102 return PP_ERROR_FAILED; |
| 81 config_ = config; | 103 config_ = config; |
| 82 | 104 |
| 83 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( | 105 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( |
| 84 PepperPluginInstance::Get(pp_instance())); | 106 PepperPluginInstance::Get(pp_instance())); |
| 85 if (!instance) | 107 if (!instance) |
| 86 return PP_ERROR_FAILED; | 108 return PP_ERROR_FAILED; |
| 87 | 109 |
| 88 // Prevent any throttling since we are playing audio. This stopgap prevents | |
| 89 // video from appearing 'frozen' while the audio track plays. | |
| 90 if (instance->throttler() && instance->throttler()->power_saver_enabled()) { | |
| 91 instance->throttler()->MarkPluginEssential( | |
| 92 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_AUDIO); | |
| 93 } | |
| 94 | |
| 95 // When the stream is created, we'll get called back on StreamCreated(). | 110 // When the stream is created, we'll get called back on StreamCreated(). |
| 96 DCHECK(!audio_); | 111 DCHECK(!audio_); |
| 97 audio_ = PepperPlatformAudioOutput::Create( | 112 audio_ = PepperPlatformAudioOutput::Create( |
| 98 static_cast<int>(enter.object()->GetSampleRate()), | 113 static_cast<int>(enter.object()->GetSampleRate()), |
| 99 static_cast<int>(enter.object()->GetSampleFrameCount()), | 114 static_cast<int>(enter.object()->GetSampleFrameCount()), |
| 100 instance->GetRenderView()->GetRoutingID(), | 115 instance->GetRenderView()->GetRoutingID(), |
| 101 instance->render_frame()->GetRoutingID(), | 116 instance->render_frame()->GetRoutingID(), |
| 102 this); | 117 this); |
| 103 if (!audio_) | 118 if (!audio_) |
| 104 return PP_ERROR_FAILED; | 119 return PP_ERROR_FAILED; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 125 base::SyncSocket::Handle socket_handle) { | 140 base::SyncSocket::Handle socket_handle) { |
| 126 EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true); | 141 EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true); |
| 127 SetStreamInfo(pp_instance(), | 142 SetStreamInfo(pp_instance(), |
| 128 shared_memory_handle, | 143 shared_memory_handle, |
| 129 shared_memory_size, | 144 shared_memory_size, |
| 130 socket_handle, | 145 socket_handle, |
| 131 enter.object()->GetSampleRate(), | 146 enter.object()->GetSampleRate(), |
| 132 enter.object()->GetSampleFrameCount()); | 147 enter.object()->GetSampleFrameCount()); |
| 133 } | 148 } |
| 134 | 149 |
| 150 void PPB_Audio_Impl::OnPowerSaverStateChange( |
| 151 PluginInstanceThrottler::PowerSaverState state) { |
| 152 // Start deferred playback if necessary. |
| 153 if (IsMutedForPowerSaver() && |
| 154 state == PluginInstanceThrottler::POWER_SAVER_MARKED_ESSENTIAL) { |
| 155 SetMutedForPowerSaver(false); |
| 156 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( |
| 157 PepperPluginInstance::Get(pp_instance())); |
| 158 if (instance->throttler()) { |
| 159 instance->throttler()->RemoveObserver(this); |
| 160 } |
| 161 } |
| 162 } |
| 163 |
| 135 } // namespace content | 164 } // namespace content |
| OLD | NEW |