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