| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/pepper/pepper_media_stream_track_host_base.h" | 5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
| 9 #include "content/public/renderer/renderer_ppapi_host.h" | 9 #include "content/public/renderer/renderer_ppapi_host.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/host/dispatch_host_message.h" | 11 #include "ppapi/host/dispatch_host_message.h" |
| 12 #include "ppapi/host/host_message_context.h" | 12 #include "ppapi/host/host_message_context.h" |
| 13 #include "ppapi/host/ppapi_host.h" | 13 #include "ppapi/host/ppapi_host.h" |
| 14 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
| 15 #include "ppapi/shared_impl/media_stream_buffer.h" | 15 #include "ppapi/shared_impl/media_stream_buffer.h" |
| 16 | 16 |
| 17 using ppapi::host::HostMessageContext; | 17 using ppapi::host::HostMessageContext; |
| 18 using ppapi::proxy::SerializedHandle; | 18 using ppapi::proxy::SerializedHandle; |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase( | 22 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase( |
| 23 RendererPpapiHost* host, | 23 RendererPpapiHost* host, |
| 24 PP_Instance instance, | 24 PP_Instance instance, |
| 25 PP_Resource resource) | 25 PP_Resource resource) |
| 26 : ResourceHost(host->GetPpapiHost(), instance, resource), | 26 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 27 host_(host), | 27 host_(host), |
| 28 buffer_manager_(this) { | 28 buffer_manager_(this) {} |
| 29 } | |
| 30 | 29 |
| 31 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() { | 30 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {} |
| 32 } | |
| 33 | 31 |
| 34 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers, | 32 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers, |
| 35 int32_t buffer_size) { | 33 int32_t buffer_size) { |
| 36 DCHECK_GT(number_of_buffers, 0); | 34 DCHECK_GT(number_of_buffers, 0); |
| 37 DCHECK_GT(buffer_size, | 35 DCHECK_GT(buffer_size, |
| 38 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header))); | 36 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header))); |
| 39 // Make each buffer 4 byte aligned. | 37 // Make each buffer 4 byte aligned. |
| 40 buffer_size = (buffer_size + 3) & ~0x3; | 38 buffer_size = (buffer_size + 3) & ~0x3; |
| 41 | 39 |
| 42 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should | 40 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should |
| 43 // avoid it. | 41 // avoid it. |
| 44 int32_t size = number_of_buffers * buffer_size; | 42 int32_t size = number_of_buffers * buffer_size; |
| 45 content::RenderThread* render_thread = content::RenderThread::Get(); | 43 content::RenderThread* render_thread = content::RenderThread::Get(); |
| 46 scoped_ptr<base::SharedMemory> shm( | 44 scoped_ptr<base::SharedMemory> shm( |
| 47 render_thread->HostAllocateSharedMemoryBuffer(size).Pass()); | 45 render_thread->HostAllocateSharedMemoryBuffer(size).Pass()); |
| 48 if (!shm) | 46 if (!shm) |
| 49 return false; | 47 return false; |
| 50 | 48 |
| 51 base::SharedMemoryHandle shm_handle = shm->handle(); | 49 base::SharedMemoryHandle shm_handle = shm->handle(); |
| 52 if (!buffer_manager_.SetBuffers( | 50 if (!buffer_manager_.SetBuffers( |
| 53 number_of_buffers, buffer_size, shm.Pass(), true)) { | 51 number_of_buffers, buffer_size, shm.Pass(), true)) { |
| 54 return false; | 52 return false; |
| 55 } | 53 } |
| 56 | 54 |
| 57 base::PlatformFile platform_file = | 55 base::PlatformFile platform_file = |
| 58 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
| 59 shm_handle; | 57 shm_handle; |
| 60 #elif defined(OS_POSIX) | 58 #elif defined(OS_POSIX) |
| 61 shm_handle.fd; | 59 shm_handle.fd; |
| 62 #else | 60 #else |
| 63 #error Not implemented. | 61 #error Not implemented. |
| 64 #endif | 62 #endif |
| 65 SerializedHandle handle( | 63 SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false), |
| 66 host_->ShareHandleWithRemote(platform_file, false), size); | 64 size); |
| 67 host()->SendUnsolicitedReplyWithHandles(pp_resource(), | 65 host()->SendUnsolicitedReplyWithHandles( |
| 66 pp_resource(), |
| 68 PpapiPluginMsg_MediaStreamTrack_InitBuffers(number_of_buffers, | 67 PpapiPluginMsg_MediaStreamTrack_InitBuffers(number_of_buffers, |
| 69 buffer_size), | 68 buffer_size), |
| 70 std::vector<SerializedHandle>(1, handle)); | 69 std::vector<SerializedHandle>(1, handle)); |
| 71 return true; | 70 return true; |
| 72 } | 71 } |
| 73 | 72 |
| 74 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin( | 73 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin( |
| 75 int32_t index) { | 74 int32_t index) { |
| 76 DCHECK_GE(index, 0); | 75 DCHECK_GE(index, 0); |
| 77 DCHECK_LT(index, buffer_manager_.number_of_buffers()); | 76 DCHECK_LT(index, buffer_manager_.number_of_buffers()); |
| 78 host()->SendUnsolicitedReply(pp_resource(), | 77 host()->SendUnsolicitedReply( |
| 79 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index)); | 78 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index)); |
| 80 } | 79 } |
| 81 | 80 |
| 82 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived( | 81 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived( |
| 83 const IPC::Message& msg, | 82 const IPC::Message& msg, |
| 84 HostMessageContext* context) { | 83 HostMessageContext* context) { |
| 85 IPC_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg) | 84 IPC_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg) |
| 86 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | 85 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_MediaStreamTrack_EnqueueBuffer, |
| 87 PpapiHostMsg_MediaStreamTrack_EnqueueBuffer, OnHostMsgEnqueueBuffer) | 86 OnHostMsgEnqueueBuffer) |
| 88 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | 87 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close, |
| 89 PpapiHostMsg_MediaStreamTrack_Close, OnHostMsgClose) | 88 OnHostMsgClose) |
| 90 IPC_END_MESSAGE_MAP() | 89 IPC_END_MESSAGE_MAP() |
| 91 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context); | 90 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context); |
| 92 } | 91 } |
| 93 | 92 |
| 94 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer( | 93 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer( |
| 95 HostMessageContext* context, | 94 HostMessageContext* context, |
| 96 int32_t index) { | 95 int32_t index) { |
| 97 buffer_manager_.EnqueueBuffer(index); | 96 buffer_manager_.EnqueueBuffer(index); |
| 98 return PP_OK; | 97 return PP_OK; |
| 99 } | 98 } |
| 100 | 99 |
| 101 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose( | 100 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose( |
| 102 HostMessageContext* context) { | 101 HostMessageContext* context) { |
| 103 OnClose(); | 102 OnClose(); |
| 104 return PP_OK; | 103 return PP_OK; |
| 105 } | 104 } |
| 106 | 105 |
| 107 } // namespace content | 106 } // namespace content |
| OLD | NEW |