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 "media/audio/shared_memory_util.h" |
18 #include "ppapi/c/ppb_audio.h" | 19 #include "ppapi/c/ppb_audio.h" |
19 #include "ppapi/c/ppb_audio_config.h" | 20 #include "ppapi/c/ppb_audio_config.h" |
20 #include "ppapi/cpp/module_impl.h" | 21 #include "ppapi/cpp/module_impl.h" |
21 #include "srpcgen/ppb_rpc.h" | 22 #include "srpcgen/ppb_rpc.h" |
22 #include "srpcgen/ppp_rpc.h" | 23 #include "srpcgen/ppp_rpc.h" |
23 | 24 |
24 namespace ppapi_proxy { | 25 namespace ppapi_proxy { |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Round size up to next 64k; required for NaCl's version of mmap(). | 28 // Round size up to next 64k; required for NaCl's version of mmap(). |
28 size_t ceil64k(size_t n) { | 29 size_t ceil64k(size_t n) { |
29 return (n + 0xFFFF) & (~0xFFFF); | 30 return (n + 0xFFFF) & (~0xFFFF); |
30 } | 31 } |
31 | 32 |
32 // The following function SetAudioActualDataSizeInBytes, is copied & similar | |
33 // to the one in audio_util.cc. | |
34 void SetAudioActualDataSizeInBytes(void* audio_buffer, | |
35 uint32_t buffer_size_in_bytes, | |
36 uint32_t actual_size_in_bytes) { | |
37 char* end = static_cast<char*>(audio_buffer) + buffer_size_in_bytes; | |
38 DCHECK(0 == (reinterpret_cast<size_t>(end) & 3)); | |
39 volatile uint32_t* end32 = reinterpret_cast<volatile uint32_t*>(end); | |
40 // Set actual data size at the end of the buffer. | |
41 __sync_synchronize(); | |
42 *end32 = actual_size_in_bytes; | |
43 } | |
44 | |
45 } // namespace | 33 } // namespace |
46 | 34 |
47 PluginAudio::PluginAudio() : | 35 PluginAudio::PluginAudio() : |
48 resource_(kInvalidResourceId), | 36 resource_(kInvalidResourceId), |
49 socket_(-1), | 37 socket_(-1), |
50 shm_(-1), | 38 shm_(-1), |
51 shm_size_(0), | 39 shm_size_(0), |
52 shm_buffer_(NULL), | 40 shm_buffer_(NULL), |
53 state_(AUDIO_INCOMPLETE), | 41 state_(AUDIO_INCOMPLETE), |
54 thread_id_(), | 42 thread_id_(), |
55 thread_active_(false), | 43 thread_active_(false), |
56 user_callback_(NULL), | 44 user_callback_(NULL), |
57 user_data_(NULL) { | 45 user_data_(NULL) { |
58 DebugPrintf("PluginAudio::PluginAudio\n"); | 46 DebugPrintf("PluginAudio::PluginAudio\n"); |
59 } | 47 } |
60 | 48 |
61 PluginAudio::~PluginAudio() { | 49 PluginAudio::~PluginAudio() { |
62 DebugPrintf("PluginAudio::~PluginAudio\n"); | 50 DebugPrintf("PluginAudio::~PluginAudio\n"); |
63 // Ensure audio thread is not active. | 51 // Ensure audio thread is not active. |
64 if (resource_ != kInvalidResourceId) | 52 if (resource_ != kInvalidResourceId) |
65 GetInterface()->StopPlayback(resource_); | 53 GetInterface()->StopPlayback(resource_); |
66 // Unmap the shared memory buffer, if present. | 54 // Unmap the shared memory buffer, if present. |
67 if (shm_buffer_) { | 55 if (shm_buffer_) { |
68 munmap(shm_buffer_, ceil64k(TotalAudioSharedMemorySizeInBytes(shm_size_))); | 56 munmap(shm_buffer_, |
| 57 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size_))); |
69 shm_buffer_ = NULL; | 58 shm_buffer_ = NULL; |
70 shm_size_ = 0; | 59 shm_size_ = 0; |
71 } | 60 } |
72 // Close the handles. | 61 // Close the handles. |
73 if (shm_ != -1) { | 62 if (shm_ != -1) { |
74 close(shm_); | 63 close(shm_); |
75 shm_ = -1; | 64 shm_ = -1; |
76 } | 65 } |
77 if (socket_ != -1) { | 66 if (socket_ != -1) { |
78 close(socket_); | 67 close(socket_); |
(...skipping 17 matching lines...) Expand all Loading... |
96 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); | 85 ssize_t r = read(audio->socket_, &sync_value, sizeof(sync_value)); |
97 // StopPlayback() will send a value of -1 over the sync_socket. | 86 // StopPlayback() will send a value of -1 over the sync_socket. |
98 if ((sizeof(sync_value) != r) || (-1 == sync_value)) | 87 if ((sizeof(sync_value) != r) || (-1 == sync_value)) |
99 break; | 88 break; |
100 // Invoke user callback, get next buffer of audio data. | 89 // Invoke user callback, get next buffer of audio data. |
101 audio->user_callback_(audio->shm_buffer_, | 90 audio->user_callback_(audio->shm_buffer_, |
102 audio->shm_size_, | 91 audio->shm_size_, |
103 audio->user_data_); | 92 audio->user_data_); |
104 // Signal audio backend by writing buffer length at end of buffer. | 93 // Signal audio backend by writing buffer length at end of buffer. |
105 // (Note: NaCl applications will always write the entire buffer.) | 94 // (Note: NaCl applications will always write the entire buffer.) |
106 SetAudioActualDataSizeInBytes(audio->shm_buffer_, | 95 media::SetActualDataSizeInBytes(audio->shm_buffer_, |
107 audio->shm_size_, | 96 audio->shm_size_, |
108 audio->shm_size_); | 97 audio->shm_size_); |
109 } | 98 } |
110 } | 99 } |
111 | 100 |
112 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, | 101 void PluginAudio::StreamCreated(NaClSrpcImcDescType socket, |
113 NaClSrpcImcDescType shm, size_t shm_size) { | 102 NaClSrpcImcDescType shm, size_t shm_size) { |
114 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" | 103 DebugPrintf("PluginAudio::StreamCreated: shm=%"NACL_PRIu32"" |
115 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); | 104 " shm_size=%"NACL_PRIuS"\n", shm, shm_size); |
116 socket_ = socket; | 105 socket_ = socket; |
117 shm_ = shm; | 106 shm_ = shm; |
118 shm_size_ = shm_size; | 107 shm_size_ = shm_size; |
119 shm_buffer_ = mmap(NULL, | 108 shm_buffer_ = mmap(NULL, |
120 ceil64k(TotalAudioSharedMemorySizeInBytes(shm_size)), | 109 ceil64k(media::TotalSharedMemorySizeInBytes(shm_size)), |
121 PROT_READ | PROT_WRITE, | 110 PROT_READ | PROT_WRITE, |
122 MAP_SHARED, | 111 MAP_SHARED, |
123 shm, | 112 shm, |
124 0); | 113 0); |
125 if (MAP_FAILED != shm_buffer_) { | 114 if (MAP_FAILED != shm_buffer_) { |
126 if (state() == AUDIO_PENDING) { | 115 if (state() == AUDIO_PENDING) { |
127 StartAudioThread(); | 116 StartAudioThread(); |
128 } else { | 117 } else { |
129 set_state(AUDIO_READY); | 118 set_state(AUDIO_READY); |
130 } | 119 } |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 GetAs<ppapi_proxy::PluginAudio>(audio_resource); | 318 GetAs<ppapi_proxy::PluginAudio>(audio_resource); |
330 if (NULL == audio.get()) { | 319 if (NULL == audio.get()) { |
331 // Ignore if no audio_resource -> audio_instance mapping exists, | 320 // Ignore if no audio_resource -> audio_instance mapping exists, |
332 // the app may have shutdown audio before StreamCreated() invoked. | 321 // the app may have shutdown audio before StreamCreated() invoked. |
333 rpc->result = NACL_SRPC_RESULT_OK; | 322 rpc->result = NACL_SRPC_RESULT_OK; |
334 return; | 323 return; |
335 } | 324 } |
336 audio->StreamCreated(sync_socket, shm, shm_size); | 325 audio->StreamCreated(sync_socket, shm, shm_size); |
337 rpc->result = NACL_SRPC_RESULT_OK; | 326 rpc->result = NACL_SRPC_RESULT_OK; |
338 } | 327 } |
OLD | NEW |