Only include those parts of AVFoundation.framework which are strictly needed.
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / CDMSessionAVFoundationObjC.mm
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 #import "config.h"
27 #import "CDMSessionAVFoundationObjC.h"
28
29 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
30
31 #import "LegacyCDM.h"
32 #import "LegacyCDMSession.h"
33 #import "MediaPlayer.h"
34 #import "MediaPlayerPrivateAVFoundationObjC.h"
35 #import "SoftLinking.h"
36 #import "UUID.h"
37 #import "WebCoreNSErrorExtras.h"
38 #import <AVFoundation/AVAsset.h>
39 #import <AVFoundation/AVAssetResourceLoader.h>
40 #import <objc/objc-runtime.h>
41 #import <wtf/MainThread.h>
42
43 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
44 SOFT_LINK_CLASS(AVFoundation, AVURLAsset)
45 SOFT_LINK_CLASS(AVFoundation, AVAssetResourceLoadingRequest)
46 #define AVURLAsset getAVURLAssetClass()
47 #define AVAssetResourceLoadingRequest getAVAssetResourceLoadingRequest()
48
49 namespace WebCore {
50
51 CDMSessionAVFoundationObjC::CDMSessionAVFoundationObjC(MediaPlayerPrivateAVFoundationObjC* parent, CDMSessionClient* client)
52     : m_parent(parent->createWeakPtr())
53     , m_client(client)
54     , m_sessionId(createCanonicalUUIDString())
55     , m_weakPtrFactory(this)
56 {
57 }
58
59 CDMSessionAVFoundationObjC::~CDMSessionAVFoundationObjC()
60 {
61 }
62
63 RefPtr<Uint8Array> CDMSessionAVFoundationObjC::generateKeyRequest(const String& mimeType, Uint8Array* initData, String& destinationURL, unsigned short& errorCode, uint32_t& systemCode)
64 {
65     UNUSED_PARAM(mimeType);
66
67     if (!m_parent) {
68         errorCode = CDM::UnknownError;
69         return nullptr;
70     }
71
72     String keyURI;
73     String keyID;
74     RefPtr<Uint8Array> certificate;
75     if (!MediaPlayerPrivateAVFoundationObjC::extractKeyURIKeyIDAndCertificateFromInitData(initData, keyURI, keyID, certificate)) {
76         errorCode = CDM::UnknownError;
77         return nullptr;
78     }
79
80     m_request = m_parent->takeRequestForKeyURI(keyURI);
81     if (!m_request) {
82         errorCode = CDM::UnknownError;
83         return nullptr;
84     }
85
86     RetainPtr<NSData> certificateData = adoptNS([[NSData alloc] initWithBytes:certificate->baseAddress() length:certificate->byteLength()]);
87     NSString* assetStr = keyID;
88     RetainPtr<NSData> assetID = [NSData dataWithBytes: [assetStr cStringUsingEncoding:NSUTF8StringEncoding] length:[assetStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
89     NSError* nsError = 0;
90     RetainPtr<NSData> keyRequest = [m_request streamingContentKeyRequestDataForApp:certificateData.get() contentIdentifier:assetID.get() options:nil error:&nsError];
91
92     if (!keyRequest) {
93         errorCode = CDM::DomainError;
94         systemCode = mediaKeyErrorSystemCode(nsError);
95         return nullptr;
96     }
97
98     errorCode = MediaPlayer::NoError;
99     systemCode = 0;
100     destinationURL = String();
101
102     RefPtr<ArrayBuffer> keyRequestBuffer = ArrayBuffer::create([keyRequest.get() bytes], [keyRequest.get() length]);
103     return Uint8Array::create(keyRequestBuffer, 0, keyRequestBuffer->byteLength());
104 }
105
106 void CDMSessionAVFoundationObjC::releaseKeys()
107 {
108 }
109
110 bool CDMSessionAVFoundationObjC::update(Uint8Array* key, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, uint32_t& systemCode)
111 {
112     RetainPtr<NSData> keyData = adoptNS([[NSData alloc] initWithBytes:key->baseAddress() length:key->byteLength()]);
113     [[m_request dataRequest] respondWithData:keyData.get()];
114     [m_request finishLoading];
115     errorCode = MediaPlayer::NoError;
116     systemCode = 0;
117     nextMessage = nullptr;
118
119     return true;
120 }
121
122 void CDMSessionAVFoundationObjC::playerDidReceiveError(NSError *error)
123 {
124     if (!m_client)
125         return;
126
127     unsigned long code = mediaKeyErrorSystemCode(error);
128     m_client->sendError(CDMSessionClient::MediaKeyErrorDomain, code);
129 }
130
131 }
132
133 #endif