| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/plugins/ppapi/ppb_audio_impl.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "media/audio/audio_output_controller.h" | |
| 9 #include "ppapi/c/pp_completion_callback.h" | |
| 10 #include "ppapi/c/ppb_audio.h" | |
| 11 #include "ppapi/c/ppb_audio_config.h" | |
| 12 #include "ppapi/shared_impl/resource_tracker.h" | |
| 13 #include "ppapi/thunk/enter.h" | |
| 14 #include "ppapi/thunk/ppb_audio_config_api.h" | |
| 15 #include "ppapi/thunk/thunk.h" | |
| 16 #include "webkit/plugins/ppapi/common.h" | |
| 17 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 18 | |
| 19 using ppapi::PpapiGlobals; | |
| 20 using ppapi::thunk::EnterResourceNoLock; | |
| 21 using ppapi::thunk::PPB_Audio_API; | |
| 22 using ppapi::thunk::PPB_AudioConfig_API; | |
| 23 using ppapi::TrackedCallback; | |
| 24 | |
| 25 namespace webkit { | |
| 26 namespace ppapi { | |
| 27 | |
| 28 // PPB_Audio_Impl -------------------------------------------------------------- | |
| 29 | |
| 30 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance) | |
| 31 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | |
| 32 audio_(NULL), | |
| 33 sample_frame_count_(0) { | |
| 34 } | |
| 35 | |
| 36 PPB_Audio_Impl::~PPB_Audio_Impl() { | |
| 37 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and | |
| 38 // releases the audio data associated with the pointer. Note however, that | |
| 39 // until ShutDown returns, StreamCreated may still be called. This will be | |
| 40 // OK since we'll just immediately clean up the data it stored later in this | |
| 41 // destructor. | |
| 42 if (audio_) { | |
| 43 audio_->ShutDown(); | |
| 44 audio_ = NULL; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // static | |
| 49 PP_Resource PPB_Audio_Impl::Create(PP_Instance instance, | |
| 50 PP_Resource config, | |
| 51 PPB_Audio_Callback audio_callback, | |
| 52 void* user_data) { | |
| 53 scoped_refptr<PPB_Audio_Impl> audio(new PPB_Audio_Impl(instance)); | |
| 54 if (!audio->Init(config, audio_callback, user_data)) | |
| 55 return 0; | |
| 56 return audio->GetReference(); | |
| 57 } | |
| 58 | |
| 59 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { | |
| 60 return this; | |
| 61 } | |
| 62 | |
| 63 bool PPB_Audio_Impl::Init(PP_Resource config, | |
| 64 PPB_Audio_Callback callback, void* user_data) { | |
| 65 // Validate the config and keep a reference to it. | |
| 66 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); | |
| 67 if (enter.failed()) | |
| 68 return false; | |
| 69 config_ = config; | |
| 70 | |
| 71 if (!callback) | |
| 72 return false; | |
| 73 SetCallback(callback, user_data); | |
| 74 | |
| 75 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 76 if (!plugin_delegate) | |
| 77 return false; | |
| 78 | |
| 79 // When the stream is created, we'll get called back on StreamCreated(). | |
| 80 CHECK(!audio_); | |
| 81 audio_ = plugin_delegate->CreateAudioOutput( | |
| 82 enter.object()->GetSampleRate(), enter.object()->GetSampleFrameCount(), | |
| 83 this); | |
| 84 sample_frame_count_ = enter.object()->GetSampleFrameCount(); | |
| 85 return audio_ != NULL; | |
| 86 } | |
| 87 | |
| 88 PP_Resource PPB_Audio_Impl::GetCurrentConfig() { | |
| 89 // AddRef on behalf of caller, while keeping a ref for ourselves. | |
| 90 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); | |
| 91 return config_; | |
| 92 } | |
| 93 | |
| 94 PP_Bool PPB_Audio_Impl::StartPlayback() { | |
| 95 if (!audio_) | |
| 96 return PP_FALSE; | |
| 97 if (playing()) | |
| 98 return PP_TRUE; | |
| 99 SetStartPlaybackState(); | |
| 100 return BoolToPPBool(audio_->StartPlayback()); | |
| 101 } | |
| 102 | |
| 103 PP_Bool PPB_Audio_Impl::StopPlayback() { | |
| 104 if (!audio_) | |
| 105 return PP_FALSE; | |
| 106 if (!playing()) | |
| 107 return PP_TRUE; | |
| 108 if (!audio_->StopPlayback()) | |
| 109 return PP_FALSE; | |
| 110 SetStopPlaybackState(); | |
| 111 return PP_TRUE; | |
| 112 } | |
| 113 | |
| 114 int32_t PPB_Audio_Impl::Open( | |
| 115 PP_Resource config, | |
| 116 scoped_refptr<TrackedCallback> create_callback) { | |
| 117 // Validate the config and keep a reference to it. | |
| 118 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); | |
| 119 if (enter.failed()) | |
| 120 return PP_ERROR_FAILED; | |
| 121 config_ = config; | |
| 122 | |
| 123 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 124 if (!plugin_delegate) | |
| 125 return PP_ERROR_FAILED; | |
| 126 | |
| 127 // When the stream is created, we'll get called back on StreamCreated(). | |
| 128 DCHECK(!audio_); | |
| 129 audio_ = plugin_delegate->CreateAudioOutput( | |
| 130 enter.object()->GetSampleRate(), enter.object()->GetSampleFrameCount(), | |
| 131 this); | |
| 132 if (!audio_) | |
| 133 return PP_ERROR_FAILED; | |
| 134 | |
| 135 // At this point, we are guaranteeing ownership of the completion | |
| 136 // callback. Audio promises to fire the completion callback | |
| 137 // once and only once. | |
| 138 SetCreateCallback(create_callback); | |
| 139 | |
| 140 return PP_OK_COMPLETIONPENDING; | |
| 141 } | |
| 142 | |
| 143 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) { | |
| 144 return GetSyncSocketImpl(sync_socket); | |
| 145 } | |
| 146 | |
| 147 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle, | |
| 148 uint32_t* shm_size) { | |
| 149 return GetSharedMemoryImpl(shm_handle, shm_size); | |
| 150 } | |
| 151 | |
| 152 void PPB_Audio_Impl::OnSetStreamInfo( | |
| 153 base::SharedMemoryHandle shared_memory_handle, | |
| 154 size_t shared_memory_size, | |
| 155 base::SyncSocket::Handle socket_handle) { | |
| 156 SetStreamInfo(pp_instance(), shared_memory_handle, shared_memory_size, | |
| 157 socket_handle, sample_frame_count_); | |
| 158 } | |
| 159 | |
| 160 } // namespace ppapi | |
| 161 } // namespace webkit | |
| OLD | NEW |