Unreviewed, rolling out r244627.
[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 "WebCoreNSErrorExtras.h"
36 #import <AVFoundation/AVAsset.h>
37 #import <AVFoundation/AVAssetResourceLoader.h>
38 #import <JavaScriptCore/TypedArrayInlines.h>
39 #import <objc/objc-runtime.h>
40 #import <wtf/MainThread.h>
41 #import <wtf/SoftLinking.h>
42 #import <wtf/UUID.h>
43
44 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
45 SOFT_LINK_CLASS(AVFoundation, AVURLAsset)
46 SOFT_LINK_CLASS(AVFoundation, AVAssetResourceLoadingRequest)
47 #define AVURLAsset getAVURLAssetClass()
48 #define AVAssetResourceLoadingRequest getAVAssetResourceLoadingRequest()
49
50 namespace WebCore {
51
52 CDMSessionAVFoundationObjC::CDMSessionAVFoundationObjC(MediaPlayerPrivateAVFoundationObjC* parent, LegacyCDMSessionClient* client)
53     : m_parent(makeWeakPtr(*parent))
54     , m_client(client)
55     , m_sessionId(createCanonicalUUIDString())
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 = LegacyCDM::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 = LegacyCDM::UnknownError;
77         return nullptr;
78     }
79
80     m_request = m_parent->takeRequestForKeyURI(keyURI);
81     if (!m_request) {
82         errorCode = LegacyCDM::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 = LegacyCDM::DomainError;
94         systemCode = mediaKeyErrorSystemCode(nsError);
95         return nullptr;
96     }
97
98     errorCode = MediaPlayer::NoError;
99     systemCode = 0;
100     destinationURL = String();
101
102     auto keyRequestBuffer = ArrayBuffer::create([keyRequest.get() bytes], [keyRequest.get() length]);
103     unsigned byteLength = keyRequestBuffer->byteLength();
104     return Uint8Array::tryCreate(WTFMove(keyRequestBuffer), 0, byteLength);
105 }
106
107 void CDMSessionAVFoundationObjC::releaseKeys()
108 {
109 }
110
111 bool CDMSessionAVFoundationObjC::update(Uint8Array* key, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, uint32_t& systemCode)
112 {
113     RetainPtr<NSData> keyData = adoptNS([[NSData alloc] initWithBytes:key->baseAddress() length:key->byteLength()]);
114     [[m_request dataRequest] respondWithData:keyData.get()];
115     [m_request finishLoading];
116     errorCode = MediaPlayer::NoError;
117     systemCode = 0;
118     nextMessage = nullptr;
119
120     return true;
121 }
122
123 void CDMSessionAVFoundationObjC::playerDidReceiveError(NSError *error)
124 {
125     if (!m_client)
126         return;
127
128     unsigned long code = mediaKeyErrorSystemCode(error);
129     m_client->sendError(LegacyCDMSessionClient::MediaKeyErrorDomain, code);
130 }
131
132 }
133
134 #endif