2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Multimedia
22 /// Represents a media source using memory.
25 /// The buffer has to be filled with appropriate data which means it must be well-formatted.
26 /// If you provide invalid data, you won't receive an error until <see cref="Player.Start"/> is called.
28 /// <seealso cref="Player.SetSource(MediaSource)"/>
29 public sealed class MediaBufferSource : MediaSource
31 private byte[] _buffer;
34 /// Initialize a new instance of the MediaBufferSource class with an allocated buffer.
36 /// <param name="length">The value indicating the size of the buffer.</param>
37 /// <exception cref="ArgumentOutOfRangeException">
38 /// <paramref name="length"/> is zero.\n
40 /// <paramref name="length"/> is less than zero.
42 public MediaBufferSource(int length)
46 Log.Error(PlayerLog.Tag, "invalid length : " + length);
47 throw new ArgumentOutOfRangeException(nameof(length), length,
48 "length can't be equal to or less than zero.");
50 _buffer = new byte[length];
54 /// Initialize a new instance of the MediaBufferSource class from the buffer.
56 /// <param name="buffer">The source array to be copied into the buffer.</param>
57 /// <exception cref="ArgumentNullException">buffer is null.</exception>
58 public MediaBufferSource(byte[] buffer) : this(buffer, buffer == null ? 0 : buffer.Length)
62 //TODO remove default parameter.
64 /// Initialize a new instance of the MediaBufferSource class from the buffer
65 /// with the specified length and the specified offset.
67 /// <param name="buffer">The source array to be copied into the buffer.</param>
68 /// <param name="length">The value indicating the number of bytes to copy from the buffer.</param>
69 /// <param name="offset">The value indicating the offset in the buffer of the first byte to copy.</param>
70 /// <exception cref="ArgumentNullException">buffer is null.</exception>
71 /// <exception cref="ArgumentOutOfRangeException">
72 /// <paramref name="offset"/> is less than zero.\n
74 /// <paramref name="length"/> is equal to or less than zero.\n
76 /// <paramref name="offset"/>+<paramref name="length"/> is greater than buffer.Length.
78 public MediaBufferSource(byte[] buffer, int length, int offset = 0)
82 Log.Error(PlayerLog.Tag, "invalid buffer");
83 throw new ArgumentNullException(nameof(buffer));
87 Log.Error(PlayerLog.Tag, "invalid offset : " + offset);
88 throw new ArgumentOutOfRangeException(nameof(offset), offset, "offset can't be less than zero.");
92 Log.Error(PlayerLog.Tag, "invalid length : " + length);
93 throw new ArgumentOutOfRangeException(nameof(length), length, "length can't be equal to or less than zero.");
95 if (length + offset > buffer.Length)
97 Log.Error(PlayerLog.Tag, "invalid total length : " + (int)(length + offset));
98 throw new ArgumentOutOfRangeException($"length + offset can't be greater than the length of the { nameof(buffer) }.");
101 _buffer = new byte[length];
103 Array.Copy(buffer, offset, _buffer, 0, length);
106 private MediaBufferSource()
111 /// Create a MediaBufferSource that wraps a byte array.
113 /// <param name="buffer">The array to be wrapped.</param>
114 /// <returns>A MediaBufferSource wrapping the byte array.</returns>
115 public static MediaBufferSource Wrap(byte[] buffer)
119 Log.Error(PlayerLog.Tag, "invalid buffer");
120 throw new ArgumentNullException(nameof(buffer));
123 MediaBufferSource source = new MediaBufferSource();
124 source._buffer = buffer;
129 /// Gets the byte array of this buffer.
131 public byte[] Buffer => _buffer;
133 internal override void OnAttached(Player player)
135 NativePlayer.SetMemoryBuffer(player.Handle, _buffer, _buffer.Length).
136 ThrowIfFailed("Failed to set the memory buffer");