Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_media_stream_track_host_base.cc
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.
4
5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
6
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"
16
17 using ppapi::host::HostMessageContext;
18 using ppapi::proxy::SerializedHandle;
19
20 namespace content {
21
22 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
23     RendererPpapiHost* host,
24     PP_Instance instance,
25     PP_Resource resource)
26     : ResourceHost(host->GetPpapiHost(), instance, resource),
27       host_(host),
28       buffer_manager_(this) {}
29
30 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
31
32 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
33                                                  int32_t buffer_size,
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;
40
41   // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
42   // avoid it.
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());
47   if (!shm)
48     return false;
49
50   base::SharedMemoryHandle shm_handle = shm->handle();
51   if (!buffer_manager_.SetBuffers(
52           number_of_buffers, buffer_size, shm.Pass(), true)) {
53     return false;
54   }
55
56   base::PlatformFile platform_file =
57 #if defined(OS_WIN)
58       shm_handle;
59 #elif defined(OS_POSIX)
60       shm_handle.fd;
61 #else
62 #error Not implemented.
63 #endif
64   SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
65                           size);
66   bool readonly = (track_type == kRead);
67   host()->SendUnsolicitedReplyWithHandles(
68       pp_resource(),
69       PpapiPluginMsg_MediaStreamTrack_InitBuffers(number_of_buffers,
70                                                   buffer_size,
71                                                   readonly),
72       std::vector<SerializedHandle>(1, handle));
73   return true;
74 }
75
76 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
77     int32_t index) {
78   DCHECK_GE(index, 0);
79   DCHECK_LT(index, buffer_manager_.number_of_buffers());
80   host()->SendUnsolicitedReply(
81       pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index));
82 }
83
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));
89 }
90
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,
98                                       OnHostMsgClose)
99   IPC_END_MESSAGE_MAP()
100   return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
101 }
102
103 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
104     HostMessageContext* context,
105     int32_t index) {
106   buffer_manager_.EnqueueBuffer(index);
107   return PP_OK;
108 }
109
110 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
111     HostMessageContext* context) {
112   OnClose();
113   return PP_OK;
114 }
115
116 }  // namespace content