OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/shared_impl/audio_impl.h" | 5 #include "ppapi/shared_impl/ppb_audio_shared.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace ppapi { | 9 namespace ppapi { |
10 | 10 |
11 AudioImpl::AudioImpl() | 11 PPB_Audio_Shared::PPB_Audio_Shared() |
12 : playing_(false), | 12 : playing_(false), |
13 shared_memory_size_(0), | 13 shared_memory_size_(0), |
14 callback_(NULL), | 14 callback_(NULL), |
15 user_data_(NULL) { | 15 user_data_(NULL) { |
16 } | 16 } |
17 | 17 |
18 AudioImpl::~AudioImpl() { | 18 PPB_Audio_Shared::~PPB_Audio_Shared() { |
19 // Closing the socket causes the thread to exit - wait for it. | 19 // Closing the socket causes the thread to exit - wait for it. |
20 if (socket_.get()) | 20 if (socket_.get()) |
21 socket_->Close(); | 21 socket_->Close(); |
22 if (audio_thread_.get()) { | 22 if (audio_thread_.get()) { |
23 audio_thread_->Join(); | 23 audio_thread_->Join(); |
24 audio_thread_.reset(); | 24 audio_thread_.reset(); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 void AudioImpl::SetCallback(PPB_Audio_Callback callback, void* user_data) { | 28 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, |
| 29 void* user_data) { |
29 callback_ = callback; | 30 callback_ = callback; |
30 user_data_ = user_data; | 31 user_data_ = user_data; |
31 } | 32 } |
32 | 33 |
33 void AudioImpl::SetStartPlaybackState() { | 34 void PPB_Audio_Shared::SetStartPlaybackState() { |
34 DCHECK(!playing_); | 35 DCHECK(!playing_); |
35 DCHECK(!audio_thread_.get()); | 36 DCHECK(!audio_thread_.get()); |
36 | 37 |
37 // If the socket doesn't exist, that means that the plugin has started before | 38 // If the socket doesn't exist, that means that the plugin has started before |
38 // the browser has had a chance to create all the shared memory info and | 39 // the browser has had a chance to create all the shared memory info and |
39 // notify us. This is a common case. In this case, we just set the playing_ | 40 // notify us. This is a common case. In this case, we just set the playing_ |
40 // flag and the playback will automatically start when that data is available | 41 // flag and the playback will automatically start when that data is available |
41 // in SetStreamInfo. | 42 // in SetStreamInfo. |
42 if (callback_ && socket_.get()) | 43 if (callback_ && socket_.get()) |
43 StartThread(); | 44 StartThread(); |
44 playing_ = true; | 45 playing_ = true; |
45 } | 46 } |
46 | 47 |
47 void AudioImpl::SetStopPlaybackState() { | 48 void PPB_Audio_Shared::SetStopPlaybackState() { |
48 DCHECK(playing_); | 49 DCHECK(playing_); |
49 | 50 |
50 if (audio_thread_.get()) { | 51 if (audio_thread_.get()) { |
51 audio_thread_->Join(); | 52 audio_thread_->Join(); |
52 audio_thread_.reset(); | 53 audio_thread_.reset(); |
53 } | 54 } |
54 playing_ = false; | 55 playing_ = false; |
55 } | 56 } |
56 | 57 |
57 void AudioImpl::SetStreamInfo(base::SharedMemoryHandle shared_memory_handle, | 58 void PPB_Audio_Shared::SetStreamInfo( |
58 size_t shared_memory_size, | 59 base::SharedMemoryHandle shared_memory_handle, |
59 base::SyncSocket::Handle socket_handle) { | 60 size_t shared_memory_size, |
| 61 base::SyncSocket::Handle socket_handle) { |
60 socket_.reset(new base::SyncSocket(socket_handle)); | 62 socket_.reset(new base::SyncSocket(socket_handle)); |
61 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 63 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
62 shared_memory_size_ = shared_memory_size; | 64 shared_memory_size_ = shared_memory_size; |
63 | 65 |
64 if (callback_) { | 66 if (callback_) { |
65 shared_memory_->Map(shared_memory_size_); | 67 shared_memory_->Map(shared_memory_size_); |
66 | 68 |
67 // In common case StartPlayback() was called before StreamCreated(). | 69 // In common case StartPlayback() was called before StreamCreated(). |
68 if (playing_) | 70 if (playing_) |
69 StartThread(); | 71 StartThread(); |
70 } | 72 } |
71 } | 73 } |
72 | 74 |
73 void AudioImpl::StartThread() { | 75 void PPB_Audio_Shared::StartThread() { |
74 DCHECK(callback_); | 76 DCHECK(callback_); |
75 DCHECK(!audio_thread_.get()); | 77 DCHECK(!audio_thread_.get()); |
76 audio_thread_.reset(new base::DelegateSimpleThread( | 78 audio_thread_.reset(new base::DelegateSimpleThread( |
77 this, "plugin_audio_thread")); | 79 this, "plugin_audio_thread")); |
78 audio_thread_->Start(); | 80 audio_thread_->Start(); |
79 } | 81 } |
80 | 82 |
81 void AudioImpl::Run() { | 83 void PPB_Audio_Shared::Run() { |
82 int pending_data; | 84 int pending_data; |
83 void* buffer = shared_memory_->memory(); | 85 void* buffer = shared_memory_->memory(); |
84 | 86 |
85 while (sizeof(pending_data) == | 87 while (sizeof(pending_data) == |
86 socket_->Receive(&pending_data, sizeof(pending_data)) && | 88 socket_->Receive(&pending_data, sizeof(pending_data)) && |
87 pending_data >= 0) { | 89 pending_data >= 0) { |
88 callback_(buffer, shared_memory_size_, user_data_); | 90 callback_(buffer, shared_memory_size_, user_data_); |
89 } | 91 } |
90 } | 92 } |
91 | 93 |
92 } // namespace ppapi | 94 } // namespace ppapi |
OLD | NEW |