Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / encrypted_media_message_filter_android.cc
1 // Copyright 2013 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 "chrome/browser/media/encrypted_media_message_filter_android.h"
6
7 #include <string>
8
9 #include "chrome/common/encrypted_media_messages_android.h"
10 #include "ipc/ipc_message_macros.h"
11 #include "media/base/android/media_codec_bridge.h"
12 #include "media/base/android/media_drm_bridge.h"
13
14 using content::BrowserThread;
15 using media::MediaCodecBridge;
16 using media::MediaDrmBridge;
17
18 namespace chrome {
19
20 const size_t kMaxKeySystemLength = 256;
21
22 // Check whether the available codecs are supported.
23 static android::SupportedCodecs GetSupportedCodecs(
24     android::SupportedCodecs requested_codecs,
25     bool video_must_be_compositable) {
26   android::SupportedCodecs supported_codecs = android::NO_SUPPORTED_CODECS;
27   // TODO(qinmin): Remove this DCHECK and query VP8/Vorbis capabilities
28   // once webm support is added to Android.
29   DCHECK(!(requested_codecs & android::WEBM_VP8_AND_VORBIS));
30
31 #if defined(USE_PROPRIETARY_CODECS)
32   if ((requested_codecs & android::MP4_AAC) &&
33       MediaCodecBridge::CanDecode("mp4a", false)) {
34     supported_codecs = static_cast<android::SupportedCodecs>(
35         supported_codecs | android::MP4_AAC);
36   }
37
38   // TODO(qinmin): Remove the composition logic when secure contents can be
39   // composited.
40   if ((requested_codecs & android::MP4_AVC1) &&
41       MediaCodecBridge::CanDecode("avc1", !video_must_be_compositable)) {
42     supported_codecs = static_cast<android::SupportedCodecs>(
43         supported_codecs | android::MP4_AVC1);
44   }
45 #endif  // defined(USE_PROPRIETARY_CODECS)
46
47   return supported_codecs;
48 }
49
50 EncryptedMediaMessageFilterAndroid::EncryptedMediaMessageFilterAndroid()
51     : BrowserMessageFilter(EncryptedMediaMsgStart) {}
52
53 EncryptedMediaMessageFilterAndroid::~EncryptedMediaMessageFilterAndroid() {}
54
55 bool EncryptedMediaMessageFilterAndroid::OnMessageReceived(
56     const IPC::Message& message, bool* message_was_ok) {
57   bool handled = true;
58   IPC_BEGIN_MESSAGE_MAP_EX(
59       EncryptedMediaMessageFilterAndroid, message, *message_was_ok)
60     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GetSupportedKeySystems,
61                         OnGetSupportedKeySystems)
62     IPC_MESSAGE_UNHANDLED(handled = false)
63   IPC_END_MESSAGE_MAP_EX()
64   return handled;
65 }
66
67 void EncryptedMediaMessageFilterAndroid::OverrideThreadForMessage(
68     const IPC::Message& message, BrowserThread::ID* thread) {
69   // Move the IPC handling to FILE thread as it is not very cheap.
70   if (message.type() == ChromeViewHostMsg_GetSupportedKeySystems::ID)
71     *thread = BrowserThread::FILE;
72 }
73
74 void EncryptedMediaMessageFilterAndroid::OnGetSupportedKeySystems(
75     const SupportedKeySystemRequest& request,
76     SupportedKeySystemResponse* response) {
77   if (!response) {
78     NOTREACHED() << "NULL response pointer provided.";
79     return;
80   }
81
82   if (request.key_system.size() > kMaxKeySystemLength) {
83     NOTREACHED() << "Invalid key system: " << request.key_system;
84     return;
85   }
86
87   // TODO(qinmin): Convert codecs to container types and check whether they
88   // are supported with the key system.
89   if (!MediaDrmBridge::IsKeySystemSupportedWithType(request.key_system, ""))
90     return;
91
92   DCHECK_EQ(request.codecs >> 3, 0) << "unrecognized codec";
93   response->key_system = request.key_system;
94   // TODO(qinmin): check composition is supported or not.
95   response->compositing_codecs = GetSupportedCodecs(request.codecs, true);
96   response->non_compositing_codecs = GetSupportedCodecs(request.codecs, false);
97 }
98
99 }  // namespace chrome