| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.drm; |
| 18 | |
| 19 | import static android.drm.DrmConvertedStatus.STATUS_OK; |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 20 | import static android.drm.DrmManagerClient.INVALID_SESSION; |
| Elliott Hughes | c185822 | 2014-04-28 20:49:24 -0700 | [diff] [blame] | 21 | import static android.system.OsConstants.SEEK_SET; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 22 | |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 23 | import android.os.ParcelFileDescriptor; |
| Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 24 | import android.system.ErrnoException; |
| 25 | import android.system.Os; |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | |
| Victor Chang | 198ced8 | 2020-12-16 13:56:28 +0000 | [diff] [blame] | 28 | import com.android.internal.util.ArrayUtils; |
| 29 | |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 30 | import libcore.io.IoBridge; |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 31 | import libcore.io.Streams; |
| 32 | |
| 33 | import java.io.FileDescriptor; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 34 | import java.io.FilterOutputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.io.OutputStream; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 37 | import java.net.UnknownServiceException; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 38 | |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Stream that applies a {@link DrmManagerClient} transformation to data before |
| 41 | * writing to disk, similar to a {@link FilterOutputStream}. |
| 42 | * |
| 43 | * @hide |
| Robert Shih | ea42f76 | 2020-01-21 14:00:04 -0800 | [diff] [blame] | 44 | * @deprecated Please use {@link android.media.MediaDrm} |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 45 | */ |
| Robert Shih | ea42f76 | 2020-01-21 14:00:04 -0800 | [diff] [blame] | 46 | @Deprecated |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 47 | public class DrmOutputStream extends OutputStream { |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 48 | private static final String TAG = "DrmOutputStream"; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 49 | |
| 50 | private final DrmManagerClient mClient; |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 51 | private final ParcelFileDescriptor mPfd; |
| 52 | private final FileDescriptor mFd; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 53 | |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 54 | private int mSessionId = INVALID_SESSION; |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 55 | |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 56 | /** |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 57 | * @param pfd Opened with "rw" mode. |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 58 | */ |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 59 | public DrmOutputStream(DrmManagerClient client, ParcelFileDescriptor pfd, String mimeType) |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 60 | throws IOException { |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 61 | mClient = client; |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 62 | mPfd = pfd; |
| 63 | mFd = pfd.getFileDescriptor(); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 64 | |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 65 | 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 Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 74 | try { |
| Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 75 | Os.lseek(mFd, status.offset, SEEK_SET); |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 76 | } catch (ErrnoException e) { |
| 77 | e.rethrowAsIOException(); |
| 78 | } |
| 79 | IoBridge.write(mFd, status.convertedData, 0, status.convertedData.length); |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 80 | mSessionId = INVALID_SESSION; |
| 81 | } else { |
| 82 | throw new IOException("Unexpected DRM status: " + status.statusCode); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void close() throws IOException { |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 88 | if (mSessionId == INVALID_SESSION) { |
| 89 | Log.w(TAG, "Closing stream without finishing"); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 90 | } |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 91 | |
| Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 92 | mPfd.close(); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void write(byte[] buffer, int offset, int count) throws IOException { |
| Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 97 | ArrayUtils.throwsIfOutOfBounds(buffer.length, offset, count); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 98 | |
| 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 Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 109 | IoBridge.write(mFd, status.convertedData, 0, status.convertedData.length); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 110 | } else { |
| 111 | throw new IOException("Unexpected DRM status: " + status.statusCode); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void write(int oneByte) throws IOException { |
| Jeff Sharkey | 7ccc909 | 2012-12-17 17:03:11 -0800 | [diff] [blame] | 117 | Streams.writeSingleByte(this, oneByte); |
| Jeff Sharkey | f67c8a9 | 2012-12-13 08:55:59 -0800 | [diff] [blame] | 118 | } |
| 119 | } |