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; |
| 44 DCHECK(0 == (reinterpret_cast<size_t>(end) & 3)); |
| 45 volatile uint32_t* end32 = reinterpret_cast<volatile uint32_t*>(end); |
| 46 // Set actual data size at the end of the buffer. |
| 47 __sync_synchronize(); |
| 48 *end32 = actual_size_in_bytes; |
| 49 } |
| 50 |
32 } // namespace | 51 } // namespace |
33 | 52 |
34 PluginAudio::PluginAudio() : | 53 PluginAudio::PluginAudio() : |
35 resource_(kInvalidResourceId), | 54 resource_(kInvalidResourceId), |
36 socket_(-1), | 55 socket_(-1), |
37 shm_(-1), | 56 shm_(-1), |
38 shm_size_(0), | 57 shm_size_(0), |
39 shm_buffer_(NULL), | 58 shm_buffer_(NULL), |
40 state_(AUDIO_INCOMPLETE), | 59 state_(AUDIO_INCOMPLETE), |
41 thread_id_(), | 60 thread_id_(), |
42 thread_active_(false), | 61 thread_active_(false), |
43 user_callback_(NULL), | 62 user_callback_(NULL), |
44 user_data_(NULL) { | 63 user_data_(NULL) { |
45 DebugPrintf("PluginAudio::PluginAudio\n"); | 64 DebugPrintf("PluginAudio::PluginAudio\n"); |
46 } | 65 } |
47 | 66 |
48 PluginAudio::~PluginAudio() { | 67 PluginAudio::~PluginAudio() { |
49 DebugPrintf("PluginAudio::~PluginAudio\n"); | 68 DebugPrintf("PluginAudio::~PluginAudio\n"); |
50 // Ensure audio thread is not active. | 69 // Ensure audio thread is not active. |
51 if (resource_ != kInvalidResourceId) | 70 if (resource_ != kInvalidResourceId) |
52 GetInterface()->StopPlayback(resource_); | 71 GetInterface()->StopPlayback(resource_); |
53 // Unmap the shared memory buffer, if present. | 72 // Unmap the shared memory buffer, if present. |
54 if (shm_buffer_) { | 73 if (shm_buffer_) { |
55 munmap(shm_buffer_, ceil64k(shm_size_)); | 74 munmap(shm_buffer_, TotalSharedMemorySizeInBytes(shm_size_)); |
56 shm_buffer_ = NULL; | 75 shm_buffer_ = NULL; |
57 shm_size_ = 0; | 76 shm_size_ = 0; |
58 } | 77 } |
59 // Close the handles. | 78 // Close the handles. |
60 if (shm_ != -1) { | 79 if (shm_ != -1) { |
61 close(shm_); | 80 close(shm_); |
62 shm_ = -1; | 81 shm_ = -1; |
63 } | 82 } |
64 if (socket_ != -1) { | 83 if (socket_ != -1) { |
65 close(socket_); | 84 close(socket_); |
66 socket_ = -1; | 85 socket_ = -1; |
67 } | 86 } |
68 } | 87 } |
69 | 88 |
70 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) { | 89 bool PluginAudio::InitFromBrowserResource(PP_Resource resource) { |
71 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n", | 90 DebugPrintf("PluginAudio::InitFromBrowserResource: resource=%"NACL_PRId32"\n", |
72 resource); | 91 resource); |
73 resource_ = resource; | 92 resource_ = resource; |
74 return true; | 93 return true; |
75 } | 94 } |
76 | 95 |
77 void PluginAudio::AudioThread(void* self) { | 96 void PluginAudio::AudioThread(void* self) { |
78 PluginAudio* audio = static_cast<PluginAudio*>(self); | 97 PluginAudio* audio = static_cast<PluginAudio*>(self); |
79 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self); | 98 DebugPrintf("PluginAudio::AudioThread: self=%p\n", self); |
80 while (true) { | 99 while (true) { |
81 int32_t sync_value; | 100 int32_t sync_value; |
82 // block on socket read | 101 // Block on socket read. |
83 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); | 102 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); |
84 // StopPlayback() will send a value of -1 over the sync_socket | 103 // StopPlayback() will send a value of -1 over the sync_socket. |
85 if ((sizeof(sync_value) != r) || (-1 == sync_value)) | 104 if ((sizeof(sync_value) != r) || (-1 == sync_value)) |
86 break; | 105 break; |
87 // invoke user callback, get next buffer of audio data | 106 // Invoke user callback, get next buffer of audio data. |
88 audio->user_callback_(audio->shm_buffer_, | 107 audio->user_callback_(audio->shm_buffer_, |
89 audio->shm_size_, | 108 audio->shm_size_, |
90 audio->user_data_); | 109 audio->user_data_); |
| 110 // Signal audio backend by writing buffer length at end of buffer. |
| 111 // (Note: NaCl applications will always write the entire buffer.) |
| 112 SetActualDataSizeInBytes(audio->shm_buffer_, |
| 113 audio->shm_size_, |
| 114 audio->shm_size_); |
91 } | 115 } |
92 } | 116 } |
93 | 117 |
94 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, | 118 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, |
95 NaClSrpcImcDescType shm, size_t shm_size) { | 119 NaClSrpcImcDescType shm, size_t shm_size) { |
96 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" | 120 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" |
97 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); | 121 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); |
98 socket_ = socket; | 122 socket_ = socket; |
99 shm_ = shm; | 123 shm_ = shm; |
100 shm_size_ = shm_size; | 124 shm_size_ = shm_size; |
101 shm_buffer_ = mmap(NULL, | 125 shm_buffer_ = mmap(NULL, |
102 ceil64k(shm_size), | 126 TotalSharedMemorySizeInBytes(shm_size), |
103 PROT_READ | PROT_WRITE, | 127 PROT_READ | PROT_WRITE, |
104 MAP_SHARED, | 128 MAP_SHARED, |
105 shm, | 129 shm, |
106 0); | 130 0); |
107 if (MAP_FAILED != shm_buffer_) { | 131 if (MAP_FAILED != shm_buffer_) { |
108 if (state() == AUDIO_PENDING) { | 132 if (state() == AUDIO_PENDING) { |
109 StartAudioThread(); | 133 StartAudioThread(); |
110 } else { | 134 } else { |
111 set_state(AUDIO_READY); | 135 set_state(AUDIO_READY); |
112 } | 136 } |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 GetAs<ppapi_proxy::PluginAudio>(audio_resource); | 332 GetAs<ppapi_proxy::PluginAudio>(audio_resource); |
309 if (NULL == audio.get()) { | 333 if (NULL == audio.get()) { |
310 // Ignore if no audio_resource -> audio_instance mapping exists, | 334 // Ignore if no audio_resource -> audio_instance mapping exists, |
311 // the app may have shutdown audio before StreamCreated() invoked. | 335 // the app may have shutdown audio before StreamCreated() invoked. |
312 rpc->result = NACL_SRPC_RESULT_OK; | 336 rpc->result = NACL_SRPC_RESULT_OK; |
313 return; | 337 return; |
314 } | 338 } |
315 audio->StreamCreated(sync_socket, shm, shm_size); | 339 audio->StreamCreated(sync_socket, shm, shm_size); |
316 rpc->result = NACL_SRPC_RESULT_OK; | 340 rpc->result = NACL_SRPC_RESULT_OK; |
317 } | 341 } |
OLD | NEW |