6db6edf410d911d3b1632f305428efa8b5becc54
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / ppb_audio_impl.cc
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 "content/renderer/pepper/ppb_audio_impl.h"
6
7 #include "base/logging.h"
8 #include "content/renderer/pepper/pepper_platform_audio_output.h"
9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
10 #include "content/renderer/render_frame_impl.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "media/audio/audio_output_controller.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/ppb_audio.h"
15 #include "ppapi/c/ppb_audio_config.h"
16 #include "ppapi/shared_impl/resource_tracker.h"
17 #include "ppapi/thunk/enter.h"
18 #include "ppapi/thunk/ppb_audio_config_api.h"
19 #include "ppapi/thunk/thunk.h"
20
21 using ppapi::PpapiGlobals;
22 using ppapi::thunk::EnterResourceNoLock;
23 using ppapi::thunk::PPB_Audio_API;
24 using ppapi::thunk::PPB_AudioConfig_API;
25 using ppapi::TrackedCallback;
26
27 namespace content {
28
29 // PPB_Audio_Impl --------------------------------------------------------------
30
31 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance)
32     : Resource(ppapi::OBJECT_IS_IMPL, instance), audio_(NULL) {}
33
34 PPB_Audio_Impl::~PPB_Audio_Impl() {
35   // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
36   // releases the audio data associated with the pointer. Note however, that
37   // until ShutDown returns, StreamCreated may still be called. This will be
38   // OK since we'll just immediately clean up the data it stored later in this
39   // destructor.
40   if (audio_) {
41     audio_->ShutDown();
42     audio_ = NULL;
43   }
44 }
45
46 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { return this; }
47
48 PP_Resource PPB_Audio_Impl::GetCurrentConfig() {
49   // AddRef on behalf of caller, while keeping a ref for ourselves.
50   PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
51   return config_;
52 }
53
54 PP_Bool PPB_Audio_Impl::StartPlayback() {
55   if (!audio_)
56     return PP_FALSE;
57   if (playing())
58     return PP_TRUE;
59   SetStartPlaybackState();
60   return PP_FromBool(audio_->StartPlayback());
61 }
62
63 PP_Bool PPB_Audio_Impl::StopPlayback() {
64   if (!audio_)
65     return PP_FALSE;
66   if (!playing())
67     return PP_TRUE;
68   if (!audio_->StopPlayback())
69     return PP_FALSE;
70   SetStopPlaybackState();
71   return PP_TRUE;
72 }
73
74 int32_t PPB_Audio_Impl::Open(PP_Resource config,
75                              scoped_refptr<TrackedCallback> create_callback) {
76   // Validate the config and keep a reference to it.
77   EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
78   if (enter.failed())
79     return PP_ERROR_FAILED;
80   config_ = config;
81
82   PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
83       PepperPluginInstance::Get(pp_instance()));
84   if (!instance)
85     return PP_ERROR_FAILED;
86
87   // When the stream is created, we'll get called back on StreamCreated().
88   DCHECK(!audio_);
89   audio_ = PepperPlatformAudioOutput::Create(
90       static_cast<int>(enter.object()->GetSampleRate()),
91       static_cast<int>(enter.object()->GetSampleFrameCount()),
92       instance->GetRenderView()->GetRoutingID(),
93       instance->render_frame()->GetRoutingID(),
94       this);
95   if (!audio_)
96     return PP_ERROR_FAILED;
97
98   // At this point, we are guaranteeing ownership of the completion
99   // callback.  Audio promises to fire the completion callback
100   // once and only once.
101   SetCreateCallback(create_callback);
102
103   return PP_OK_COMPLETIONPENDING;
104 }
105
106 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) {
107   return GetSyncSocketImpl(sync_socket);
108 }
109
110 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
111   return GetSharedMemoryImpl(shm_handle, shm_size);
112 }
113
114 void PPB_Audio_Impl::OnSetStreamInfo(
115     base::SharedMemoryHandle shared_memory_handle,
116     size_t shared_memory_size,
117     base::SyncSocket::Handle socket_handle) {
118   EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true);
119   SetStreamInfo(pp_instance(),
120                 shared_memory_handle,
121                 shared_memory_size,
122                 socket_handle,
123                 enter.object()->GetSampleRate(),
124                 enter.object()->GetSampleFrameCount());
125 }
126
127 }  // namespace content