[Win] Extend AVFoundationCF Media Implementation
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / cf / CDMSessionAVFoundationCF.cpp
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
45 namespace WebCore {
46
47 CDMSessionAVFoundationCF::CDMSessionAVFoundationCF(MediaPlayerPrivateAVFoundationCF* parent)
48     : m_parent(parent)
49     , m_client(nullptr)
50     , m_sessionId(createCanonicalUUIDString())
51 {
52 }
53
54 PassRefPtr<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                 if (CFErrorRef underlyingError = (CFErrorRef)CFDictionaryGetValue(userInfo.get(), kCFErrorUnderlyingErrorKey))
89                     systemCode = CFErrorGetCode(underlyingError);
90             }
91
92             CFRelease(cfError);
93         }
94
95         return nullptr;
96     }
97
98     errorCode = MediaPlayer::NoError;
99     systemCode = 0;
100     destinationURL = String();
101
102     RefPtr<ArrayBuffer> keyRequestBuffer = ArrayBuffer::create(CFDataGetBytePtr(keyRequest.get()), CFDataGetLength(keyRequest.get()));
103     return Uint8Array::create(keyRequestBuffer, 0, keyRequestBuffer->byteLength());
104 }
105
106 void CDMSessionAVFoundationCF::releaseKeys()
107 {
108 }
109
110 bool CDMSessionAVFoundationCF::update(Uint8Array* key, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, unsigned long& systemCode)
111 {
112     RetainPtr<CFMutableDataRef> keyData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, key->byteLength()));
113     CFDataAppendBytes(keyData.get(), reinterpret_cast<const UInt8*>(key->baseAddress()), key->byteLength());
114
115     AVCFAssetResourceLoadingRequestFinishLoadingWithResponse(m_request.get(), nullptr, keyData.get(), nullptr);
116
117     errorCode = MediaPlayer::NoError;
118     systemCode = 0;
119     nextMessage = nullptr;
120
121     return true;
122 }
123
124 }
125
126 #endif