1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "CDMSessionAVFoundationCF.h"
28
29#if ENABLE(ENCRYPTED_MEDIA_V2) && HAVE(AVFOUNDATION_LEGIBLE_OUTPUT_SUPPORT)
30
31#include "CDM.h"
32#include "CDMSession.h"
33#include "ExceptionCode.h"
34#include "MediaPlayer.h"
35#include "MediaPlayerPrivateAVFoundationCF.h"
36#include "NotImplemented.h"
37#include "SoftLinking.h"
38#include "UUID.h"
39#include <AVFoundationCF/AVFoundationCF.h>
40#include <wtf/text/CString.h>
41
42// The softlink header files must be included after the AVCF and CoreMedia header files.
43#include "AVFoundationCFSoftLinking.h"
44
45namespace WebCore {
46
47CDMSessionAVFoundationCF::CDMSessionAVFoundationCF(MediaPlayerPrivateAVFoundationCF* parent)
48    : m_parent(parent)
49    , m_client(nullptr)
50    , m_sessionId(createCanonicalUUIDString())
51{
52}
53
54PassRefPtr<Uint8Array> CDMSessionAVFoundationCF::generateKeyRequest(const String& mimeType, Uint8Array* initData, String& destinationURL, unsigned short& errorCode, unsigned long& systemCode)
55{
56    UNUSED_PARAM(mimeType);
57
58    String keyURI;
59    String keyID;
60    RefPtr<Uint8Array> certificate;
61    if (!MediaPlayerPrivateAVFoundationCF::extractKeyURIKeyIDAndCertificateFromInitData(initData, keyURI, keyID, certificate)) {
62        errorCode = MediaPlayer::InvalidPlayerState;
63        return nullptr;
64    }
65
66    m_request = m_parent->takeRequestForKeyURI(keyURI);
67    if (!m_request) {
68        errorCode = MediaPlayer::InvalidPlayerState;
69        return nullptr;
70    }
71
72    RetainPtr<CFMutableDataRef> certificateData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, certificate->byteLength()));
73    CFDataAppendBytes(certificateData.get(), reinterpret_cast<const UInt8*>(certificate->baseAddress()), certificate->byteLength());
74
75    CString assetStr = keyID.utf8();
76    RetainPtr<CFMutableDataRef> assetID = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, assetStr.length()));
77    CFDataAppendBytes(assetID.get(), reinterpret_cast<const UInt8*>(assetStr.data()), assetStr.length());
78
79    CFErrorRef cfError = nullptr;
80    RetainPtr<CFDataRef> keyRequest = AVCFAssetResourceLoadingRequestCreateStreamingContentKeyRequestDataForApp(m_request.get(), certificateData.get(), assetID.get(), nullptr, &cfError);
81
82    if (!keyRequest) {
83        RetainPtr<CFDictionaryRef> userInfo;
84        if (cfError) {
85            userInfo = adoptCF(CFErrorCopyUserInfo(cfError));
86
87            if (userInfo) {
88                CFErrorRef underlyingError = (CFErrorRef)CFDictionaryGetValue(userInfo.get(), kCFErrorUnderlyingErrorKey);
89                if (underlyingError)
90                    systemCode = CFErrorGetCode(underlyingError);
91            }
92
93            CFRelease(cfError);
94        }
95
96        return nullptr;
97    }
98
99    errorCode = MediaPlayer::NoError;
100    systemCode = 0;
101    destinationURL = String();
102
103    RefPtr<ArrayBuffer> keyRequestBuffer = ArrayBuffer::create(CFDataGetBytePtr(keyRequest.get()), CFDataGetLength(keyRequest.get()));
104    return Uint8Array::create(keyRequestBuffer, 0, keyRequestBuffer->byteLength());
105}
106
107void CDMSessionAVFoundationCF::releaseKeys()
108{
109}
110
111bool CDMSessionAVFoundationCF::update(Uint8Array* key, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, unsigned long& systemCode)
112{
113    RetainPtr<CFMutableDataRef> keyData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, key->byteLength()));
114    CFDataAppendBytes(keyData.get(), reinterpret_cast<const UInt8*>(key->baseAddress()), key->byteLength());
115
116    // TODO: AVCFAssetResourceLoadingDataRequestRespondWithData(m_request.get(), keyData.get());
117    // TODO: AVCFAssetResourceLoadingRequestFinishedLoading(m_request.get());
118    notImplemented();
119
120    errorCode = MediaPlayer::NoError;
121    systemCode = 0;
122    nextMessage = nullptr;
123
124    return true;
125}
126
127}
128
129#endif
130