/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using static Interop; namespace Tizen.Multimedia { /// /// Represents a media source using memory. /// /// /// The buffer has to be filled with appropriate data which means it must be well-formatted. /// If you provide invalid data, you won't receive an error until is called. /// /// public sealed class MediaBufferSource : MediaSource { private byte[] _buffer; /// /// Initializes a new instance of the MediaBufferSource class with an allocated buffer. /// /// The value indicating the size of the buffer. /// /// is zero.\n /// -or-\n /// is less than zero. /// public MediaBufferSource(int length) { if (length <= 0) { Log.Error(PlayerLog.Tag, "invalid length : " + length); throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero."); } _buffer = new byte[length]; } /// /// Initializes a new instance of the MediaBufferSource class from the buffer. /// /// The source array to be copied into the buffer. /// is null. public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length) { } //TODO remove default parameter. /// /// Initializes a new instance of the MediaBufferSource class from the buffer /// with the specified length and the specified offset. /// /// The source array to be copied into the buffer. /// The value indicating the number of bytes to copy from the buffer. /// The value indicating the offset in the buffer of the first byte to copy. /// is null. /// /// is less than zero.\n /// -or-\n /// is equal to or less than zero.\n /// -or-\n /// + is greater than buffer.Length. /// public MediaBufferSource(byte[] buffer, int length, int offset = 0) { if (buffer == null) { Log.Error(PlayerLog.Tag, "invalid buffer"); throw new ArgumentNullException(nameof(buffer)); } if (offset < 0) { Log.Error(PlayerLog.Tag, "invalid offset : " + offset); throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero."); } if (length <= 0) { Log.Error(PlayerLog.Tag, "invalid length : " + length); throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero."); } if (length + offset > buffer.Length) { Log.Error(PlayerLog.Tag, "invalid total length : " + (int)(length + offset)); throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }."); } _buffer = new byte[length]; Array.Copy(buffer, offset, _buffer, 0, length); } private MediaBufferSource() { } /// /// Creates a MediaBufferSource that wraps a byte array. /// /// The array to be wrapped. /// A MediaBufferSource wrapping the byte array. public static MediaBufferSource Wrap(byte[] buffer) { if (buffer == null) { Log.Error(PlayerLog.Tag, "invalid buffer"); throw new ArgumentNullException(nameof(buffer)); } MediaBufferSource source = new MediaBufferSource(); source._buffer = buffer; return source; } /// /// Gets the byte array of this buffer. /// public byte[] Buffer => _buffer; internal override void OnAttached(Player player) { NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length). ThrowIfFailed("Failed to set the memory buffer"); } } }