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 "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(???): 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 audio_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 audio_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 audio_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 } |
(...skipping 10 matching lines...) Expand all Loading... | |
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); |
82 while (true) { | 91 while (true) { |
83 int32_t sync_value; | 92 int32_t sync_value; |
84 // Block on socket read. | 93 // Block on socket read. |
85 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); | 94 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); |
86 // StopPlayback() will send a value of -1 over the sync_socket. | 95 // StopPlayback() will send a value of -1 over the sync_socket. |
87 if ((sizeof(sync_value) != r) || (-1 == sync_value)) | 96 if ((sizeof(sync_value) != r) || (-1 == sync_value)) |
88 break; | 97 break; |
89 // Invoke user callback, get next buffer of audio data. | 98 // Invoke user callback, get next buffer of audio data. |
90 audio->user_callback_(audio->shm_buffer_, | 99 audio->user_callback_(audio->audio_buffer_.get(), |
91 audio->shm_size_, | 100 audio->audio_buffer_size_bytes_, |
92 audio->user_data_); | 101 audio->user_data_); |
102 | |
103 // Deinterleave the audio data into the shared memory as float. | |
104 audio->audio_bus_->FromInterleaved( | |
105 audio->audio_buffer_.get(), audio->audio_bus_->frames(), | |
106 kBytesPerSample); | |
107 | |
93 // Signal audio backend by writing buffer length at end of buffer. | 108 // Signal audio backend by writing buffer length at end of buffer. |
94 // (Note: NaCl applications will always write the entire buffer.) | 109 // (Note: NaCl applications will always write the entire buffer.) |
95 media::SetActualDataSizeInBytes(audio->shm_buffer_, | 110 // TODO(dalecurtis): Technically this is not the exact size. Due to channel |
96 audio->shm_size_, | 111 // padding for alignment, there may be more data available than this. We're |
97 audio->shm_size_); | 112 // relying on AudioSyncReader::Read() to parse this with that in mind. |
113 // Rename these methods to Set/GetActualFrameCount(). | |
114 media::SetActualDataSizeInBytes( | |
115 audio->shm_buffer_, audio->shm_size_, audio->audio_bus_->frames() * | |
116 sizeof(*(audio->audio_bus_->channel(0))) * | |
117 audio->audio_bus_->channels()); | |
nfullagar
2012/08/29 18:38:22
Perhaps cleaner if AudioBus had a member func to r
DaleCurtis
2012/08/29 18:52:23
Per the TODO this will just be temporary until I r
| |
98 } | 118 } |
99 } | 119 } |
100 | 120 |
101 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, | 121 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, |
102 NaClSrpcImcDescType shm, size_t shm_size) { | 122 NaClSrpcImcDescType shm, size_t shm_size) { |
103 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" | 123 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" |
104 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); | 124 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); |
105 socket_ = socket; | 125 socket_ = socket; |
106 shm_ = shm; | 126 shm_ = shm; |
107 shm_size_ = shm_size; | 127 shm_size_ = shm_size; |
108 shm_buffer_ = mmap(NULL, | 128 shm_buffer_ = mmap(NULL, |
109 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size)), | 129 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size)), |
110 PROT_READ | PROT_WRITE, | 130 PROT_READ | PROT_WRITE, |
111 MAP_SHARED, | 131 MAP_SHARED, |
112 shm, | 132 shm, |
113 0); | 133 0); |
114 if (MAP_FAILED != shm_buffer_) { | 134 if (MAP_FAILED != shm_buffer_) { |
135 PP_Resource ac = GetInterface()->GetCurrentConfig(resource_); | |
136 int frames = PluginAudioConfig::GetInterface()->GetSampleFrameCount(ac); | |
137 | |
138 audio_bus_ = media::AudioBus::WrapMemory(kChannels, frames, shm_buffer_); | |
139 // Setup integer audio buffer for user audio data. | |
140 audio_buffer_size_bytes_ = | |
141 audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample; | |
142 audio_buffer_.reset(new uint8_t[audio_buffer_size_bytes_]); | |
143 | |
115 if (state() == AUDIO_PENDING) { | 144 if (state() == AUDIO_PENDING) { |
116 StartAudioThread(); | 145 StartAudioThread(); |
117 } else { | 146 } else { |
118 set_state(AUDIO_READY); | 147 set_state(AUDIO_READY); |
119 } | 148 } |
120 } else { | 149 } else { |
121 shm_buffer_ = NULL; | 150 shm_buffer_ = NULL; |
122 } | 151 } |
123 } | 152 } |
124 | 153 |
125 bool PluginAudio::StartAudioThread() { | 154 bool PluginAudio::StartAudioThread() { |
126 // clear contents of shm buffer before spinning up audio thread | 155 // clear contents of shm buffer before spinning up audio thread |
127 DebugPrintf("PluginAudio::StartAudioThread\n"); | 156 DebugPrintf("PluginAudio::StartAudioThread\n"); |
128 memset(shm_buffer_, 0, shm_size_); | 157 memset(shm_buffer_, 0, shm_size_); |
158 memset(audio_buffer_.get(), 0, audio_buffer_size_bytes_); | |
129 const struct PP_ThreadFunctions* thread_funcs = GetThreadCreator(); | 159 const struct PP_ThreadFunctions* thread_funcs = GetThreadCreator(); |
130 if (NULL == thread_funcs->thread_create || | 160 if (NULL == thread_funcs->thread_create || |
131 NULL == thread_funcs->thread_join) { | 161 NULL == thread_funcs->thread_join) { |
132 return false; | 162 return false; |
133 } | 163 } |
134 int ret = thread_funcs->thread_create(&thread_id_, AudioThread, this); | 164 int ret = thread_funcs->thread_create(&thread_id_, AudioThread, this); |
135 if (0 == ret) { | 165 if (0 == ret) { |
136 thread_active_ = true; | 166 thread_active_ = true; |
137 set_state(AUDIO_PLAYING); | 167 set_state(AUDIO_PLAYING); |
138 return true; | 168 return true; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 GetAs<ppapi_proxy::PluginAudio>(audio_resource); | 348 GetAs<ppapi_proxy::PluginAudio>(audio_resource); |
319 if (NULL == audio.get()) { | 349 if (NULL == audio.get()) { |
320 // Ignore if no audio_resource -> audio_instance mapping exists, | 350 // Ignore if no audio_resource -> audio_instance mapping exists, |
321 // the app may have shutdown audio before StreamCreated() invoked. | 351 // the app may have shutdown audio before StreamCreated() invoked. |
322 rpc->result = NACL_SRPC_RESULT_OK; | 352 rpc->result = NACL_SRPC_RESULT_OK; |
323 return; | 353 return; |
324 } | 354 } |
325 audio->StreamCreated(sync_socket, shm, shm_size); | 355 audio->StreamCreated(sync_socket, shm, shm_size); |
326 rpc->result = NACL_SRPC_RESULT_OK; | 356 rpc->result = NACL_SRPC_RESULT_OK; |
327 } | 357 } |
OLD | NEW |