Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_media_stream_track_host_base.cc
blobc9a75f6ffcafc0f3013152c182721843fcf52c0f
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 "base/numerics/safe_math.h"
9 #include "content/common/pepper_file_util.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "content/public/renderer/renderer_ppapi_host.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/media_stream_buffer.h"
19 using ppapi::host::HostMessageContext;
20 using ppapi::proxy::SerializedHandle;
22 namespace content {
24 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
25 RendererPpapiHost* host,
26 PP_Instance instance,
27 PP_Resource resource)
28 : ResourceHost(host->GetPpapiHost(), instance, resource),
29 host_(host),
30 buffer_manager_(this) {}
32 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
34 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
35 int32_t buffer_size,
36 TrackType track_type) {
37 DCHECK_GT(number_of_buffers, 0);
38 DCHECK_GT(buffer_size,
39 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header)));
40 // Make each buffer 4 byte aligned.
41 base::CheckedNumeric<int32_t> buffer_size_aligned = buffer_size;
42 // TODO(amistry): "buffer size" might not == "buffer stride", in the same way
43 // that width != stride in an image buffer.
44 buffer_size_aligned += (4 - buffer_size % 4);
46 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
47 // avoid it.
48 base::CheckedNumeric<int32_t> size = number_of_buffers * buffer_size_aligned;
49 if (!size.IsValid())
50 return false;
52 content::RenderThread* render_thread = content::RenderThread::Get();
53 scoped_ptr<base::SharedMemory> shm(
54 render_thread->HostAllocateSharedMemoryBuffer(size.ValueOrDie()).Pass());
55 if (!shm)
56 return false;
58 base::SharedMemoryHandle shm_handle = shm->handle();
59 if (!buffer_manager_.SetBuffers(number_of_buffers,
60 buffer_size_aligned.ValueOrDie(),
61 shm.Pass(),
62 true)) {
63 return false;
66 base::PlatformFile platform_file =
67 PlatformFileFromSharedMemoryHandle(shm_handle);
68 SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
69 size.ValueOrDie());
70 bool readonly = (track_type == kRead);
71 host()->SendUnsolicitedReplyWithHandles(
72 pp_resource(),
73 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
74 number_of_buffers,
75 buffer_size_aligned.ValueOrDie(),
76 readonly),
77 std::vector<SerializedHandle>(1, handle));
78 return true;
81 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
82 int32_t index) {
83 DCHECK_GE(index, 0);
84 DCHECK_LT(index, buffer_manager_.number_of_buffers());
85 host()->SendUnsolicitedReply(
86 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index));
89 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
90 const std::vector<int32_t>& indices) {
91 DCHECK_GE(indices.size(), 0U);
92 host()->SendUnsolicitedReply(pp_resource(),
93 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices));
96 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
97 const IPC::Message& msg,
98 HostMessageContext* context) {
99 PPAPI_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg)
100 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
101 PpapiHostMsg_MediaStreamTrack_EnqueueBuffer, OnHostMsgEnqueueBuffer)
102 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close,
103 OnHostMsgClose)
104 PPAPI_END_MESSAGE_MAP()
105 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
108 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
109 HostMessageContext* context,
110 int32_t index) {
111 buffer_manager_.EnqueueBuffer(index);
112 return PP_OK;
115 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
116 HostMessageContext* context) {
117 OnClose();
118 return PP_OK;
121 } // namespace content