summaryrefslogtreecommitdiffstats
path: root/media/mojo/services/mojo_cdm.h
blob: b865cacb7ce3e7fe01e8a2bc85abe5b2a1ff9fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_H_
#define MEDIA_MOJO_SERVICES_MOJO_CDM_H_

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "media/base/media_keys.h"
#include "media/mojo/interfaces/content_decryption_module.mojom.h"
#include "media/mojo/services/mojo_type_trait.h"

namespace mojo {
class ServiceProvider;
}

namespace media {

// A MediaKeys that proxies to a mojo::ContentDecryptionModule. That
// mojo::ContentDecryptionModule proxies back to the MojoCdm via the
// mojo::ContentDecryptionModuleClient interface.
class MojoCdm : public MediaKeys, public mojo::ContentDecryptionModuleClient {
 public:
  // |media_renderer_provider| is a ServiceProvider from a connected
  //     Application that is hosting a mojo::MediaRenderer.
  MojoCdm(mojo::ContentDecryptionModulePtr remote_cdm,
          const SessionMessageCB& session_message_cb,
          const SessionReadyCB& session_ready_cb,
          const SessionClosedCB& session_closed_cb,
          const SessionErrorCB& session_error_cb,
          const SessionKeysChangeCB& session_keys_change_cb,
          const SessionExpirationUpdateCB& session_expiration_update_cb);
  ~MojoCdm() final;

  // MediaKeys implementation.
  void SetServerCertificate(const uint8_t* certificate_data,
                            int certificate_data_length,
                            scoped_ptr<SimpleCdmPromise> promise) final;
  void CreateSession(const std::string& init_data_type,
                     const uint8_t* init_data,
                     int init_data_length,
                     SessionType session_type,
                     scoped_ptr<NewSessionCdmPromise> promise) final;
  void LoadSession(const std::string& session_id,
                   scoped_ptr<NewSessionCdmPromise> promise) final;
  void UpdateSession(const std::string& session_id,
                     const uint8_t* response,
                     int response_length,
                     scoped_ptr<SimpleCdmPromise> promise) final;
  void CloseSession(const std::string& session_id,
                    scoped_ptr<SimpleCdmPromise> promise) final;
  void RemoveSession(const std::string& session_id,
                     scoped_ptr<SimpleCdmPromise> promise) final;
  CdmContext* GetCdmContext() final;

 private:
  // mojo::ContentDecryptionModuleClient implementation.
  void OnSessionMessage(const mojo::String& session_id,
                        mojo::Array<uint8_t> message,
                        const mojo::String& destination_url) final;
  void OnSessionReady(const mojo::String& session_id) final;
  void OnSessionClosed(const mojo::String& session_id) final;
  void OnSessionError(const mojo::String& session_id,
                      mojo::CdmException exception,
                      uint32_t system_code,
                      const mojo::String& error_message) final;
  void OnSessionKeysChange(const mojo::String& session_id,
                           bool has_additional_usable_key) final;
  void OnSessionExpirationUpdate(const mojo::String& session_id,
                                 int64_t new_expiry_time_usec) final;

  // Callbacks to handle CDM promises.
  template <typename... T>
  void OnPromiseResult(scoped_ptr<CdmPromiseTemplate<T...>> promise,
                       mojo::CdmPromiseResultPtr result,
                       typename MojoTypeTrait<T>::MojoType... args);

  mojo::ContentDecryptionModulePtr remote_cdm_;

  // Callbacks for firing session events.
  SessionMessageCB session_message_cb_;
  SessionReadyCB session_ready_cb_;
  SessionClosedCB session_closed_cb_;
  SessionErrorCB session_error_cb_;
  SessionKeysChangeCB session_keys_change_cb_;
  SessionExpirationUpdateCB session_expiration_update_cb_;

  base::WeakPtrFactory<MojoCdm> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(MojoCdm);
};

}  // namespace media

#endif  // MEDIA_MOJO_SERVICES_MOJO_CDM_H_