Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_audio.cc

Issue 10832285: Switch OnMoreData() to use AudioBus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Comments. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "native_client/src/shared/ppapi_proxy/plugin_ppb_audio.h" 5 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_audio.h"
6 6
7 #include <pthread.h> 7 #include <pthread.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <string.h> 9 #include <string.h>
10 #include <sys/errno.h> 10 #include <sys/errno.h>
11 #include <sys/mman.h> 11 #include <sys/mman.h>
12 #include "native_client/src/include/nacl_scoped_ptr.h" 12 #include "native_client/src/include/nacl_scoped_ptr.h"
13 #include "native_client/src/include/portability.h" 13 #include "native_client/src/include/portability.h"
14 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" 14 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h"
15 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_audio_config.h"
15 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" 16 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h"
16 #include "native_client/src/shared/ppapi_proxy/utility.h" 17 #include "native_client/src/shared/ppapi_proxy/utility.h"
17 #include "native_client/src/shared/srpc/nacl_srpc.h" 18 #include "native_client/src/shared/srpc/nacl_srpc.h"
18 #include "media/audio/shared_memory_util.h" 19 #include "media/audio/shared_memory_util.h"
19 #include "ppapi/c/ppb_audio.h" 20 #include "ppapi/c/ppb_audio.h"
20 #include "ppapi/c/ppb_audio_config.h" 21 #include "ppapi/c/ppb_audio_config.h"
21 #include "ppapi/cpp/module_impl.h" 22 #include "ppapi/cpp/module_impl.h"
22 #include "srpcgen/ppb_rpc.h" 23 #include "srpcgen/ppb_rpc.h"
23 #include "srpcgen/ppp_rpc.h" 24 #include "srpcgen/ppp_rpc.h"
24 25
25 namespace ppapi_proxy { 26 namespace ppapi_proxy {
26 namespace { 27 namespace {
27 28
28 // Round size up to next 64k; required for NaCl's version of mmap(). 29 // Round size up to next 64k; required for NaCl's version of mmap().
29 size_t ceil64k(size_t n) { 30 size_t ceil64k(size_t n) {
30 return (n + 0xFFFF) & (~0xFFFF); 31 return (n + 0xFFFF) & (~0xFFFF);
31 } 32 }
32 33
34 // Hard coded values from PepperPlatformAudioOutputImpl.
35 // TODO(dalecurtis): PPAPI shouldn't hard code these values for all clients.
36 enum { kChannels = 2, kBytesPerSample = 2 };
37
33 } // namespace 38 } // namespace
34 39
35 PluginAudio::PluginAudio() : 40 PluginAudio::PluginAudio() :
36 resource_(kInvalidResourceId), 41 resource_(kInvalidResourceId),
37 socket_(-1), 42 socket_(-1),
38 shm_(-1), 43 shm_(-1),
39 shm_size_(0), 44 shm_size_(0),
40 shm_buffer_(NULL), 45 shm_buffer_(NULL),
41 state_(AUDIO_INCOMPLETE), 46 state_(AUDIO_INCOMPLETE),
42 thread_id_(), 47 thread_id_(),
43 thread_active_(false), 48 thread_active_(false),
44 user_callback_(NULL), 49 user_callback_(NULL),
45 user_data_(NULL) { 50 user_data_(NULL),
51 client_buffer_size_bytes_(0) {
46 DebugPrintf("PluginAudio::PluginAudio\n"); 52 DebugPrintf("PluginAudio::PluginAudio\n");
47 } 53 }
48 54
49 PluginAudio::~PluginAudio() { 55 PluginAudio::~PluginAudio() {
50 DebugPrintf("PluginAudio::~PluginAudio\n"); 56 DebugPrintf("PluginAudio::~PluginAudio\n");
51 // Ensure audio thread is not active. 57 // Ensure audio thread is not active.
52 if (resource_ != kInvalidResourceId) 58 if (resource_ != kInvalidResourceId)
53 GetInterface()->StopPlayback(resource_); 59 GetInterface()->StopPlayback(resource_);
54 // Unmap the shared memory buffer, if present. 60 // Unmap the shared memory buffer, if present.
55 if (shm_buffer_) { 61 if (shm_buffer_) {
62 audio_bus_.reset();
63 client_buffer_.reset();
56 munmap(shm_buffer_, 64 munmap(shm_buffer_,
57 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size_))); 65 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size_)));
58 shm_buffer_ = NULL; 66 shm_buffer_ = NULL;
59 shm_size_ = 0; 67 shm_size_ = 0;
68 client_buffer_size_bytes_ = 0;
60 } 69 }
61 // Close the handles. 70 // Close the handles.
62 if (shm_ != -1) { 71 if (shm_ != -1) {
63 close(shm_); 72 close(shm_);
64 shm_ = -1; 73 shm_ = -1;
65 } 74 }
66 if (socket_ != -1) { 75 if (socket_ != -1) {
67 close(socket_); 76 close(socket_);
68 socket_ = -1; 77 socket_ = -1;
69 } 78 }
70 } 79 }
71 80
72 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) { 81 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) {
73 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n", 82 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n",
74 resource); 83 resource);
75 resource_ = resource; 84 resource_ = resource;
76 return true; 85 return true;
77 } 86 }
78 87
79 void PluginAudio::AudioThread(void* self) { 88 void PluginAudio::AudioThread(void* self) {
80 PluginAudio* audio = static_cast<PluginAudio*>(self); 89 PluginAudio* audio = static_cast<PluginAudio*>(self);
81 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self); 90 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self);
91 const int bytes_per_frame =
92 sizeof(*(audio->audio_bus_->channel(0))) * audio->audio_bus_->channels();
82 while (true) { 93 while (true) {
83 int32_t sync_value; 94 int32_t sync_value;
84 // Block on socket read. 95 // Block on socket read.
85 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); 96 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value));
86 // StopPlayback() will send a value of -1 over the sync_socket. 97 // StopPlayback() will send a value of -1 over the sync_socket.
87 if ((sizeof(sync_value) != r) || (-1 == sync_value)) 98 if ((sizeof(sync_value) != r) || (-1 == sync_value))
88 break; 99 break;
89 // Invoke user callback, get next buffer of audio data. 100 // Invoke user callback, get next buffer of audio data.
90 audio->user_callback_(audio->shm_buffer_, 101 audio->user_callback_(audio->client_buffer_.get(),
91 audio->shm_size_, 102 audio->client_buffer_size_bytes_,
92 audio->user_data_); 103 audio->user_data_);
104
105 // Deinterleave the audio data into the shared memory as float.
106 audio->audio_bus_->FromInterleaved(
107 audio->client_buffer_.get(), audio->audio_bus_->frames(),
108 kBytesPerSample);
109
93 // Signal audio backend by writing buffer length at end of buffer. 110 // Signal audio backend by writing buffer length at end of buffer.
94 // (Note: NaCl applications will always write the entire buffer.) 111 // (Note: NaCl applications will always write the entire buffer.)
95 media::SetActualDataSizeInBytes(audio->shm_buffer_, 112 // TODO(dalecurtis): Technically this is not the exact size. Due to channel
96 audio->shm_size_, 113 // padding for alignment, there may be more data available than this. We're
97 audio->shm_size_); 114 // relying on AudioSyncReader::Read() to parse this with that in mind.
115 // Rename these methods to Set/GetActualFrameCount().
116 media::SetActualDataSizeInBytes(
117 audio->shm_buffer_, audio->shm_size_,
118 audio->audio_bus_->frames() * bytes_per_frame);
98 } 119 }
99 } 120 }
100 121
101 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, 122 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket,
102 NaClSrpcImcDescType shm, size_t shm_size) { 123 NaClSrpcImcDescType shm, size_t shm_size) {
103 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" 124 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32""
104 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); 125 " shm_size=%"NACL_PRIuS"\n", shm, shm_size);
105 socket_ = socket; 126 socket_ = socket;
106 shm_ = shm; 127 shm_ = shm;
107 shm_size_ = shm_size; 128 shm_size_ = shm_size;
108 shm_buffer_ = mmap(NULL, 129 shm_buffer_ = mmap(NULL,
109 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size)), 130 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size)),
110 PROT_READ | PROT_WRITE, 131 PROT_READ | PROT_WRITE,
111 MAP_SHARED, 132 MAP_SHARED,
112 shm, 133 shm,
113 0); 134 0);
114 if (MAP_FAILED != shm_buffer_) { 135 if (MAP_FAILED != shm_buffer_) {
136 PP_Resource ac = GetInterface()->GetCurrentConfig(resource_);
137 int frames = PluginAudioConfig::GetInterface()->GetSampleFrameCount(ac);
138
139 audio_bus_ = media::AudioBus::WrapMemory(kChannels, frames, shm_buffer_);
140 // Setup integer audio buffer for user audio data.
141 client_buffer_size_bytes_ =
142 audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample;
143 client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]);
144
115 if (state() == AUDIO_PENDING) { 145 if (state() == AUDIO_PENDING) {
116 StartAudioThread(); 146 StartAudioThread();
117 } else { 147 } else {
118 set_state(AUDIO_READY); 148 set_state(AUDIO_READY);
119 } 149 }
120 } else { 150 } else {
121 shm_buffer_ = NULL; 151 shm_buffer_ = NULL;
122 } 152 }
123 } 153 }
124 154
125 bool PluginAudio::StartAudioThread() { 155 bool PluginAudio::StartAudioThread() {
126 // clear contents of shm buffer before spinning up audio thread 156 // clear contents of shm buffer before spinning up audio thread
127 DebugPrintf("PluginAudio::StartAudioThread\n"); 157 DebugPrintf("PluginAudio::StartAudioThread\n");
128 memset(shm_buffer_, 0, shm_size_); 158 memset(shm_buffer_, 0, shm_size_);
159 memset(client_buffer_.get(), 0, client_buffer_size_bytes_);
129 const struct PP_ThreadFunctions* thread_funcs = GetThreadCreator(); 160 const struct PP_ThreadFunctions* thread_funcs = GetThreadCreator();
130 if (NULL == thread_funcs->thread_create || 161 if (NULL == thread_funcs->thread_create ||
131 NULL == thread_funcs->thread_join) { 162 NULL == thread_funcs->thread_join) {
132 return false; 163 return false;
133 } 164 }
134 int ret = thread_funcs->thread_create(&thread_id_, AudioThread, this); 165 int ret = thread_funcs->thread_create(&thread_id_, AudioThread, this);
135 if (0 == ret) { 166 if (0 == ret) {
136 thread_active_ = true; 167 thread_active_ = true;
137 set_state(AUDIO_PLAYING); 168 set_state(AUDIO_PLAYING);
138 return true; 169 return true;
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 GetAs<ppapi_proxy::PluginAudio>(audio_resource); 349 GetAs<ppapi_proxy::PluginAudio>(audio_resource);
319 if (NULL == audio.get()) { 350 if (NULL == audio.get()) {
320 // Ignore if no audio_resource -> audio_instance mapping exists, 351 // Ignore if no audio_resource -> audio_instance mapping exists,
321 // the app may have shutdown audio before StreamCreated() invoked. 352 // the app may have shutdown audio before StreamCreated() invoked.
322 rpc->result = NACL_SRPC_RESULT_OK; 353 rpc->result = NACL_SRPC_RESULT_OK;
323 return; 354 return;
324 } 355 }
325 audio->StreamCreated(sync_socket, shm, shm_size); 356 audio->StreamCreated(sync_socket, shm, shm_size);
326 rpc->result = NACL_SRPC_RESULT_OK; 357 rpc->result = NACL_SRPC_RESULT_OK;
327 } 358 }
OLDNEW
« no previous file with comments | « ppapi/native_client/src/shared/ppapi_proxy/plugin_ppb_audio.h ('k') | ppapi/proxy/ppb_audio_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698