Chromium Code Reviews| 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_resource.h" | 15 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" |
| 16 #include "native_client/src/shared/ppapi_proxy/utility.h" | 16 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 17 #include "native_client/src/shared/srpc/nacl_srpc.h" | 17 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 18 #include "ppapi/c/ppb_audio.h" | 18 #include "ppapi/c/ppb_audio.h" |
| 19 #include "ppapi/c/ppb_audio_config.h" | 19 #include "ppapi/c/ppb_audio_config.h" |
| 20 #include "ppapi/cpp/module_impl.h" | 20 #include "ppapi/cpp/module_impl.h" |
| 21 #include "srpcgen/ppb_rpc.h" | 21 #include "srpcgen/ppb_rpc.h" |
| 22 #include "srpcgen/ppp_rpc.h" | 22 #include "srpcgen/ppp_rpc.h" |
| 23 | 23 |
| 24 namespace ppapi_proxy { | 24 namespace ppapi_proxy { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // round size up to next 64k | 27 // Round size up to next 64k; required for NaCl's version of mmap(). |
| 28 size_t ceil64k(size_t n) { | 28 size_t ceil64k(size_t n) { |
| 29 return (n + 0xFFFF) & (~0xFFFF); | 29 return (n + 0xFFFF) & (~0xFFFF); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // The following two functions (TotalSharedMemorySizeInBytes, | |
| 33 // SetActualDataSizeInBytes) are copied & similar to audio_util.cc. | |
| 34 | |
| 35 uint32_t TotalSharedMemorySizeInBytes(size_t packet_size) { | |
| 36 // Need to reserve extra 4 bytes for size of data. | |
| 37 return ceil64k(packet_size + sizeof(uint32_t)); | |
| 38 } | |
| 39 | |
| 40 void SetActualDataSizeInBytes(void* audio_buffer, | |
| 41 uint32_t buffer_size_in_bytes, | |
| 42 uint32_t actual_size_in_bytes) { | |
| 43 char* end = static_cast<char*>(audio_buffer) + buffer_size_in_bytes; | |
|
enal1
2012/04/20 21:56:27
DCHECK() that address is properly aligned.
| |
| 44 volatile uint32_t* end32 = reinterpret_cast<volatile uint32_t*>(end); | |
| 45 // Set actual data size at the end of the buffer. | |
| 46 __sync_synchronize(); | |
| 47 *end32 = actual_size_in_bytes; | |
| 48 } | |
| 49 | |
| 32 } // namespace | 50 } // namespace |
| 33 | 51 |
| 34 PluginAudio::PluginAudio() : | 52 PluginAudio::PluginAudio() : |
| 35 resource_(kInvalidResourceId), | 53 resource_(kInvalidResourceId), |
| 36 socket_(-1), | 54 socket_(-1), |
| 37 shm_(-1), | 55 shm_(-1), |
| 38 shm_size_(0), | 56 shm_size_(0), |
| 39 shm_buffer_(NULL), | 57 shm_buffer_(NULL), |
| 40 state_(AUDIO_INCOMPLETE), | 58 state_(AUDIO_INCOMPLETE), |
| 41 thread_id_(), | 59 thread_id_(), |
| 42 thread_active_(false), | 60 thread_active_(false), |
| 43 user_callback_(NULL), | 61 user_callback_(NULL), |
| 44 user_data_(NULL) { | 62 user_data_(NULL) { |
| 45 DebugPrintf("PluginAudio::PluginAudio\n"); | 63 DebugPrintf("PluginAudio::PluginAudio\n"); |
| 46 } | 64 } |
| 47 | 65 |
| 48 PluginAudio::~PluginAudio() { | 66 PluginAudio::~PluginAudio() { |
| 49 DebugPrintf("PluginAudio::~PluginAudio\n"); | 67 DebugPrintf("PluginAudio::~PluginAudio\n"); |
| 50 // Ensure audio thread is not active. | 68 // Ensure audio thread is not active. |
| 51 if (resource_ != kInvalidResourceId) | 69 if (resource_ != kInvalidResourceId) |
| 52 GetInterface()->StopPlayback(resource_); | 70 GetInterface()->StopPlayback(resource_); |
| 53 // Unmap the shared memory buffer, if present. | 71 // Unmap the shared memory buffer, if present. |
| 54 if (shm_buffer_) { | 72 if (shm_buffer_) { |
| 55 munmap(shm_buffer_, ceil64k(shm_size_)); | 73 munmap(shm_buffer_, TotalSharedMemorySizeInBytes(shm_size_)); |
| 56 shm_buffer_ = NULL; | 74 shm_buffer_ = NULL; |
| 57 shm_size_ = 0; | 75 shm_size_ = 0; |
| 58 } | 76 } |
| 59 // Close the handles. | 77 // Close the handles. |
| 60 if (shm_ != -1) { | 78 if (shm_ != -1) { |
| 61 close(shm_); | 79 close(shm_); |
| 62 shm_ = -1; | 80 shm_ = -1; |
| 63 } | 81 } |
| 64 if (socket_ != -1) { | 82 if (socket_ != -1) { |
| 65 close(socket_); | 83 close(socket_); |
| 66 socket_ = -1; | 84 socket_ = -1; |
| 67 } | 85 } |
| 68 } | 86 } |
| 69 | 87 |
| 70 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) { | 88 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) { |
| 71 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n", | 89 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n", |
| 72 resource); | 90 resource); |
| 73 resource_ = resource; | 91 resource_ = resource; |
| 74 return true; | 92 return true; |
| 75 } | 93 } |
| 76 | 94 |
| 77 void PluginAudio::AudioThread(void* self) { | 95 void PluginAudio::AudioThread(void* self) { |
| 78 PluginAudio* audio = static_cast<PluginAudio*>(self); | 96 PluginAudio* audio = static_cast<PluginAudio*>(self); |
| 79 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self); | 97 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self); |
| 80 while (true) { | 98 while (true) { |
| 81 int32_t sync_value; | 99 int32_t sync_value; |
| 82 // block on socket read | 100 // Block on socket read. |
| 83 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); | 101 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); |
| 84 // StopPlayback() will send a value of -1 over the sync_socket | 102 // StopPlayback() will send a value of -1 over the sync_socket. |
| 85 if ((sizeof(sync_value) != r) || (-1 == sync_value)) | 103 if ((sizeof(sync_value) != r) || (-1 == sync_value)) |
| 86 break; | 104 break; |
| 87 // invoke user callback, get next buffer of audio data | 105 // Invoke user callback, get next buffer of audio data. |
| 88 audio->user_callback_(audio->shm_buffer_, | 106 audio->user_callback_(audio->shm_buffer_, |
| 89 audio->shm_size_, | 107 audio->shm_size_, |
| 90 audio->user_data_); | 108 audio->user_data_); |
| 109 // Signal audio backend by writing buffer length at end of buffer. | |
| 110 // (Note: NaCl applications will always write the entire buffer.) | |
| 111 SetActualDataSizeInBytes(audio->shm_buffer_, | |
| 112 audio->shm_size_, | |
|
enal1
2012/04/20 21:56:27
Wrong ident.
| |
| 113 audio->shm_size_); | |
| 91 } | 114 } |
| 92 } | 115 } |
| 93 | 116 |
| 94 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, | 117 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, |
| 95 NaClSrpcImcDescType shm, size_t shm_size) { | 118 NaClSrpcImcDescType shm, size_t shm_size) { |
| 96 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" | 119 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" |
| 97 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); | 120 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); |
| 98 socket_ = socket; | 121 socket_ = socket; |
| 99 shm_ = shm; | 122 shm_ = shm; |
| 100 shm_size_ = shm_size; | 123 shm_size_ = shm_size; |
| 101 shm_buffer_ = mmap(NULL, | 124 shm_buffer_ = mmap(NULL, |
| 102 ceil64k(shm_size), | 125 TotalSharedMemorySizeInBytes(shm_size), |
| 103 PROT_READ | PROT_WRITE, | 126 PROT_READ | PROT_WRITE, |
| 104 MAP_SHARED, | 127 MAP_SHARED, |
| 105 shm, | 128 shm, |
| 106 0); | 129 0); |
| 107 if (MAP_FAILED != shm_buffer_) { | 130 if (MAP_FAILED != shm_buffer_) { |
| 108 if (state() == AUDIO_PENDING) { | 131 if (state() == AUDIO_PENDING) { |
| 109 StartAudioThread(); | 132 StartAudioThread(); |
| 110 } else { | 133 } else { |
| 111 set_state(AUDIO_READY); | 134 set_state(AUDIO_READY); |
| 112 } | 135 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 GetAs<ppapi_proxy::PluginAudio>(audio_resource); | 331 GetAs<ppapi_proxy::PluginAudio>(audio_resource); |
| 309 if (NULL == audio.get()) { | 332 if (NULL == audio.get()) { |
| 310 // Ignore if no audio_resource -> audio_instance mapping exists, | 333 // Ignore if no audio_resource -> audio_instance mapping exists, |
| 311 // the app may have shutdown audio before StreamCreated() invoked. | 334 // the app may have shutdown audio before StreamCreated() invoked. |
| 312 rpc->result = NACL_SRPC_RESULT_OK; | 335 rpc->result = NACL_SRPC_RESULT_OK; |
| 313 return; | 336 return; |
| 314 } | 337 } |
| 315 audio->StreamCreated(sync_socket, shm, shm_size); | 338 audio->StreamCreated(sync_socket, shm, shm_size); |
| 316 rpc->result = NACL_SRPC_RESULT_OK; | 339 rpc->result = NACL_SRPC_RESULT_OK; |
| 317 } | 340 } |
| OLD | NEW |