summaryrefslogtreecommitdiff
path: root/chromium/content/common/gpu/media/media_channel.cc
blob: 7baeba075e4b46b02aa131f377456607aa8b2277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/gpu/media/media_channel.h"

#include "content/common/gpu/media/gpu_video_decode_accelerator.h"
#include "content/common/gpu/media/gpu_video_encode_accelerator.h"
#include "gpu/ipc/service/gpu_channel.h"
#include "media/gpu/ipc/common/media_messages.h"

namespace content {

namespace {

void SendCreateJpegDecoderResult(
    scoped_ptr<IPC::Message> reply_message,
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
    base::WeakPtr<gpu::GpuChannel> channel,
    scoped_refptr<gpu::GpuChannelMessageFilter> filter,
    bool result) {
  GpuChannelMsg_CreateJpegDecoder::WriteReplyParams(reply_message.get(),
                                                    result);
  if (io_task_runner->BelongsToCurrentThread()) {
    filter->Send(reply_message.release());
  } else if (channel) {
    channel->Send(reply_message.release());
  }
}

}  // namespace

class MediaChannelDispatchHelper {
 public:
  MediaChannelDispatchHelper(MediaChannel* channel, int32_t routing_id)
      : channel_(channel), routing_id_(routing_id) {}

  bool Send(IPC::Message* msg) { return channel_->Send(msg); }

  void OnCreateVideoDecoder(const media::VideoDecodeAccelerator::Config& config,
                            int32_t decoder_route_id,
                            IPC::Message* reply_message) {
    channel_->OnCreateVideoDecoder(routing_id_, config, decoder_route_id,
                                   reply_message);
  }

  void OnCreateVideoEncoder(const media::CreateVideoEncoderParams& params,
                            IPC::Message* reply_message) {
    channel_->OnCreateVideoEncoder(routing_id_, params, reply_message);
  }

 private:
  MediaChannel* const channel_;
  const int32_t routing_id_;
  DISALLOW_COPY_AND_ASSIGN(MediaChannelDispatchHelper);
};

MediaChannel::MediaChannel(gpu::GpuChannel* channel) : channel_(channel) {}

MediaChannel::~MediaChannel() {}

bool MediaChannel::Send(IPC::Message* msg) {
  return channel_->Send(msg);
}

bool MediaChannel::OnMessageReceived(const IPC::Message& message) {
  MediaChannelDispatchHelper helper(this, message.routing_id());
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(MediaChannel, message)
    IPC_MESSAGE_FORWARD_DELAY_REPLY(
        GpuCommandBufferMsg_CreateVideoDecoder, &helper,
        MediaChannelDispatchHelper::OnCreateVideoDecoder)
    IPC_MESSAGE_FORWARD_DELAY_REPLY(
        GpuCommandBufferMsg_CreateVideoEncoder, &helper,
        MediaChannelDispatchHelper::OnCreateVideoEncoder)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(GpuChannelMsg_CreateJpegDecoder,
                                    OnCreateJpegDecoder)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void MediaChannel::OnCreateJpegDecoder(int32_t route_id,
                                       IPC::Message* reply_msg) {
  scoped_ptr<IPC::Message> msg(reply_msg);
  if (!jpeg_decoder_) {
    jpeg_decoder_.reset(
        new GpuJpegDecodeAccelerator(channel_, channel_->io_task_runner()));
  }
  jpeg_decoder_->AddClient(
      route_id, base::Bind(&SendCreateJpegDecoderResult, base::Passed(&msg),
                           channel_->io_task_runner(), channel_->AsWeakPtr(),
                           make_scoped_refptr(channel_->filter())));
}

void MediaChannel::OnCreateVideoDecoder(
    int32_t command_buffer_route_id,
    const media::VideoDecodeAccelerator::Config& config,
    int32_t decoder_route_id,
    IPC::Message* reply_message) {
  TRACE_EVENT0("gpu", "MediaChannel::OnCreateVideoDecoder");
  gpu::GpuCommandBufferStub* stub =
      channel_->LookupCommandBuffer(command_buffer_route_id);
  if (!stub) {
    reply_message->set_reply_error();
    Send(reply_message);
    return;
  }
  GpuVideoDecodeAccelerator* decoder = new GpuVideoDecodeAccelerator(
      decoder_route_id, stub, stub->channel()->io_task_runner());
  bool succeeded = decoder->Initialize(config);
  GpuCommandBufferMsg_CreateVideoDecoder::WriteReplyParams(reply_message,
                                                           succeeded);
  Send(reply_message);

  // decoder is registered as a DestructionObserver of this stub and will
  // self-delete during destruction of this stub.
}

void MediaChannel::OnCreateVideoEncoder(
    int32_t command_buffer_route_id,
    const media::CreateVideoEncoderParams& params,
    IPC::Message* reply_message) {
  TRACE_EVENT0("gpu", "MediaChannel::OnCreateVideoEncoder");
  gpu::GpuCommandBufferStub* stub =
      channel_->LookupCommandBuffer(command_buffer_route_id);
  if (!stub) {
    reply_message->set_reply_error();
    Send(reply_message);
    return;
  }
  GpuVideoEncodeAccelerator* encoder =
      new GpuVideoEncodeAccelerator(params.encoder_route_id, stub);
  bool succeeded =
      encoder->Initialize(params.input_format, params.input_visible_size,
                          params.output_profile, params.initial_bitrate);
  GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(reply_message,
                                                           succeeded);
  Send(reply_message);

  // encoder is registered as a DestructionObserver of this stub and will
  // self-delete during destruction of this stub.
}

}  // namespace content