blob: 2a3f530e04c462a31eea8dd98e58942076b29d42 [file] [log] [blame]
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.drm;
18
19import static android.drm.DrmConvertedStatus.STATUS_OK;
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080020import static android.drm.DrmManagerClient.INVALID_SESSION;
Elliott Hughesc1858222014-04-28 20:49:24 -070021import static android.system.OsConstants.SEEK_SET;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080022
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080023import android.os.ParcelFileDescriptor;
Elliott Hughesf97c6332014-04-28 16:38:43 -070024import android.system.ErrnoException;
25import android.system.Os;
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080026import android.util.Log;
27
Victor Chang198ced82020-12-16 13:56:28 +000028import com.android.internal.util.ArrayUtils;
29
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080030import libcore.io.IoBridge;
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080031import libcore.io.Streams;
32
33import java.io.FileDescriptor;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080034import java.io.FilterOutputStream;
35import java.io.IOException;
36import java.io.OutputStream;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080037import java.net.UnknownServiceException;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080038
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080039/**
40 * Stream that applies a {@link DrmManagerClient} transformation to data before
41 * writing to disk, similar to a {@link FilterOutputStream}.
42 *
43 * @hide
Robert Shihea42f762020-01-21 14:00:04 -080044 * @deprecated Please use {@link android.media.MediaDrm}
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080045 */
Robert Shihea42f762020-01-21 14:00:04 -080046@Deprecated
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080047public class DrmOutputStream extends OutputStream {
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080048 private static final String TAG = "DrmOutputStream";
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080049
50 private final DrmManagerClient mClient;
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080051 private final ParcelFileDescriptor mPfd;
52 private final FileDescriptor mFd;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080053
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080054 private int mSessionId = INVALID_SESSION;
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080055
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080056 /**
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080057 * @param pfd Opened with "rw" mode.
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080058 */
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080059 public DrmOutputStream(DrmManagerClient client, ParcelFileDescriptor pfd, String mimeType)
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080060 throws IOException {
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080061 mClient = client;
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080062 mPfd = pfd;
63 mFd = pfd.getFileDescriptor();
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080064
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080065 mSessionId = mClient.openConvertSession(mimeType);
66 if (mSessionId == INVALID_SESSION) {
67 throw new UnknownServiceException("Failed to open DRM session for " + mimeType);
68 }
69 }
70
71 public void finish() throws IOException {
72 final DrmConvertedStatus status = mClient.closeConvertSession(mSessionId);
73 if (status.statusCode == STATUS_OK) {
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080074 try {
Elliott Hughesf97c6332014-04-28 16:38:43 -070075 Os.lseek(mFd, status.offset, SEEK_SET);
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080076 } catch (ErrnoException e) {
77 e.rethrowAsIOException();
78 }
79 IoBridge.write(mFd, status.convertedData, 0, status.convertedData.length);
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080080 mSessionId = INVALID_SESSION;
81 } else {
82 throw new IOException("Unexpected DRM status: " + status.statusCode);
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080083 }
84 }
85
86 @Override
87 public void close() throws IOException {
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080088 if (mSessionId == INVALID_SESSION) {
89 Log.w(TAG, "Closing stream without finishing");
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080090 }
Jeff Sharkey7ccc9092012-12-17 17:03:11 -080091
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -080092 mPfd.close();
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080093 }
94
95 @Override
96 public void write(byte[] buffer, int offset, int count) throws IOException {
Pete Gillin60f55a252018-05-10 15:40:32 +010097 ArrayUtils.throwsIfOutOfBounds(buffer.length, offset, count);
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -080098
99 final byte[] exactBuffer;
100 if (count == buffer.length) {
101 exactBuffer = buffer;
102 } else {
103 exactBuffer = new byte[count];
104 System.arraycopy(buffer, offset, exactBuffer, 0, count);
105 }
106
107 final DrmConvertedStatus status = mClient.convertData(mSessionId, exactBuffer);
108 if (status.statusCode == STATUS_OK) {
Jeff Sharkeyebf8ad52014-01-30 15:01:22 -0800109 IoBridge.write(mFd, status.convertedData, 0, status.convertedData.length);
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -0800110 } else {
111 throw new IOException("Unexpected DRM status: " + status.statusCode);
112 }
113 }
114
115 @Override
116 public void write(int oneByte) throws IOException {
Jeff Sharkey7ccc9092012-12-17 17:03:11 -0800117 Streams.writeSingleByte(this, oneByte);
Jeff Sharkeyf67c8a92012-12-13 08:55:59 -0800118 }
119}