Rename ENABLE_ENCRYPTED_MEDIA_V2 to ENABLE_LEGACY_ENCRYPTED_MEDIA
[WebKit.git] / Source / WebCore / Modules / encryptedmedia / CDM.cpp
1 /*
2  * Copyright (C) 2013 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
28 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
29
30 #include "CDM.h"
31
32 #include "CDMPrivateClearKey.h"
33 #include "CDMPrivateMediaPlayer.h"
34 #include "CDMSession.h"
35 #include "MediaKeyError.h"
36 #include "MediaKeys.h"
37 #include "MediaPlayer.h"
38 #include <wtf/NeverDestroyed.h>
39 #include <wtf/text/WTFString.h>
40
41 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
42 #include "CDMPrivateMediaSourceAVFObjC.h"
43 #endif
44
45 namespace WebCore {
46
47 struct CDMFactory {
48     WTF_MAKE_NONCOPYABLE(CDMFactory); WTF_MAKE_FAST_ALLOCATED;
49 public:
50     CDMFactory(CreateCDM constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
51         : constructor(constructor)
52         , supportsKeySystem(supportsKeySystem)
53         , supportsKeySystemAndMimeType(supportsKeySystemAndMimeType)
54     {
55     }
56
57     CreateCDM constructor;
58     CDMSupportsKeySystem supportsKeySystem;
59     CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType;
60 };
61
62 static Vector<CDMFactory*>& installedCDMFactories()
63 {
64     static NeverDestroyed<Vector<CDMFactory*>> cdms;
65     static bool queriedCDMs = false;
66     if (!queriedCDMs) {
67         queriedCDMs = true;
68
69         cdms.get().append(new CDMFactory([](CDM* cdm) { return std::make_unique<CDMPrivateClearKey>(cdm); },
70             CDMPrivateClearKey::supportsKeySystem, CDMPrivateClearKey::supportsKeySystemAndMimeType));
71
72         // FIXME: initialize specific UA CDMs. http://webkit.org/b/109318, http://webkit.org/b/109320
73         cdms.get().append(new CDMFactory([](CDM* cdm) { return std::make_unique<CDMPrivateMediaPlayer>(cdm); },
74             CDMPrivateMediaPlayer::supportsKeySystem, CDMPrivateMediaPlayer::supportsKeySystemAndMimeType));
75
76 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
77         cdms.get().append(new CDMFactory([](CDM* cdm) { return std::make_unique<CDMPrivateMediaSourceAVFObjC>(cdm); },
78             CDMPrivateMediaSourceAVFObjC::supportsKeySystem, CDMPrivateMediaSourceAVFObjC::supportsKeySystemAndMimeType));
79 #endif
80     }
81
82     return cdms;
83 }
84
85 void CDM::registerCDMFactory(CreateCDM constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
86 {
87     installedCDMFactories().append(new CDMFactory(constructor, supportsKeySystem, supportsKeySystemAndMimeType));
88 }
89
90 static CDMFactory* CDMFactoryForKeySystem(const String& keySystem)
91 {
92     for (auto& factory : installedCDMFactories()) {
93         if (factory->supportsKeySystem(keySystem))
94             return factory;
95     }
96     return 0;
97 }
98
99 bool CDM::supportsKeySystem(const String& keySystem)
100 {
101     return CDMFactoryForKeySystem(keySystem);
102 }
103
104 bool CDM::keySystemSupportsMimeType(const String& keySystem, const String& mimeType)
105 {
106     if (CDMFactory* factory = CDMFactoryForKeySystem(keySystem))
107         return factory->supportsKeySystemAndMimeType(keySystem, mimeType);
108     return false;
109 }
110
111 std::unique_ptr<CDM> CDM::create(const String& keySystem)
112 {
113     if (!supportsKeySystem(keySystem))
114         return nullptr;
115
116     return std::make_unique<CDM>(keySystem);
117 }
118
119 CDM::CDM(const String& keySystem)
120     : m_keySystem(keySystem)
121     , m_client(nullptr)
122 {
123     m_private = CDMFactoryForKeySystem(keySystem)->constructor(this);
124 }
125
126 CDM::~CDM()
127 {
128 }
129
130 bool CDM::supportsMIMEType(const String& mimeType) const
131 {
132     return m_private->supportsMIMEType(mimeType);
133 }
134
135 std::unique_ptr<CDMSession> CDM::createSession(CDMSessionClient* client)
136 {
137     std::unique_ptr<CDMSession> session = m_private->createSession(client);
138     if (mediaPlayer())
139         mediaPlayer()->setCDMSession(session.get());
140     return session;
141 }
142
143 MediaPlayer* CDM::mediaPlayer() const
144 {
145     if (!m_client)
146         return 0;
147     return m_client->cdmMediaPlayer(this);
148 }
149
150 }
151
152 #endif // ENABLE(LEGACY_ENCRYPTED_MEDIA)