media_keys.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef MEDIA_BASE_MEDIA_KEYS_H_
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define MEDIA_BASE_MEDIA_KEYS_H_
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <string>
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <vector>
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/callback.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "media/base/media_export.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace media {
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass Decryptor;
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Performs media key operations.
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// All key operations are called on the renderer thread. Therefore, these calls
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// should be fast and nonblocking; key events should be fired asynchronously.
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MEDIA_EXPORT MediaKeys {
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Reported to UMA, so never reuse a value!
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Must be kept in sync with WebKit::WebMediaPlayerClient::MediaKeyErrorCode
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // (enforced in webmediaplayer_impl.cc).
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum KeyError {
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    kUnknownError = 1,
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    kClientError,
3268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // The commented v0.1b values below have never been used.
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // kServiceError,
3468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    kOutputError = 4,
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // kHardwareChangeError,
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // kDomainError,
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    kMaxKeyError  // Must be last and greater than any legit value.
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MediaKeys();
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~MediaKeys();
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Generates a key request with the |type| and |init_data| provided.
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if generating key request succeeded, false otherwise.
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Note: AddKey() and CancelKeyRequest() should only be called after
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // GenerateKeyRequest() returns true.
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual bool GenerateKeyRequest(const std::string& type,
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  const uint8* init_data,
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  int init_data_length) = 0;
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Adds a |key| to the session. The |key| is not limited to a decryption
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // key. It can be any data that the key system accepts, such as a license.
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // If multiple calls of this function set different keys for the same
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // key ID, the older key will be replaced by the newer key.
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void AddKey(const uint8* key, int key_length,
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      const uint8* init_data, int init_data_length,
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      const std::string& session_id) = 0;
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Cancels the key request specified by |session_id|.
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void CancelKeyRequest(const std::string& session_id) = 0;
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Gets the Decryptor object associated with the MediaKeys. Returns NULL if
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // no Decryptor object is associated. The returned object is only guaranteed
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // to be valid during the MediaKeys' lifetime.
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual Decryptor* GetDecryptor();
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MediaKeys);
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Key event callbacks. See the spec for details:
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#event-summary
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)typedef base::Callback<void(const std::string& session_id)> KeyAddedCB;
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)typedef base::Callback<void(const std::string& session_id,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            media::MediaKeys::KeyError error_code,
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            int system_code)> KeyErrorCB;
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)typedef base::Callback<void(const std::string& session_id,
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                            const std::vector<uint8>& message,
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const std::string& default_url)> KeyMessageCB;
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)typedef base::Callback<void(const std::string& session_id,
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const std::string& type,
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                            const std::vector<uint8>& init_data)> NeedKeyCB;
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace media
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // MEDIA_BASE_MEDIA_KEYS_H_
90