001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019
020package com.avpkit.core.io;
021
022
023import com.avpkit.core.io.FfmpegIOHandle;
024import com.avpkit.core.io.IURLProtocolHandlerFactory;
025import com.avpkit.core.io.URLProtocolManager;
026
027/**
028 * For Internal Use Only.
029 * This is the global (static) object that implements the AVPKit IO library.
030 * <p>
031 * The following methods are not meant to be generally
032 * called by code (although they can be).
033 * </p></p>
034 * You should never need to call this method.  Calling {@link URLProtocolManager#registerFactory(String, IURLProtocolHandlerFactory)}
035 * should cause it to be instantiated.
036 *</p></p>
037 * They are generally 'thread-aware' but not 'thread-safe', meaning if you use a handle
038 * on a thread, you are a responsible for making sure you don't
039 * reuse it on other threads.
040 * </p></p>
041 * They forward into ffmpeg's URL read/write/seek capabilities
042 * and allow our test scripts to make sure our URLProtocolHandlers
043 * (and FFMPEG's native handlers) work as expected.
044 * </p></p>
045 * Lastly this class, unlike other classes in the library, does not use SWIG to generate
046 * the Java objects, so you need to be careful not to change method names as the corresponding
047 * native code relies on specific naming and parameters.
048 * </p>
049 * @author aclarke
050 *
051 */
052public class FfmpegIO
053{
054
055  static
056  {
057    // this will load the one shared library
058    com.avpkit.ferry.Ferry.init();
059    FfmpegIO.init();
060    // And force the URLProtocolManager global
061    // object to be created.
062    URLProtocolManager.init();
063  }
064
065  /** Force a load of all native libraries; not normally needed */
066  public static void load() {}
067  
068  /**
069   * Internal Only.  Do not use.
070   */
071  public native static void init();
072  
073  /**
074   * This method is called by URLProtocolManager to register itself as a 
075   * protocol manager for different FFMPEG protocols.
076   * 
077   * @param protocol The protocol
078   * @param manager The manager for that protocol.
079   */
080  static synchronized void registerProtocolHandler(String protocol,
081      URLProtocolManager manager)
082  {
083    native_registerProtocolHandler(protocol, manager);
084  }
085  
086  /**
087   * This method is called by URLProtocolManager to register itself as a 
088   * protocol manager for different FFMPEG protocols.
089   * 
090   * @param container
091   * @param factory
092   */
093  public static synchronized int registerProtocolHandlerFactory(long containerSwigCPtr, 
094    com.avpkit.core.IContainer container, IURLProtocolHandlerFactory factory)
095  {
096    return native_registerProtocolHandlerFactory(containerSwigCPtr, container, factory);
097  }
098
099  public static int url_open(FfmpegIOHandle handle, String filename, int flags)
100  {
101    return native_url_open(handle, filename, flags);
102  }
103
104  public static int url_read(FfmpegIOHandle handle, byte[] buffer, int length)
105  {
106    return native_url_read(handle, buffer, length);
107  }
108
109  public static int url_close(FfmpegIOHandle handle)
110  {
111    return native_url_close(handle);
112  }
113
114  public static int url_write(FfmpegIOHandle handle, byte[] buffer, int length)
115  {
116    return native_url_write(handle, buffer, length);
117  }
118
119  public static long url_seek(FfmpegIOHandle handle, long position, int whence)
120  {
121    return native_url_seek(handle, position, whence);
122  }
123
124  private static native int native_registerProtocolHandler(
125      String urlPrefix, URLProtocolManager proto);
126  
127  private static native int native_registerProtocolHandlerFactory(long containerSwigCPtr,
128      com.avpkit.core.IContainer container, IURLProtocolHandlerFactory factory);
129
130  private static native int native_url_open(FfmpegIOHandle handle,
131      String filename, int flags);
132
133  private static native int native_url_read(FfmpegIOHandle handle,
134      byte[] buffer, int length);
135
136  private static native int native_url_write(FfmpegIOHandle handle,
137      byte[] buffer, int length);
138
139  private static native long native_url_seek(FfmpegIOHandle handle,
140      long position, int whence);
141
142  private static native int native_url_close(FfmpegIOHandle handle);
143
144}