1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "media/cdm/ppapi/cdm_adapter.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "media/base/limits.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cdm/ppapi/cdm_file_io_impl.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "media/cdm/ppapi/cdm_helpers.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cdm/ppapi/cdm_logging.h"
110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "media/cdm/ppapi/supported_cdm_versions.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/c/ppb_console.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "ppapi/cpp/private/uma_private.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(CHECK_DOCUMENT_URL)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ppapi/cpp/dev/url_util_dev.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ppapi/cpp/instance_handle.h"
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // defined(CHECK_DOCUMENT_URL)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Constants for UMA reporting of file size (in KB) via HistogramCustomCounts().
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Note that the histogram is log-scaled (rather than linear).
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst uint32_t kSizeKBMin = 1;
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst uint32_t kSizeKBMax = 512 * 1024;  // 512MB
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst uint32_t kSizeKBBuckets = 100;
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(NDEBUG)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  #define DLOG_TO_CONSOLE(message) LogToConsole(message);
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  #define DLOG_TO_CONSOLE(message) (void)(message);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsMainThread() {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pp::Module::Get()->core()->IsMainThread();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Posts a task to run |cb| on the main thread. The task is posted even if the
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// current thread is the main thread.
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PostOnMain(pp::CompletionCallback cb) {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Ensures |cb| is called on the main thread, either because the current thread
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is the main thread or by posting it to the main thread.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CallOnMain(pp::CompletionCallback cb) {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(tomfinegan): This is only necessary because PPAPI doesn't allow calls
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // off the main thread yet. Remove this once the change lands.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (IsMainThread())
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cb.Run(PP_OK);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PostOnMain(cb);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Configures a cdm::InputBuffer. |subsamples| must exist as long as
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |input_buffer| is in use.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ConfigureInputBuffer(
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const pp::Buffer_Dev& encrypted_buffer,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PP_EncryptedBlockInfo& encrypted_block_info,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<cdm::SubsampleEntry>* subsamples,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm::InputBuffer* input_buffer) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(subsamples);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(!encrypted_buffer.is_null());
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  input_buffer->data = static_cast<uint8_t*>(encrypted_buffer.data());
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  input_buffer->data_size = encrypted_block_info.data_size;
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  PP_DCHECK(encrypted_buffer.size() >= input_buffer->data_size);
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PP_DCHECK(encrypted_block_info.key_id_size <=
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            arraysize(encrypted_block_info.key_id));
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  input_buffer->key_id_size = encrypted_block_info.key_id_size;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  input_buffer->key_id = input_buffer->key_id_size > 0 ?
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      encrypted_block_info.key_id : NULL;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PP_DCHECK(encrypted_block_info.iv_size <= arraysize(encrypted_block_info.iv));
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  input_buffer->iv_size = encrypted_block_info.iv_size;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  input_buffer->iv = encrypted_block_info.iv_size > 0 ?
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      encrypted_block_info.iv : NULL;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  input_buffer->num_subsamples = encrypted_block_info.num_subsamples;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (encrypted_block_info.num_subsamples > 0) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subsamples->reserve(encrypted_block_info.num_subsamples);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (uint32_t i = 0; i < encrypted_block_info.num_subsamples; ++i) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      subsamples->push_back(cdm::SubsampleEntry(
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          encrypted_block_info.subsamples[i].clear_bytes,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          encrypted_block_info.subsamples[i].cipher_bytes));
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    input_buffer->subsamples = &(*subsamples)[0];
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  input_buffer->timestamp = encrypted_block_info.tracking_info.timestamp;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PP_DecryptResult CdmStatusToPpDecryptResult(cdm::Status status) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (status) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kSuccess:
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_SUCCESS;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kNoKey:
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_DECRYPT_NOKEY;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kNeedMoreData:
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_NEEDMOREDATA;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kDecryptError:
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_DECRYPT_ERROR;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kDecodeError:
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_DECODE_ERROR;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_NOTREACHED();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTRESULT_DECODE_ERROR;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PP_DecryptedFrameFormat CdmVideoFormatToPpDecryptedFrameFormat(
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm::VideoFormat format) {
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (format) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kYv12:
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTEDFRAMEFORMAT_YV12;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case cdm::kI420:
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTEDFRAMEFORMAT_I420;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return PP_DECRYPTEDFRAMEFORMAT_UNKNOWN;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)PP_DecryptedSampleFormat CdmAudioFormatToPpDecryptedSampleFormat(
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    cdm::AudioFormat format) {
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (format) {
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatU8:
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_U8;
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatS16:
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_S16;
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatS32:
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_S32;
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatF32:
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_F32;
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatPlanarS16:
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_PLANAR_S16;
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kAudioFormatPlanarF32:
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_PLANAR_F32;
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return PP_DECRYPTEDSAMPLEFORMAT_UNKNOWN;
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cdm::AudioDecoderConfig::AudioCodec PpAudioCodecToCdmAudioCodec(
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_AudioCodec codec) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (codec) {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_AUDIOCODEC_VORBIS:
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::AudioDecoderConfig::kCodecVorbis;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_AUDIOCODEC_AAC:
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::AudioDecoderConfig::kCodecAac;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::AudioDecoderConfig::kUnknownAudioCodec;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cdm::VideoDecoderConfig::VideoCodec PpVideoCodecToCdmVideoCodec(
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_VideoCodec codec) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (codec) {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODEC_VP8:
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kCodecVp8;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODEC_H264:
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kCodecH264;
165a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    case PP_VIDEOCODEC_VP9:
166a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      return cdm::VideoDecoderConfig::kCodecVp9;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kUnknownVideoCodec;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cdm::VideoDecoderConfig::VideoCodecProfile PpVCProfileToCdmVCProfile(
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_VideoCodecProfile profile) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (profile) {
175a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    case PP_VIDEOCODECPROFILE_NOT_NEEDED:
176a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      return cdm::VideoDecoderConfig::kProfileNotNeeded;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_BASELINE:
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileBaseline;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_MAIN:
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileMain;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_EXTENDED:
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileExtended;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_HIGH:
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileHigh;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_HIGH_10:
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileHigh10;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_HIGH_422:
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileHigh422;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_VIDEOCODECPROFILE_H264_HIGH_444_PREDICTIVE:
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kH264ProfileHigh444Predictive;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::VideoDecoderConfig::kUnknownVideoCodecProfile;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cdm::VideoFormat PpDecryptedFrameFormatToCdmVideoFormat(
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_DecryptedFrameFormat format) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (format) {
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTEDFRAMEFORMAT_YV12:
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::kYv12;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTEDFRAMEFORMAT_I420:
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::kI420;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::kUnknownVideoFormat;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)cdm::StreamType PpDecryptorStreamTypeToCdmStreamType(
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_DecryptorStreamType stream_type) {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (stream_type) {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTORSTREAMTYPE_AUDIO:
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::kStreamTypeAudio;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTORSTREAMTYPE_VIDEO:
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return cdm::kStreamTypeVideo;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_NOTREACHED();
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return cdm::kStreamTypeVideo;
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
221f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)cdm::SessionType PpSessionTypeToCdmSessionType(PP_SessionType session_type) {
222f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  switch (session_type) {
223f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case PP_SESSIONTYPE_TEMPORARY:
224f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return cdm::kTemporary;
225f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case PP_SESSIONTYPE_PERSISTENT:
226f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return cdm::kPersistent;
227f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    default:
228f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      PP_NOTREACHED();
229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return cdm::kTemporary;
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
233f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)PP_CdmExceptionCode CdmExceptionTypeToPpCdmExceptionType(cdm::Error error) {
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  switch (error) {
235f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kNotSupportedError:
236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_NOTSUPPORTEDERROR;
237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kInvalidStateError:
238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_INVALIDSTATEERROR;
239f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kInvalidAccessError:
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_INVALIDACCESSERROR;
241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kQuotaExceededError:
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_QUOTAEXCEEDEDERROR;
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kUnknownError:
244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_UNKNOWNERROR;
245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kClientError:
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_CLIENTERROR;
247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kOutputError:
248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_OUTPUTERROR;
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    default:
250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      PP_NOTREACHED();
251f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return PP_CDMEXCEPTIONCODE_UNKNOWNERROR;
252f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
254f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
257ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace media {
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)CdmAdapter::CdmAdapter(PP_Instance instance, pp::Module* module)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : pp::Instance(instance),
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pp::ContentDecryptor_Private(this),
2628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
2638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      output_protection_(this),
2648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      platform_verification_(this),
2658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      output_link_mask_(0),
2668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      output_protection_mask_(0),
2678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      query_output_protection_in_progress_(false),
268010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      uma_for_output_protection_query_reported_(false),
269010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      uma_for_output_protection_positive_result_reported_(false),
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      allocator_(this),
2721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      cdm_(NULL),
2731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_initialize_audio_decoder_(false),
2741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_audio_decoder_config_id_(0),
2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_initialize_video_decoder_(false),
2761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      deferred_video_decoder_config_id_(0),
2771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      last_read_file_size_kb_(0),
2781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      file_size_uma_reported_(false) {
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  callback_factory_.Initialize(this);
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)CdmAdapter::~CdmAdapter() {}
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool CdmAdapter::CreateCdmInstance(const std::string& key_system) {
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PP_DCHECK(!cdm_);
2868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  cdm_ = make_linked_ptr(CdmWrapper::Create(
2878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      key_system.data(), key_system.size(), GetCdmHost, this));
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool success = cdm_ != NULL;
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string message = "CDM instance for " + key_system +
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              (success ? "" : " could not be") + " created.";
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DLOG_TO_CONSOLE(message);
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CDM_DLOG() << message;
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return success;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// No errors should be reported in this function because the spec says:
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// "Store this new error object internally with the MediaKeys instance being
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// created. This will be used to fire an error against any session created for
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// this instance." These errors will be reported during session creation
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// (CreateSession()) or session loading (LoadSession()).
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// TODO(xhwang): If necessary, we need to store the error here if we want to
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// support more specific error reporting (other than "Unknown").
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void CdmAdapter::Initialize(const std::string& key_system) {
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(!key_system.empty());
30768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  PP_DCHECK(key_system_.empty() || (key_system_ == key_system && cdm_));
30868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#if defined(CHECK_DOCUMENT_URL)
3100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  PP_URLComponents_Dev url_components = {};
3110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const pp::URLUtil_Dev* url_util = pp::URLUtil_Dev::Get();
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!url_util)
3130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
3140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  pp::Var href = url_util->GetDocumentURL(pp::InstanceHandle(pp_instance()),
3150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                          &url_components);
3160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  PP_DCHECK(href.is_string());
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  std::string url = href.AsString();
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  PP_DCHECK(!url.empty());
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  std::string url_scheme =
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      url.substr(url_components.scheme.begin, url_components.scheme.len);
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (url_scheme != "file") {
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Skip this check for file:// URLs as they don't have a host component.
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    PP_DCHECK(url_components.host.begin);
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    PP_DCHECK(0 < url_components.host.len);
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif  // defined(CHECK_DOCUMENT_URL)
3270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!cdm_ && !CreateCdmInstance(key_system))
3294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
33168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  PP_DCHECK(cdm_);
33268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  key_system_ = key_system;
33368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
33468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
3351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CdmAdapter::SetServerCertificate(uint32_t promise_id,
3361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                      pp::VarArrayBuffer server_certificate) {
3371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const uint8_t* server_certificate_ptr =
3381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      static_cast<const uint8_t*>(server_certificate.Map());
3391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const uint32_t server_certificate_size = server_certificate.ByteLength();
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!server_certificate_ptr ||
3421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      server_certificate_size < media::limits::kMinCertificateLength ||
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      server_certificate_size > media::limits::kMaxCertificateLength) {
3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    RejectPromise(
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        promise_id, cdm::kInvalidAccessError, 0, "Incorrect certificate.");
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
3471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Initialize() doesn't report an error, so SetServerCertificate() can be
3501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // called even if Initialize() failed.
3511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(jrummell): Remove this code when prefixed EME gets removed.
3521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!cdm_) {
3531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    RejectPromise(promise_id,
3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  cdm::kInvalidStateError,
3551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  0,
3561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  "CDM has not been initialized.");
3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
3581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!cdm_->SetServerCertificate(
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          promise_id, server_certificate_ptr, server_certificate_size)) {
3621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // CDM_4 and CDM_5 don't support this method, so reject the promise.
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    RejectPromise(promise_id, cdm::kNotSupportedError, 0, "Not implemented.");
3641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
3661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
367f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::CreateSession(uint32_t promise_id,
368f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               const std::string& init_data_type,
369f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               pp::VarArrayBuffer init_data,
370f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               PP_SessionType session_type) {
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Initialize() doesn't report an error, so CreateSession() can be called
3724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // even if Initialize() failed.
373f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(jrummell): Remove this code when prefixed EME gets removed.
374f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(jrummell): Verify that Initialize() failing does not resolve the
375f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // MediaKeys.create() promise.
3764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!cdm_) {
377f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    RejectPromise(promise_id,
378f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  cdm::kInvalidStateError,
379f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  0,
380f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  "CDM has not been initialized.");
3814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
3824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
384f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm_->CreateSession(promise_id,
385f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      init_data_type.data(),
386f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      init_data_type.size(),
387a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      static_cast<const uint8_t*>(init_data.Map()),
388f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      init_data.ByteLength(),
389f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      PpSessionTypeToCdmSessionType(session_type));
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
392f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::LoadSession(uint32_t promise_id,
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             const std::string& web_session_id) {
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Initialize() doesn't report an error, so LoadSession() can be called
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // even if Initialize() failed.
396f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(jrummell): Remove this code when prefixed EME gets removed.
397f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(jrummell): Verify that Initialize() failing does not resolve the
398f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // MediaKeys.create() promise.
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!cdm_) {
400f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    RejectPromise(promise_id,
401f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  cdm::kInvalidStateError,
402f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  0,
403f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                  "CDM has not been initialized.");
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
407f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm_->LoadSession(promise_id, web_session_id.data(), web_session_id.size());
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
410f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::UpdateSession(uint32_t promise_id,
411f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               const std::string& web_session_id,
412a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                               pp::VarArrayBuffer response) {
413a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const uint8_t* response_ptr = static_cast<const uint8_t*>(response.Map());
414a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const uint32_t response_size = response.ByteLength();
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
416f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PP_DCHECK(!web_session_id.empty());
417f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PP_DCHECK(response_ptr);
418f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PP_DCHECK(response_size > 0);
419f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
420f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm_->UpdateSession(promise_id,
421f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      web_session_id.data(),
422f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      web_session_id.length(),
423f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      response_ptr,
424f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      response_size);
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CdmAdapter::CloseSession(uint32_t promise_id,
4281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              const std::string& web_session_id) {
4291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!cdm_->CloseSession(
4301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          promise_id, web_session_id.data(), web_session_id.length())) {
4311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // CDM_4 doesn't support this method, so reject the promise.
4321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    RejectPromise(promise_id, cdm::kNotSupportedError, 0, "Not implemented.");
4331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::RemoveSession(uint32_t promise_id,
4376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               const std::string& web_session_id) {
4381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  cdm_->RemoveSession(
4391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      promise_id, web_session_id.data(), web_session_id.length());
4406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
4416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
4426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::GetUsableKeyIds(uint32_t promise_id,
4436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                 const std::string& web_session_id) {
4446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!cdm_->GetUsableKeyIds(
4456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          promise_id, web_session_id.data(), web_session_id.length())) {
4461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // CDM_4 doesn't support this method, so reject the promise.
4476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    RejectPromise(promise_id, cdm::kNotSupportedError, 0, "Not implemented.");
4486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
4496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
4506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note: In the following decryption/decoding related functions, errors are NOT
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// reported via KeyError, but are reported via corresponding PPB calls.
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::Decrypt(pp::Buffer_Dev encrypted_buffer,
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const PP_EncryptedBlockInfo& encrypted_block_info) {
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(!encrypted_buffer.is_null());
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Release a buffer that the caller indicated it is finished with.
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  allocator_.Release(encrypted_block_info.tracking_info.buffer_id);
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cdm::Status status = cdm::kDecryptError;
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LinkedDecryptedBlock decrypted_block(new DecryptedBlockImpl());
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_) {
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm::InputBuffer input_buffer;
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<cdm::SubsampleEntry> subsamples;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ConfigureInputBuffer(encrypted_buffer, encrypted_block_info, &subsamples,
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         &input_buffer);
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status = cdm_->Decrypt(input_buffer, decrypted_block.get());
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_DCHECK(status != cdm::kSuccess ||
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              (decrypted_block->DecryptedBuffer() &&
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               decrypted_block->DecryptedBuffer()->Size()));
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CallOnMain(callback_factory_.NewCallback(
4768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &CdmAdapter::DeliverBlock,
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      status,
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_block,
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      encrypted_block_info.tracking_info));
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::InitializeAudioDecoder(
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PP_AudioDecoderConfig& decoder_config,
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pp::Buffer_Dev extra_data_buffer) {
4851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  PP_DCHECK(!deferred_initialize_audio_decoder_);
4861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  PP_DCHECK(deferred_audio_decoder_config_id_ == 0);
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cdm::Status status = cdm::kSessionError;
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_) {
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm::AudioDecoderConfig cdm_decoder_config;
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.codec =
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PpAudioCodecToCdmAudioCodec(decoder_config.codec);
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.channel_count = decoder_config.channel_count;
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.bits_per_channel = decoder_config.bits_per_channel;
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.samples_per_second = decoder_config.samples_per_second;
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.extra_data =
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        static_cast<uint8_t*>(extra_data_buffer.data());
4971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    cdm_decoder_config.extra_data_size = extra_data_buffer.size();
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status = cdm_->InitializeAudioDecoder(cdm_decoder_config);
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (status == cdm::kDeferredInitialization) {
5021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    deferred_initialize_audio_decoder_ = true;
5031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    deferred_audio_decoder_config_id_ = decoder_config.request_id;
5041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
5051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
5061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CallOnMain(callback_factory_.NewCallback(
5088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &CdmAdapter::DecoderInitializeDone,
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_DECRYPTORSTREAMTYPE_AUDIO,
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decoder_config.request_id,
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      status == cdm::kSuccess));
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::InitializeVideoDecoder(
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PP_VideoDecoderConfig& decoder_config,
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pp::Buffer_Dev extra_data_buffer) {
5171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  PP_DCHECK(!deferred_initialize_video_decoder_);
5181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  PP_DCHECK(deferred_video_decoder_config_id_ == 0);
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cdm::Status status = cdm::kSessionError;
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_) {
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm::VideoDecoderConfig cdm_decoder_config;
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.codec =
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PpVideoCodecToCdmVideoCodec(decoder_config.codec);
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.profile =
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PpVCProfileToCdmVCProfile(decoder_config.profile);
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.format =
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PpDecryptedFrameFormatToCdmVideoFormat(decoder_config.format);
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.coded_size.width = decoder_config.width;
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.coded_size.height = decoder_config.height;
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_decoder_config.extra_data =
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        static_cast<uint8_t*>(extra_data_buffer.data());
5321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    cdm_decoder_config.extra_data_size = extra_data_buffer.size();
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status = cdm_->InitializeVideoDecoder(cdm_decoder_config);
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (status == cdm::kDeferredInitialization) {
5371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    deferred_initialize_video_decoder_ = true;
5381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    deferred_video_decoder_config_id_ = decoder_config.request_id;
5391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
5401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
5411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CallOnMain(callback_factory_.NewCallback(
5438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &CdmAdapter::DecoderInitializeDone,
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_DECRYPTORSTREAMTYPE_VIDEO,
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decoder_config.request_id,
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      status == cdm::kSuccess));
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DeinitializeDecoder(PP_DecryptorStreamType decoder_type,
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     uint32_t request_id) {
5514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  PP_DCHECK(cdm_);  // InitializeXxxxxDecoder should have succeeded.
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_) {
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_->DeinitializeDecoder(
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PpDecryptorStreamTypeToCdmStreamType(decoder_type));
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CallOnMain(callback_factory_.NewCallback(
5588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &CdmAdapter::DecoderDeinitializeDone,
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decoder_type,
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request_id));
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::ResetDecoder(PP_DecryptorStreamType decoder_type,
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              uint32_t request_id) {
5654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  PP_DCHECK(cdm_);  // InitializeXxxxxDecoder should have succeeded.
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_)
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cdm_->ResetDecoder(PpDecryptorStreamTypeToCdmStreamType(decoder_type));
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  CallOnMain(callback_factory_.NewCallback(&CdmAdapter::DecoderResetDone,
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           decoder_type,
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           request_id));
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DecryptAndDecode(
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PP_DecryptorStreamType decoder_type,
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pp::Buffer_Dev encrypted_buffer,
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PP_EncryptedBlockInfo& encrypted_block_info) {
5784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  PP_DCHECK(cdm_);  // InitializeXxxxxDecoder should have succeeded.
5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Release a buffer that the caller indicated it is finished with.
5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  allocator_.Release(encrypted_block_info.tracking_info.buffer_id);
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cdm::InputBuffer input_buffer;
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<cdm::SubsampleEntry> subsamples;
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (cdm_ && !encrypted_buffer.is_null()) {
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ConfigureInputBuffer(encrypted_buffer,
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         encrypted_block_info,
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         &subsamples,
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         &input_buffer);
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cdm::Status status = cdm::kDecodeError;
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (decoder_type) {
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTORSTREAMTYPE_VIDEO: {
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LinkedVideoFrame video_frame(new VideoFrameImpl());
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (cdm_)
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        status = cdm_->DecryptAndDecodeFrame(input_buffer, video_frame.get());
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CallOnMain(callback_factory_.NewCallback(
5998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          &CdmAdapter::DeliverFrame,
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          status,
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          video_frame,
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          encrypted_block_info.tracking_info));
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case PP_DECRYPTORSTREAMTYPE_AUDIO: {
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LinkedAudioFrames audio_frames(new AudioFramesImpl());
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (cdm_) {
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        status = cdm_->DecryptAndDecodeSamples(input_buffer,
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               audio_frames.get());
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CallOnMain(callback_factory_.NewCallback(
6138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          &CdmAdapter::DeliverSamples,
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          status,
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          audio_frames,
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          encrypted_block_info.tracking_info));
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_NOTREACHED();
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)cdm::Buffer* CdmAdapter::Allocate(uint32_t capacity) {
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return allocator_.Allocate(capacity);
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::SetTimer(int64_t delay_ms, void* context) {
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: doesn't really need to run on the main thread; could just as well run
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on a helper thread if |cdm_| were thread-friendly and care was taken.  We
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only use CallOnMainThread() here to get delayed-execution behavior.
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::Module::Get()->core()->CallOnMainThread(
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delay_ms,
6368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      callback_factory_.NewCallback(&CdmAdapter::TimerExpired, context),
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_OK);
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::TimerExpired(int32_t result, void* context) {
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  cdm_->TimerExpired(context);
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// cdm::Host_4 methods
646f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
6478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)double CdmAdapter::GetCurrentWallTimeInSeconds() {
6481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return GetCurrentWallTime();
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
651a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void CdmAdapter::OnSessionCreated(uint32_t session_id,
652a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  const char* web_session_id,
653a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  uint32_t web_session_id_length) {
654f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  uint32_t promise_id = cdm_->LookupPromiseId(session_id);
655f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm_->AssignWebSessionId(session_id, web_session_id, web_session_id_length);
656f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  OnResolveNewSessionPromise(promise_id, web_session_id, web_session_id_length);
657f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
658f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
659f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionMessage(uint32_t session_id,
660f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  const char* message,
661f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  uint32_t message_length,
662f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  const char* destination_url,
663f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  uint32_t destination_url_length) {
664f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string web_session_id = cdm_->LookupWebSessionId(session_id);
665f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  OnSessionMessage(web_session_id.data(),
666f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   web_session_id.length(),
667f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   message,
668f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   message_length,
669f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   destination_url,
670f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   destination_url_length);
671f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
672f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
673f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionReady(uint32_t session_id) {
674f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  uint32_t promise_id = cdm_->LookupPromiseId(session_id);
675f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (promise_id) {
676f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    OnResolvePromise(promise_id);
677f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  } else {
678f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    std::string web_session_id = cdm_->LookupWebSessionId(session_id);
6791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PostOnMain(callback_factory_.NewCallback(
6801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        &CdmAdapter::SendSessionReadyInternal, web_session_id));
681f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
682f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
683f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
684f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionClosed(uint32_t session_id) {
685f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  uint32_t promise_id = cdm_->LookupPromiseId(session_id);
686f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string web_session_id = cdm_->LookupWebSessionId(session_id);
687f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm_->DropWebSessionId(web_session_id);
688f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (promise_id) {
689f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    OnResolvePromise(promise_id);
690f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  } else {
691f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    OnSessionClosed(web_session_id.data(), web_session_id.length());
692f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
693f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
694f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
695f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionError(uint32_t session_id,
696f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                cdm::MediaKeyError error_code,
697f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                uint32_t system_code) {
698f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  uint32_t promise_id = cdm_->LookupPromiseId(session_id);
699f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
700f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Existing cdm::MediaKeyError don't map to DOM error names. Convert them
701f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // into non-standard names so that the prefixed API can extract them.
702f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(jrummell): Remove this conversion and the inverse when CDM4 is gone.
703f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cdm::Error error;
704f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  switch (error_code) {
705f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kPrefixedClientError:
706f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error = cdm::kClientError;
707f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      break;
708f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kPrefixedOutputError:
709f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error = cdm::kOutputError;
710f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      break;
711f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case cdm::kPrefixedUnknownError:
712f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    default:
713f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error = cdm::kUnknownError;
714f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      break;
715f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
716f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
717f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (promise_id) {
718f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    RejectPromise(promise_id, error, system_code, std::string());
719f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  } else {
720f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    std::string web_session_id = cdm_->LookupWebSessionId(session_id);
721f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    OnSessionError(web_session_id.data(),
722f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   web_session_id.length(),
723f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   error,
724f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   system_code,
725f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   NULL,
726f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   0);
727f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
728f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
729f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// cdm::Host_6 methods
7316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)cdm::Time CdmAdapter::GetCurrentWallTime() {
733f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return pp::Module::Get()->core()->GetTime();
734f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
735f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
736f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnResolveNewSessionPromise(uint32_t promise_id,
737f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                            const char* web_session_id,
738f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                            uint32_t web_session_id_length) {
739f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
740f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      &CdmAdapter::SendPromiseResolvedWithSessionInternal,
741f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      promise_id,
742a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      std::string(web_session_id, web_session_id_length)));
7431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
7446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CdmAdapter::OnResolvePromise(uint32_t promise_id) {
7461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PostOnMain(callback_factory_.NewCallback(
7471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      &CdmAdapter::SendPromiseResolvedInternal, promise_id));
7486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
7496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
7506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::OnResolveKeyIdsPromise(uint32_t promise_id,
7516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        const cdm::BinaryData* usable_key_ids,
7526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        uint32_t usable_key_ids_length) {
7536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::vector<std::vector<uint8> > key_ids;
7546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  for (uint32_t i = 0; i < usable_key_ids_length; ++i) {
7556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    key_ids.push_back(
7566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        std::vector<uint8>(usable_key_ids[i].data,
7576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                           usable_key_ids[i].data + usable_key_ids[i].length));
7586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
7596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
7606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      &CdmAdapter::SendPromiseResolvedWithUsableKeyIdsInternal,
7616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      promise_id,
7626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      key_ids));
7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
765f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnRejectPromise(uint32_t promise_id,
766f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 cdm::Error error,
767f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 uint32_t system_code,
768f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 const char* error_message,
769f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 uint32_t error_message_length) {
7701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // UMA to investigate http://crbug.com/410630
7711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(xhwang): Remove after bug is fixed.
7721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (system_code == 0x27) {
7731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    pp::UMAPrivate uma_interface(this);
7741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    uma_interface.HistogramCustomCounts("Media.EME.CdmFileIO.FileSizeKBOnError",
7751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        last_read_file_size_kb_,
7761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        kSizeKBMin,
7771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        kSizeKBMax,
7781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        kSizeKBBuckets);
7791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
7801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
781f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  RejectPromise(promise_id,
782f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                error,
783f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                system_code,
784f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                std::string(error_message, error_message_length));
785f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
786f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
787f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::RejectPromise(uint32_t promise_id,
788f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               cdm::Error error,
789f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               uint32_t system_code,
790f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               const std::string& error_message) {
791f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
792f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      &CdmAdapter::SendPromiseRejectedInternal,
793f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      promise_id,
794f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SessionError(error, system_code, error_message)));
795f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
796f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
797f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionMessage(const char* web_session_id,
798f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  uint32_t web_session_id_length,
799a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  const char* message,
800a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  uint32_t message_length,
801a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  const char* destination_url,
802a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                  uint32_t destination_url_length) {
803a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
804a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      &CdmAdapter::SendSessionMessageInternal,
805f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      std::string(web_session_id, web_session_id_length),
806a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      std::vector<uint8>(message, message + message_length),
807a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      std::string(destination_url, destination_url_length)));
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::OnSessionUsableKeysChange(const char* web_session_id,
8116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                           uint32_t web_session_id_length,
8126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                           bool has_additional_usable_key) {
8136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
8146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      &CdmAdapter::SendSessionUsableKeysChangeInternal,
8156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      std::string(web_session_id, web_session_id_length),
8166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      has_additional_usable_key));
817f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
818f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
819f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnExpirationChange(const char* web_session_id,
820f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                    uint32_t web_session_id_length,
821f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                    cdm::Time new_expiry_time) {
8226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
8236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      &CdmAdapter::SendExpirationChangeInternal,
8246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      std::string(web_session_id, web_session_id_length),
8256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      new_expiry_time));
826f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
827f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
828f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionClosed(const char* web_session_id,
829f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                 uint32_t web_session_id_length) {
830a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
831f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      &CdmAdapter::SendSessionClosedInternal,
832f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      std::string(web_session_id, web_session_id_length)));
833a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
834a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
835f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::OnSessionError(const char* web_session_id,
836f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                uint32_t web_session_id_length,
837f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                cdm::Error error,
838f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                uint32_t system_code,
839f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                const char* error_message,
840f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                uint32_t error_message_length) {
841a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  PostOnMain(callback_factory_.NewCallback(
842a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      &CdmAdapter::SendSessionErrorInternal,
843f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      std::string(web_session_id, web_session_id_length),
844f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SessionError(error,
845f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   system_code,
846f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   std::string(error_message, error_message_length))));
847a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
848a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
849f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Helpers to pass the event to Pepper.
850f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
851f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::SendPromiseResolvedInternal(int32_t result,
852f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                             uint32_t promise_id) {
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
854f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pp::ContentDecryptor_Private::PromiseResolved(promise_id);
855f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
856f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
857f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::SendPromiseResolvedWithSessionInternal(
858f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int32_t result,
859f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    uint32_t promise_id,
860f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const std::string& web_session_id) {
861f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
862f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pp::ContentDecryptor_Private::PromiseResolvedWithSession(promise_id,
863f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                                           web_session_id);
864f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
865f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
8666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::SendPromiseResolvedWithUsableKeyIdsInternal(
8676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    int32_t result,
8686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    uint32_t promise_id,
8696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    std::vector<std::vector<uint8> > key_ids) {
8706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
8711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::ContentDecryptor_Private::PromiseResolvedWithKeyIds(promise_id, key_ids);
8726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
8736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
874f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::SendPromiseRejectedInternal(int32_t result,
875f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                             uint32_t promise_id,
876f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                             const SessionError& error) {
877f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
878f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pp::ContentDecryptor_Private::PromiseRejected(
879f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      promise_id,
880f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      CdmExceptionTypeToPpCdmExceptionType(error.error),
881f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error.system_code,
882f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error.error_description);
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
88546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void CdmAdapter::SendSessionMessageInternal(
88646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    int32_t result,
887f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const std::string& web_session_id,
88846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    const std::vector<uint8>& message,
88946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    const std::string& destination_url) {
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
8912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  pp::VarArrayBuffer message_array_buffer(message.size());
8932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (message.size() > 0) {
8942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    memcpy(message_array_buffer.Map(), message.data(), message.size());
8952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
897a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  pp::ContentDecryptor_Private::SessionMessage(
898f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      web_session_id, message_array_buffer, destination_url);
899a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
900a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
901f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void CdmAdapter::SendSessionReadyInternal(int32_t result,
902f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                          const std::string& web_session_id) {
903a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
904f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pp::ContentDecryptor_Private::SessionReady(web_session_id);
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
907a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void CdmAdapter::SendSessionClosedInternal(int32_t result,
908f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                           const std::string& web_session_id) {
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
910f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pp::ContentDecryptor_Private::SessionClosed(web_session_id);
911f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
912f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
913a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void CdmAdapter::SendSessionErrorInternal(int32_t result,
914f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                          const std::string& web_session_id,
915f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                          const SessionError& error) {
916f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PP_DCHECK(result == PP_OK);
917a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  pp::ContentDecryptor_Private::SessionError(
918f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      web_session_id,
919f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      CdmExceptionTypeToPpCdmExceptionType(error.error),
920f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error.system_code,
921f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      error.error_description);
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::SendSessionUsableKeysChangeInternal(
9256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    int32_t result,
9266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const std::string& web_session_id,
9276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool has_additional_usable_key) {
9286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
9291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::ContentDecryptor_Private::SessionKeysChange(web_session_id,
9301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                  has_additional_usable_key);
9316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void CdmAdapter::SendExpirationChangeInternal(int32_t result,
9346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                              const std::string& web_session_id,
9356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                              cdm::Time new_expiry_time) {
9366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
9371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::ContentDecryptor_Private::SessionExpirationChange(web_session_id,
9381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                        new_expiry_time);
9396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
9406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DeliverBlock(int32_t result,
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const cdm::Status& status,
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const LinkedDecryptedBlock& decrypted_block,
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const PP_DecryptTrackingInfo& tracking_info) {
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
946116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PP_DecryptedBlockInfo decrypted_block_info = {};
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  decrypted_block_info.tracking_info = tracking_info;
9482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decrypted_block_info.tracking_info.timestamp = decrypted_block->Timestamp();
9492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decrypted_block_info.tracking_info.buffer_id = 0;
9502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decrypted_block_info.data_size = 0;
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  decrypted_block_info.result = CdmStatusToPpDecryptResult(status);
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::Buffer_Dev buffer;
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (decrypted_block_info.result == PP_DECRYPTRESULT_SUCCESS) {
9562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_DCHECK(decrypted_block.get() && decrypted_block->DecryptedBuffer());
9572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!decrypted_block.get() || !decrypted_block->DecryptedBuffer()) {
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_NOTREACHED();
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_block_info.result = PP_DECRYPTRESULT_DECRYPT_ERROR;
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
9612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PpbBuffer* ppb_buffer =
9622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          static_cast<PpbBuffer*>(decrypted_block->DecryptedBuffer());
9632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_block_info.tracking_info.buffer_id = ppb_buffer->buffer_id();
9642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_block_info.data_size = ppb_buffer->Size();
965116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
966116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      buffer = ppb_buffer->TakeBuffer();
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::ContentDecryptor_Private::DeliverBlock(buffer, decrypted_block_info);
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DecoderInitializeDone(int32_t result,
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       PP_DecryptorStreamType decoder_type,
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       uint32_t request_id,
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       bool success) {
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::ContentDecryptor_Private::DecoderInitializeDone(decoder_type,
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                      request_id,
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                      success);
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DecoderDeinitializeDone(int32_t result,
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         PP_DecryptorStreamType decoder_type,
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         uint32_t request_id) {
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::ContentDecryptor_Private::DecoderDeinitializeDone(decoder_type,
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                        request_id);
9885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DecoderResetDone(int32_t result,
9915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  PP_DecryptorStreamType decoder_type,
9925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  uint32_t request_id) {
9935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::ContentDecryptor_Private::DecoderResetDone(decoder_type, request_id);
9945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DeliverFrame(
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int32_t result,
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const cdm::Status& status,
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const LinkedVideoFrame& video_frame,
10005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PP_DecryptTrackingInfo& tracking_info) {
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
1002116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PP_DecryptedFrameInfo decrypted_frame_info = {};
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  decrypted_frame_info.tracking_info.request_id = tracking_info.request_id;
10042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decrypted_frame_info.tracking_info.buffer_id = 0;
10055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  decrypted_frame_info.result = CdmStatusToPpDecryptResult(status);
10065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::Buffer_Dev buffer;
10085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (decrypted_frame_info.result == PP_DECRYPTRESULT_SUCCESS) {
10102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!IsValidVideoFrame(video_frame)) {
10115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_NOTREACHED();
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.result = PP_DECRYPTRESULT_DECODE_ERROR;
10135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
10142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PpbBuffer* ppb_buffer =
10152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          static_cast<PpbBuffer*>(video_frame->FrameBuffer());
10162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_frame_info.tracking_info.timestamp = video_frame->Timestamp();
10182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_frame_info.tracking_info.buffer_id = ppb_buffer->buffer_id();
10192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_frame_info.format =
10202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          CdmVideoFormatToPpDecryptedFrameFormat(video_frame->Format());
10212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_frame_info.width = video_frame->Size().width;
10222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decrypted_frame_info.height = video_frame->Size().height;
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.plane_offsets[PP_DECRYPTEDFRAMEPLANES_Y] =
10242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->PlaneOffset(cdm::VideoFrame::kYPlane);
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.plane_offsets[PP_DECRYPTEDFRAMEPLANES_U] =
10262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->PlaneOffset(cdm::VideoFrame::kUPlane);
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.plane_offsets[PP_DECRYPTEDFRAMEPLANES_V] =
10282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->PlaneOffset(cdm::VideoFrame::kVPlane);
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.strides[PP_DECRYPTEDFRAMEPLANES_Y] =
10302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->Stride(cdm::VideoFrame::kYPlane);
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.strides[PP_DECRYPTEDFRAMEPLANES_U] =
10322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->Stride(cdm::VideoFrame::kUPlane);
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      decrypted_frame_info.strides[PP_DECRYPTEDFRAMEPLANES_V] =
10342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          video_frame->Stride(cdm::VideoFrame::kVPlane);
1035116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1036116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      buffer = ppb_buffer->TakeBuffer();
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1039116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::ContentDecryptor_Private::DeliverFrame(buffer, decrypted_frame_info);
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::DeliverSamples(int32_t result,
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const cdm::Status& status,
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const LinkedAudioFrames& audio_frames,
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const PP_DecryptTrackingInfo& tracking_info) {
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PP_DCHECK(result == PP_OK);
10485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1049116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PP_DecryptedSampleInfo decrypted_sample_info = {};
10501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  decrypted_sample_info.tracking_info = tracking_info;
10511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  decrypted_sample_info.tracking_info.timestamp = 0;
10521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  decrypted_sample_info.tracking_info.buffer_id = 0;
10531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  decrypted_sample_info.data_size = 0;
10541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  decrypted_sample_info.result = CdmStatusToPpDecryptResult(status);
10555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pp::Buffer_Dev buffer;
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (decrypted_sample_info.result == PP_DECRYPTRESULT_SUCCESS) {
10592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_DCHECK(audio_frames.get() && audio_frames->FrameBuffer());
10602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!audio_frames.get() || !audio_frames->FrameBuffer()) {
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PP_NOTREACHED();
10621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      decrypted_sample_info.result = PP_DECRYPTRESULT_DECRYPT_ERROR;
10635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
10642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PpbBuffer* ppb_buffer =
10652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          static_cast<PpbBuffer*>(audio_frames->FrameBuffer());
1066116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      decrypted_sample_info.tracking_info.buffer_id = ppb_buffer->buffer_id();
10681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      decrypted_sample_info.data_size = ppb_buffer->Size();
10691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      decrypted_sample_info.format =
10701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          CdmAudioFormatToPpDecryptedSampleFormat(audio_frames->Format());
1071116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1072116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      buffer = ppb_buffer->TakeBuffer();
10735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  pp::ContentDecryptor_Private::DeliverSamples(buffer, decrypted_sample_info);
10775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool CdmAdapter::IsValidVideoFrame(const LinkedVideoFrame& video_frame) {
10802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!video_frame.get() ||
10812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !video_frame->FrameBuffer() ||
10822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (video_frame->Format() != cdm::kI420 &&
10832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       video_frame->Format() != cdm::kYv12)) {
10845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CDM_DLOG() << "Invalid video frame!";
10852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
10862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PpbBuffer* ppb_buffer = static_cast<PpbBuffer*>(video_frame->FrameBuffer());
10892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (uint32_t i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
10912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int plane_height = (i == cdm::VideoFrame::kYPlane) ?
10922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        video_frame->Size().height : (video_frame->Size().height + 1) / 2;
10932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cdm::VideoFrame::VideoPlane plane =
10942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        static_cast<cdm::VideoFrame::VideoPlane>(i);
10952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (ppb_buffer->Size() < video_frame->PlaneOffset(plane) +
10962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             plane_height * video_frame->Stride(plane)) {
10972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
10982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
10992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
11022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CdmAdapter::OnFirstFileRead(int32_t file_size_bytes) {
11051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PP_DCHECK(IsMainThread());
11061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PP_DCHECK(file_size_bytes >= 0);
11071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  last_read_file_size_kb_ = file_size_bytes / 1024;
11091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (file_size_uma_reported_)
11111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
11121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::UMAPrivate uma_interface(this);
11141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  uma_interface.HistogramCustomCounts(
11151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "Media.EME.CdmFileIO.FileSizeKBOnFirstRead",
11161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      last_read_file_size_kb_,
11171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      kSizeKBMin,
11181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      kSizeKBMax,
11191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      kSizeKBBuckets);
11201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  file_size_uma_reported_ = true;
11211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
11221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
11235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(NDEBUG)
11245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void CdmAdapter::LogToConsole(const pp::Var& value) {
11255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PP_DCHECK(IsMainThread());
11265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const PPB_Console* console = reinterpret_cast<const PPB_Console*>(
11275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      pp::Module::Get()->GetBrowserInterface(PPB_CONSOLE_INTERFACE));
11285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  console->Log(pp_instance(), PP_LOGLEVEL_LOG, value.pp_var());
11295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
11305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(NDEBUG)
11315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
11328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::SendPlatformChallenge(
11331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const char* service_id, uint32_t service_id_length,
11341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const char* challenge, uint32_t challenge_length) {
11358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
11368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  pp::VarArrayBuffer challenge_var(challenge_length);
11378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  uint8_t* var_data = static_cast<uint8_t*>(challenge_var.Map());
11388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  memcpy(var_data, challenge, challenge_length);
11398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string service_id_str(service_id, service_id_length);
1141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  linked_ptr<PepperPlatformChallengeResponse> response(
1143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new PepperPlatformChallengeResponse());
1144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
11458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int32_t result = platform_verification_.ChallengePlatform(
1146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      pp::Var(service_id_str),
1147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      challenge_var,
1148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      &response->signed_data,
1149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      &response->signed_data_signature,
1150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      &response->platform_key_certificate,
1151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      callback_factory_.NewCallback(&CdmAdapter::SendPlatformChallengeDone,
1152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                    response));
11538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  challenge_var.Unmap();
1154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (result == PP_OK_COMPLETIONPENDING)
11558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
11568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Fall through on error and issue an empty OnPlatformChallengeResponse().
11588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PP_DCHECK(result != PP_OK);
11598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
11608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cdm::PlatformChallengeResponse platform_challenge_response = {};
1162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cdm_->OnPlatformChallengeResponse(platform_challenge_response);
11638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
11648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::EnableOutputProtection(uint32_t desired_protection_mask) {
11668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
11675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int32_t result = output_protection_.EnableProtection(
11688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      desired_protection_mask, callback_factory_.NewCallback(
11698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          &CdmAdapter::EnableProtectionDone));
11708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Errors are ignored since clients must call QueryOutputProtectionStatus() to
11728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // inspect the protection status on a regular basis.
11735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
11745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (result != PP_OK && result != PP_OK_COMPLETIONPENDING)
11755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CDM_DLOG() << __FUNCTION__ << " failed!";
11768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
11778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
11788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void CdmAdapter::QueryOutputProtectionStatus() {
11808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
11818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PP_DCHECK(!query_output_protection_in_progress_);
11828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  output_link_mask_ = output_protection_mask_ = 0;
11848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const int32_t result = output_protection_.QueryStatus(
11858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &output_link_mask_,
11868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &output_protection_mask_,
11878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      callback_factory_.NewCallback(
11888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          &CdmAdapter::QueryOutputProtectionStatusDone));
11898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (result == PP_OK_COMPLETIONPENDING) {
11908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    query_output_protection_in_progress_ = true;
1191010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ReportOutputProtectionQuery();
11928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
11938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
11948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Fall through on error and issue an empty OnQueryOutputProtectionStatus().
11968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PP_DCHECK(result != PP_OK);
11978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
11988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  cdm_->OnQueryOutputProtectionStatus(0, 0);
12008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void CdmAdapter::OnDeferredInitializationDone(cdm::StreamType stream_type,
12031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                              cdm::Status decoder_status) {
12041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (stream_type) {
12051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kStreamTypeAudio:
12061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      PP_DCHECK(deferred_initialize_audio_decoder_);
12071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      CallOnMain(
12081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          callback_factory_.NewCallback(&CdmAdapter::DecoderInitializeDone,
12091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        PP_DECRYPTORSTREAMTYPE_AUDIO,
12101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        deferred_audio_decoder_config_id_,
12111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        decoder_status == cdm::kSuccess));
12121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_initialize_audio_decoder_ = false;
12131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_audio_decoder_config_id_ = 0;
12141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
12151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case cdm::kStreamTypeVideo:
12161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      PP_DCHECK(deferred_initialize_video_decoder_);
12171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      CallOnMain(
12181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          callback_factory_.NewCallback(&CdmAdapter::DecoderInitializeDone,
12191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        PP_DECRYPTORSTREAMTYPE_VIDEO,
12201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        deferred_video_decoder_config_id_,
12211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                        decoder_status == cdm::kSuccess));
12221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_initialize_video_decoder_ = false;
12231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      deferred_video_decoder_config_id_ = 0;
12241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
12251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
12261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
12271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
12285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The CDM owns the returned object and must call FileIO::Close() to release it.
12295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)cdm::FileIO* CdmAdapter::CreateFileIO(cdm::FileIOClient* client) {
12301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return new CdmFileIOImpl(
12311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      client,
12321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      pp_instance(),
12331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      callback_factory_.NewCallback(&CdmAdapter::OnFirstFileRead));
12345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
12355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
1237010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void CdmAdapter::ReportOutputProtectionUMA(OutputProtectionStatus status) {
12381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::UMAPrivate uma_interface(this);
12391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  uma_interface.HistogramEnumeration(
1240010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      "Media.EME.OutputProtection", status, OUTPUT_PROTECTION_MAX);
1241010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
1242010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1243010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void CdmAdapter::ReportOutputProtectionQuery() {
1244010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (uma_for_output_protection_query_reported_)
1245010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
1246010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1247010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ReportOutputProtectionUMA(OUTPUT_PROTECTION_QUERIED);
1248010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  uma_for_output_protection_query_reported_ = true;
1249010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
1250010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1251010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void CdmAdapter::ReportOutputProtectionQueryResult() {
1252010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (uma_for_output_protection_positive_result_reported_)
1253010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
1254010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1255010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Report UMAs for output protection query result.
1256010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  uint32_t external_links = (output_link_mask_ & ~cdm::kLinkTypeInternal);
1257010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1258010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!external_links) {
1259