Core Java Media Framework
Authors: Linden deCarmo
Venue:  (1999)

From the Book:PREFACE: Preface The Java Media Framework (JMF) is the most exciting addition to the Java Family of APIs since the introduction of Swing. No longer are you restricted to passive dialogs and silent windows. Now you can use JMF to spice up your applications with audio and visual effects. JMF is divided into three phases: media playback, media capture and conferencing. This book concentrates on the media playback portion of JMF, the Java Media Player API, and how you can use it in real-world scenarios. Who Should Read This Book? Multimedia has transitioned from an exotic feature used by few applications to a core feature of Web sites, databases, games and virtually all business applications. As a result, anyone who programs in Java needs to read Core JMF. Core JMF is targeted at serious programmers who don't want a warmed-over version of Sun's JMF's documentation. It not only clarifies the confusing areas in Sun's documentation, but it also provides unique insights to the inner working of the JMF classes and interfaces. Throughout the text, you'll find honest evaluations of JMF's strengths and weaknesses and advice on how to work around bugs. Core JMF does not assume that the entire world revolves around Microsoft and its Win32 API. You'll discover how to leverage JMF's cross-platform capabilities on Microsoft Windows 95, Windows 98, Windows NT, Sun Solaris and other flavors of UNIX. You'll see comparisons of how JMF runs on each platform and how to avoid platform specific issues sothatyour applications are portable. Core JMF does not contain fluffy example programs that show you how to make noise or play video in a window. Rather, you'll encounter example programs that solve real-world multimedia problems. Some of sample programs will show you how to: Enhance your applets with multimedia. Integrate JMF with a Swing file chooser dialog. Create multimedia beans. Use JMF objects with native C/C++ programs. Create a CD Player. Synchronize text with an audio CD. Stream multimedia content over the Internet. This book assumes you have a solid knowledge of Java programming concepts. However, you do not have to be a multimedia expert to read it. You'll find thorough explanations of each multimedia acronym or term. If you already are a multimedia aficionado and are familiar with environments such as QuickTime, MCI, or DirectShow, this book will show you how each component of JMF compares to the API you already know, so you can rapidly transition to JMF programming. About This Book Core JMF covers the core JMF classes and interfaces that you will use in your programs. If you are looking for an exhaustive overview of every JMF API, you should read Rob Gordon and Stephen Talley's Essential JMF. After you've finished reading Core JMF, you'll have thorough knowledge of the following JMF topics: Multimedia Applets. Synchronization. Player Design and Development. Real-time streaming over the Internet. Enhancing other Java API's such as Swing, JavaBeans, and JNI with multimedia. Cross platform multimedia development and issues. The book is not a disjointed set of unrelated example programs. Rather, it is a cohesive unit. Each chapter builds on the lessons learned in previous chapters. As a result, you'll be able to create sophisticated Java applications and objects without being overwhelmed with details. Here are some highlights of what you'll learn in each chapter: Chapter 1: Multimedia Evolution. Chapter 1 explains why the Java Media Framework (JMF) was created and why you'll want to use it in your programs. It starts with a digital audio primer that defines how analog audio waveforms are captured and stored in the digital realm. Readers who are familiar with digital audio and Video can jump directly to the "Audio Woes" section of the chapter. The chapter then explores the primitive audio features found in JDK 1.0.2 and explains why they are inadequate for non-trivial programs. We then uncover an undocumented audio interface present in JDK 1.x and explain how to use it. You'll be introduced into Sun's Java Media and Communication APIs, the most exciting of which is JMF. Finally, you'll discover three key reasons why you should incorporate JMF into your Java programs. Chapter 2: JMF's Growing Pains. This chapter examines how JMF operates on platforms such as Solaris, Microsoft Windows 95 and 98, Windows NT and the Apple Macintosh. We reveal the companies that have been involved in developing JMF runtimes and uncover why JMF was a crucial component of the United States Justice Department's lawsuit against Microsoft. You'll also discover the challenges that developers face when they port JMF to environments, such as QuickTime and DirectShow, and how these challenges affect your JMF programs. We then instruct you how to install JMF on your operating system and set up your development tools so you can compile and test the JMF programs we'll create in subsequent chapters. Chapter 3: Everything Revolves Around Time. This chapter introduces you to the two core components of JMF: the Player interface and the Manager . You'll learn about the variety of media formats used by a Player and how to differentiate between a TimeBase and MediaTime . You'll also discover a clock's states (or phases) and how clocks affect synchronization. Chapter 4: Taking Control of the Situation. Chapter 4 examines Controllers and their impact on the utilization of multimedia resources in your programs. You'll learn what a Controller is, its modes of operation and why this model is superior to the older multimedia architectures. We'll also show you the most efficient techniques to manipulate Controller states and explain how to monitor a Controller 's status by listening for events. Chapter 5: Origins. This chapter explores DataSources and how they are used by the Manager and the Player . You'll learn why a DataSource removes all input/output responsibilities from a Player and how this makes the Player more flexible. We'll discover the differences between a Push and Pull DataSource and explain why the Pushed multimedia content will play a larger role in the future. Finally, we'll introduce you to MediaLocators and show you how they can be used to connect a DataSource to a Player. Chapter 6: AlohaJMF: A JMF Applet. In this chapter, you'll leverage all the information learned in Chapters 1 through 5 to write your first JMF Applet. We'll explain the similarities between the Applet interface and the JMF's Player interface and explain how to use Player methods in your applets. We'll also give you tips to make your JMF enhanced applets more user-friendly. Chapter 7: Getting Control Over Your Applications. Chapter 7 will show you why Controls are a crucial part of the Player and why every application must use them. You'll learn the three types of controls that a Player can surface and the most efficient means to obtain these controls. Finally, we'll show you how to incorporate the most popular controls in your applications and squeeze the best performance from them. Chapter 8: Multimedia Swing Set. Swing and JMF can be a dangerous combination and Chapter 8 teaches you how to safely mix JMF in Swing-based applications. First, you'll find out how to enhance Swing's JFileChooser class with multimedia previewing capabilities. Then, you'll discover how to simultaneously handle JMF and Swing events. Finally, we'll explain the problems you'll encounter in Swing multimedia development and show you how to avoid these pitfalls. Chapter 9: The Next Generation: JavaBeans and JMF. Chapter 9 introduces you to the JavaBeans object model and how it can be used to create multimedia objects. You'll learn what an object model is and how JavaBeans compares to other object models. We'll then take the multimedia chooser created in Chapter 8 and create a JavaBean from it. While we're creating this multimedia object, we'll reveal guidelines for converting a general purpose multimedia class into a multimedia bean and give you specific examples of how to use these guidelines. Chapter 10: Going Native. Chapter 10 teaches you how to reuse JMF objects in your C or C++ programs with the Java Native Interface (JNI). You'll discover how to start the Java virtual machine in C++ and learn how to avoid serious performance bottlenecks present in JNI. You'll then find out how to launch and use the JavaBean from Chapter 9 in a C++ program. We'll show you the most efficient means to access Java constants, member variables and methods and how to optimize your JNI code. Chapter 11: The Art of Synchronization. In Chapter 11 you'll learn what synchronization is and why it is a vital feature of all multimedia platforms. We'll use the timing concepts you learned in Chapter 3 to explain how JMF synchronizes multiple Player s. You'll learn about the complexities involved with manually synchronizing two or more Player s and find out about Controller methods which make synchronizing Players simple and fun. We'll also teach you the things you shouldn't try with synchronized Player s and the catastrophic results if you ignore these warnings. Finally, you'll learn how synchronization affects event reporting and how to respond intelligently to events from a synchronized Player . Chapter 12: Journey to the Center of the Player. Chapter 12 introduces you to the mysteries of Player creation. We shatter the myth that Player development is an arduous task and explain why you'll want to create your own Player s. You'll learn how to install a JMF Player and discover how the JMF Manager locates installed Players and associates them with DataSource s. We'll show you how to find out what kind of content a DataSource is transporting and whether the DataSource is cramming the content into one stream or separating its output into multiple streams. We then explain how Players retrieve media from the DataSource 's streams and play this content. Finally, in order to illustrate synchronization, we create a real world synchronization example program. Over the next five chapters, we create a Caption Player and CD Audio Player and an application that shows you how to synchronize text to the music on a CD. Chapter 13: The Multimedia Caption DataSource. In Chapter 13 you'll learn how to create a DataSource that processes multimedia captions. You'll find out about the Multimedia Caption Markup Language (MCML) and why it is ideal for synchronizing text to audio/visual content. We then review how the MCML is used inside our DataSource . You'll learn why this DataSource has multiple output streams and how these output streams are important for multiple language support. Chapter 14: MediaHandler Primer. Chapter 14 provides the infrastructure necessary to create robust Player s. You'll be provided with a Player shell that you can use to build your Player s. Then, we'll tell you why certain methods must be performed on threads and explain the side-effects of improper thread usage. You'll learn how to properly divide the Player 's workload so that client applications are responsive to user input. Finally, you'll discover when to clean up multimedia resources so your Player can gracefully shut down. Chapter 15: The Caption MediaHandler. We'll show you how to write a MCML Player using the principles revealed in Chapter 14. You'll parse MCML streams, decode the captions and present it via Swing. You'll learn how to create a ControlPanel component to start, pause, stop and search within a caption stream. You'll find out how to use a VisualControl within a Player . We'll also explain how threads improve the interactivity of the Player . We'll discuss the techniques Player s should use to report events and the dangers of reporting events on worker threads. You'll also find out how to create custom events that let you enhance applications that use your Player . Throughout the chapter, you'll receive valuable debugging tips that will save you hours of development and debugging. Chapter 16: Grapling with the CD. This chapter uncovers a gaping hole in the JMF architecture and shows how you can work around it. Since JMF prevents you from writing non-streaming Player s, we'll show you a secret technique to work around the problem. You'll also learn how to leverage this technology to create a CD Player that doesn't use JMF's streaming model. This chapter advises you when you should enhance a Player with native code. It also guides you through the minefield of Windows multimedia development and shows you how to safely integrate a JMF Player with native Windows' multimedia calls. Chapter 17: The Ultimate Synchronization: The Marriage of Players. This is the concluding chapter in the synchronization series. You'll discover how to combine and synchronize the CD Player created in Chapter 16, with the MCML Player and DataSource created in Chapters 13 through 15. In addition, we'll review the responsibilities a Player must assume in order to synchronize with other Players and point out problem areas in synchronized Player development. Chapter 18: Meddling in a Player's Business With a MediaProxy. Chapter 18 introduces you to the MediaProxy interface. You'll learn how a MediaProxy modifies content after it leaves a DataSource but before it reaches the Player . We'll also provide realistic examples of when you want to use a MediaProxy . We'll show you how to create a MediaProxy by modifying the MCML DataSource to transmit a single stream of caption content. Then, we'll use a MediaProxy to transform the single caption stream into the multi-stream format expected by the Player we created in Chapter 15. Chapter 19: Real-time Streaming. This chapter provides a thorough explanation of real-time streaming on the Internet and shows you how they are used in JMF. You'll learn about technologies such as RTP and RTSP and how they are superior to HTTP or FTP for transporting multimedia content over the Internet Protocol. We'll also introduce you to the RTSP-based RealNetwork's RealPlayer G2 and show you how to exploit the custom RealPlayer methods and events in your programs. Chapter 20: A Real-Time Hybrid. Chapter 20 will examine Sun's mysterious RTP architecture and show you how to use RTP in your applets and applications. You'll discover how Sun uses a MediaProxy to bridge the chasm between their RTP-specific DataSource and generic MediaHandlers . We'll also show you how the RTP Depacketizer provides data type independence and enables you to stream custom media types. You'll learn how to create RTP applets. We'll show you where to find RTP servers or create your own RTP conferences in order to test the RTP applet. Chapter 21: Where Is JMF Headed? We conclude the book by examining the current strengths and weaknesses in JMF and preview how future JMF releases will address these weaknesses. You'll learn about Pure JMF 1.1, the first version of JMF that runs on all Java platforms. We'll also examine the web server version of JMF that dramatically simplifies the installation and usage of JMF applets. Then we'll review JavaSound: the API that gives you finer control over audio and MIDI hardware. Finally, we'll take a peek at JMF 2.0. This release promises to provide evolutionary new functions such as the Plug-in API. The Plug-in API will let you create and dynamically insert special effect routines into Players. Appendices The Appendices give you additional details on error conditions, file formats and sources for additional information on the Internet. Feedback All books will contain unforeseen errors in the text and bugs in the example programs, and this book is no exception. If you'd like to report a bug, e-mail comments and other feedback at ftp.prenhall.com/pub/ptr/unix_ and_enabling_technologies.w-048/deCarmo. Conventions Table P-1 shows the coding conventions used in this book. Convention Example Class names have initial Capital letters public class Handler implements MediaProxy Method names have an initial lowercase letter and the rest of the words start with a capital letter.* public DataSource getDataSource() throws IOException, NoDataSourceException Variable names have an initial lowercase letter and the rest of the words start with a capital letter. protected byte timebuffer; static variables begin with an underscore. *Note: methods are always referred to without their associated parameters. Table P-2 shows the typographic conventions used in this book. Typeface or Symbol Description courier Used in source code, command-line entries, and other technical terms. italics Refers to definitions, quotes, emphasis, book title or a variable.