OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h" |
| 6 |
| 7 #include "content/public/renderer/render_thread.h" |
| 8 #include "content/public/renderer/renderer_ppapi_host.h" |
| 9 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/host/dispatch_host_message.h" |
| 11 #include "ppapi/host/host_message_context.h" |
| 12 #include "ppapi/host/ppapi_host.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/shared_impl/media_stream_frame.h" |
| 15 |
| 16 using ppapi::host::HostMessageContext; |
| 17 using ppapi::proxy::SerializedHandle; |
| 18 |
| 19 namespace content { |
| 20 |
| 21 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase( |
| 22 RendererPpapiHost* host, |
| 23 PP_Instance instance, |
| 24 PP_Resource resource) |
| 25 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 26 host_(host), |
| 27 frame_buffer_(this) { |
| 28 } |
| 29 |
| 30 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() { |
| 31 } |
| 32 |
| 33 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived( |
| 34 const IPC::Message& msg, |
| 35 HostMessageContext* context) { |
| 36 IPC_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg) |
| 37 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 38 PpapiHostMsg_MediaStreamTrack_EnqueueFrame, OnHostMsgEnqueueFrame) |
| 39 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 40 PpapiHostMsg_MediaStreamTrack_Close, OnHostMsgClose) |
| 41 IPC_END_MESSAGE_MAP() |
| 42 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context); |
| 43 } |
| 44 |
| 45 bool PepperMediaStreamTrackHostBase::InitFrames(int32_t number_of_frames, |
| 46 int32_t frame_size) { |
| 47 DCHECK_GT(number_of_frames, 0); |
| 48 DCHECK_GT(frame_size, |
| 49 static_cast<int32_t>(sizeof(ppapi::MediaStreamFrame::Header))); |
| 50 // Make each frame 4 byte aligned. |
| 51 frame_size = (frame_size + 3) & ~0x3; |
| 52 |
| 53 int32_t size = number_of_frames * frame_size; |
| 54 content::RenderThread* render_thread = content::RenderThread::Get(); |
| 55 scoped_ptr<base::SharedMemory> shm( |
| 56 render_thread->HostAllocateSharedMemoryBuffer(size).Pass()); |
| 57 if (!shm) |
| 58 return false; |
| 59 |
| 60 base::SharedMemoryHandle shm_handle = shm->handle(); |
| 61 if (!frame_buffer_.SetFrames(number_of_frames, frame_size, shm.Pass(), true)) |
| 62 return false; |
| 63 |
| 64 base::PlatformFile platform_file = |
| 65 #if defined(OS_WIN) |
| 66 reinterpret_cast<HANDLE>(static_cast<intptr_t>(shm_handle)); |
| 67 #elif defined(OS_POSIX) |
| 68 shm_handle.fd; |
| 69 #else |
| 70 #error Not implemented. |
| 71 #endif |
| 72 SerializedHandle handle( |
| 73 host_->ShareHandleWithRemote(platform_file, false), size); |
| 74 host()->SendUnsolicitedReplyWithHandles(pp_resource(), |
| 75 PpapiPluginMsg_MediaStreamTrack_InitFrames(number_of_frames, frame_size), |
| 76 std::vector<SerializedHandle>(1, handle)); |
| 77 return true; |
| 78 } |
| 79 |
| 80 void PepperMediaStreamTrackHostBase::PluginEnqueueFrame(int32_t index) { |
| 81 DCHECK_GE(index, 0); |
| 82 DCHECK_LT(index, frame_buffer_.number_of_frames()); |
| 83 host()->SendUnsolicitedReply(pp_resource(), |
| 84 PpapiPluginMsg_MediaStreamTrack_EnqueueFrame(index)); |
| 85 } |
| 86 |
| 87 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueFrame( |
| 88 HostMessageContext* context, |
| 89 int32_t index) { |
| 90 frame_buffer_.EnqueueFrame(index); |
| 91 return PP_OK; |
| 92 } |
| 93 |
| 94 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose( |
| 95 HostMessageContext* context) { |
| 96 OnClose(); |
| 97 return PP_OK; |
| 98 } |
| 99 |
| 100 } // namespace content |
OLD | NEW |