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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
7 #include "base/logging.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "content/public/renderer/renderer_ppapi_host.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/host/dispatch_host_message.h"
12 #include "ppapi/host/host_message_context.h"
13 #include "ppapi/host/ppapi_host.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/shared_impl/media_stream_buffer.h"
17 using ppapi::host::HostMessageContext;
18 using ppapi::proxy::SerializedHandle;
22 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
23 RendererPpapiHost* host,
26 : ResourceHost(host->GetPpapiHost(), instance, resource),
28 buffer_manager_(this) {}
30 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
32 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
34 TrackType track_type) {
35 DCHECK_GT(number_of_buffers, 0);
36 DCHECK_GT(buffer_size,
37 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header)));
38 // Make each buffer 4 byte aligned.
39 buffer_size = (buffer_size + 3) & ~0x3;
41 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
43 int32_t size = number_of_buffers * buffer_size;
44 content::RenderThread* render_thread = content::RenderThread::Get();
45 scoped_ptr<base::SharedMemory> shm(
46 render_thread->HostAllocateSharedMemoryBuffer(size).Pass());
50 base::SharedMemoryHandle shm_handle = shm->handle();
51 if (!buffer_manager_.SetBuffers(
52 number_of_buffers, buffer_size, shm.Pass(), true)) {
56 base::PlatformFile platform_file =
59 #elif defined(OS_POSIX)
62 #error Not implemented.
64 SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
66 bool readonly = (track_type == kRead);
67 host()->SendUnsolicitedReplyWithHandles(
69 PpapiPluginMsg_MediaStreamTrack_InitBuffers(number_of_buffers,
72 std::vector<SerializedHandle>(1, handle));
76 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
79 DCHECK_LT(index, buffer_manager_.number_of_buffers());
80 host()->SendUnsolicitedReply(
81 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index));
84 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
85 const std::vector<int32_t>& indices) {
86 DCHECK_GE(indices.size(), 0U);
87 host()->SendUnsolicitedReply(pp_resource(),
88 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices));
91 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
92 const IPC::Message& msg,
93 HostMessageContext* context) {
94 IPC_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg)
95 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_MediaStreamTrack_EnqueueBuffer,
96 OnHostMsgEnqueueBuffer)
97 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close,
100 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
103 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
104 HostMessageContext* context,
106 buffer_manager_.EnqueueBuffer(index);
110 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
111 HostMessageContext* context) {
116 } // namespace content